| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 23 |
pub enum Action { |
| 24 |
NextFocus, |
| 25 |
PrevFocus, |
| 26 |
NextTab, |
| 27 |
PrevTab, |
| 28 |
Activate, |
| 29 |
Cancel, |
| 30 |
Save, |
| 31 |
Help, |
| 32 |
Quit, |
| 33 |
Filter, |
| 34 |
Command, |
| 35 |
Passthrough, |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
pub fn classify(key: KeyEvent) -> Action { |
| 58 |
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL); |
| 59 |
let shift = key.modifiers.contains(KeyModifiers::SHIFT); |
| 60 |
|
| 61 |
match key.code { |
| 62 |
KeyCode::Char('s') | KeyCode::Char('S') if ctrl => Action::Save, |
| 63 |
KeyCode::BackTab => Action::PrevFocus, |
| 64 |
KeyCode::Tab if shift => Action::PrevFocus, |
| 65 |
KeyCode::Tab => Action::NextFocus, |
| 66 |
KeyCode::Char('l') => Action::NextTab, |
| 67 |
KeyCode::Char('h') => Action::PrevTab, |
| 68 |
KeyCode::Enter => Action::Activate, |
| 69 |
KeyCode::Esc => Action::Cancel, |
| 70 |
KeyCode::Char('?') => Action::Help, |
| 71 |
KeyCode::Char('q') => Action::Quit, |
| 72 |
KeyCode::Char('/') => Action::Filter, |
| 73 |
KeyCode::Char(':') => Action::Command, |
| 74 |
_ => Action::Passthrough, |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
#[cfg(test)] |
| 79 |
mod tests { |
| 80 |
use super::*; |
| 81 |
|
| 82 |
fn key(code: KeyCode, mods: KeyModifiers) -> KeyEvent { |
| 83 |
KeyEvent::new(code, mods) |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn ctrl_s_saves_but_bare_s_does_not() { |
| 88 |
assert_eq!(classify(key(KeyCode::Char('s'), KeyModifiers::CONTROL)), Action::Save); |
| 89 |
assert_eq!(classify(key(KeyCode::Char('s'), KeyModifiers::NONE)), Action::Passthrough); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#[test] |
| 97 |
fn both_shift_tab_encodings_move_focus_backward() { |
| 98 |
assert_eq!(classify(key(KeyCode::BackTab, KeyModifiers::NONE)), Action::PrevFocus); |
| 99 |
assert_eq!(classify(key(KeyCode::BackTab, KeyModifiers::SHIFT)), Action::PrevFocus); |
| 100 |
assert_eq!(classify(key(KeyCode::Tab, KeyModifiers::SHIFT)), Action::PrevFocus); |
| 101 |
assert_eq!(classify(key(KeyCode::Tab, KeyModifiers::NONE)), Action::NextFocus); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn unreserved_keys_pass_through() { |
| 106 |
assert_eq!(classify(key(KeyCode::Char('j'), KeyModifiers::NONE)), Action::Passthrough); |
| 107 |
assert_eq!(classify(key(KeyCode::Down, KeyModifiers::NONE)), Action::Passthrough); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn h_and_l_move_between_tabs() { |
| 112 |
assert_eq!(classify(key(KeyCode::Char('l'), KeyModifiers::NONE)), Action::NextTab); |
| 113 |
assert_eq!(classify(key(KeyCode::Char('h'), KeyModifiers::NONE)), Action::PrevTab); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#[test] |
| 120 |
fn tab_key_still_means_focus_not_tabs() { |
| 121 |
assert_eq!(classify(key(KeyCode::Tab, KeyModifiers::NONE)), Action::NextFocus); |
| 122 |
assert_eq!(classify(key(KeyCode::BackTab, KeyModifiers::NONE)), Action::PrevFocus); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
#[test] |
| 128 |
fn vertical_vim_keys_are_not_claimed_by_tabs() { |
| 129 |
assert_eq!(classify(key(KeyCode::Char('j'), KeyModifiers::NONE)), Action::Passthrough); |
| 130 |
assert_eq!(classify(key(KeyCode::Char('k'), KeyModifiers::NONE)), Action::Passthrough); |
| 131 |
} |
| 132 |
} |
| 133 |
|