Skip to content

Commit

Permalink
only check console writability once
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrit committed Jan 22, 2024
1 parent e253be4 commit 10411b3
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions SeeSharp/Common/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,26 @@ public class ProgressBar {

string curText = "";
int activeBlocks;
readonly bool supportsRewrite;
readonly static bool supportsRewrite;

public static bool Silent = false;

static ProgressBar() {
// Check if the console / output stream supports altering previous output
// e.g., not (always) the case if its forwarded to a file or the VS Code Debug Console is used.
try {
(int left, int top) = Console.GetCursorPosition();
Console.SetCursorPosition(left, top);

if (Console.WindowHeight == 0 || Console.WindowWidth == 0)
supportsRewrite = false;
else
supportsRewrite = true;
} catch (Exception) {
supportsRewrite = false;
}
}

/// <summary>
/// Initializes a new progress bar for a given amount of work. The amount of work should be specified
/// as a number of (roughly) equally expensive steps.
Expand All @@ -54,20 +70,6 @@ public ProgressBar(int numBlocks = 20, bool displayWork = true, bool displayTime
watcher = new(Console.Out);
Console.SetOut(watcher);
watcher.WriteCharEvent += OnOtherOutput;

// Check if the console / output stream supports altering previous output
// e.g., not (always) the case if its forwarded to a file or the VS Code Debug Console is used.
try {
(int left, int top) = Console.GetCursorPosition();
Console.SetCursorPosition(left, top);

if (Console.WindowHeight == 0 || Console.WindowWidth == 0)
supportsRewrite = false;
else
supportsRewrite = true;
} catch (Exception) {
supportsRewrite = false;
}
}

/// <summary>
Expand Down

0 comments on commit 10411b3

Please sign in to comment.