Skip to content

Commit

Permalink
add parameter for fractional bar length
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Hedström committed Nov 21, 2023
1 parent 5b3bd1d commit 237e252
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 48 deletions.
13 changes: 8 additions & 5 deletions src/ProgressMeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ mutable struct Progress <: AbstractProgress
printed::Bool # true if we have issued at least one status update
desc::String # prefix to the percentage, e.g. "Computing..."
barlen::Union{Int,Nothing} # progress bar size (default is available terminal width)
barlen_fraction::Float64 # fraction of progress bar size
barglyphs::BarGlyphs # the characters to be used in the bar
color::Symbol # default to green
output::IO # output stream into which the progress is written
Expand All @@ -85,6 +86,7 @@ mutable struct Progress <: AbstractProgress
color::Symbol=:green,
output::IO=stderr,
barlen=nothing,
barlen_fraction=1.0,
barglyphs::BarGlyphs=BarGlyphs('|','', Sys.iswindows() ? '' : ['','','','','','',''],' ','|',),
offset::Integer=0,
start::Integer=0,
Expand All @@ -96,7 +98,8 @@ mutable struct Progress <: AbstractProgress
counter = start
tinit = tsecond = tlast = time()
printed = false
new(n, reentrantlocker, dt, counter, tinit, tsecond, tlast, printed, desc, barlen, barglyphs, color, output, offset, 0, start, enabled, showspeed, 1, 1, Int[])
barlen = barlen isa Nothing ? barlen : Int(round(barlen*barlen_fraction))
new(n, reentrantlocker, dt, counter, tinit, tsecond, tlast, printed, desc, barlen, barlen_fraction, barglyphs, color, output, offset, 0, start, enabled, showspeed, 1, 1, Int[])
end
end

Expand Down Expand Up @@ -202,8 +205,8 @@ function ProgressUnknown(;
end

#...length of percentage and ETA string with days is 29 characters, speed string is always 14 extra characters
function tty_width(desc, output, showspeed::Bool)
full_width = displaysize(output)[2]
function tty_width(desc, output, showspeed::Bool, width_fraction::Float64 = 1.0)
full_width = Int(round(displaysize(output)[2]*width_fraction))
desc_width = length(desc)
eta_width = 29
speed_width = showspeed ? 14 : 0
Expand Down Expand Up @@ -260,7 +263,7 @@ function updateProgress!(p::Progress; showvalues = (),
if p.counter >= p.n
if p.counter == p.n && p.printed
t = time()
barlen = p.barlen isa Nothing ? tty_width(p.desc, p.output, p.showspeed) : p.barlen
barlen = p.barlen isa Nothing ? tty_width(p.desc, p.output, p.showspeed, p.barlen_fraction) : p.barlen
percentage_complete = 100.0 * p.counter / p.n
bar = barstring(barlen, percentage_complete, barglyphs=p.barglyphs)
elapsed_time = t - p.tinit
Expand Down Expand Up @@ -290,7 +293,7 @@ function updateProgress!(p::Progress; showvalues = (),
p.check_iterations = calc_check_iterations(p, t)
end
if t > p.tlast+p.dt
barlen = p.barlen isa Nothing ? tty_width(p.desc, p.output, p.showspeed) : p.barlen
barlen = p.barlen isa Nothing ? tty_width(p.desc, p.output, p.showspeed, p.barlen_fraction) : p.barlen
percentage_complete = 100.0 * p.counter / p.n
bar = barstring(barlen, percentage_complete, barglyphs=p.barglyphs)
elapsed_time = t - p.tinit
Expand Down
4 changes: 2 additions & 2 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@deprecate Progress(n::Integer, dt::Real, desc::AbstractString="Progress: ",
barlen=nothing, color::Symbol=:green, output::IO=stderr;
offset::Integer=0) Progress(n; dt=dt, desc=desc, barlen=barlen, color=color, output=output, offset=offset)
barlen=nothing, barlen_fraction::Float64=1.0, color::Symbol=:green, output::IO=stderr;
offset::Integer=0) Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction, color=color, output=output, offset=offset)

@deprecate Progress(n::Integer, desc::AbstractString, offset::Integer=0; kwargs...) Progress(n; desc=desc, offset=offset, kwargs...)

Expand Down
3 changes: 2 additions & 1 deletion test/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ end
println("Testing deprecated Progress building")

@test_deprecated begin
local p = Progress(10, 23, "ABC", 47, :red, stdout)
local p = Progress(10, 23, "ABC", 47, 0.5, :red, stdout)
@test p.n == 10
@test p.dt == 23
@test p.desc == "ABC"
@test p.barlen == 47
@test p.barlen_fraction == 0.5
@test p.color == :red
@test p.output == stdout
end
Expand Down
28 changes: 15 additions & 13 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ println("Testing original interface...")
testfunc(107, 0.01, 0.01)


function testfunc2(n, dt, tsleep, desc, barlen)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen)
function testfunc2(n, dt, tsleep, desc, barlen, barlen_fraction)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction)
for i = 1:n
sleep(tsleep)
ProgressMeter.next!(p)
end
end
println("Testing desc and progress bar")
testfunc2(107, 0.01, 0.01, "Computing...", 50)
testfunc2(107, 0.01, 0.01, "Computing...", 50, 0.5)
println("Testing no desc and no progress bar")
testfunc2(107, 0.01, 0.01, "", 0)
testfunc2(107, 0.01, 0.01, "", 0, 1.0)
testfunc2(107, 0.01, 0.01, "", 100, 0.0)


