Skip to content

Commit

Permalink
show data-based progress bars
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Aug 13, 2024
1 parent b6a94e7 commit 54f6963
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
14 changes: 9 additions & 5 deletions src/MiniProgressBars.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Base.@kwdef mutable struct MiniProgressBar
header::String = ""
color::Symbol = :nothing
width::Int = 40
current::Int = 0.0
prev::Int = 0.0
current::Int = 0
prev::Int = 0
has_shown::Bool = false
time_shown::Float64 = 0.0
percentage::Bool = true
mode::Symbol = :percentage # :percentage :int :data
always_reprint::Bool = false
indent::Int = 4
end
Expand Down Expand Up @@ -47,10 +47,14 @@ function show_progress(io::IO, p::MiniProgressBar; termwidth=nothing, carriagere
p.prev = p.current
p.has_shown = true

progress_text = if p.percentage
progress_text = if p.mode == :percentage
@sprintf "%2.1f %%" perc
else
elseif p.mode == :int
string(p.current, "/", p.max)
elseif p.mode == :data
string(Base.format_bytes(p.current), "/", Base.format_bytes(p.max))
else
error("Unknown mode $(p.mode)")
end
termwidth = @something termwidth displaysize(io)[2]
max_progress_width = max(0, min(termwidth - textwidth(p.header) - textwidth(progress_text) - 10 , p.width))
Expand Down
10 changes: 5 additions & 5 deletions src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ function download_artifacts(ctx::Context;
# For each Artifacts.toml, install each artifact we've collected from it
for name in keys(artifacts)
is_done && break
bar = MiniProgressBar(; indent=2, header = rpad(name, longest_name), color = Base.info_color(), percentage=true, always_reprint=true)
bar = MiniProgressBar(; indent=2, header = rpad(name, longest_name), color = Base.info_color(), mode=:data, always_reprint=true)
progress = (total, current) -> (bar.max = total; bar.current = current)
download_states[name] = (true, bar)
push!(download_jobs,
Expand All @@ -881,7 +881,7 @@ function download_artifacts(ctx::Context;
print(io, ansi_disablecursor)
first = true
timer = Timer(0, interval=1/10)
main_bar = MiniProgressBar(; indent=0, header = "Downloading artifacts", color = :green, percentage=false, always_reprint=true)
main_bar = MiniProgressBar(; indent=0, header = "Downloading artifacts", color = :green, mode = :int, always_reprint=true)
main_bar.max = length(download_states)
while !is_done
main_bar.current = length(filter(x -> !x[2][1], download_states))
Expand All @@ -891,7 +891,7 @@ function download_artifacts(ctx::Context;
show_progress(iostr, main_bar; termwidth, carriagereturn=false)
println(iostr)
for (name, (running, bar)) in download_states
running || continue
running && bar.max > 1000 && bar.current > 0 || continue
show_progress(iostr, bar; termwidth, carriagereturn=false)
println(iostr)
n_printed += 1
Expand Down Expand Up @@ -1046,7 +1046,7 @@ function download_source(ctx::Context; readonly=true)
end

bar = MiniProgressBar(; indent=2, header = "Progress", color = Base.info_color(),
percentage=false, always_reprint=true)
mode=:int, always_reprint=true)
bar.max = length(pkgs_to_install)
fancyprint = can_fancyprint(ctx.io)
try
Expand Down Expand Up @@ -1277,7 +1277,7 @@ function build_versions(ctx::Context, uuids::Set{UUID}; verbose=false)
max_name = maximum(build->textwidth(build[2]), builds; init=0)

bar = MiniProgressBar(; indent=2, header = "Progress", color = Base.info_color(),
percentage=false, always_reprint=true)
mode=:int, always_reprint=true)
bar.max = length(builds)
fancyprint = can_fancyprint(ctx.io)
fancyprint && start_progress(ctx.io, bar)
Expand Down

0 comments on commit 54f6963

Please sign in to comment.