-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogressbar.pas
More file actions
78 lines (66 loc) · 1.96 KB
/
progressbar.pas
File metadata and controls
78 lines (66 loc) · 1.96 KB
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
{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
uses
sysutils,
dterm;
const
Stage: array[0 .. 3] of Char = ('/', '-', '\', '|');
var
I: LongInt;
Percent: AnsiString;
// If user interrupts the loop with Ctrl+C, color for the terminal would not
// be restored at the end of the program, so we want to do it in the Ctrl+C
// handler.
function OnCtrlC(CtrlBreak: Boolean): Boolean;
begin
ResetTerminalColor;
Exit(False);
end;
begin
SysSetCtrlBreakHandler(@OnCtrlC);
StartTerminalStatusLine;
for I := 0 to 100 do begin
Percent := IntToStr(I);
while Length(Percent) < 3 do
Percent := ' ' + Percent;
case I of
10: begin
SetTerminalColor(TERMINAL_COLOR_GREEN);
UpdateTerminalStatusLine('A line can be emitted while maintaining progress bar');
FinishTerminalStatusLine;
StartTerminalStatusLine;
ResetTerminalColor;
end;
20: begin
SetTerminalColor(TERMINAL_COLOR_YELLOW);
UpdateTerminalStatusLine('And even');
FinishTerminalStatusLine;
SetTerminalColor(TERMINAL_COLOR_YELLOW);
Writeln('several');
Writeln('lines');
StartTerminalStatusLine;
ResetTerminalColor;
end;
30: begin
SetTerminalColor(TERMINAL_COLOR_RED);
UpdateTerminalStatusLine('Now just wait until 100% or interrupt it with Ctrl-C');
FinishTerminalStatusLine;
StartTerminalStatusLine;
ResetTerminalColor;
end;
end;
UpdateTerminalStatusLine('[' + Percent + '%]'
+ ' simulating activity '
+ Stage[I mod Length(Stage)]
+ ' (Ctrl-C to stop)');
if ParamStr(1) <> 'nosleep' then
Sleep(600);
end;
SetTerminalColor(TERMINAL_COLOR_GREEN);
UpdateTerminalStatusLine('[100%] completed!');
FinishTerminalStatusLine;
// don't forget to restore color
ResetTerminalColor;
end.