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
use crate::app::App;
use crate::app::Mode;
use crossterm::event::{self, Event, KeyCode};
pub fn watch_keys(app: &mut App, view_width: u16) -> std::io::Result<bool> {
if event::poll(std::time::Duration::from_millis(100))? {
if let Event::Key(key) = event::read()? {
match app.mode {
// --- NORMAL MODE ---
Mode::Normal => match key.code {
// [q] quit - exit the program
KeyCode::Char('q') => return Ok(true),
// [i] insert - enter insert mode to write commands
KeyCode::Char('i') => app.mode = Mode::Insert,
// [h] left - select command to the left
KeyCode::Char('h') => app.previous_command(view_width),
// [l] right - select command to the right
KeyCode::Char('l') => app.next_command(view_width),
// [ret] run - run the selected command
KeyCode::Enter => app.run_selected_command(),
KeyCode::Char('s') => app.save_command(),
// [d] or [x] delete - delete selected command
KeyCode::Char('d') => app.delete_selected_command(),
KeyCode::Char('x') => app.delete_selected_command(),
_ => {}
},
// --- INSERT MODE ---
Mode::Insert => match key.code {
// [esc] normal - enter normal mode and manage commands
KeyCode::Esc => app.mode = Mode::Normal,
// <key> - write key to input
KeyCode::Char(c) => app.input.push(c),
// <bspc> - delete char from input
KeyCode::Backspace => {
app.input.pop();
}
// [ret] run - run the input command without saving
KeyCode::Enter => app.run_input(),
_ => {}
},
}
}
}
Ok(false)
}