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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 | with Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
package body WL.Processes is
Send_Escape_Sequences : constant Boolean := True;
Progress_Characters : constant String :=
"-\|/";
type Escape_Sequence is array (Positive range <>) of Natural;
Cursor_On : constant Escape_Sequence :=
(16#1B#, 16#5B#, 16#3F#, 16#32#, 16#35#, 16#68#);
Cursor_Off : constant Escape_Sequence :=
(16#1B#, 16#5B#, 16#3F#, 16#32#, 16#35#, 16#6C#);
procedure Send_Escape_Sequence
(Sequence : Escape_Sequence);
function "+" (X : String) return Ada.Strings.Unbounded.Unbounded_String
renames Ada.Strings.Unbounded.To_Unbounded_String;
function "-" (X : Ada.Strings.Unbounded.Unbounded_String) return String
renames Ada.Strings.Unbounded.To_String;
------------
-- Finish --
------------
procedure Finish (Process : in out Process_Type) is
begin
case Process.Display is
when Spinner =>
Ada.Text_IO.Put (Ada.Text_IO.Standard_Error,
Character'Val (8) & ' ');
Ada.Text_IO.Flush (Ada.Text_IO.Standard_Error);
when Bar =>
declare
Count : constant Natural := Process.Bar_Length;
Final : constant String (1 .. Count) :=
(others => '=');
begin
Put
(Ada.Text_IO.Standard_Error,
Character'Val (13)
& (-Process.Name)
& ": ");
if Process.Percent then
Put (Standard_Error, "100% ");
end if;
Put
(Ada.Text_IO.Standard_Error,
"[" & Final & "]");
end;
when Percentage =>
Put (Ada.Text_IO.Standard_Error,
Character'Val (13) &
(-Process.Name) & ": 100%");
when Counter =>
Put (Ada.Text_IO.Standard_Error,
Character'Val (13)
& (-Process.Name) & ":"
& Natural'Image (Process.Tick));
end case;
Process.Tick := 0;
New_Line (Ada.Text_IO.Standard_Error);
Send_Escape_Sequence (Cursor_On);
exception
when others =>
Send_Escape_Sequence (Cursor_On);
raise;
end Finish;
--------------------------
-- Send_Escape_Sequence --
--------------------------
procedure Send_Escape_Sequence
(Sequence : Escape_Sequence)
is
begin
if Send_Escape_Sequences then
for Code of Sequence loop
Ada.Text_IO.Put (Ada.Text_IO.Standard_Error,
Character'Val (Code));
end loop;
end if;
end Send_Escape_Sequence;
---------------
-- Start_Bar --
---------------
procedure Start_Bar
(Process : out Process_Type;
Name : String;
Finish : Positive;
With_Percentage : Boolean := False;
Bar_Length : Natural := 40;
Tick_Size : Positive := 1)
is
Spaces : constant String (1 .. Process.Bar_Length) :=
(others => ' ');
begin
Send_Escape_Sequence (Cursor_Off);
Put (Ada.Text_IO.Standard_Error,
Name & ": [" & Spaces & "]");
Flush (Ada.Text_IO.Standard_Error);
Process.Name := +Name;
Process.Display := Bar;
Process.Percent := With_Percentage;
Process.Tick := 0;
Process.Finish := Finish;
Process.Step := Tick_Size;
Process.Acc := 1;
Process.Bar_Length := Natural'Min (Bar_Length, Finish);
end Start_Bar;
-------------------
-- Start_Counter --
-------------------
procedure Start_Counter (Process : out Process_Type;
Name : String;
Tick_Size : Positive := 1)
is
begin
Put (Ada.Text_IO.Standard_Error, Name & ": 0");
Flush (Ada.Text_IO.Standard_Error);
Process.Name := +Name;
Process.Display := Counter;
Process.Tick := 0;
Process.Step := Tick_Size;
Process.Prev := Ada.Calendar.Clock;
Send_Escape_Sequence (Cursor_Off);
end Start_Counter;
----------------------
-- Start_Percentage --
----------------------
procedure Start_Percentage
(Process : out Process_Type;
Name : String;
Finish : Positive;
Tick_Size : Positive := 1)
is
begin
Put (Ada.Text_IO.Standard_Error, Name & ": 0%");
Flush (Ada.Text_IO.Standard_Error);
Process.Name := +Name;
Process.Display := Percentage;
Process.Tick := 0;
Process.Finish := Finish;
Process.Step := Tick_Size;
Process.Acc := 1;
Send_Escape_Sequence (Cursor_Off);
end Start_Percentage;
-------------------
-- Start_Spinner --
-------------------
procedure Start_Spinner
(Process : out Process_Type;
Name : String;
Tick_Size : Positive := 1)
is
begin
Put (Ada.Text_IO.Standard_Error, Name & ": " &
Progress_Characters (1));
Flush (Ada.Text_IO.Standard_Error);
Process.Display := Spinner;
Process.Tick := 0;
Process.Step := Tick_Size;
Process.Acc := 1;
Send_Escape_Sequence (Cursor_Off);
end Start_Spinner;
----------
-- Tick --
----------
procedure Tick (Process : in out Process_Type) is
begin
Process.Tick := Process.Tick + 1;
if Process.Tick > Process.Finish then
Process.Tick := Process.Finish;
end if;
if Process.Tick mod Process.Step = 0 then
case Process.Display is
when Spinner =>
Process.Acc := Process.Acc + 1;
if Process.Acc > Progress_Characters'Last then
Process.Acc := 1;
end if;
Ada.Text_IO.Put (Ada.Text_IO.Standard_Error,
Character'Val (8) &
Progress_Characters (Process.Acc));
Ada.Text_IO.Flush (Ada.Text_IO.Standard_Error);
when Bar =>
declare
Count : constant Natural :=
Process.Finish / Process.Step;
Current : constant Natural :=
Process.Tick / Process.Step;
Bar_Step : constant Natural :=
Count / Process.Bar_Length;
Partial : constant Natural :=
Process.Tick mod Bar_Step;
Filled_Length : constant Natural :=
Current * Process.Bar_Length
/ Count;
Half_Length : constant Natural :=
(if Partial > Bar_Step / 2
and then Current < Count
then 1 else 0);
Empty_Length : constant Natural :=
Process.Bar_Length
- Filled_Length - Half_Length;
Filled : constant String (1 .. Filled_Length) :=
(others => '=');
Half : constant String (1 .. Half_Length) :=
(others => '-');
Empty : constant String (1 .. Empty_Length) :=
(others => ' ');
Bar : constant String := Filled & Half & Empty;
begin
if Current * 2 + Half_Length /= Process.Last_Value then
Put
(Ada.Text_IO.Standard_Error,
Character'Val (13)
& (-Process.Name)
& ": ");
if Process.Percent then
Ada.Integer_Text_IO.Put
(Standard_Error,
100 * Current / Count, 3);
Ada.Text_IO.Put
(Standard_Error,
"% ");
end if;
Ada.Text_IO.Put
(Standard_Error,
"[" & Bar & "]");
Process.Last_Value := Current * 2 + Half_Length;
Flush (Ada.Text_IO.Standard_Error);
end if;
end;
when Percentage =>
declare
Last_Value : constant Natural :=
Natural (Float (Process.Tick - 1)
/ Float (Process.Finish) * 100.0);
Value : constant Natural :=
Natural (Float (Process.Tick)
/ Float (Process.Finish) * 100.0);
begin
if Last_Value /= Value then
Put (Ada.Text_IO.Standard_Error,
Character'Val (13) &
(-Process.Name) & ":"
& Natural'Image (Value) & "%");
Flush (Ada.Text_IO.Standard_Error);
end if;
end;
when Counter =>
declare
use Ada.Calendar;
Now : constant Time := Clock;
begin
if Now - Process.Prev > 0.1 then
Put (Ada.Text_IO.Standard_Error,
Character'Val (13)
& (-Process.Name) & ":"
& Natural'Image (Process.Tick));
Flush (Ada.Text_IO.Standard_Error);
Process.Prev := Now;
end if;
end;
end case;
end if;
exception
when others =>
Send_Escape_Sequence (Cursor_On);
raise;
end Tick;
end WL.Processes;
|