Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove MultiProgress::join() based on #231 #284

Merged
merged 3 commits into from May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 24 additions & 19 deletions examples/finebars.rs
Expand Up @@ -15,24 +15,29 @@ fn main() {

let m = MultiProgress::new();

for s in styles.iter() {
let pb = m.add(ProgressBar::new(512));
pb.set_style(
ProgressStyle::default_bar()
.template(&format!("{{prefix:.bold}}▕{{bar:.{}}}▏{{msg}}", s.2))
.progress_chars(s.1),
);
pb.set_prefix(s.0);
let wait = Duration::from_millis(thread_rng().gen_range(10..30));
thread::spawn(move || {
for i in 0..512 {
pb.inc(1);
pb.set_message(format!("{:3}%", 100 * i / 512));
thread::sleep(wait);
}
pb.finish_with_message("100%");
});
}
let handles: Vec<_> = styles
.iter()
.map(|s| {
let pb = m.add(ProgressBar::new(512));
pb.set_style(
ProgressStyle::default_bar()
.template(&format!("{{prefix:.bold}}▕{{bar:.{}}}▏{{msg}}", s.2))
.progress_chars(s.1),
);
pb.set_prefix(s.0);
let wait = Duration::from_millis(thread_rng().gen_range(10..30));
thread::spawn(move || {
for i in 0..512 {
pb.inc(1);
pb.set_message(format!("{:3}%", 100 * i / 512));
thread::sleep(wait);
}
pb.finish_with_message("100%");
})
})
.collect();

m.join().unwrap();
for h in handles {
let _ = h.join();
}
}
31 changes: 13 additions & 18 deletions examples/morebars.rs
Expand Up @@ -11,23 +11,18 @@ fn main() {
let pb = m.add(ProgressBar::new(5));
pb.set_style(sty.clone());

let m2 = m.clone();
let _ = thread::spawn(move || {
// make sure we show up at all. otherwise no rendering
// event.
pb.tick();
for _ in 0..5 {
let pb2 = m2.add(ProgressBar::new(128));
pb2.set_style(sty.clone());
for _ in 0..128 {
pb2.inc(1);
thread::sleep(Duration::from_millis(5));
}
pb2.finish();
pb.inc(1);
// make sure we show up at all. otherwise no rendering
// event.
pb.tick();
for _ in 0..5 {
let pb2 = m.add(ProgressBar::new(128));
pb2.set_style(sty.clone());
for _ in 0..128 {
pb2.inc(1);
thread::sleep(Duration::from_millis(5));
}
pb.finish_with_message("done");
});

m.join().unwrap();
pb2.finish();
pb.inc(1);
}
pb.finish_with_message("done");
}
78 changes: 37 additions & 41 deletions examples/multi-tree-ext.rs
Expand Up @@ -189,51 +189,47 @@ pub fn main() {
let mut items: Vec<&Item> = Vec::with_capacity(ELEMENTS.len());

let mp2 = Arc::clone(&mp);
let _ = thread::spawn(move || {
let mut rng = ThreadRng::default();
pb_main.tick();
loop {
match get_action(&mut rng, &items) {
Action::Stop => {
// all elements were exhausted
pb_main.finish();
return;
let mut rng = ThreadRng::default();
pb_main.tick();
loop {
match get_action(&mut rng, &items) {
Action::Stop => {
// all elements were exhausted
pb_main.finish();
return;
}
Action::ModifyTree(elem_idx) => match &ELEMENTS[elem_idx] {
Elem::AddItem(item) => {
let pb = mp2.insert(item.index + 1, item.progress_bar.clone());
pb.set_prefix(" ".repeat(item.indent));
pb.set_message(&item.key);
items.insert(item.index, &item);
}
Elem::RemoveItem(Index(index)) => {
let item = items.remove(*index);
let pb = &item.progress_bar;
mp2.remove(pb);
pb_main.inc(pb.length() - pb.position());
}
Action::ModifyTree(elem_idx) => match &ELEMENTS[elem_idx] {
Elem::AddItem(item) => {
let pb = mp2.insert(item.index + 1, item.progress_bar.clone());
pb.set_prefix(" ".repeat(item.indent));
pb.set_message(&item.key);
items.insert(item.index, &item);
}
Elem::RemoveItem(Index(index)) => {
let item = items.remove(*index);
let pb = &item.progress_bar;
mp2.remove(pb);
pb_main.inc(pb.length() - pb.position());
}
},
Action::IncProgressBar(item_idx) => {
let item = &items[item_idx];
item.progress_bar.inc(1);
let pos = item.progress_bar.position();
let len = item.progress_bar.length();
if pos >= len {
item.progress_bar.set_style(sty_fin.clone());
item.progress_bar.finish_with_message(format!(
"{} {}",
style("✔").green(),
item.key
));
}
pb_main.inc(1);
},
Action::IncProgressBar(item_idx) => {
let item = &items[item_idx];
item.progress_bar.inc(1);
let pos = item.progress_bar.position();
let len = item.progress_bar.length();
if pos >= len {
item.progress_bar.set_style(sty_fin.clone());
item.progress_bar.finish_with_message(format!(
"{} {}",
style("✔").green(),
item.key
));
}
pb_main.inc(1);
}
thread::sleep(Duration::from_millis(20));
}
});

mp.join().unwrap();
thread::sleep(Duration::from_millis(20));
}
}

/// The function guarantees to return the action, that is valid for the current tree.
Expand Down
5 changes: 2 additions & 3 deletions examples/multi-tree.rs
Expand Up @@ -135,9 +135,8 @@ fn main() {
}
thread::sleep(Duration::from_millis(15));
}
});

mp.join().unwrap();
})
.join();

