From 237e252a576a9641a4a39c2155a2e0afc445ad8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Hedstr=C3=B6m?= Date: Tue, 14 Nov 2023 23:32:29 +0100 Subject: [PATCH] add parameter for fractional bar length --- src/ProgressMeter.jl | 13 ++++++++----- src/deprecated.jl | 4 ++-- test/deprecated.jl | 3 ++- test/test.jl | 28 +++++++++++++++------------- test/test_float.jl | 22 +++++++++++----------- test/test_showvalues.jl | 32 ++++++++++++++++---------------- 6 files changed, 54 insertions(+), 48 deletions(-) diff --git a/src/ProgressMeter.jl b/src/ProgressMeter.jl index 4b03c62..d0be183 100644 --- a/src/ProgressMeter.jl +++ b/src/ProgressMeter.jl @@ -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 @@ -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, @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/deprecated.jl b/src/deprecated.jl index 0cb5d6b..2329375 100644 --- a/src/deprecated.jl +++ b/src/deprecated.jl @@ -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...) diff --git a/test/deprecated.jl b/test/deprecated.jl index 3c2f7dc..c6d68ce 100644 --- a/test/deprecated.jl +++ b/test/deprecated.jl @@ -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 diff --git a/test/test.jl b/test/test.jl index 0a86bd5..b99dc7d 100644 --- a/test/test.jl +++ b/test/test.jl @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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) diff --git a/test/test_float.jl b/test/test_float.jl index 9908da3..65e1b0f 100644 --- a/test/test_float.jl +++ b/test/test_float.jl @@ -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), @@ -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) @@ -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) diff --git a/test/test_showvalues.jl b/test/test_showvalues.jl index 50d80f4..abde0f3 100644 --- a/test/test_showvalues.jl +++ b/test/test_showvalues.jl @@ -6,30 +6,30 @@ 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"), @@ -37,11 +37,11 @@ function testfunc3(n, dt, tsleep, desc, barlen) 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"), @@ -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) @@ -63,8 +63,8 @@ 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)] @@ -72,7 +72,7 @@ function testfunc5(n, dt, tsleep, desc, barlen) 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:") @@ -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))