Debian Codegraph @

debian/rust-vendor/indicatif/examples/finebars.rs

raw
1 use std::thread;
2 use std::time::Duration;
3
4 use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
5 use rand::RngExt;
6
7 fn main() {
8 let styles = [
9 ("Rough bar:", "█ ", "red"),
10 ("Fine bar: ", "█▉▊▋▌▍▎▏ ", "yellow"),
11 ("Vertical: ", "█▇▆▅▄▃▂▁ ", "green"),
12 ("Fade in: ", "█▓▒░ ", "blue"),
13 ("Blocky: ", "█▛▌▖ ", "magenta"),
14 ];
15
16 let m = MultiProgress::new();
17
18 let handles: Vec<_> = styles
19 .iter()
20 .map(|s| {
21 let pb = m.add(ProgressBar::new(512));
22 pb.set_style(
23 ProgressStyle::with_template(&format!("{{prefix:.bold}}▕{{bar:.{}}}▏{{msg}}", s.2))
24 .unwrap()
25 .progress_chars(s.1),
26 );
27 pb.set_prefix(s.0);
28 let wait = Duration::from_millis(rand::rng().random_range(10..30));
29 thread::spawn(move || {
30 for i in 0..512 {
31 thread::sleep(wait);
32 pb.inc(1);
33 pb.set_message(format!("{:3}%", 100 * i / 512));
34 }
35 pb.finish_with_message("100%");
36 })
37 })
38 .collect();
39
40 for h in handles {
41 let _ = h.join();
42 }
43 }