Skip to content

Commit

Permalink
add support for offset in ProgressUnknown (#217)
Browse files Browse the repository at this point in the history
* add support for offset in ProgressUnknown

* add support for offset in ProgressUnknown

* add test for cancel with offset and keep

* add more descritpive logging in test

---------

Signed-off-by: MarcMush <marc.ittel@gmail.com>
  • Loading branch information
MarcMush authored Aug 29, 2023
1 parent e213f2b commit 9b178b6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
29 changes: 24 additions & 5 deletions src/ProgressMeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,28 @@ mutable struct ProgressUnknown <: AbstractProgress
spinner::Bool # show a spinner
output::IO # output stream into which the progress is written
numprintedvalues::Int # num values printed below progress in last iteration
offset::Int # position offset of progress bar (default is 0)
enabled::Bool # is the output enabled
showspeed::Bool # should the output include average time per iteration
check_iterations::Int
prev_update_count::Int
threads_used::Vector{Int}
end

function ProgressUnknown(;dt::Real=0.1, desc::AbstractString="Progress: ", color::Symbol=:green, spinner::Bool=false, output::IO=stderr, enabled::Bool = true, showspeed::Bool = false)
function ProgressUnknown(;
dt::Real=0.1,
desc::AbstractString="Progress: ",
color::Symbol=:green,
spinner::Bool=false,
output::IO=stderr,
offset::Integer=0,
enabled::Bool = true,
showspeed::Bool = false)
CLEAR_IJULIA[] = clear_ijulia()
reentrantlocker = Threads.ReentrantLock()
tinit = tlast = time()
printed = false
ProgressUnknown(false, reentrantlocker, dt, 0, 0, false, tinit, tlast, printed, desc, color, spinner, output, 0, enabled, showspeed, 1, 1, Int[])
ProgressUnknown(false, reentrantlocker, dt, 0, 0, false, tinit, tlast, printed, desc, color, spinner, output, 0, offset, enabled, showspeed, 1, 1, Int[])
end

ProgressUnknown(dt::Real, desc::AbstractString="Progress: ",
Expand Down Expand Up @@ -400,9 +409,12 @@ spinner_char(p::ProgressUnknown, spinner::AbstractVector{<:AbstractChar}) =
spinner_char(p::ProgressUnknown, spinner::AbstractString) =
p.done ? spinner_done : spinner[nextind(spinner, 1, p.spincounter % length(spinner))]

function updateProgress!(p::ProgressUnknown; showvalues = (), truncate_lines = false, valuecolor = :blue, desc = p.desc,
ignore_predictor = false, spinner::Union{AbstractChar,AbstractString,AbstractVector{<:AbstractChar}} = spinner_chars)
function updateProgress!(p::ProgressUnknown; showvalues = (), truncate_lines = false,
valuecolor = :blue, desc = p.desc, ignore_predictor = false,
spinner::Union{AbstractChar,AbstractString,AbstractVector{<:AbstractChar}} = spinner_chars,
offset::Integer = p.offset, keep = (offset == 0))
!p.enabled && return
p.offset = offset
p.desc = desc
if p.done
if p.printed
Expand All @@ -419,10 +431,15 @@ function updateProgress!(p::ProgressUnknown; showvalues = (), truncate_lines = f
sec_per_iter = elapsed_time / p.counter
msg = @sprintf "%s (%s)" msg speedstring(sec_per_iter)
end
print(p.output, "\n" ^ (p.offset + p.numprintedvalues))
move_cursor_up_while_clearing_lines(p.output, p.numprintedvalues)
printover(p.output, msg, p.color)
printvalues!(p, showvalues; color = valuecolor, truncate = truncate_lines)
println(p.output)
if keep
println(p.output)
else
print(p.output, "\r\u1b[A" ^ (p.offset + p.numprintedvalues))
end
flush(p.output)
end
return
Expand All @@ -445,9 +462,11 @@ function updateProgress!(p::ProgressUnknown; showvalues = (), truncate_lines = f
sec_per_iter = elapsed_time / p.counter
msg = @sprintf "%s (%s)" msg speedstring(sec_per_iter)
end
print(p.output, "\n" ^ (p.offset + p.numprintedvalues))
move_cursor_up_while_clearing_lines(p.output, p.numprintedvalues)
printover(p.output, msg, p.color)
printvalues!(p, showvalues; color = valuecolor, truncate = truncate_lines)
print(p.output, "\r\u1b[A" ^ (p.offset + p.numprintedvalues))
flush(p.output)
# Compensate for any overhead of printing. This can be
# especially important if you're running over a slow network
Expand Down
18 changes: 17 additions & 1 deletion test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ end
@test ProgressMeter.ProgressThresh(1.0f0; desc = "Desc: ") isa ProgressMeter.ProgressThresh{Float32}

# Threshold-based progress reports with increment=false
println("Testing threshold-based progress")
println("Testing threshold-based progress with increment=false")
prog = ProgressMeter.ProgressThresh(1e-5, "Minimizing:")
for val in 10 .^ range(2, stop=-6, length=20)
ProgressMeter.update!(prog, val; increment=false)
Expand Down Expand Up @@ -438,3 +438,19 @@ function testfunc19()
end
println("Testing speed display with no update")
testfunc19()

function testfunc20(r, p)
for i in r
sleep(0.03)
update!(p, i)
end
cancel(p; keep=true)
end
println("Testing early cancel")
testfunc20(1:50, Progress(100))
testfunc20(1:50, ProgressUnknown())
testfunc20(50:-1:1, ProgressThresh(0))
println("Testing early cancel with offset 1 and keep")
testfunc20(1:50, Progress(100, offset=1))
testfunc20(1:50, ProgressUnknown(offset=1))
testfunc20(50:-1:1, ProgressThresh(0, offset=1))
12 changes: 12 additions & 0 deletions test/test_float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ function testfunc5(n, tsleep)
print("\n" ^ 2)
end
testfunc5(5, 0.1)

println("Testing floating unknown progress bar (offset 2)")
function testfunc6(desc, n, tsleep, offset)
p = ProgressMeter.ProgressUnknown(desc; offset=offset)
for i = 1:n
sleep(tsleep)
ProgressMeter.next!(p)
end
ProgressMeter.finish!(p)
print("\n" ^ 3)
end
testfunc6("Computing... ", 100, 0.02, 2)

0 comments on commit 9b178b6

Please sign in to comment.