From 1976e3ad0803df77ae2fc576f588800f477eb9f4 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sun, 10 Mar 2019 14:40:08 +0200 Subject: [PATCH] [CLI] Don't write progress if output is redirected FIxes #155 --- .../Internal/InlineProgress.cs | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/DiscordChatExporter.Cli/Internal/InlineProgress.cs b/DiscordChatExporter.Cli/Internal/InlineProgress.cs index f45d3a8..c50d862 100644 --- a/DiscordChatExporter.Cli/Internal/InlineProgress.cs +++ b/DiscordChatExporter.Cli/Internal/InlineProgress.cs @@ -9,19 +9,31 @@ namespace DiscordChatExporter.Cli.Internal public InlineProgress() { - _posX = Console.CursorLeft; - _posY = Console.CursorTop; + // If output is not redirected - save initial cursor position + if (!Console.IsOutputRedirected) + { + _posX = Console.CursorLeft; + _posY = Console.CursorTop; + } } public void Report(double progress) { - Console.SetCursorPosition(_posX, _posY); - Console.WriteLine($"{progress:P1}"); + // If output is not redirected - reset cursor position and write progress + if (!Console.IsOutputRedirected) + { + Console.SetCursorPosition(_posX, _posY); + Console.WriteLine($"{progress:P1}"); + } } public void Dispose() { - Console.SetCursorPosition(_posX, _posY); + // If output is not redirected - reset cursor position + if (!Console.IsOutputRedirected) + Console.SetCursorPosition(_posX, _posY); + + // Inform about completion Console.WriteLine("Completed ✓"); } }