Skip to content

Commit

Permalink
Do not display progress bar when output is redirected
Browse files Browse the repository at this point in the history
  • Loading branch information
jheinzel committed Oct 27, 2023
1 parent 61e73cf commit 28ae71f
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/TutorBot.Cli/Infrastructure/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,25 @@ public void Increment(int increment)
{
progress += increment;

Console.CursorLeft = curserPosition;
var progressPercent = (int)((double)progress / max * 100);
var progressBarLength = (int)((double)progress / max * Constants.MAX_PROGESSBAR_LENGTH);
var progressString = new string('█', progressBarLength);
var remainingString = new string(' ', Math.Max(0, Constants.MAX_PROGESSBAR_LENGTH - progressBarLength));

Console.ForegroundColor = color;
Console.Write($"{title}{(title=="" ? "" : ": ")}[{progressString}{remainingString}] {progressPercent,3}%");
Console.ResetColor();
if (!Console.IsOutputRedirected)
{
Console.CursorLeft = curserPosition;
var progressPercent = (int)((double)progress / max * 100);
var progressBarLength = (int)((double)progress / max * Constants.MAX_PROGESSBAR_LENGTH);
var progressString = new string('█', progressBarLength);
var remainingString = new string(' ', Math.Max(0, Constants.MAX_PROGESSBAR_LENGTH - progressBarLength));

Console.ForegroundColor = color;
Console.Write($"{title}{(title == "" ? "" : ": ")}[{progressString}{remainingString}] {progressPercent,3}%");
Console.ResetColor();
}
}

public void Dispose()
{
Console.WriteLine();
if (!Console.IsOutputRedirected)
{
Console.WriteLine();
}
}
}

0 comments on commit 28ae71f

Please sign in to comment.