| 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' | '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!( |
| 89 |
classify(key(KeyCode::Char('s'), KeyModifiers::CONTROL)), |
| 90 |
Action::Save |
| 91 |
); |
| 92 |
assert_eq!( |
| 93 |
classify(key(KeyCode::Char('s'), KeyModifiers::NONE)), |
| 94 |
Action::Passthrough |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
#[test] |
| 103 |
fn both_shift_tab_encodings_move_focus_backward() { |
| 104 |
assert_eq!( |
| 105 |
classify(key(KeyCode::BackTab, KeyModifiers::NONE)), |
| 106 |
Action::PrevFocus |
| 107 |
); |
| 108 |
assert_eq!( |
| 109 |
classify(key(KeyCode::BackTab, KeyModifiers::SHIFT)), |
| 110 |
Action::PrevFocus |
| 111 |
); |
| 112 |
assert_eq!( |
| 113 |
classify(key(KeyCode::Tab, KeyModifiers::SHIFT)), |
| 114 |
Action::PrevFocus |
| 115 |
); |
| 116 |
assert_eq!( |
| 117 |
classify(key(KeyCode::Tab, KeyModifiers::NONE)), |
| 118 |
Action::NextFocus |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn unreserved_keys_pass_through() { |
| 124 |
assert_eq!( |
| 125 |
classify(key(KeyCode::Char('j'), KeyModifiers::NONE)), |
| 126 |
Action::Passthrough |
| 127 |
); |
| 128 |
assert_eq!( |
| 129 |
classify(key(KeyCode::Down, KeyModifiers::NONE)), |
| 130 |
Action::Passthrough |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn h_and_l_move_between_tabs() { |
| 136 |
assert_eq!( |
| 137 |
classify(key(KeyCode::Char('l'), KeyModifiers::NONE)), |
| 138 |
Action::NextTab |
| 139 |
); |
| 140 |
assert_eq!( |
| 141 |
classify(key(KeyCode::Char('h'), KeyModifiers::NONE)), |
| 142 |
Action::PrevTab |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
#[test] |
| 150 |
fn tab_key_still_means_focus_not_tabs() { |
| 151 |
assert_eq!( |
| 152 |
classify(key(KeyCode::Tab, KeyModifiers::NONE)), |
| 153 |
Action::NextFocus |
| 154 |
); |
| 155 |
assert_eq!( |
| 156 |
classify(key(KeyCode::BackTab, KeyModifiers::NONE)), |
| 157 |
Action::PrevFocus |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
#[test] |
| 164 |
fn vertical_vim_keys_are_not_claimed_by_tabs() { |
| 165 |
assert_eq!( |
| 166 |
classify(key(KeyCode::Char('j'), KeyModifiers::NONE)), |
| 167 |
Action::Passthrough |
| 168 |
); |
| 169 |
assert_eq!( |
| 170 |
classify(key(KeyCode::Char('k'), KeyModifiers::NONE)), |
| 171 |
Action::Passthrough |
| 172 |
); |
| 173 |
} |
| 174 |
} |
| 175 |
|