Skip to content

Commit

Permalink
#patch
Browse files Browse the repository at this point in the history
+ supporting abort
  • Loading branch information
Heiko Alexander Weber committed Sep 13, 2020
1 parent 7a35b98 commit a41ec02
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions src/print/mod.rs
Expand Up @@ -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 {}

Expand All @@ -150,21 +150,26 @@ mod ui {
impl UserInput for UIBackend {
async fn prompt(&self, text: &str) -> Result<String> {
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<String> {
Expand All @@ -173,14 +178,17 @@ mod ui {

async fn select(&self, prompt: &str, options: &[String]) -> Result<String> {
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::<String>::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);
Expand All @@ -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<String> {
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();
Expand All @@ -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()
Expand Down

0 comments on commit a41ec02

Please sign in to comment.