Skip to main content

trail/input/
mod.rs

1//! Input handling: dispatches keystrokes by current mode.
2//!
3//! Routes key events to the appropriate handler based on `state.mode`:
4//! Navigation → `keymap::navigation`, Search → `keymap::search`,
5//! Command → `command_parser::feed` (Phase 3).
6
7pub mod command_parser;
8pub mod keymap;
9
10use crossterm::event::{KeyEvent, KeyEventKind, KeyModifiers};
11
12use crate::actions::Action;
13
14/// Whether a `Char` key with `modifiers` is a typed character rather than a chord.
15///
16/// Shift alone is text (capitals). Ctrl or Alt alone is a chord. Ctrl **and**
17/// Alt together are text too: that is how Windows reports AltGr, which many
18/// layouts need for `\`, `@`, `|` or `~` — on Spanish (Latin America) `\` is
19/// AltGr plus the key left of `1`. The console also replays pasted text as one
20/// keystroke per character with the modifiers the layout needs, so treating
21/// AltGr as a chord dropped every backslash of a pasted Windows path.
22///
23/// ```
24/// use crossterm::event::KeyModifiers;
25/// use trail::input::is_text_modifiers;
26///
27/// assert!(is_text_modifiers(KeyModifiers::SHIFT));
28/// assert!(is_text_modifiers(KeyModifiers::CONTROL | KeyModifiers::ALT));
29/// assert!(!is_text_modifiers(KeyModifiers::CONTROL));
30/// assert!(!is_text_modifiers(KeyModifiers::ALT));
31/// ```
32#[must_use]
33pub fn is_text_modifiers(modifiers: KeyModifiers) -> bool {
34    let altgr = KeyModifiers::CONTROL | KeyModifiers::ALT;
35    modifiers.contains(altgr) || !modifiers.intersects(altgr)
36}
37use crate::app::state::AppState;
38
39/// Mutable context shared with the keymap across ticks.
40///
41/// Holds any state that spans multiple key events (e.g. multi-key sequences)
42/// without polluting `AppState` for concerns that are purely input-layer.
43///
44/// Multi-key sequences such as `gg`, `ya`, `yr`, `yn`, and `dd` are handled
45/// through `AppState::pending_nav_key` (which the status bar can render) rather
46/// than `InputCtx` fields, so this struct is currently empty. It is kept as a
47/// named type so callers do not need updating if input-layer state is added
48/// in the future.
49#[derive(Debug, Default)]
50pub struct InputCtx {}
51
52/// Dispatches `key` to the appropriate mode handler.
53///
54/// Returns `Some(Action)` when the key maps to an action, `None` otherwise.
55/// The caller is responsible for calling `actions::apply` with the returned
56/// action and handling any resulting error.
57///
58/// Only `KeyEventKind::Press` events produce actions; `Release` and `Repeat`
59/// are ignored so crossterm's Windows double-fire doesn't double-process.
60pub fn dispatch(key: KeyEvent, state: &AppState, ctx: &mut InputCtx) -> Option<Action> {
61    // Ignore non-press events (crossterm fires Press + Release on Windows).
62    if key.kind != KeyEventKind::Press {
63        return None;
64    }
65
66    use crate::app::mode::Mode;
67    match &state.mode {
68        Mode::Navigation => keymap::navigation(key, ctx, state),
69
70        Mode::Visual { .. } => keymap::visual(key, ctx, state),
71
72        Mode::Search { .. } => keymap::search(key, &state.config.keymap),
73
74        Mode::Command { .. } => {
75            // Delegate entirely to the command_parser feed path via CommandKey.
76            // The actual buffer manipulation and submit/cancel logic is handled
77            // in actions::apply(Action::CommandKey) so it stays testable.
78            Some(Action::CommandKey(key))
79        }
80    }
81}