Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add parameter for fractional bar length #285

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 : trunc(Int, (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 = trunc(Int, (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
24 changes: 21 additions & 3 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,35 @@ println("Testing original interface...")
testfunc(107, 0.01, 0.01)


function testfunc2(n, dt, tsleep, desc, barlen)
function testfunc2A(n, dt, tsleep, desc, barlen)
p = ProgressMeter.Progress(n; dt=dt, desc=desc, barlen=barlen)
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)
testfunc2A(107, 0.01, 0.01, "Computing...", 50)
println("Testing no desc and no progress bar")
testfunc2(107, 0.01, 0.01, "", 0)
testfunc2A(107, 0.01, 0.01, "", 0)


function testfunc2B(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 with fractional size")
testfunc2B(107, 0.01, 0.01, "Computing...", nothing, 1.0)
println("this one should be half the size")
testfunc2B(107, 0.01, 0.01, "Computing...", nothing, 0.5)

println("Combining barlen and fraction")
testfunc2B(107, 0.01, 0.01, "Computing...", 50, 0.5)
println("Testing no desc and no progress bar by scaling to zero")
testfunc2B(107, 0.01, 0.01, "", nothing, 0.0)


function testfunc3(n, tsleep, desc)
Expand Down
Loading