diff --git a/src/print/mod.rs b/src/print/mod.rs index 7c37bac..8a633f8 100644 --- a/src/print/mod.rs +++ b/src/print/mod.rs @@ -136,7 +136,7 @@ mod ui { use cursive::traits::*; use fui::views::Multiselect; use std::collections::HashSet; - use std::io::Result; + use std::io::{Error, ErrorKind, Result}; pub struct UIBackend {} @@ -150,21 +150,26 @@ mod ui { impl UserInput for UIBackend { async fn prompt(&self, text: &str) -> Result { let mut siv = cursive::Cursive::default(); - let v = std::rc::Rc::new(std::cell::Cell::new("".to_owned())); + siv.add_global_callback(cursive::event::Event::CtrlChar('c'), |s| { + s.quit(); + }); + let v = std::rc::Rc::new(std::cell::Cell::new(None)); let vx = v.clone(); siv.add_layer( cursive::views::Dialog::new().title(text).content( cursive::views::EditView::new() .on_submit(move |s, x| { - vx.set(x.to_string()); + vx.set(Some(x.to_string())); s.quit(); }) .fixed_width(40), ), ); siv.run(); - Ok(v.take()) + + v.take() + .ok_or_else(|| Error::new(ErrorKind::Other, "user abort")) } async fn shell(&self, command: &str, shell_trust: &super::ShellTrust) -> Result { @@ -173,14 +178,17 @@ mod ui { async fn select(&self, prompt: &str, options: &[String]) -> Result { let mut siv = cursive::Cursive::default(); - let v = std::rc::Rc::new(std::cell::Cell::new("".to_owned())); + siv.add_global_callback(cursive::event::Event::CtrlChar('c'), |s| { + s.quit(); + }); + let v = std::rc::Rc::new(std::cell::Cell::new(None)); let vx = v.clone(); let mut select = cursive::views::SelectView::::new() .h_align(cursive::align::HAlign::Left) .autojump() .on_submit(move |s, x: &str| { - vx.set(x.to_owned()); + vx.set(Some(x.to_owned())); s.quit(); }); select.add_all_str(options); @@ -191,11 +199,14 @@ mod ui { ); siv.run(); - Ok(v.take()) + v.take() + .ok_or_else(|| Error::new(ErrorKind::Other, "user abort")) } async fn check(&self, _: &str, options: &[String]) -> Result { - let mut c = cursive::Cursive::default(); + let mut siv = cursive::Cursive::default(); + let ok_pressed = std::sync::Arc::new(std::cell::Cell::new(false)); + let ok_pressed_siv = ok_pressed.clone(); let items = std::sync::Arc::new(std::sync::RwLock::new(HashSet::new())); let items_view = items.clone(); let items_view2 = items.clone(); @@ -207,11 +218,17 @@ mod ui { .on_deselect(move |_, v| { items_view2.try_write().unwrap().remove(&v); }); - let dlg = cursive::views::Dialog::around(view).button("Ok", |s| s.quit()); + let dlg = cursive::views::Dialog::around(view).button("Ok", move |s| { + ok_pressed_siv.set(true); + s.quit(); + }); - c.add_layer(dlg); + siv.add_layer(dlg); - c.run(); + siv.run(); + if !ok_pressed.take() { + return Err(Error::new(ErrorKind::Other, "user abort")); + } let it = items.try_read().unwrap(); Ok(it .iter()