Created
February 24, 2025 12:04
-
-
Save keesj/3bf569a441fee34e9aaf3ff9048ad8c5 to your computer and use it in GitHub Desktop.
indicatif custom formatter https://github.com/console-rs/indicatif/issues/610
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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