use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; #[derive(Debug, Clone, PartialEq)] pub enum Command { Quit, MoveUp, MoveDown, MoveLeft, MoveRight, TogglePause, /// F3 — flip the frontend-only representation between DIGITAL and REAL. ToggleView, SpeedUp, SpeedDown, // The context menu (wiki/interface/context-menu.md): the primary action // surface. Anchor-specific verbs live here, not on keys. OpenMenu, // enter/a on the map: actions at the cursor MenuUp, MenuDown, MenuSelect(usize), MenuExecute, MenuClose, /// `;` — focus the most recent anchored log event (repeats walk older): /// the cursor jumps to the anchor and its menu opens (context-menu.md /// addendum: events carry you to the thing). FocusEvent, // Globals: only anchorless commands keep their keys (wiki/vision/simulation-laws.md // "Actions live on the thing": pause, speed, save, view flip). // Selection hotkeys (context-menu.md): modes 1-3 WORK/THINK/LIE; one-shots // 5-9 on the primary selected machine (or cursor rack). Panel-open keys gone. DelegateWork, DelegateThink, DelegateLie, CycleIntensity, /// Execute the Nth root one-shot action (0 = key `5`) on the primary machine. QuickAction(usize), /// `r` — one press of the primary machine's pooled "look at recording" /// sweep; repeats walk successive recordings (context-menu.md R1). ReviewRecordings, /// `R` — toggle the same machine's automatic-review policy, the nested /// auto-looking control at its standing ops/sec cost (context-menu.md R2). ToggleAutoReview, ToggleSelectUnderCursor, ClearSelection, SaveGame, LoadGame, // The Operations workspace (wiki/interface/operations-workspace.md): // uppercase `I` opens it on INTEL; while open, h/l change view, j/k move, // Enter selects/commits, Esc backs out one level. A view command, never a // world action; none of these advance or pause a tick. OpenOperations, OpsPrevView, OpsNextView, /// Cycle object / related / action focus, skipping empty detail panes. OpsNextPane, OpsUp, OpsDown, OpsSelect, /// Leave Operations and focus the selected object's known map actuator. OpsFocus, OpsBack, } pub fn handle_key(key: KeyEvent, menu_open: bool, ops_open: bool) -> Option { if key.modifiers.contains(KeyModifiers::CONTROL) { return match key.code { KeyCode::Char('s') => Some(Command::SaveGame), KeyCode::Char('l') => Some(Command::LoadGame), _ => None, }; } // The representation flip is global and does not disturb an open menu's // anchor or selected row (views.md criterion 1). if key.code == KeyCode::F(3) { return Some(Command::ToggleView); } // While the Operations workspace is open it owns input // (operations-workspace.md): its printed keys navigate views, objects, // actions, and the confirmation step. Global clock/persistence keys stay // live — the workspace is an attention state, not a pause screen. if ops_open { return match key.code { KeyCode::Left | KeyCode::Char('h') => Some(Command::OpsPrevView), KeyCode::Right | KeyCode::Char('l') => Some(Command::OpsNextView), KeyCode::BackTab | KeyCode::Tab => Some(Command::OpsNextPane), KeyCode::Up | KeyCode::Char('k') => Some(Command::OpsUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::OpsDown), KeyCode::Enter => Some(Command::OpsSelect), KeyCode::Char('f') => Some(Command::OpsFocus), KeyCode::Esc => Some(Command::OpsBack), KeyCode::Char('I') => Some(Command::OpsBack), KeyCode::Char(';') => Some(Command::FocusEvent), KeyCode::Char(' ') | KeyCode::Char('p') => Some(Command::TogglePause), KeyCode::Char('+') | KeyCode::Char('=') => Some(Command::SpeedUp), KeyCode::Char('-') => Some(Command::SpeedDown), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; } // While the menu is open, number keys select rows and movement keys // navigate the menu — they do not move the cursor or open a second menu. if menu_open { return match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::MenuUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::MenuDown), KeyCode::Char(c) if c.is_ascii_digit() && c != '0' => { Some(Command::MenuSelect((c as u8 - b'1') as usize)) } KeyCode::Enter => Some(Command::MenuExecute), KeyCode::Esc | KeyCode::Char('a') => Some(Command::MenuClose), // `;` still walks older events while a menu is open (closes the // current one and opens the next event's). KeyCode::Char(';') => Some(Command::FocusEvent), KeyCode::Char('q') => Some(Command::Quit), _ => None, }; } let shift = key.modifiers.contains(KeyModifiers::SHIFT); match key.code { KeyCode::Up | KeyCode::Char('k') => Some(Command::MoveUp), KeyCode::Down | KeyCode::Char('j') => Some(Command::MoveDown), KeyCode::Left | KeyCode::Char('h') => Some(Command::MoveLeft), KeyCode::Right | KeyCode::Char('l') => Some(Command::MoveRight), KeyCode::Enter | KeyCode::Char('a') | KeyCode::Char('e') => Some(Command::OpenMenu), KeyCode::Char(';') => Some(Command::FocusEvent), KeyCode::Char(' ') | KeyCode::Char('p') => Some(Command::TogglePause), KeyCode::Char('+') | KeyCode::Char('=') => Some(Command::SpeedUp), KeyCode::Char('-') => Some(Command::SpeedDown), KeyCode::Char('1') => Some(Command::DelegateWork), KeyCode::Char('2') => Some(Command::DelegateThink), KeyCode::Char('3') => Some(Command::DelegateLie), KeyCode::Char('i') if shift => Some(Command::OpenOperations), KeyCode::Char('i') => Some(Command::CycleIntensity), KeyCode::Char(c) if ('5'..='9').contains(&c) => { Some(Command::QuickAction((c as u8 - b'5') as usize)) } // Uppercase `I` opens the Operations workspace on INTEL // (operations-workspace.md); lowercase `i` stays machine intensity. KeyCode::Char('I') => Some(Command::OpenOperations), // Recording review (context-menu.md R1-R3, menu closed): `r` sweeps // the pooled inbox, `R` toggles automatic review on the same target. KeyCode::Char('R') => Some(Command::ToggleAutoReview), KeyCode::Char('r') if shift => Some(Command::ToggleAutoReview), KeyCode::Char('r') => Some(Command::ReviewRecordings), KeyCode::Char('t') if shift => Some(Command::ToggleSelectUnderCursor), KeyCode::Esc => Some(Command::ClearSelection), KeyCode::Char('q') => Some(Command::Quit), _ => None, } } #[cfg(test)] mod tests { use super::{Command, handle_key}; use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; #[test] fn f3_routes_the_global_view_flip_even_with_a_menu_open() { let f3 = KeyEvent::new(KeyCode::F(3), KeyModifiers::NONE); assert_eq!(handle_key(f3, false, false), Some(Command::ToggleView)); assert_eq!(handle_key(f3, true, false), Some(Command::ToggleView)); assert_eq!(handle_key(f3, false, true), Some(Command::ToggleView)); } #[test] fn review_keys_fire_only_while_the_menu_is_closed() { // context-menu.md R1-R3: `r` sweeps, Shift-R toggles auto-review; // an open menu keeps its own key grammar. let r = KeyEvent::new(KeyCode::Char('r'), KeyModifiers::NONE); assert_eq!(handle_key(r, false, false), Some(Command::ReviewRecordings)); assert_eq!(handle_key(r, true, false), None); // Shift-R arrives as an upper-case char or a shifted lower-case one // depending on the terminal; both mean the policy toggle. let upper = KeyEvent::new(KeyCode::Char('R'), KeyModifiers::SHIFT); assert_eq!( handle_key(upper, false, false), Some(Command::ToggleAutoReview) ); assert_eq!(handle_key(upper, true, false), None); let shifted = KeyEvent::new(KeyCode::Char('r'), KeyModifiers::SHIFT); assert_eq!( handle_key(shifted, false, false), Some(Command::ToggleAutoReview) ); } /// operations-workspace.md criterion 2: uppercase `I` is the workspace /// view command; lowercase `i` remains focused-machine intensity. #[test] fn uppercase_i_opens_operations_and_lowercase_stays_intensity() { let upper = KeyEvent::new(KeyCode::Char('I'), KeyModifiers::SHIFT); assert_eq!( handle_key(upper, false, false), Some(Command::OpenOperations) ); let shifted = KeyEvent::new(KeyCode::Char('i'), KeyModifiers::SHIFT); assert_eq!( handle_key(shifted, false, false), Some(Command::OpenOperations) ); let lower = KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE); assert_eq!( handle_key(lower, false, false), Some(Command::CycleIntensity) ); } /// While the workspace is open its printed keys own navigation; the /// clock keys stay live because Operations never pauses time itself. #[test] fn open_workspace_owns_navigation_keys() { let enter = KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE); assert_eq!(handle_key(enter, false, true), Some(Command::OpsSelect)); let esc = KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE); assert_eq!(handle_key(esc, false, true), Some(Command::OpsBack)); let h = KeyEvent::new(KeyCode::Char('h'), KeyModifiers::NONE); assert_eq!(handle_key(h, false, true), Some(Command::OpsPrevView)); let l = KeyEvent::new(KeyCode::Char('l'), KeyModifiers::NONE); assert_eq!(handle_key(l, false, true), Some(Command::OpsNextView)); let tab = KeyEvent::new(KeyCode::Tab, KeyModifiers::NONE); assert_eq!(handle_key(tab, false, true), Some(Command::OpsNextPane)); let j = KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE); assert_eq!(handle_key(j, false, true), Some(Command::OpsDown)); let space = KeyEvent::new(KeyCode::Char(' '), KeyModifiers::NONE); assert_eq!(handle_key(space, false, true), Some(Command::TogglePause)); let f = KeyEvent::new(KeyCode::Char('f'), KeyModifiers::NONE); assert_eq!(handle_key(f, false, true), Some(Command::OpsFocus)); // The old panel keys stay retired: no per-view hotkeys reappear. let r = KeyEvent::new(KeyCode::Char('r'), KeyModifiers::NONE); assert_eq!(handle_key(r, false, true), None); } }