Skip to content

Commit 3b13ca7

Browse files
authored
Add a feature to the progress bar that displays additional variables … (#256)
* Add a feature to the progress bar that displays additional variables and their values. ```julia using Term.Progress a = 10 b = 0.1 extra_info = Dict("Current loss" => a, "Learning rate" => b) pbar = ProgressBar(; extra_info) job = addjob!(pbar; N = 5) start!(pbar) for i in 1:5 a = 10-i b = 0.1i println("Epoch $i") pbar.extra_info = Dict("Current loss" => a, "Learning rate" => b) update!(job) render(pbar) sleep(1) end stop!(pbar) ``` And the output will be: ``` Epoch 1 ... ─────────────────────────── progress ─────────────────────────── Running... ● ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ● 5/5 100% Current loss : 5 Learning rate : 0.5 ``` modified: src/progress.jl * Modify the results of the test set corresponding to https://github.com/abcdvvvv/Term.jl/blob/4145a63d52c1185cb828f162931e7d8103a71545/src/progress.jl#L115 modified: test/15_test_progress.jl
1 parent 82bf8dc commit 3b13ca7

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/progress.jl

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ mutable struct ProgressJob
112112
)
113113
return new(
114114
id,
115-
isnothing(N) ? 0 : 1,
115+
0,
116116
N,
117117
description,
118118
columns,
@@ -183,9 +183,11 @@ end
183183
Update a job's progress `i` by setting its value or adding `+1`.
184184
"""
185185
function update!(job::ProgressJob; i = nothing)
186-
(!isnothing(job.N) && job.i job.N) && return stop!(job)
187-
188-
job.i += something(i, 1)
186+
if !isnothing(job.N) && job.i job.N
187+
stop!(job)
188+
else
189+
job.i += something(i, 1)
190+
end
189191
return nothing
190192
end
191193

@@ -249,7 +251,8 @@ Arguments:
249251
- `running`: true if the progress bar is active
250252
- `paused`: false when the bar is running but briefly paused (e.g. to update `jobs`)
251253
- `task`: references a `Task` for updating the progress bar in parallel
252-
- `renderstatus`: a `RenderStatus` instance.
254+
- `renderstatus`: a `RenderStatus` instance
255+
- `extra_info`: a `Dict{String,<:Any}` to show extra information.
253256
"""
254257
mutable struct ProgressBar
255258
jobs::Vector{ProgressJob}
@@ -267,21 +270,23 @@ mutable struct ProgressBar
267270
paused::Bool
268271
task::Union{Task,Nothing}
269272
renderstatus::Any
273+
extra_info::Union{Dict{String,<:Any},Nothing}
270274
end
271275

272276
"""
273277
ProgressBar(;
274-
width::Int=$(default_width()),
278+
width::Int = $(default_width()),
275279
columns::Union{Vector{DataType}, Symbol} = :default,
276280
columns_kwargs::Dict = Dict(),
277-
expand::Bool=false,
281+
expand::Bool = false,
278282
transient::Bool = false,
279283
colors::Vector{RGBColor} = [
280284
RGBColor("(1, .05, .05)"),
281285
RGBColor("(.05, .05, 1)"),
282286
RGBColor("(.05, 1, .05)"),
283287
],
284-
refresh_rate::Int=60, # FPS of rendering
288+
refresh_rate::Int = 60, # FPS of rendering
289+
extra_info::Union{Dict{String,<:Any},Nothing} = nothing,
285290
)
286291
287292
Create a ProgressBar instance.
@@ -294,6 +299,7 @@ function ProgressBar(;
294299
transient::Bool = false,
295300
colors::Union{String,Vector{RGBColor}} = [RGBColor("(1, .05, .05)"), RGBColor("(.05, .05, 1)"), RGBColor("(.05, 1, .05)")],
296301
refresh_rate::Int = 60, # FPS of rendering
302+
extra_info::Union{Dict{String,<:Any},Nothing} = nothing,
297303
)
298304
columns = columns isa Symbol ? get_columns(columns) : columns
299305

@@ -313,6 +319,7 @@ function ProgressBar(;
313319
false,
314320
nothing,
315321
RenderStatus(),
322+
extra_info,
316323
)
317324
end
318325

@@ -485,7 +492,7 @@ function render(pbar::ProgressBar)
485492
end
486493

487494
# get variables
488-
njobs, height = length(pbar.jobs) + 1, console_height()
495+
njobs, height = 1 + length(pbar.jobs) + length(pbar.extra_info), console_height()
489496
iob = pbar.buff
490497

491498
# on the first render, create sticky region
@@ -522,6 +529,14 @@ function render(pbar::ProgressBar)
522529
write(iob, contents * coda)
523530
end
524531

532+
# render the extra information
533+
if !isnothing(pbar.extra_info)
534+
max_len = maximum(length.(keys(pbar.extra_info)))
535+
for (k, v) in pbar.extra_info
536+
write(iob, "\n" * k * " "^(max_len - length(k)) * " : " * string(v))
537+
end
538+
end
539+
525540
# restore position and write
526541
move_to_line(iob, pbar.renderstatus.scrollline)
527542
return print(String(take!(iob)))

test/15_test_progress.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ using ProgressLogging
1515

1616
j1 = addjob!(pbar; description = "test", N = 10)
1717
@test render(j1) isa String # coverage
18-
@test jobcolor(j1) == "(190, 35, 112)"
18+
@test jobcolor(j1) == "(209, 16, 112)"
1919

2020
@test j1.id == 1
2121
@test j1.N == 10
22-
@test j1.i == 1
22+
@test j1.i == 0
2323
@test j1.description == "test"
2424
@test j1.started
2525
@test j1.columns isa Vector{AbstractColumn}
2626

2727
update!(j1)
28-
@test j1.i == 2
28+
@test j1.i == 1
2929
update!(j1; i = 10)
30-
@test j1.i == 12
30+
@test j1.i == 11
3131

3232
@test getjob(pbar, j1.id).id == j1.id
3333

0 commit comments

Comments
 (0)