-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextend_prompt.rs
74 lines (61 loc) · 1.86 KB
/
extend_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use promptuity::event::*;
use promptuity::prompts::Confirm;
use promptuity::themes::MinimalTheme;
use promptuity::{Error, Prompt, Promptuity, Term};
struct ExtendedConfirm {
original: Confirm,
}
impl ExtendedConfirm {
fn new(message: impl std::fmt::Display) -> Self {
Self {
original: Confirm::new(message),
}
}
}
impl AsMut<ExtendedConfirm> for ExtendedConfirm {
fn as_mut(&mut self) -> &mut ExtendedConfirm {
self
}
}
impl Prompt for ExtendedConfirm {
type Output = bool;
fn handle(
&mut self,
code: crossterm::event::KeyCode,
modifiers: crossterm::event::KeyModifiers,
) -> promptuity::PromptState {
match (code, modifiers) {
(KeyCode::Left, KeyModifiers::NONE) => {
// forward to `y` key
Prompt::handle(&mut self.original, KeyCode::Char('y'), KeyModifiers::NONE)
}
(KeyCode::Right, KeyModifiers::NONE) => {
// forward to `n` key
Prompt::handle(&mut self.original, KeyCode::Char('n'), KeyModifiers::NONE)
}
_ => {
// forward to original handler
Prompt::handle(&mut self.original, code, modifiers)
}
}
}
fn submit(&mut self) -> Self::Output {
Prompt::submit(&mut self.original)
}
fn render(
&mut self,
state: &promptuity::PromptState,
) -> Result<promptuity::RenderPayload, String> {
Prompt::render(&mut self.original, state)
}
}
fn main() -> Result<(), Error> {
let mut term = Term::default();
let mut theme = MinimalTheme::new();
let mut p = Promptuity::new(&mut term, &mut theme);
p.begin()?;
let result = p.prompt(ExtendedConfirm::new("input message").as_mut())?;
p.finish()?;
println!("result: {}", result);
Ok(())
}