rustic_rs/commands/tui/widgets/
prompt.rs

1use super::{Draw, Event, Frame, KeyCode, KeyEventKind, ProcessEvent, Rect, SizedWidget};
2
3pub struct Prompt<T>(pub T);
4
5pub enum PromptResult {
6    Ok,
7    Cancel,
8    None,
9}
10
11impl<T: SizedWidget> SizedWidget for Prompt<T> {
12    fn height(&self) -> Option<u16> {
13        self.0.height()
14    }
15    fn width(&self) -> Option<u16> {
16        self.0.width()
17    }
18}
19
20impl<T: Draw> Draw for Prompt<T> {
21    fn draw(&mut self, area: Rect, f: &mut Frame<'_>) {
22        self.0.draw(area, f);
23    }
24}
25
26impl<T> ProcessEvent for Prompt<T> {
27    type Result = PromptResult;
28    fn input(&mut self, event: Event) -> PromptResult {
29        use KeyCode::{Char, Enter, Esc};
30        match event {
31            Event::Key(key) if key.kind == KeyEventKind::Press => match key.code {
32                Char('q' | 'n' | 'c') | Esc => PromptResult::Cancel,
33                Enter | Char('y' | 'j' | ' ') => PromptResult::Ok,
34                _ => PromptResult::None,
35            },
36            _ => PromptResult::None,
37        }
38    }
39}