Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
decorate progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Jan 4, 2025
1 parent ba3a74d commit 55c2158
Showing 1 changed file with 45 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/install_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@ pub async fn install_multi(
state.counter += 1;
if state.counter == n && !silent {
let bar = ProgressBar::new(state.total_size);
bar.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
bar.set_style(ProgressStyle::with_template("{elapsed:.dim} ❲{wide_bar:.red}❳ {percent}% {bytes_per_sec:.dim} {bytes:.dim}")

Check failure on line 42 in src/install_multi.rs

View workflow job for this annotation

GitHub Actions / Clippy (ubuntu-latest)

these look like formatting arguments but are not part of a formatting macro

Check failure on line 42 in src/install_multi.rs

View workflow job for this annotation

GitHub Actions / Clippy (macos-latest)

these look like formatting arguments but are not part of a formatting macro
.expect("failed to construct progress bar")
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));
.with_key("elapsed", |state: &ProgressState, w: &mut dyn Write| {
let s = state.elapsed().as_secs_f64();
let precision = precision(s);
write!(w, "{:.precision$}s", s, precision = precision).unwrap()
})
.with_key("bytes", |state: &ProgressState, w: &mut dyn Write| {
let (right, divisor) = pretty_size(state.len().unwrap(), None);
let left = state.pos() as f64 / divisor as f64;
let leftprecision = precision(left);
write!(w, "{:.precision$}/{}", left, right, precision = leftprecision).unwrap()
})
.progress_chars("⌬ "));
bar.tick();
state.pb = Some(bar);
}
Expand All @@ -58,17 +68,42 @@ pub async fn install_multi(
promises.push(promise);
}

let results = join_all(promises).await;
let mut installations = vec![];
for result in join_all(promises).await {
installations.push(result?);
}

if let Some(bar) = &shared_state.lock().unwrap().pb {
bar.finish();
bar.finish_and_clear();
}

let mut installations = vec![];
Ok(installations)
}

// Handle results
for result in results {
installations.push(result?);
fn pretty_size(n: u64, fixed: Option<usize>) -> (String, u64) {
let units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
let mut size = n as f64;
let mut i = 0;
let mut divisor = 1;

while size > 1024.0 && i < units.len() - 1 {
size /= 1024.0;
i += 1;
divisor *= 1024;
}

Ok(installations)
let precision = fixed.unwrap_or_else(|| precision(size));

let formatted = format!("{:.precision$} {}", size, units[i], precision = precision);
(formatted, divisor)
}

fn precision(n: f64) -> usize {
if n < 10.0 {
2
} else if n < 100.0 {
1
} else {
0
}
}

0 comments on commit 55c2158

Please sign in to comment.