alopex_cli/tui/
keymap.rs

1//! Keybindings for TUI actions.
2
3use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Action {
7    MoveUp,
8    MoveDown,
9    MoveLeft,
10    MoveRight,
11    PageUp,
12    PageDown,
13    JumpTop,
14    JumpBottom,
15    ToggleDetail,
16    ToggleHelp,
17    SearchMode,
18    SearchNext,
19    SearchPrev,
20    InputChar(char),
21    Backspace,
22    ConfirmSearch,
23    CancelSearch,
24    DetailUp,
25    DetailDown,
26    Quit,
27}
28
29pub fn action_for_key(event: KeyEvent, search_active: bool) -> Option<Action> {
30    if search_active {
31        return match event.code {
32            KeyCode::Char('n') => Some(Action::SearchNext),
33            KeyCode::Char('N') => Some(Action::SearchPrev),
34            KeyCode::Enter => Some(Action::ConfirmSearch),
35            KeyCode::Esc => Some(Action::CancelSearch),
36            KeyCode::Backspace => Some(Action::Backspace),
37            KeyCode::Char(ch) => Some(Action::InputChar(ch)),
38            _ => None,
39        };
40    }
41
42    match (event.code, event.modifiers) {
43        (KeyCode::Char('q'), _) => Some(Action::Quit),
44        (KeyCode::Esc, _) => Some(Action::Quit),
45        (KeyCode::Char('?'), _) => Some(Action::ToggleHelp),
46        (KeyCode::Char('/'), _) => Some(Action::SearchMode),
47        (KeyCode::Char('n'), _) => Some(Action::SearchNext),
48        (KeyCode::Char('N'), _) => Some(Action::SearchPrev),
49        (KeyCode::Char('k'), _) | (KeyCode::Up, _) => Some(Action::MoveUp),
50        (KeyCode::Char('j'), _) | (KeyCode::Down, _) => Some(Action::MoveDown),
51        (KeyCode::Char('h'), _) | (KeyCode::Left, _) => Some(Action::MoveLeft),
52        (KeyCode::Char('l'), _) | (KeyCode::Right, _) => Some(Action::MoveRight),
53        (KeyCode::Char('g'), KeyModifiers::NONE) => Some(Action::JumpTop),
54        (KeyCode::Char('G'), _) => Some(Action::JumpBottom),
55        (KeyCode::Char('d'), KeyModifiers::CONTROL) => Some(Action::PageDown),
56        (KeyCode::Char('u'), KeyModifiers::CONTROL) => Some(Action::PageUp),
57        (KeyCode::Enter, _) => Some(Action::ToggleDetail),
58        (KeyCode::PageDown, _) => Some(Action::PageDown),
59        (KeyCode::PageUp, _) => Some(Action::PageUp),
60        (KeyCode::Char('J'), _) => Some(Action::DetailDown),
61        (KeyCode::Char('K'), _) => Some(Action::DetailUp),
62        _ => None,
63    }
64}
65
66pub fn help_items() -> Vec<(&'static str, &'static str)> {
67    vec![
68        ("q", "Quit"),
69        ("Esc", "Quit"),
70        ("?", "Toggle help"),
71        ("/", "Search"),
72        ("n/N", "Next/prev match"),
73        ("hjkl", "Move selection"),
74        ("g/G", "Jump top/bottom"),
75        ("Ctrl+d/u", "Page down/up"),
76        ("Enter", "Toggle detail"),
77        ("J/K", "Scroll detail"),
78    ]
79}