use cursive::{
views::{Dialog, TextView},
Cursive, CursiveExt,
};
use cursive_async_view::{AsyncState, AsyncView};
use std::sync::mpsc::{channel, TryRecvError};
use std::time::{Duration, Instant};
fn main() {
let mut siv = Cursive::default();
siv.add_global_callback('q', Cursive::quit);
let start_time = Instant::now();
let (tx, rx) = channel();
std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(20));
tx.send(TextView::new("Content has loaded!")).ok();
});
let loading_view = AsyncView::new(&mut siv, move || {
if start_time.elapsed() > Duration::from_secs(5) {
AsyncState::Error("Oh no, the view has timed out!".to_string())
} else {
match rx.try_recv() {
Ok(view) => AsyncState::Available(view),
Err(TryRecvError::Empty) => AsyncState::Pending,
Err(TryRecvError::Disconnected) => {
AsyncState::Error("Shoot, view creation thread exited...".to_string())
}
}
}
})
.with_width(40);
siv.add_layer(Dialog::around(loading_view).button("Ok", |s| s.quit()));
siv.run();
}