-
Notifications
You must be signed in to change notification settings - Fork 4
/
Demo.cs
executable file
·166 lines (144 loc) · 5.84 KB
/
Demo.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Linq;
using System.Threading;
using Yaap;
using static Yaap.YaapConsole;
using static System.Linq.Enumerable;
namespace Demo
{
static class Demo
{
static void ResetCursor()
{
Console.CursorTop = 0;
Console.CursorLeft = 0;
}
static void Main(string[] args)
{
var demos = new Action[] {Demo1, Demo2, Demo3, Demo4, Demo5, Demo6, Demo7};
var startDemo = args.Length > 0
? (int.TryParse(args[0], out var tmp) ? tmp : 1)
: 1;
var lastDemo = args.Length > 0 ? startDemo : demos.Length;
for (var i = startDemo - 1; i < lastDemo; i++) {
ResetCursor();
ClearScreen();
demos[i]();
}
}
static void Demo1()
{
WriteLine("Here's a plain vanilla progress bar (_can_ be with nice smooth unicode even on windows*)");
WriteLine("It's width is constrained to 100 characters in total");
WriteLine("* for more on Windows, go to http://xxxxx");
WriteLine();
foreach (var i in Range(0, 200).Yaap(settings: new YaapSettings {Description = "regular", Width = 100})) {
Thread.Sleep(100);
switch (i) {
case 50:
WriteLine("The (re)drawing of the progress bar, happens in the background");
break;
case 100:
Write("As long as you use YaapConsole.Write* methods....");
break;
case 150:
WriteLine(" ... you can continue writing to the terminal");
break;
}
}
}
static void Demo2()
{
WriteLine("Here's the same demo as before, but this time with colors");
WriteLine();
foreach (var _ in Range(0, 200).Yaap(settings: new YaapSettings
{Description = "regular", Width = 100, ColorScheme = YaapColorScheme.Bright}))
Thread.Sleep(100);
}
static void Demo3()
{
WriteLine("When in color mode, Yaap can also express pauses and detect stalls");
WriteLine();
var yaap = Range(0, 2000).Yaap(settings: new YaapSettings {
Description = "regular",
Width = 100,
ColorScheme = YaapColorScheme.Bright,
SmoothingFactor = 0.5,
});
foreach (var i in yaap) {
if (i == 900) {
yaap.State = YaapState.Paused;
Thread.Sleep(5000);
yaap.State = YaapState.Running;
continue;
}
if (i == 1900) {
yaap.State = YaapState.Stalled;
Thread.Sleep(5000);
yaap.State = YaapState.Running;
continue;
}
Thread.Sleep(10);
}
}
static void Demo4()
{
WriteLine("Here's a progress bar that adapts to the width of the terminal");
WriteLine("It's pre-configured to slow down, and the rate/time estimation uses EMA to adapt more quickly");
WriteLine();
foreach (var i in Range(0, 200).Yaap(settings: new YaapSettings
{Description = "smoothing", SmoothingFactor = 0.1, MetricAbbreviations = true}))
Thread.Sleep(i / 2);
}
static void Demo5()
{
WriteLine("You can even have nested loops, each with its own progress bar");
WriteLine("These bars also use metric abbreviation(s) for the progress/rate/total counts");
WriteLine();
foreach (var i in Range(0, 3).Yaap(settings: new YaapSettings { Description = "nested1", MetricAbbreviations = true})) {
foreach (var j in Range(0, 10).Yaap(settings: new YaapSettings { Description = "nested2", MetricAbbreviations = true})) {
foreach (var k in Range(0, 100_000_000).Yaap(settings: new YaapSettings { Description = "nested3", MetricAbbreviations = true})) {
;
}
}
}
}
static void Demo6()
{
WriteLine("You can also launch multiple threads and have them progress independently");
WriteLine("While still updating the progress bars in a coherent way...");
WriteLine();
using (var mre = new ManualResetEvent(false))
using (var allReady = new CountdownEvent(10)) {
var threads = Range(0, 10).Select(ti => new Thread(() => {
var r = new Random((int) (DateTime.Now.Ticks % int.MaxValue));
var y = Range(0, 200).Yaap(settings: new YaapSettings
{Description = $"thread{ti}", VerticalPosition = ti});
allReady.Signal();
mre.WaitOne();
foreach (var _ in y) {
Thread.Sleep(r.Next(90, 110) / (ti + 1));
}
})).ToList();
foreach (var t in threads) {
t.Start();
}
allReady.Wait();
mre.Set();
foreach (var t in threads) {
t.Join();
}
}
}
static void Demo7()
{
foreach (var i in Range(0, 200).Yaap(settings: new YaapSettings {
Description = "bottom-pinned", Width = 100,
Positioning = YaapPositioning.FixToBottom,
})) {
Thread.Sleep(100);
WriteLine($"Scrolling is fun! ({i}/200)");
}
}
}
}