-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
_progress.jl
396 lines (319 loc) · 11.6 KB
/
_progress.jl
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import MyterialColors: orange_light, teal, purple_light, pink
"""
Definition of several type of columns for progress bars.
Used in progress.jl.
"""
# ---------------------------------------------------------------------------- #
# columns #
# ---------------------------------------------------------------------------- #
abstract type AbstractColumn <: AbstractRenderable end
setwidth!(col::AbstractColumn, width::Int) = nothing
# ---------------------------------------------------------------------------- #
# TEXT COLUMNS #
# ---------------------------------------------------------------------------- #
# ---------------------------- description column ---------------------------- #
mutable struct DescriptionColumn <: AbstractColumn
job::ProgressJob
segments::Vector{Segment}
measure::Measure
text::String
function DescriptionColumn(job::ProgressJob; style::String=orange_light)
seg = Segment(job.description, style)
new(job, [seg], seg.measure, seg.text)
end
end
update!(col::DescriptionColumn, args...)::String = col.text
# ----------------------------- separator column ----------------------------- #
struct SeparatorColumn <: AbstractColumn
job::ProgressJob
segments::Vector{Segment}
measure::Measure
text::String
function SeparatorColumn(job::ProgressJob)
seg = Segment("●", pink)
return new(job, [seg], Measure(1, 1), seg.text)
end
end
update!(col::SeparatorColumn, args...)::String = col.text
struct SpaceColumn <: AbstractColumn
job::ProgressJob
segments::Vector{Segment}
measure::Measure
text::String
function SpaceColumn(job::ProgressJob; width=1)
seg = Segment(" "^width, pink)
return new(job, [seg], seg.measure, seg.text)
end
end
update!(col::SpaceColumn, args...)::String = col.text
# ----------------------------- completed column ----------------------------- #
struct CompletedColumn <: AbstractColumn
job::ProgressJob
segments::Vector{Segment}
measure::Measure
text::String
padwidth::Int
style::String
function CompletedColumn(job::ProgressJob; style::String="white bold")
if isnothing(job.N)
seg = Segment(" "^10)
return new(job, [seg], seg.measure, "", 0, style)
else
width = length(string(job.N))*2+1
seg = Segment(" "^width)
text = apply_style("[white bold]/[/white bold][(.1, .8, .4) underline]$(job.N)[/(.1, .8, .4) underline]")
return new(job, [seg], seg.measure, text, length(digits(job.N)), style)
end
end
end
function update!(col::CompletedColumn, color::String, args...)::String
isnothing(col.job.N) && return apply_style(string(col.job.i), col.style)
return apply_style(lpad(string(col.job.i), col.padwidth) * col.text, color*" bold")
end
# ----------------------------- percentage column ---------------------------- #
struct PercentageColumn <: AbstractColumn
job::ProgressJob
segments::Vector{Segment}
measure::Measure
function PercentageColumn(job::ProgressJob)
seg = Segment(" "^4) # "xxx %
return new(job, [seg], seg.measure)
end
end
function update!(col::PercentageColumn, args...)::String
isnothing(col.job.N) && return ""
frac = int(col.job.i / col.job.N * 100)
p = string(frac)
p = frac == 100 ? p : lpad(p, 3)
return "\e[2m"*p*"%\e[0m"
end
# ----------------------------- downloaded column ---------------------------- #
struct DownloadedColumn <: AbstractColumn
job::ProgressJob
segments::Vector{Segment}
measure::Measure
tot_size::String
function DownloadedColumn(job::ProgressJob)
tot_size = get_file_format(job.N)
seg = Segment(" "^(length(tot_size)*2+1))
return new(job, [seg], seg.measure, tot_size)
end
end
function update!(col::DownloadedColumn, args...)::String
isnothing(col.job.N) && return ""
completed = get_file_format(col.job.i)
return lpad(completed * "/" * col.tot_size, col.measure.w)
end
# ---------------------------------------------------------------------------- #
# TIMING COLUMNS #
# ---------------------------------------------------------------------------- #
# ------------------------------ elapsed column ------------------------------ #
struct ElapsedColumn <: AbstractColumn
job::ProgressJob
segments::Vector{Segment}
measure::Measure
style::String
padwidth::Int
ElapsedColumn(job::ProgressJob; style=purple_light) = new(job, [], Measure(6+9, 1), style, 6)
end
function update!(col::ElapsedColumn, args...)::String
isnothing(col.job.startime) && return " "^(col.measure.w)
elapsedtime = (now() - col.job.startime).value # in ms
# format elapsed message
if elapsedtime < 1000
msg = "$(elapsedtime)ms"
elseif elapsedtime < (60 * 1000)
# under a minute
elapsed = round(elapsedtime/1000; digits=2)
msg = "$(elapsed)s"
else
# show minutes
elapsed = round(elapsedtime/(60*1000); digits=2)
msg = "$(elapsed)min"
end
msg = lpad(truncate(msg, col.padwidth), col.padwidth)
return apply_style("elapsed: $(msg)", col.style)
end
# -------------------------------- ETA column -------------------------------- #
struct ETAColumn <: AbstractColumn
job::ProgressJob
segments::Vector{Segment}
measure::Measure
style::String
padwidth::Int
ETAColumn(job::ProgressJob; style=teal) = new(job, [], Measure(7+11, 1), style, 7)
end
function update!(col::ETAColumn, args...)::String
isnothing(col.job.startime) && return " "^(col.measure.w)
isnothing(col.job.N) && return " "^(col.measure.w)
# get remaining time in ms
elapsed = (now() - col.job.startime).value # in ms
perc = col.job.i/col.job.N
remaining = elapsed * (1 - perc) / perc
# format elapsed message
if remaining < 1000
msg = "$(round(remaining; digits=0))ms"
elseif remaining < (60 * 1000)
# under a minute
remaining = round(remaining/1000; digits=2)
msg = "$(remaining)s"
else
# show minutes
remaining = round(remaining/(60*1000); digits=2)
msg = "$(remaining)min"
end
msg = lpad(truncate(msg, col.padwidth), col.padwidth)
return apply_style("remaining: $(msg)", col.style)
end
# ---------------------------------------------------------------------------- #
# PROGRESS COLUMNS #
# ---------------------------------------------------------------------------- #
# ------------------------------ progress column ----------------------------- #
mutable struct ProgressColumn <: AbstractColumn
job::ProgressJob
segments::Vector{Segment}
measure::Measure
nsegs::Int
ProgressColumn(job::ProgressJob) = new(job, Vector{Segment}(), Measure(0, 0), 0)
end
function setwidth!(col::ProgressColumn, width::Int)
col.measure = Measure(width, 1)
col.nsegs = width
end
function update!(col::ProgressColumn, color::String, args...)::String
completed = int(col.nsegs * col.job.i/col.job.N)
remaining = col.nsegs - completed
return apply_style("[" *color*" bold]" * '━'^(completed) * "[/"*color*" bold]"* " "^(remaining))
end
# ------------------------------ spinner columns ----------------------------- #
SPINNERS = Dict(
:dot => Dict(
"period" => 100, "frames" => [
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"( ●)",
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"(● )",
]
),
:circle => Dict(
"period" => 125, "frames" => ["◐", "◓", "◑", "◒"],
),
:toggle => Dict(
"period" => 250, "frames" => ["⦾⦿", "⦿⦾"],
),
:toggle2 => Dict(
"period" => 250, "frames" => ["◯", "⬤",]
),
:bar => Dict(
"period" => 100, "frames" => [
"(= )",
"(== )",
"(=== )",
"( ===)",
"( ==)",
"( =)",
"( =)",
"( ==)",
"( ===)",
"(====)",
"(=== )",
"(== )",
"(= )",
]
),
:greek => Dict(
"period"=> 350, "frames" => [
"ϴ",
"Ω",
"Φ",
"Ο"
]
)
)
mutable struct SpinnerColumn <: AbstractColumn
job::ProgressJob
segments::Vector{Segment}
measure::Measure
frames::Vector{String}
Δt::Float64 # how frequently to update display, in milliseconds
frameidx::Int
nframes::Int
lastupdated::Int
lasttext::String
function SpinnerColumn(job::ProgressJob; spinnertype::Symbol=:dot, style="bold blue")
spinnerdata = copy(SPINNERS[spinnertype])
spinnerdata["frames"] = map(frame -> apply_style(frame, style), spinnerdata["frames"])
seg = Segment(spinnerdata["frames"][1])
new(
job,
[seg],
seg.measure,
spinnerdata["frames"],
spinnerdata["period"],
1,
length(spinnerdata["frames"]),
0,
spinnerdata["frames"][1]
)
end
end
function update!(col::SpinnerColumn, args...)::String
col.job.started || return " "^(col.measure.w)
t = (now() - col.job.startime).value
if t - col.lastupdated >= col.Δt
col.lastupdated = t
col.frameidx = col.frameidx == col.nframes ? 1 : col.frameidx + 1
col.lasttext = col.frames[col.frameidx]
end
return col.lasttext
end
# ---------------------------------------------------------------------------- #
# COLUMNS PRESETS #
# ---------------------------------------------------------------------------- #
function get_columns(columnsset::Symbol)::Vector{DataType}
if columnsset == :minimal
return [
DescriptionColumn,
ProgressColumn,
]
elseif columnsset == :default
return [
DescriptionColumn,
SeparatorColumn,
ProgressColumn,
SeparatorColumn,
CompletedColumn,
PercentageColumn,
]
elseif columnsset == :spinner
return [
DescriptionColumn,
SpaceColumn,
SpinnerColumn,
SpaceColumn,
CompletedColumn,
]
elseif columnsset == :detailed
# extensive
return [
DescriptionColumn,
SeparatorColumn,
ProgressColumn,
SeparatorColumn,
CompletedColumn,
PercentageColumn,
SeparatorColumn,
ElapsedColumn,
ETAColumn
]
else
@warn "Columns name not recognized: $columnsset"
return get_columns(:minimal)
end
end