2016-07-25 06:00:13 +00:00
|
|
|
extern crate cursive;
|
|
|
|
|
|
|
|
use cursive::prelude::*;
|
|
|
|
|
|
|
|
use std::thread;
|
|
|
|
use std::time::Duration;
|
2016-07-26 17:13:36 +00:00
|
|
|
use std::sync::Arc;
|
2016-07-25 06:00:13 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut siv = Cursive::new();
|
|
|
|
|
2016-07-26 03:43:01 +00:00
|
|
|
siv.add_layer(Dialog::empty()
|
|
|
|
.title("Progress bar example")
|
|
|
|
.padding((0, 0, 1, 1))
|
|
|
|
.content(Button::new("Start", |s| {
|
2016-07-25 06:00:13 +00:00
|
|
|
// These two values will allow us to communicate.
|
|
|
|
let value = Arc::new(AtomicUsize::new(0));
|
|
|
|
|
2016-07-26 06:54:33 +00:00
|
|
|
let n_max = 1000;
|
2016-07-25 06:00:13 +00:00
|
|
|
|
2016-07-25 20:35:46 +00:00
|
|
|
s.pop_layer();
|
|
|
|
s.add_layer(Panel::new(FullView::full_width(ProgressBar::new()
|
2016-07-25 06:00:13 +00:00
|
|
|
.range(0, n_max)
|
2016-07-26 17:13:36 +00:00
|
|
|
.with_value(value.clone()))));
|
|
|
|
|
|
|
|
let cb = s.cb_sink().clone();
|
2016-07-25 06:00:13 +00:00
|
|
|
|
|
|
|
// Spawn a thread to process things in the background.
|
|
|
|
thread::spawn(move || {
|
|
|
|
for _ in 0..n_max {
|
2016-07-26 03:43:01 +00:00
|
|
|
thread::sleep(Duration::from_millis(20));
|
2016-07-25 06:00:13 +00:00
|
|
|
value.fetch_add(1, Ordering::Relaxed);
|
|
|
|
}
|
2016-07-26 17:13:36 +00:00
|
|
|
cb.send(Box::new(move |s| {
|
|
|
|
s.pop_layer();
|
|
|
|
s.add_layer(Dialog::empty()
|
|
|
|
.title("Work done!")
|
|
|
|
.content(TextView::new("Phew, that was some \
|
|
|
|
work!"))
|
|
|
|
.button("Sure!", |s| s.quit()));
|
|
|
|
}))
|
|
|
|
.unwrap();
|
2016-07-25 06:00:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
}))
|
|
|
|
.with_id("dialog"));
|
|
|
|
|
2016-07-25 20:35:46 +00:00
|
|
|
siv.set_fps(30);
|
2016-07-25 06:00:13 +00:00
|
|
|
|
|
|
|
siv.run();
|
|
|
|
}
|