function testfunc3(n, tsleep, desc)
Expand Down Expand Up @@ -52,8 +53,8 @@ end
println("Testing that not even 1% required...")
testfunc4()

function testfunc5A(n, dt, tsleep, desc, barlen)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen)
function testfunc5A(n, dt, tsleep, desc, barlen, barlen_fraction)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction)
for i = 1:round(Int, floor(n/2))
sleep(tsleep)
ProgressMeter.next!(p)
Expand All @@ -65,10 +66,10 @@ function testfunc5A(n, dt, tsleep, desc, barlen)
end

println("\nTesting changing the bar color")
testfunc5A(107, 0.01, 0.01, "Computing...", 50)
testfunc5A(107, 0.01, 0.01, "Computing...", 50, 1.0)

function testfunc5B(n, dt, tsleep, desc, barlen)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen)
function testfunc5B(n, dt, tsleep, desc, barlen, barlen_fraction)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction)
for i = 1:n
sleep(tsleep)
ProgressMeter.next!(p)
Expand All @@ -80,7 +81,8 @@ function testfunc5B(n, dt, tsleep, desc, barlen)
end

println("\nTesting changing the description")
testfunc5B(107, 0.01, 0.02, "Step 1...", 50)
testfunc5B(107, 0.01, 0.02, "Step 1...", 50, 1.0)
testfunc5B(107, 0.01, 0.02, "Step 1...", 50, 0.5)


function testfunc6(n, dt, tsleep)
Expand Down Expand Up @@ -214,7 +216,7 @@ function testfunc13a()
end
# full keyword arguments
start = 15
p = ProgressMeter.Progress(n; dt=0.01, desc="", color=:red, output=stderr, barlen=40, start = start)
p = ProgressMeter.Progress(n; dt=0.01, desc="", color=:red, output=stderr, barlen=40, barlen_fraction=0.8, start = start)
for i in 1:n-start
sleep(0.05)
ProgressMeter.next!(p)
Expand All @@ -223,7 +225,7 @@ end

function testfunc13b()
# same with keyword arguments only
@showprogress dt=0.01 color=:red output=stderr barlen=40 for i=1:15
@showprogress dt=0.01 color=:red output=stderr barlen=40 barlen_fraction=0.8 for i=1:15
sleep(0.1)
end
end
Expand All @@ -248,7 +250,7 @@ function testfunc14(barglyphs)
ProgressMeter.next!(p)
end
p = ProgressMeter.Progress(n, dt=0.01, desc="",
color=:red, output=stderr, barlen=40,
color=:red, output=stderr, barlen=40, barlen_fraction=0.5,
barglyphs=ProgressMeter.BarGlyphs(barglyphs))
for i in 1:n
sleep(0.05)
Expand Down
22 changes: 11 additions & 11 deletions test/test_float.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
println("Testing floating normal progress bar (offset 4)")
function testfunc1(n, dt, tsleep, desc, barlen, offset)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, offset=offset)
function testfunc1(n, dt, tsleep, desc, barlen, barlen_fraction, offset)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction, offset=offset)
for i = 1:n
sleep(tsleep)
ProgressMeter.next!(p)
end
print("\n" ^ 5)
end
testfunc1(30, 0.1, 0.1, "progress ", 70, 4)
testfunc1(30, 0.1, 0.1, "progress ", 70, 0.8, 4)

println("Testing floating normal progress bars with values and keep (2 levels)")
function testfunc2(n, dt, tsleep, desc, barlen)
p1 = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, offset=0)
p2 = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, offset=5)
function testfunc2(n, dt, tsleep, desc, barlen, barlen_fraction)
p1 = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction, offset=0)
p2 = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction, offset=5)
for i = 1:n
sleep(tsleep)
ProgressMeter.next!(p1; showvalues = [(:i, i),
Expand All @@ -22,12 +22,12 @@ function testfunc2(n, dt, tsleep, desc, barlen)
end
print("\n" ^ 10)
end
testfunc2(50, 0.2, 0.2, "progress ", 70)
testfunc2(50, 0.2, 0.2, "progress ", 70, 0.8)

println("Testing floating normal progress bars with changing offset")
function testfunc3(n, dt, tsleep, desc, barlen)
p1 = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, offset=0)
p2 = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen,offset=1)
function testfunc3(n, dt, tsleep, desc, barlen, barlen_fraction)
p1 = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction, offset=0)
p2 = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction, offset=1)
for i = 1:n
sleep(tsleep)
ProgressMeter.next!(p1; showvalues = [(:i, i) for _ in 1:i], keep=false)
Expand All @@ -36,7 +36,7 @@ function testfunc3(n, dt, tsleep, desc, barlen)
end
print("\n" ^ (10 + 5))
end
testfunc3(10, 0.2, 0.5, "progress ", 70)
testfunc3(10, 0.2, 0.5, "progress ", 70, 0.8)

