Skip to main content

igv_rs/
command.rs

1use crossterm::event::{Event, KeyCode, KeyEvent};
2use tui_input::backend::crossterm::EventHandler;
3use tui_input::Input;
4
5use crate::app::action::Action;
6
7#[derive(Debug, Default)]
8pub struct CommandPalette {
9    pub input: Input,
10    pub open: bool,
11}
12
13impl CommandPalette {
14    pub fn open(&mut self) {
15        self.open = true;
16        self.input = Input::default();
17    }
18
19    pub fn close(&mut self) {
20        self.open = false;
21        self.input = Input::default();
22    }
23
24    /// Returns an `Action::CommandSubmit` on Enter, `CommandCancel` on Esc,
25    /// or `None` for typing.
26    pub fn handle(&mut self, event: &Event) -> Action {
27        if let Event::Key(KeyEvent { code, .. }) = event {
28            match code {
29                KeyCode::Enter => {
30                    let buf = self.input.value().to_string();
31                    self.close();
32                    return Action::CommandSubmit(buf);
33                }
34                KeyCode::Esc => {
35                    self.close();
36                    return Action::CommandCancel;
37                }
38                _ => {
39                    self.input.handle_event(event);
40                    return Action::None;
41                }
42            }
43        }
44        Action::None
45    }
46}