use crossterm::event::{
Event as CtEvent, KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseEventKind,
};
use crate::action::Action;
use crate::app::{App, Focus, Message, Overlay, TabMode};
use crate::config::keymap::{KeyChord, KeyContext, Resolution};
use crate::search::content::SearchEvent;
use crate::ui::hints;
use crate::ui::overlay::prompt::TextEdit;
use crate::vault::index::IndexEvent;
use crate::vault::walker::WalkEvent;
use crate::vault::watch::WatchEvent;
pub fn translate(app: &mut App, message: Message) -> Vec<Action> {
match message {
Message::Input(event) => translate_input(app, event),
Message::Signal(signal) => translate_signal(signal),
Message::SyntaxReady => vec![Action::SyntaxReady],
Message::ThemeChanged => vec![Action::ReloadTheme],
Message::Watch(event) => vec![match event {
WatchEvent::Changed(paths) => Action::FilesChanged(paths),
WatchEvent::Removed(paths) => Action::FilesRemoved(paths),
WatchEvent::Stopped(reason) => Action::WatchStopped(reason),
}],
Message::Search(event) => vec![match event {
SearchEvent::Hits(hits) => Action::SearchHits(hits),
SearchEvent::Finished { total, truncated } => {
Action::SearchFinished { total, truncated }
}
SearchEvent::BadPattern(e) => Action::SearchFailed(e),
}],
Message::Index(event) => vec![match event {
IndexEvent::Indexed(entries) => Action::IndexBatch(entries),
IndexEvent::Finished => Action::IndexFinished,
}],
Message::Walk(event) => vec![match event {
WalkEvent::Entries(entries) => Action::VaultEntries(entries),
WalkEvent::Finished(total) => Action::VaultWalkFinished(total),
WalkEvent::Failed(reason) => Action::VaultWalkFailed(reason),
}],
}
}
fn translate_input(app: &mut App, event: CtEvent) -> Vec<Action> {
match event {
CtEvent::Key(key) => translate_key(app, key),
CtEvent::Resize(width, height) => vec![Action::Resize(width, height)],
CtEvent::Mouse(mouse) => match mouse.kind {
MouseEventKind::ScrollDown => vec![Action::ScrollWheelDown],
MouseEventKind::ScrollUp => vec![Action::ScrollWheelUp],
_ => Vec::new(),
},
CtEvent::Paste(text) if app.tab().mode == TabMode::Edit => {
vec![Action::EditPaste(text)]
}
_ => Vec::new(),
}
}
fn translate_key(app: &mut App, key: KeyEvent) -> Vec<Action> {
if key.kind == KeyEventKind::Release {
return Vec::new();
}
if key.code == KeyCode::Char('c') && key.modifiers.contains(KeyModifiers::CONTROL) {
return vec![Action::Quit];
}
let chord = KeyChord::from_event(key);
if app.overlay.is_some() {
return translate_overlay_key(app, chord);
}
if app.sidebar.filter.is_some() {
return translate_text_line(chord);
}
let context = context_for(app);
match app.keymap.resolve(context, chord) {
Resolution::Action(action) => {
app.status.pending = None;
vec![action]
}
Resolution::Pending(hint) => {
app.status.pending = Some(hint);
Vec::new()
}
Resolution::Unbound => {
app.status.pending = None;
if context == KeyContext::Edit {
return vec![Action::EditInput(key)];
}
Vec::new()
}
}
}
fn context_for(app: &App) -> KeyContext {
if app.tab().mode == TabMode::Edit {
KeyContext::Edit
} else if app.focus == Focus::Sidebar {
KeyContext::Sidebar
} else {
KeyContext::Viewport
}
}
fn translate_text_line(chord: KeyChord) -> Vec<Action> {
let edit = |edit| vec![Action::TreeFilterEdit(edit)];
let ctrl = chord.modifiers.contains(KeyModifiers::CONTROL);
match chord.code {
KeyCode::Esc => vec![Action::TreeFilterCancel],
KeyCode::Enter => vec![Action::TreeFilterAccept],
KeyCode::Backspace => edit(TextEdit::Backspace),
KeyCode::Delete => edit(TextEdit::Delete),
KeyCode::Left => edit(TextEdit::Left),
KeyCode::Right => edit(TextEdit::Right),
KeyCode::Home => edit(TextEdit::Home),
KeyCode::End => edit(TextEdit::End),
KeyCode::Down => vec![Action::SidebarDown],
KeyCode::Up => vec![Action::SidebarUp],
KeyCode::Char('u') if ctrl => edit(TextEdit::Clear),
KeyCode::Char('w') if ctrl => edit(TextEdit::DeleteWordBack),
KeyCode::Char(c) if !ctrl && !chord.modifiers.contains(KeyModifiers::ALT) => {
edit(TextEdit::Insert(c))
}
_ => Vec::new(),
}
}
fn translate_find_key(chord: KeyChord) -> Vec<Action> {
let edit = |edit| vec![Action::FindEdit(edit)];
let ctrl = chord.modifiers.contains(KeyModifiers::CONTROL);
match chord.code {
KeyCode::Esc => vec![Action::CloseFind],
KeyCode::Enter if chord.modifiers.contains(KeyModifiers::SHIFT) => vec![Action::FindPrev],
KeyCode::Enter => vec![Action::FindNext],
KeyCode::Down => vec![Action::FindNext],
KeyCode::Up => vec![Action::FindPrev],
KeyCode::Char('n') if ctrl => vec![Action::FindNext],
KeyCode::Char('p') if ctrl => vec![Action::FindPrev],
KeyCode::Backspace => edit(TextEdit::Backspace),
KeyCode::Delete => edit(TextEdit::Delete),
KeyCode::Left => edit(TextEdit::Left),
KeyCode::Right => edit(TextEdit::Right),
KeyCode::Home => edit(TextEdit::Home),
KeyCode::End => edit(TextEdit::End),
KeyCode::Char('u') if ctrl => edit(TextEdit::Clear),
KeyCode::Char('w') if ctrl => edit(TextEdit::DeleteWordBack),
KeyCode::Char(c) if !chord.modifiers.intersects(CTRL_OR_ALT) => edit(TextEdit::Insert(c)),
_ => Vec::new(),
}
}
fn translate_line_key(
chord: KeyChord,
edit: impl Fn(TextEdit) -> Action,
accept: Action,
cancel: Action,
) -> Vec<Action> {
let ctrl = chord.modifiers.contains(KeyModifiers::CONTROL);
match chord.code {
KeyCode::Esc => vec![cancel],
KeyCode::Enter => vec![accept],
KeyCode::Backspace => vec![edit(TextEdit::Backspace)],
KeyCode::Delete => vec![edit(TextEdit::Delete)],
KeyCode::Left => vec![edit(TextEdit::Left)],
KeyCode::Right => vec![edit(TextEdit::Right)],
KeyCode::Home => vec![edit(TextEdit::Home)],
KeyCode::End => vec![edit(TextEdit::End)],
KeyCode::Char('u') if ctrl => vec![edit(TextEdit::Clear)],
KeyCode::Char('w') if ctrl => vec![edit(TextEdit::DeleteWordBack)],
KeyCode::Char(c) if !chord.modifiers.intersects(CTRL_OR_ALT) => {
vec![edit(TextEdit::Insert(c))]
}
_ => Vec::new(),
}
}
const CTRL_OR_ALT: KeyModifiers = KeyModifiers::CONTROL.union(KeyModifiers::ALT);
fn translate_overlay_key(app: &mut App, chord: KeyChord) -> Vec<Action> {
let Some(overlay) = &mut app.overlay else {
return Vec::new();
};
match overlay {
Overlay::Help { scroll } => match chord.code {
KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('?') => vec![Action::Escape],
KeyCode::Char('j') | KeyCode::Down => {
*scroll = scroll.saturating_add(1);
Vec::new()
}
KeyCode::Char('k') | KeyCode::Up => {
*scroll = scroll.saturating_sub(1);
Vec::new()
}
KeyCode::Char('g') | KeyCode::Home => {
*scroll = 0;
Vec::new()
}
KeyCode::PageDown => {
*scroll = scroll.saturating_add(10);
Vec::new()
}
KeyCode::PageUp => {
*scroll = scroll.saturating_sub(10);
Vec::new()
}
_ => Vec::new(),
},
Overlay::Find => translate_find_key(chord),
Overlay::Confirm { choices, .. } => match chord.code {
KeyCode::Esc => vec![Action::Escape],
KeyCode::Char(c)
if choices
.iter()
.any(|(key, _)| *key == c.to_ascii_lowercase()) =>
{
vec![Action::Confirm(c.to_ascii_lowercase())]
}
_ => Vec::new(),
},
Overlay::Prompt { .. } => translate_line_key(
chord,
Action::PromptEdit,
Action::PromptAccept,
Action::Escape,
),
Overlay::Switcher { .. } => match chord.code {
KeyCode::Down | KeyCode::Tab => vec![Action::SwitcherMove(1)],
KeyCode::Up | KeyCode::BackTab => vec![Action::SwitcherMove(-1)],
KeyCode::Char('n') if chord.modifiers.contains(KeyModifiers::CONTROL) => {
vec![Action::SwitcherMove(1)]
}
KeyCode::Char('p') if chord.modifiers.contains(KeyModifiers::CONTROL) => {
vec![Action::SwitcherMove(-1)]
}
KeyCode::Enter if chord.modifiers.contains(KeyModifiers::CONTROL) => {
vec![Action::SwitcherAccept { new_tab: true }]
}
_ => translate_line_key(
chord,
Action::SwitcherEdit,
Action::SwitcherAccept { new_tab: false },
Action::Escape,
),
},
Overlay::Disambiguate {
candidates,
selected,
..
} => {
let last = candidates.len().saturating_sub(1);
match chord.code {
KeyCode::Esc | KeyCode::Char('q') => vec![Action::Escape],
KeyCode::Enter | KeyCode::Char('l') => vec![Action::ChooseCandidate(*selected)],
KeyCode::Char('j') | KeyCode::Down => {
*selected = (*selected + 1).min(last);
Vec::new()
}
KeyCode::Char('k') | KeyCode::Up => {
*selected = selected.saturating_sub(1);
Vec::new()
}
KeyCode::Char(c) if c.is_ascii_digit() && c != '0' => {
let at = c as usize - '1' as usize;
if at <= last {
vec![Action::ChooseCandidate(at)]
} else {
Vec::new()
}
}
_ => Vec::new(),
}
}
Overlay::Hints { links, typed } => {
let count = links.len();
match chord.code {
KeyCode::Esc => vec![Action::Escape],
KeyCode::Backspace => {
typed.pop();
Vec::new()
}
KeyCode::Char(c) if !chord.modifiers.intersects(CTRL_OR_ALT) => {
typed.push(c.to_ascii_lowercase());
match hints::match_typed(typed, count) {
hints::HintMatch::Complete(at) => {
let link = links[at];
vec![Action::Escape, Action::FollowHintedLink(link)]
}
hints::HintMatch::Partial => Vec::new(),
hints::HintMatch::None => {
typed.pop();
Vec::new()
}
}
}
_ => Vec::new(),
}
}
}
}
fn translate_signal(signal: i32) -> Vec<Action> {
#[cfg(unix)]
{
use signal_hook::consts::{SIGCONT, SIGHUP, SIGINT, SIGTERM, SIGTSTP};
match signal {
SIGINT | SIGTERM | SIGHUP => vec![Action::ForceQuit(exit_code_for_signal(signal))],
SIGTSTP => vec![Action::Suspend],
SIGCONT => Vec::new(),
_ => Vec::new(),
}
}
#[cfg(not(unix))]
{
let _ = signal;
Vec::new()
}
}
pub fn exit_code_for_signal(signal: i32) -> u8 {
(128 + signal).clamp(0, 255) as u8
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::keymap::Keymap;
use crate::config::schema::{FilesConfig, UiConfig};
use crate::theme::Theme;
fn app() -> App {
let mut app = App::new(
Theme::dark(),
Keymap::defaults(),
UiConfig::default(),
FilesConfig::default(),
);
app.update(Action::Resize(120, 40));
app
}
fn press(app: &mut App, code: KeyCode, modifiers: KeyModifiers) -> Vec<Action> {
translate_key(app, KeyEvent::new(code, modifiers))
}
#[test]
fn a_bound_key_produces_its_action() {
let mut app = app();
assert_eq!(
press(&mut app, KeyCode::Char('q'), KeyModifiers::NONE),
vec![Action::Quit]
);
}
#[test]
fn context_follows_focus() {
let mut app = app();
assert_eq!(
press(&mut app, KeyCode::Char('j'), KeyModifiers::NONE),
vec![Action::ScrollLineDown]
);
app.update(Action::FocusNext);
assert_eq!(app.focus, Focus::Sidebar);
assert_eq!(
press(&mut app, KeyCode::Char('j'), KeyModifiers::NONE),
vec![Action::SidebarDown]
);
}
#[test]
fn a_pending_sequence_is_shown_in_the_status_bar() {
let mut app = app();
assert!(press(&mut app, KeyCode::Char('g'), KeyModifiers::NONE).is_empty());
assert_eq!(app.status.pending.as_deref(), Some("g…"));
assert_eq!(
press(&mut app, KeyCode::Char('t'), KeyModifiers::NONE),
vec![Action::NextTab]
);
assert!(app.status.pending.is_none());
}
#[test]
fn key_release_events_are_ignored() {
let mut app = app();
let mut key = KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE);
key.kind = KeyEventKind::Release;
assert!(translate_key(&mut app, key).is_empty());
}
#[test]
fn ctrl_c_always_quits_even_behind_an_overlay() {
let mut app = app();
app.update(Action::ToggleHelp);
assert_eq!(
press(&mut app, KeyCode::Char('c'), KeyModifiers::CONTROL),
vec![Action::Quit]
);
}
#[test]
fn an_overlay_swallows_unrelated_keys() {
let mut app = app();
app.update(Action::ToggleHelp);
assert_eq!(
press(&mut app, KeyCode::Char('q'), KeyModifiers::NONE),
vec![Action::Escape]
);
assert!(app.overlay.is_some());
assert!(press(&mut app, KeyCode::Char('t'), KeyModifiers::CONTROL).is_empty());
}
#[test]
fn the_help_overlay_scrolls() {
let mut app = app();
app.update(Action::ToggleHelp);
press(&mut app, KeyCode::Char('j'), KeyModifiers::NONE);
press(&mut app, KeyCode::Char('j'), KeyModifiers::NONE);
assert_eq!(app.overlay, Some(Overlay::Help { scroll: 2 }));
press(&mut app, KeyCode::Char('k'), KeyModifiers::NONE);
assert_eq!(app.overlay, Some(Overlay::Help { scroll: 1 }));
}
#[test]
fn a_resize_event_becomes_a_resize_action() {
let mut app = app();
assert_eq!(
translate_input(&mut app, CtEvent::Resize(80, 24)),
vec![Action::Resize(80, 24)]
);
}
#[cfg(unix)]
#[test]
fn signals_map_to_the_right_actions_and_codes() {
use signal_hook::consts::{SIGCONT, SIGINT, SIGTERM, SIGTSTP};
assert_eq!(translate_signal(SIGINT), vec![Action::ForceQuit(130)]);
assert_eq!(translate_signal(SIGTERM), vec![Action::ForceQuit(143)]);
assert_eq!(translate_signal(SIGTSTP), vec![Action::Suspend]);
assert!(translate_signal(SIGCONT).is_empty());
assert_eq!(exit_code_for_signal(SIGINT), 130);
assert_eq!(exit_code_for_signal(SIGTERM), 143);
}
}