extern crate cursive;
extern crate rand;
use rand::Rng;
use cursive::Cursive;
use cursive::views::{Button, Dialog, LinearLayout, ProgressBar, TextView};
use cursive::views::Counter;
use cursive::traits::*;
use std::thread;
use std::cmp::min;
use std::time::Duration;
fn main() {
let mut siv = Cursive::new();
siv.add_layer(Dialog::new()
.title("Progress bar example")
.padding((0, 0, 1, 1))
.content(Button::new("Start", phase_1)));
siv.set_fps(30);
siv.run();
}
fn fake_load(n_max: usize, counter: Counter) {
for _ in 0..n_max {
thread::sleep(Duration::from_millis(5));
counter.tick(1);
}
}
fn phase_1(s: &mut Cursive) {
let n_max = 500;
let cb = s.cb_sink().clone();
s.pop_layer();
s.add_layer(Dialog::around(ProgressBar::new()
.range(0, n_max)
.with_task(move |counter| {
fake_load(n_max, counter);
cb.send(Box::new(coffee_break)).unwrap();
})
.full_width()));
}
fn coffee_break(s: &mut Cursive) {
s.pop_layer();
s.add_layer(Dialog::new()
.title("Preparation complete")
.content(TextView::new("Now, the real deal!").center())
.button("Again??", phase_2));
}
fn phase_2(s: &mut Cursive) {
let n_bars = 10;
let counters: Vec<_> = (0..n_bars).map(|_| Counter::new(0)).collect();
let speeds: Vec<_> =
(0..n_bars).map(|_| rand::thread_rng().gen_range(50, 150)).collect();
let n_max = 100000;
let cb = s.cb_sink().clone();
let mut linear = LinearLayout::vertical();
for c in &counters {
linear.add_child(ProgressBar::new()
.max(n_max)
.with_value(c.clone()));
}
s.pop_layer();
s.add_layer(Dialog::around(linear.full_width()).title("Just a moment..."));
thread::spawn(move || {
loop {
thread::sleep(Duration::from_millis(5));
let mut done = true;
for (c, s) in counters.iter().zip(&speeds) {
let ticks = min(n_max - c.get(), *s);
c.tick(ticks);
if c.get() < n_max {
done = false;
}
}
if done {
break;
}
}
cb.send(Box::new(final_step)).unwrap();
});
}
fn final_step(s: &mut Cursive) {
s.pop_layer();
s.add_layer(Dialog::new()
.title("Report")
.content(TextView::new("Time travel was a success!\n\
We went forward a few seconds!!")
.center())
.button("That's it?", |s| s.quit()));
}