1use 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 OpenAdmin,
27 Quit,
28}
29
30pub fn action_for_key(event: KeyEvent, search_active: bool) -> Option<Action> {
31 if search_active {
32 return match event.code {
33 KeyCode::Char('n') => Some(Action::SearchNext),
34 KeyCode::Char('N') => Some(Action::SearchPrev),
35 KeyCode::Enter => Some(Action::ConfirmSearch),
36 KeyCode::Esc => Some(Action::CancelSearch),
37 KeyCode::Backspace => Some(Action::Backspace),
38 KeyCode::Char(ch) => Some(Action::InputChar(ch)),
39 _ => None,
40 };
41 }
42
43 match (event.code, event.modifiers) {
44 (KeyCode::Char('q'), _) => Some(Action::Quit),
45 (KeyCode::Esc, _) => Some(Action::Quit),
46 (KeyCode::Char('?'), _) => Some(Action::ToggleHelp),
47 (KeyCode::Char('/'), _) => Some(Action::SearchMode),
48 (KeyCode::Char('n'), _) => Some(Action::SearchNext),
49 (KeyCode::Char('N'), _) => Some(Action::SearchPrev),
50 (KeyCode::Char('k'), _) | (KeyCode::Up, _) => Some(Action::MoveUp),
51 (KeyCode::Char('j'), _) | (KeyCode::Down, _) => Some(Action::MoveDown),
52 (KeyCode::Char('h'), _) | (KeyCode::Left, _) => Some(Action::MoveLeft),
53 (KeyCode::Char('l'), _) | (KeyCode::Right, _) => Some(Action::MoveRight),
54 (KeyCode::Char('g'), KeyModifiers::NONE) => Some(Action::JumpTop),
55 (KeyCode::Char('G'), _) => Some(Action::JumpBottom),
56 (KeyCode::Char('d'), KeyModifiers::CONTROL) => Some(Action::PageDown),
57 (KeyCode::Char('u'), KeyModifiers::CONTROL) => Some(Action::PageUp),
58 (KeyCode::Enter, _) => Some(Action::ToggleDetail),
59 (KeyCode::PageDown, _) => Some(Action::PageDown),
60 (KeyCode::PageUp, _) => Some(Action::PageUp),
61 (KeyCode::Char('J'), _) => Some(Action::DetailDown),
62 (KeyCode::Char('K'), _) => Some(Action::DetailUp),
63 (KeyCode::Char('a'), _) => Some(Action::OpenAdmin),
64 _ => None,
65 }
66}
67
68pub fn help_items(admin_available: bool) -> Vec<(&'static str, &'static str)> {
69 let mut items = vec![
70 ("q", "Quit"),
71 ("Esc", "Quit"),
72 ("?", "Toggle help"),
73 ("/", "Search"),
74 ("n/N", "Next/prev match"),
75 ("hjkl", "Move selection"),
76 ("g/G", "Jump top/bottom"),
77 ("Ctrl+d/u", "Page down/up"),
78 ("Enter", "Toggle detail"),
79 ("J/K", "Scroll detail"),
80 ];
81 if admin_available {
82 items.push(("a", "Admin console (toggle)"));
83 }
84 items
85}