From 54f696336dc4d80a92b9eb5bc4634720db54514d Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Tue, 13 Aug 2024 16:48:14 +0100 Subject: [PATCH] show data-based progress bars --- src/MiniProgressBars.jl | 14 +++++++++----- src/Operations.jl | 10 +++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/MiniProgressBars.jl b/src/MiniProgressBars.jl index 4a90e237b4..0c87556b40 100644 --- a/src/MiniProgressBars.jl +++ b/src/MiniProgressBars.jl @@ -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 @@ -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)) diff --git a/src/Operations.jl b/src/Operations.jl index 79ba988ee3..1e27368dc5 100644 --- a/src/Operations.jl +++ b/src/Operations.jl @@ -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, @@ -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)) @@ -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 @@ -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 @@ -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)