Skip to content

Instantly share code, notes, and snippets.

@keesj
Created February 24, 2025 12:04
Show Gist options
  • Save keesj/3bf569a441fee34e9aaf3ff9048ad8c5 to your computer and use it in GitHub Desktop.
Save keesj/3bf569a441fee34e9aaf3ff9048ad8c5 to your computer and use it in GitHub Desktop.
use std::fmt::Write;
use std::thread;
use std::time::Duration;
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
/// Format in either item/s or s/item depending on the value
fn throughput_formatter(state: &ProgressState, w: &mut dyn Write) {
let ps = state.per_sec();
if ps > 1.0 {
write!(w, "{:.1} item/s", ps).unwrap()
} else if ps == 0.0 {
write!(w, "..... ").unwrap()
} else {
write!(w, "{:.1} s/item", 1.0 / ps).unwrap()
}
}
fn main() {
let pb = ProgressBar::new(2000);
pb.set_style(
ProgressStyle::with_template("{spinner:.green}: {throughput}")
.unwrap()
.with_key("throughput", throughput_formatter),
);
for _ in 0..1024 {
thread::sleep(Duration::from_millis(20));
pb.inc(1);
}
pb.finish();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment