Skip to content

Commit 505d282

Browse files
oli-obkchris-laplante
authored andcommitted
Edit an existing example to overflow the window size
1 parent 4f82ba7 commit 505d282

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

examples/multi.rs

+31-29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::time::Duration;
33

44
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
55

6+
use rand::Rng;
7+
68
fn main() {
79
let m = MultiProgress::new();
810
let sty = ProgressStyle::with_template(
@@ -11,41 +13,20 @@ fn main() {
1113
.unwrap()
1214
.progress_chars("##-");
1315

14-
let pb = m.add(ProgressBar::new(128));
16+
let n = 200;
17+
let pb = m.add(ProgressBar::new(n));
1518
pb.set_style(sty.clone());
16-
17-
let pb2 = m.insert_after(&pb, ProgressBar::new(128));
19+
pb.set_message("todo");
20+
let pb2 = m.add(ProgressBar::new(n));
1821
pb2.set_style(sty.clone());
22+
pb2.set_message("finished");
1923

2024
let pb3 = m.insert_after(&pb2, ProgressBar::new(1024));
2125
pb3.set_style(sty);
2226

2327
m.println("starting!").unwrap();
2428

25-
let m_clone = m.clone();
26-
let h1 = thread::spawn(move || {
27-
for i in 0..128 {
28-
thread::sleep(Duration::from_millis(15));
29-
pb.set_message(format!("item #{}", i + 1));
30-
pb.inc(1);
31-
}
32-
m_clone.println("pb1 is done!").unwrap();
33-
pb.finish_with_message("done");
34-
});
35-
36-
let m_clone = m.clone();
37-
let h2 = thread::spawn(move || {
38-
for _ in 0..3 {
39-
pb2.set_position(0);
40-
for i in 0..128 {
41-
thread::sleep(Duration::from_millis(8));
42-
pb2.set_message(format!("item #{}", i + 1));
43-
pb2.inc(1);
44-
}
45-
}
46-
m_clone.println("pb2 is done!").unwrap();
47-
pb2.finish_with_message("done");
48-
});
29+
let mut threads = vec![];
4930

5031
let m_clone = m.clone();
5132
let h3 = thread::spawn(move || {
@@ -58,8 +39,29 @@ fn main() {
5839
pb3.finish_with_message("done");
5940
});
6041

61-
let _ = h1.join();
62-
let _ = h2.join();
42+
for i in 0..n {
43+
thread::sleep(Duration::from_millis(15));
44+
if i == n / 3 {
45+
thread::sleep(Duration::from_secs(2));
46+
}
47+
pb.inc(1);
48+
let m = m.clone();
49+
let pb2 = pb2.clone();
50+
threads.push(thread::spawn(move || {
51+
let spinner = m.add(ProgressBar::new_spinner().with_message(i.to_string()));
52+
spinner.enable_steady_tick(Duration::from_millis(100));
53+
thread::sleep(
54+
rand::thread_rng().gen_range(Duration::from_secs(1)..Duration::from_secs(5)),
55+
);
56+
pb2.inc(1);
57+
}));
58+
}
59+
pb.finish_with_message("all jobs started");
60+
61+
for thread in threads {
62+
let _ = thread.join();
63+
}
6364
let _ = h3.join();
65+
pb2.finish_with_message("all jobs done");
6466
m.clear().unwrap();
6567
}

0 commit comments

Comments
 (0)