println("Testing floating thresh progress bar (offset 2)")
function testfunc4(thresh, dt, tsleep, desc, offset)
Expand Down
32 changes: 16 additions & 16 deletions test/test_showvalues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,42 @@ ProgressMeter.ijulia_behavior(ijulia_behavior)
lazy_no_lazy(values) = (rand() < 0.5) ? values : () -> values

println("Testing showvalues with a Dict (2 values)")
function testfunc1(n, dt, tsleep, desc, barlen)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen)
function testfunc1(n, dt, tsleep, desc, barlen, barlen_fraction)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction)
for i = 1:n
sleep(tsleep)
values = Dict(:i => i, :halfdone => (i >= n/2))
ProgressMeter.next!(p; showvalues = lazy_no_lazy(values))
end
end
testfunc1(50, 1, 0.2, "progress ", 70)
testfunc1(50, 1, 0.2, "progress ", 70, 0.8)

println("Testing showvalues with an Array of tuples (4 values)")
function testfunc2(n, dt, tsleep, desc, barlen)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen)
function testfunc2(n, dt, tsleep, desc, barlen, barlen_fraction)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction)
for i = 1:n
sleep(tsleep)
values = [(:i, i), (:constant, "foo"), (:isq, i^2), (:large, 2^i)]
ProgressMeter.next!(p; showvalues = lazy_no_lazy(values))
end
end
testfunc2(30, 1, 0.2, "progress ", 60)
testfunc2(30, 1, 0.2, "progress ", 60, 0.8)

println("Testing showvalues when types of names differ (3 values)")
function testfunc3(n, dt, tsleep, desc, barlen)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen)
function testfunc3(n, dt, tsleep, desc, barlen, barlen_fraction)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction)
for i = 1:n
sleep(tsleep)
values = [(:i, i*10), ("constant", "foo"),
("foobar", round(i*tsleep, digits=4))]
ProgressMeter.next!(p; showvalues = lazy_no_lazy(values))
end
end
testfunc3(30, 1, 0.2, "progress ", 70)
testfunc3(30, 1, 0.2, "progress ", 70, 0.8)

println("Testing progress with showing values when num values to print changes between iterations")
function testfunc4(n, dt, tsleep, desc, barlen)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen)
function testfunc4(n, dt, tsleep, desc, barlen, barlen_fraction)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction)
for i = 1:n
sleep(tsleep)
values_pool = [(:i, i*10), ("constant", "foo"),
Expand All @@ -50,7 +50,7 @@ function testfunc4(n, dt, tsleep, desc, barlen)
ProgressMeter.next!(p; showvalues = lazy_no_lazy(values))
end
end
testfunc4(30, 1, 0.2, "opt steps ", 70)
testfunc4(30, 1, 0.2, "opt steps ", 70, 0.8)

println("Testing showvalues with changing number of lines")
prog = ProgressMeter.Progress(50)
Expand All @@ -63,16 +63,16 @@ for i in 1:50
end

println("Testing showvalues with a different color (1 value)")
function testfunc5(n, dt, tsleep, desc, barlen)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen)
function testfunc5(n, dt, tsleep, desc, barlen, barlen_fraction)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen, barlen_fraction=barlen_fraction)
for i = 1:n
sleep(tsleep)
values = [(:large, 2^i)]
ProgressMeter.next!(p; showvalues = lazy_no_lazy(values),
valuecolor = :yellow)
end
end
testfunc5(10, 1, 0.2, "progress ", 40)
testfunc5(10, 1, 0.2, "progress ", 40, 0.8)

println("Testing showvalues with threshold-based progress")
prog = ProgressMeter.ProgressThresh(1e-5; desc="Minimizing:")
Expand All @@ -93,7 +93,7 @@ ProgressMeter.finish!(prog)


println("Testing showvalues with early cancel")
prog = ProgressMeter.Progress(100; dt=1, desc="progress: ", barlen=70)
prog = ProgressMeter.Progress(100; dt=1, desc="progress: ", barlen=70, barlen_fraction=0.8)
for i in 1:50
values = Dict(:left => 100 - i)
ProgressMeter.update!(prog, i; showvalues = lazy_no_lazy(values))
Expand Down

0 comments on commit 237e252

Please sign in to comment.