-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
prompt.rs
43 lines (34 loc) · 989 Bytes
/
prompt.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use dialoguer::console::Term;
use dialoguer::{theme::ColorfulTheme, Input, Select};
use std::process::exit;
fn show_cursor() {
Term::stderr().show_cursor().expect("failed to show cursor");
}
pub fn handle_ctrlc() {
ctrlc::set_handler(move || {
show_cursor();
exit(1);
})
.expect("Error setting Ctrl-C handler");
}
pub fn select(message: &str, script_names: Vec<&str>) -> Option<String> {
let selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt(message)
.items(&script_names)
.default(0)
.interact_on_opt(&Term::stderr())
.ok()?;
show_cursor();
selection?;
Some(script_names[selection.unwrap()].to_string())
}
pub fn input(message: &str) -> Option<String> {
let input = Input::<String>::new()
.with_prompt(message)
.allow_empty(true)
.with_initial_text("")
.interact_text_on(&Term::stderr())
.ok();
show_cursor();
input
}