Debian Codegraph @

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

raw
1 use std::sync::{mpsc, Arc, Mutex};
2 use std::thread;
3 use std::time::{Duration, Instant};
4
5 use console::{Style, Term};
6 use indicatif::{HumanDuration, ProgressBar, ProgressStyle};
7 use rand::RngExt;
8
9 static CRATES: &[(&str, &str)] = &[
10 ("console", "v0.14.1"),
11 ("lazy_static", "v1.4.0"),
12 ("libc", "v0.2.93"),
13 ("regex", "v1.4.6"),
14 ("regex-syntax", "v0.6.23"),
15 ("terminal_size", "v0.1.16"),
16 ("libc", "v0.2.93"),
17 ("unicode-width", "v0.1.8"),
18 ("lazy_static", "v1.4.0"),
19 ("number_prefix", "v0.4.0"),
20 ("regex", "v1.4.6"),
21 ("rand", "v0.8.3"),
22 ("getrandom", "v0.2.2"),
23 ("cfg-if", "v1.0.0"),
24 ("libc", "v0.2.93"),
25 ("rand_chacha", "v0.3.0"),
26 ("ppv-lite86", "v0.2.10"),
27 ("rand_core", "v0.6.2"),
28 ("getrandom", "v0.2.2"),
29 ("rand_core", "v0.6.2"),
30 ("tokio", "v1.5.0"),
31 ("bytes", "v1.0.1"),
32 ("pin-project-lite", "v0.2.6"),
33 ("slab", "v0.4.3"),
34 ("indicatif", "v0.15.0"),
35 ];
36
37 fn main() {
38 // number of cpus
39 const NUM_CPUS: usize = 4;
40 let start = Instant::now();
41
42 // mimic cargo progress bar although it behaves a bit different
43 let pb = ProgressBar::new(CRATES.len() as u64);
44 pb.set_style(
45 ProgressStyle::with_template(
46 // note that bar size is fixed unlike cargo which is dynamic
47 // and also the truncation in cargo uses trailers (`...`)
48 if Term::stdout().size().1 > 80 {
49 "{prefix:>12.cyan.bold} [{bar:57}] {pos}/{len} {wide_msg}"
50 } else {
51 "{prefix:>12.cyan.bold} [{bar:57}] {pos}/{len}"
52 },
53 )
54 .unwrap()
55 .progress_chars("=> "),
56 );
57 pb.set_prefix("Building");
58
59 // process in another thread
60 // crates to be iterated but not exactly a tree
61 let crates = Arc::new(Mutex::new(CRATES.iter()));
62 let (tx, rx) = mpsc::channel();
63 for n in 0..NUM_CPUS {
64 let tx = tx.clone();
65 let crates = crates.clone();
66 thread::spawn(move || {
67 let mut rng = rand::rng();
68 loop {
69 let krate = crates.lock().unwrap().next();
70 // notify main thread if n thread is processing a crate
71 tx.send((n, krate)).unwrap();
72 if let Some(krate) = krate {
73 thread::sleep(Duration::from_millis(
74 // last compile and linking is always slow, let's mimic that
75 if CRATES.last() == Some(krate) {
76 rng.random_range(1_000..2_000)
77 } else {
78 rng.random_range(250..1_000)
79 },
80 ));
81 } else {
82 break;
83 }
84 }
85 });
86 }
87 // drop tx to stop waiting
88 drop(tx);
89
90 let green_bold = Style::new().green().bold();
91
92 // do progress drawing in main thread
93 let mut processing = [None; NUM_CPUS];
94 while let Ok((n, krate)) = rx.recv() {
95 processing[n] = krate;
96 let crates: Vec<&str> = processing
97 .iter()
98 .filter_map(|t| t.copied().map(|(name, _)| name))
99 .collect();
100 pb.set_message(crates.join(", "));
101 if let Some((name, version)) = krate {
102 // crate is being built
103 let line = format!(
104 "{:>12} {} {}",
105 green_bold.apply_to("Compiling"),
106 name,
107 version
108 );
109 pb.println(line);
110
111 pb.inc(1);
112 }
113 }
114 pb.finish_and_clear();
115
116 // compilation is finished
117 println!(
118 "{:>12} dev [unoptimized + debuginfo] target(s) in {}",
119 green_bold.apply_to("Finished"),
120 HumanDuration(start.elapsed())
121 );
122 }