-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleUtils.cs
141 lines (120 loc) · 3.45 KB
/
ConsoleUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Threading;
class Spinner : IDisposable
{
private const string Sequence = @"/-\|";
private int counter = 0;
private readonly int left;
private readonly int top;
private readonly int delay;
private bool active;
private readonly Thread thread;
public Spinner(int left, int top, int delay = 100)
{
this.left = left;
this.top = top;
this.delay = delay;
thread = new Thread(Spin);
}
public void Start()
{
active = true;
if (!thread.IsAlive)
thread.Start();
}
public void Stop()
{
active = false;
Draw(' ');
}
private void Spin()
{
while (active)
{
Turn();
Thread.Sleep(delay);
}
}
private void Draw(char c)
{
Console.SetCursorPosition(left, top);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(c);
}
private void Turn()
{
Draw(Sequence[++counter % Sequence.Length]);
}
public void Dispose()
{
Stop();
}
}
class ConsoleSpinner
{
private static readonly string[] SpinnerFrames = { "/", "-", "\\", "|" };
private static readonly TimeSpan Interval = TimeSpan.FromSeconds(0.3);
private static Thread? spinnerThread;
private static bool stopSpinner;
public static void Start()
{
Console.CursorVisible = false;
stopSpinner = false;
spinnerThread = new Thread(Spin);
spinnerThread.Start();
}
public static void Stop()
{
stopSpinner = true;
spinnerThread?.Join();
}
private static void Spin()
{
Console.CursorVisible = false;
int frameIndex = 0;
while (!stopSpinner)
{
Console.Write(SpinnerFrames[frameIndex]);
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
frameIndex = (frameIndex + 1) % SpinnerFrames.Length;
Thread.Sleep(Interval);
}
}
}
class ConsoleProgressBar
{
private const int ProgressBarWidth = 50;
private const char ProgressBarCharacter = '#';
private const char BackgroundCharacter = '-';
public static void DisplayProgressBar(int progressPercentage)
{
Console.CursorVisible = false;
int completedWidth = (int)Math.Round(progressPercentage / 100.0 * ProgressBarWidth);
int remainingWidth = ProgressBarWidth - completedWidth;
string progressBar = new string(ProgressBarCharacter, completedWidth) + new string(BackgroundCharacter, remainingWidth);
Console.Write("[{0}] {1}%", progressBar, progressPercentage);
Console.SetCursorPosition(0, Console.CursorTop);
}
}
class ConsoleIndeterminateBar
{
private const int ProgressBarWidth = 50;
private const char ProgressBarCharacter = '#';
private const char BackgroundCharacter = '-';
public static void DisplayProgressBar()
{
Console.CursorVisible = false;
while (true)
{
for (int i = 0; i <= ProgressBarWidth; i++)
{
Console.SetCursorPosition(i, Console.CursorTop);
Console.Write(ProgressBarCharacter);
Thread.Sleep(50);
}
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(BackgroundCharacter, ProgressBarWidth + 1));
Console.SetCursorPosition(0, Console.CursorTop);
}
}
}