use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use super::{Action, InputKind};
#[derive(Debug, Default)]
pub struct NormalState {
pub pending_g: bool,
}
pub fn handle_key(key: KeyEvent, state: &mut NormalState) -> Action {
if state.pending_g {
state.pending_g = false;
return match key.code {
KeyCode::Char('g') => Action::CursorTop, KeyCode::Char('e') => Action::CursorBottom, KeyCode::Char('h') => Action::GotoHome,
KeyCode::Char('r') => Action::GotoRoot,
KeyCode::Char('o') => Action::GotoOtherPane,
KeyCode::Char('p') => Action::GotoPrevious,
KeyCode::Char(c @ '1'..='9') => Action::GotoBookmark((c as u8 - b'1') as usize),
_ => Action::None,
};
}
match key.code {
KeyCode::Char('q') => Action::Quit,
KeyCode::Char('j') | KeyCode::Down => Action::CursorDown,
KeyCode::Char('k') | KeyCode::Up => Action::CursorUp,
KeyCode::Char('h') | KeyCode::Backspace => Action::ParentDir,
KeyCode::Char('l') | KeyCode::Enter => Action::EnterDir,
KeyCode::Char('G') => Action::CursorBottom,
KeyCode::Char('g') => {
state.pending_g = true;
Action::None
}
KeyCode::PageUp => Action::PageUp,
KeyCode::PageDown => Action::PageDown,
KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => Action::HalfPageDown,
KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => Action::HalfPageUp,
KeyCode::Tab => Action::SwitchPane,
KeyCode::Left => Action::FocusLeftPane,
KeyCode::Right => Action::FocusRightPane,
KeyCode::Char('v') => Action::EnterSelect,
KeyCode::Char(' ') => Action::EnterSpace,
KeyCode::Char(':') => Action::EnterCommand,
KeyCode::Char('/') => Action::EnterInput(InputKind::Search),
KeyCode::Char('r') => Action::EnterInput(InputKind::Rename),
KeyCode::Char('d') => Action::DeleteSelected,
KeyCode::Char('y') => Action::CopyToOther,
KeyCode::Char('.') => Action::ToggleHidden,
KeyCode::Char('p') | KeyCode::Char('P') => Action::TogglePreview,
KeyCode::Char('?') => Action::ShowHelp,
KeyCode::Char('J') => Action::SelectExtendDown,
KeyCode::Char('K') => Action::SelectExtendUp,
KeyCode::Char(';') => Action::DeselectAll,
KeyCode::Esc => Action::ExitToNormal,
_ => Action::None,
}
}