println!("===============================");
println!("the tree should be the same as:");
Expand Down
8 changes: 5 additions & 3 deletions examples/multi.rs
Expand Up @@ -11,7 +11,7 @@ fn main() {

let pb = m.add(ProgressBar::new(128));
pb.set_style(sty.clone());
let _ = thread::spawn(move || {
let h1 = thread::spawn(move || {
for i in 0..128 {
pb.set_message(format!("item #{}", i + 1));
pb.inc(1);
Expand All @@ -22,7 +22,7 @@ fn main() {

let pb = m.add(ProgressBar::new(128));
pb.set_style(sty.clone());
let _ = thread::spawn(move || {
let h2 = thread::spawn(move || {
for _ in 0..3 {
pb.set_position(0);
for i in 0..128 {
Expand All @@ -45,5 +45,7 @@ fn main() {
pb.finish_with_message("done");
});

m.join_and_clear().unwrap();
let _ = h1.join();
let _ = h2.join();
m.clear().unwrap();
}
39 changes: 22 additions & 17 deletions examples/yarnish.rs
Expand Up @@ -69,24 +69,29 @@ pub fn main() {
PAPER
);
let m = MultiProgress::new();
for i in 0..4 {
let count = rng.gen_range(30..80);
let pb = m.add(ProgressBar::new(count));
pb.set_style(spinner_style.clone());
pb.set_prefix(format!("[{}/?]", i + 1));
let _ = thread::spawn(move || {
let mut rng = rand::thread_rng();
let pkg = PACKAGES.choose(&mut rng).unwrap();
for _ in 0..count {
let cmd = COMMANDS.choose(&mut rng).unwrap();
pb.set_message(format!("{}: {}", pkg, cmd));
pb.inc(1);
thread::sleep(Duration::from_millis(rng.gen_range(25..200)));
}
pb.finish_with_message("waiting...");
});
let handles: Vec<_> = (0..4u32)
.map(|i| {
let count = rng.gen_range(30..80);
let pb = m.add(ProgressBar::new(count));
pb.set_style(spinner_style.clone());
pb.set_prefix(format!("[{}/?]", i + 1));
thread::spawn(move || {
let mut rng = rand::thread_rng();
let pkg = PACKAGES.choose(&mut rng).unwrap();
for _ in 0..count {
let cmd = COMMANDS.choose(&mut rng).unwrap();
pb.set_message(format!("{}: {}", pkg, cmd));
pb.inc(1);
thread::sleep(Duration::from_millis(rng.gen_range(25..200)));
}
pb.finish_with_message("waiting...");
})
})
.collect();
for h in handles {
let _ = h.join();
}
m.join_and_clear().unwrap();
m.clear().unwrap();

println!("{} Done in {}", SPARKLE, HumanDuration(started.elapsed()));
}