use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
None,
Quit,
Up,
Down,
First,
Last,
PageUp,
PageDown,
Expand,
Open,
Edit,
Collapse,
Toggle,
ExpandAll,
CollapseAll,
CycleSort,
ReverseSort,
TogglePreview,
CycleLens,
JumpLens(u8),
ToggleZeros,
ToggleExclude,
CycleFocus,
Yank,
ToggleMouseCapture,
}
pub fn map_key(key: KeyEvent) -> Action {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let shift = key.modifiers.contains(KeyModifiers::SHIFT);
match key.code {
KeyCode::Char('c') if ctrl => Action::Quit,
KeyCode::Char('d') if ctrl => Action::PageDown,
KeyCode::Char('u') if ctrl => Action::PageUp,
KeyCode::Char('q') => Action::Quit,
KeyCode::Char('j') | KeyCode::Down => Action::Down,
KeyCode::Char('k') | KeyCode::Up => Action::Up,
KeyCode::Char('g') | KeyCode::Home => Action::First,
KeyCode::Char('G') | KeyCode::End => Action::Last,
KeyCode::PageDown => Action::PageDown,
KeyCode::PageUp => Action::PageUp,
KeyCode::Char('l') | KeyCode::Right => Action::Expand,
KeyCode::Enter if shift => Action::Edit,
KeyCode::Enter => Action::Open,
KeyCode::Char('e') => Action::Edit,
KeyCode::Char('h') | KeyCode::Left => Action::Collapse,
KeyCode::Char(' ') => Action::Toggle,
KeyCode::Char('E') => Action::ExpandAll,
KeyCode::Char('C') => Action::CollapseAll,
KeyCode::Char('s') => Action::CycleSort,
KeyCode::Char('r') => Action::ReverseSort,
KeyCode::Char('p') => Action::TogglePreview,
KeyCode::Char('m') => Action::CycleLens,
KeyCode::Char(c @ '1'..='9') => Action::JumpLens(c as u8 - b'0'),
KeyCode::Char('z') => Action::ToggleZeros,
KeyCode::Char('x') => Action::ToggleExclude,
KeyCode::Char('w') => Action::CycleFocus,
KeyCode::Char('y') => Action::Yank,
KeyCode::Char('S') => Action::ToggleMouseCapture,
_ => Action::None,
}
}