| 1 | //! Keyboard-shortcut predicates and platform-specific labels. |
| 2 | //! |
| 3 | //! These helpers normalise the cross-platform variations between |
| 4 | //! `Ctrl+…` (Linux/Windows) and `Cmd+…` (macOS), legacy `Ctrl+H`-as- |
| 5 | //! backspace handling, and the macOS Option-Latin-character escapes. |
| 6 | //! Centralising them |
| 7 | //! keeps the composer / transcript event loops in `ui.rs` short and |
| 8 | //! lets us add a new platform without touching the call sites. |
| 9 | |
| 10 | use std::borrow::Cow; |
| 11 | |
| 12 | use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; |
| 13 | |
| 14 | pub(super) fn has_control_like_modifier(modifiers: KeyModifiers) -> bool { |
| 15 | has_control_like_modifier_for_platform(modifiers, cfg!(target_os = "macos")) |
| 16 | } |
| 17 | |
| 18 | pub(super) fn has_control_like_modifier_for_platform( |
| 19 | modifiers: KeyModifiers, |
| 20 | is_macos: bool, |
| 21 | ) -> bool { |
| 22 | modifiers.contains(KeyModifiers::CONTROL) |
| 23 | || (is_macos && modifiers.contains(KeyModifiers::SUPER)) |
| 24 | } |
| 25 | |
| 26 | /// Compatibility path for enhanced terminal clients that forward `Cmd+C` or |
| 27 | /// `Ctrl+Shift+C` as key events. Most terminals consume these locally, so the |
| 28 | /// user-visible Codewhale binding remains `Ctrl+C` with an active selection. |
| 29 | pub(super) fn is_copy_shortcut(key: &KeyEvent) -> bool { |
| 30 | let is_c = matches!(key.code, KeyCode::Char('c') | KeyCode::Char('C')); |
| 31 | if !is_c { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | let cmd_c = key.modifiers.contains(KeyModifiers::SUPER) |
| 36 | && !key |
| 37 | .modifiers |
| 38 | .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT); |
| 39 | let ctrl_shift_c = key.modifiers.contains(KeyModifiers::CONTROL) |
| 40 | && key.modifiers.contains(KeyModifiers::SHIFT) |
| 41 | && !key |
| 42 | .modifiers |
| 43 | .intersects(KeyModifiers::ALT | KeyModifiers::SUPER); |
| 44 | cmd_c || ctrl_shift_c |
| 45 | } |
| 46 | |
| 47 | pub(super) fn is_agent_board_shortcut(key: &KeyEvent) -> bool { |
| 48 | let forbidden = KeyModifiers::ALT | KeyModifiers::SUPER; |
| 49 | if key.modifiers.intersects(forbidden) { |
| 50 | return false; |
| 51 | } |
| 52 | let ctrl_backslash = matches!(key.code, KeyCode::Char('\\')) |
| 53 | && key.modifiers.contains(KeyModifiers::CONTROL) |
| 54 | && !key.modifiers.contains(KeyModifiers::SHIFT); |
| 55 | let legacy_fs = matches!(key.code, KeyCode::Char('\u{1c}')); |
| 56 | ctrl_backslash || legacy_fs |
| 57 | } |
| 58 | |
| 59 | /// Toggle the file-tree pane: `Ctrl+Shift+E` on Linux/Windows or |
| 60 | /// `Cmd+Shift+E` on macOS. |
| 61 | pub(super) fn is_file_tree_toggle_shortcut(key: &KeyEvent) -> bool { |
| 62 | let is_e = matches!(key.code, KeyCode::Char('e' | 'E')); |
| 63 | if !is_e || !key.modifiers.contains(KeyModifiers::SHIFT) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | let has_forbidden_modifier = |
| 68 | key.modifiers.contains(KeyModifiers::ALT) || key.modifiers.contains(KeyModifiers::SUPER); |
| 69 | let ctrl_shift_e = key.modifiers.contains(KeyModifiers::CONTROL) && !has_forbidden_modifier; |
| 70 | |
| 71 | let cmd_shift_e = key.modifiers.contains(KeyModifiers::SUPER) |
| 72 | && !key.modifiers.contains(KeyModifiers::CONTROL) |
| 73 | && !key.modifiers.contains(KeyModifiers::ALT); |
| 74 | |
| 75 | ctrl_shift_e || cmd_shift_e |
| 76 | } |
| 77 | |
| 78 | pub(super) fn tool_details_shortcut_label() -> Cow<'static, str> { |
| 79 | crate::tui::shell_key_routing::tool_details_chord() |
| 80 | } |
| 81 | |
| 82 | /// Compact affordance: platform chord + short verb (`⌥V output`, `Alt+V list`). |
| 83 | /// Not a sentence. |
| 84 | pub(super) fn tool_details_shortcut_action_hint(verb: &str) -> String { |
| 85 | format!("{} {verb}", tool_details_shortcut_label()) |
| 86 | } |
| 87 | |
| 88 | /// Open the full reasoning detail pager for the selected or current turn. |
| 89 | /// Ctrl+O now shows the recorded reasoning timeline, not the whole-turn |
| 90 | /// inspector (#v092-reasoning-fix). |
| 91 | pub(super) fn is_reasoning_detail_shortcut(key: &KeyEvent) -> bool { |
| 92 | matches!(key.code, KeyCode::Char('o') | KeyCode::Char('O')) |
| 93 | && key.modifiers.contains(KeyModifiers::CONTROL) |
| 94 | && !key |
| 95 | .modifiers |
| 96 | .intersects(KeyModifiers::SHIFT | KeyModifiers::ALT | KeyModifiers::SUPER) |
| 97 | } |
| 98 | |
| 99 | /// Open the whole-turn inspector on a dedicated, collision-free chord. |
| 100 | /// Ctrl+Alt+O was free in the keybinding registry; it is distinct from |
| 101 | /// Ctrl+O (reasoning detail) and Ctrl+Shift+O (external editor). |
| 102 | pub(super) fn is_turn_inspector_shortcut(key: &KeyEvent) -> bool { |
| 103 | matches!(key.code, KeyCode::Char('o') | KeyCode::Char('O')) |
| 104 | && key.modifiers.contains(KeyModifiers::CONTROL) |
| 105 | && key.modifiers.contains(KeyModifiers::ALT) |
| 106 | && !key |
| 107 | .modifiers |
| 108 | .intersects(KeyModifiers::SHIFT | KeyModifiers::SUPER) |
| 109 | } |
| 110 | |
| 111 | /// Open the composer draft in `$VISUAL` / `$EDITOR` without colliding with |
| 112 | /// the reasoning detail or Turn Inspector shortcuts. Enhanced protocols can |
| 113 | /// report either character case, but SHIFT must be explicit so Windows Caps |
| 114 | /// Lock cannot misroute Ctrl+O. F4 is the fallback for legacy protocols that |
| 115 | /// cannot encode Ctrl+Shift+O. |
| 116 | pub(super) fn is_external_editor_shortcut(key: &KeyEvent) -> bool { |
| 117 | let ctrl_shift_o = matches!(key.code, KeyCode::Char('o') | KeyCode::Char('O')) |
| 118 | && key.modifiers.contains(KeyModifiers::CONTROL) |
| 119 | && key.modifiers.contains(KeyModifiers::SHIFT) |
| 120 | && !key |
| 121 | .modifiers |
| 122 | .intersects(KeyModifiers::ALT | KeyModifiers::SUPER); |
| 123 | let f4 = matches!(key.code, KeyCode::F(4)) && key.modifiers.is_empty(); |
| 124 | ctrl_shift_o || f4 |
| 125 | } |
| 126 | |
| 127 | /// Select the whole composer draft. `Ctrl+A` is intentionally NOT select-all: |
| 128 | /// it keeps its readline meaning (jump to start of input), matching every |
| 129 | /// other emacs-style binding in the composer. Select-all is therefore: |
| 130 | /// |
| 131 | /// - `Ctrl+Shift+A` on every platform (mirrors `Ctrl+Shift+O` / `Ctrl+Shift+E` |
| 132 | /// precedent for shifted-Ctrl chords; requires an enhanced-keyboard |
| 133 | /// terminal, like those precedents). |
| 134 | /// - `Cmd+A` on macOS terminals that forward Cmd to the app (kitty, WezTerm, |
| 135 | /// iTerm2 with "Left/Right Command" remapping). The event-loop macOS |
| 136 | /// normalization deliberately skips this chord so `Cmd+A` is not collapsed |
| 137 | /// into readline `Ctrl+A`. `Cmd+Shift+A` also lands here after |
| 138 | /// normalization. |
| 139 | pub(super) fn is_select_all_shortcut(key: &KeyEvent) -> bool { |
| 140 | let is_a = matches!(key.code, KeyCode::Char('a') | KeyCode::Char('A')); |
| 141 | if !is_a { |
| 142 | return false; |
| 143 | } |
| 144 | let cmd_a = key.modifiers.contains(KeyModifiers::SUPER) |
| 145 | && !key |
| 146 | .modifiers |
| 147 | .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT); |
| 148 | let ctrl_shift_a = key.modifiers.contains(KeyModifiers::CONTROL) |
| 149 | && key.modifiers.contains(KeyModifiers::SHIFT) |
| 150 | && !key |
| 151 | .modifiers |
| 152 | .intersects(KeyModifiers::ALT | KeyModifiers::SUPER); |
| 153 | cmd_a || ctrl_shift_a |
| 154 | } |
| 155 | |
| 156 | /// Run `/update install` without leaving the TUI: `Ctrl+Shift+U`. |
| 157 | /// |
| 158 | /// Distinct from readline `Ctrl+U` (clear the composer line) exactly the |
| 159 | /// way `Ctrl+Shift+A` / `Ctrl+Shift+E` / `Ctrl+Shift+O` are distinct from |
| 160 | /// their unshifted forms, and like them requires an enhanced-keyboard |
| 161 | /// terminal to report the Shift modifier. On macOS the event loop's |
| 162 | /// modifier normalization maps `Cmd+Shift+U` onto this chord; the predicate |
| 163 | /// itself still rejects a raw SUPER modifier so Linux/Windows meta chords |
| 164 | /// never collide with window-management shortcuts. |
| 165 | pub(super) fn is_update_install_shortcut(key: &KeyEvent) -> bool { |
| 166 | let is_u = matches!(key.code, KeyCode::Char('u') | KeyCode::Char('U')); |
| 167 | is_u && key.modifiers.contains(KeyModifiers::CONTROL) |
| 168 | && key.modifiers.contains(KeyModifiers::SHIFT) |
| 169 | && !key |
| 170 | .modifiers |
| 171 | .intersects(KeyModifiers::ALT | KeyModifiers::SUPER) |
| 172 | } |
| 173 | |
| 174 | /// Modifier predicate for the v0.8.30 family of `Alt+<key>` transcript- |
| 175 | /// nav shortcuts (`Alt+G` / `Alt+[` / `Alt+]` / `Alt+?` / `Alt+L`). Requires |
| 176 | /// `Alt` and disallows `Ctrl` / `Super` so the |
| 177 | /// bindings don't collide with platform clipboard / window-management |
| 178 | /// shortcuts. `Shift` is permitted so the capital-letter forms work on |
| 179 | /// any keyboard layout that produces them as `Alt+Shift+key`. |
| 180 | /// |
| 181 | /// Plain `Char` events (no modifier, or modifier=`Shift` alone for the |
| 182 | /// uppercase form) fall through to text insertion, which is the whole |
| 183 | /// point — typing "good morning" no longer eats the first `g`. |
| 184 | pub(super) fn alt_nav_modifiers(modifiers: KeyModifiers) -> bool { |
| 185 | modifiers.contains(KeyModifiers::ALT) |
| 186 | && !modifiers.contains(KeyModifiers::CONTROL) |
| 187 | && !modifiers.contains(KeyModifiers::SUPER) |
| 188 | } |
| 189 | |
| 190 | pub(super) fn is_macos_option_v_legacy_key(key: &KeyEvent) -> bool { |
| 191 | is_macos_option_v_legacy_key_for_platform(key, cfg!(target_os = "macos")) |
| 192 | } |
| 193 | |
| 194 | pub(super) fn is_macos_option_v_legacy_key_for_platform(key: &KeyEvent, is_macos: bool) -> bool { |
| 195 | is_macos && key.modifiers.is_empty() && matches!(key.code, KeyCode::Char('\u{221A}')) |
| 196 | } |
| 197 | |
| 198 | /// Paste-from-clipboard: accept `Cmd+V`, `Ctrl+V`, or the legacy raw `\u{16}` |
| 199 | /// byte some terminals emit. A remote terminal normally consumes its local |
| 200 | /// paste chord and sends an `Event::Paste`; accepting both modifier families |
| 201 | /// still keeps enhanced-keyboard clients independent of the remote host OS. |
| 202 | pub(super) fn is_paste_shortcut(key: &KeyEvent) -> bool { |
| 203 | let is_v = matches!(key.code, KeyCode::Char('v') | KeyCode::Char('V')); |
| 204 | let is_legacy_ctrl_v = matches!(key.code, KeyCode::Char('\u{16}')); |
| 205 | if !is_v && !is_legacy_ctrl_v { |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | if is_legacy_ctrl_v { |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | // Cmd+V on macOS |
| 214 | if key.modifiers.contains(KeyModifiers::SUPER) { |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | // Ctrl+V on Linux/Windows |
| 219 | key.modifiers.contains(KeyModifiers::CONTROL) |
| 220 | } |
| 221 | |
| 222 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 223 | pub(super) enum PasteEdge { |
| 224 | Route, |
| 225 | Ignore, |
| 226 | ImageOnly, |
| 227 | } |
| 228 | |
| 229 | pub(super) fn paste_edge(key: &KeyEvent, press_seen: &mut bool) -> PasteEdge { |
| 230 | match key.kind { |
| 231 | KeyEventKind::Press => { |
| 232 | *press_seen = is_paste_shortcut(key); |
| 233 | PasteEdge::Route |
| 234 | } |
| 235 | KeyEventKind::Release |
| 236 | if matches!( |
| 237 | key.code, |
| 238 | KeyCode::Char('v') | KeyCode::Char('V') | KeyCode::Char('\u{16}') |
| 239 | ) => |
| 240 | { |
| 241 | let seen = std::mem::take(press_seen); |
| 242 | if is_paste_shortcut(key) && !seen { |
| 243 | PasteEdge::ImageOnly |
| 244 | } else { |
| 245 | PasteEdge::Ignore |
| 246 | } |
| 247 | } |
| 248 | _ => PasteEdge::Ignore, |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /// `Ctrl+H` is the legacy ASCII backspace many terminals still emit |
| 253 | /// when the user presses Backspace. Disallows Alt/Super so it doesn't |
| 254 | /// shadow window-management combos. |
| 255 | pub(super) fn is_ctrl_h_backspace(key: &KeyEvent) -> bool { |
| 256 | matches!(key.code, KeyCode::Char('h')) |
| 257 | && key.modifiers.contains(KeyModifiers::CONTROL) |
| 258 | && !key.modifiers.contains(KeyModifiers::ALT) |
| 259 | && !key.modifiers.contains(KeyModifiers::SUPER) |
| 260 | } |
| 261 | |
| 262 | #[cfg(test)] |
| 263 | mod tests { |
| 264 | use super::*; |
| 265 | |
| 266 | #[test] |
| 267 | fn enhanced_keyboard_clipboard_events_are_accepted_cross_platform() { |
| 268 | let mac_copy = KeyEvent::new(KeyCode::Char('c'), KeyModifiers::SUPER); |
| 269 | let mac_paste = KeyEvent::new(KeyCode::Char('v'), KeyModifiers::SUPER); |
| 270 | let linux_copy = KeyEvent::new( |
| 271 | KeyCode::Char('c'), |
| 272 | KeyModifiers::CONTROL | KeyModifiers::SHIFT, |
| 273 | ); |
| 274 | let linux_paste = KeyEvent::new(KeyCode::Char('v'), KeyModifiers::CONTROL); |
| 275 | |
| 276 | assert!(is_copy_shortcut(&mac_copy)); |
| 277 | assert!(is_paste_shortcut(&mac_paste)); |
| 278 | assert!(is_copy_shortcut(&linux_copy)); |
| 279 | assert!(is_paste_shortcut(&linux_paste)); |
| 280 | assert!(!is_copy_shortcut(&KeyEvent::new( |
| 281 | KeyCode::Char('c'), |
| 282 | KeyModifiers::CONTROL | KeyModifiers::SHIFT | KeyModifiers::ALT, |
| 283 | ))); |
| 284 | assert!(!is_copy_shortcut(&KeyEvent::new( |
| 285 | KeyCode::Char('c'), |
| 286 | KeyModifiers::SUPER | KeyModifiers::ALT, |
| 287 | ))); |
| 288 | } |
| 289 | |
| 290 | #[test] |
| 291 | fn ctrl_o_and_ctrl_shift_o_have_stable_distinct_routes() { |
| 292 | let reasoning = KeyEvent::new(KeyCode::Char('o'), KeyModifiers::CONTROL); |
| 293 | // Crossterm's native Windows decoder applies Caps Lock to the |
| 294 | // character but does not expose Caps Lock as a modifier. |
| 295 | let reasoning_caps_lock = KeyEvent::new(KeyCode::Char('O'), KeyModifiers::CONTROL); |
| 296 | let editor_lower = KeyEvent::new( |
| 297 | KeyCode::Char('o'), |
| 298 | KeyModifiers::CONTROL | KeyModifiers::SHIFT, |
| 299 | ); |
| 300 | let editor_upper = KeyEvent::new( |
| 301 | KeyCode::Char('O'), |
| 302 | KeyModifiers::CONTROL | KeyModifiers::SHIFT, |
| 303 | ); |
| 304 | |
| 305 | for reasoning in [&reasoning, &reasoning_caps_lock] { |
| 306 | assert!(is_reasoning_detail_shortcut(reasoning)); |
| 307 | assert!(!is_turn_inspector_shortcut(reasoning)); |
| 308 | assert!(!is_external_editor_shortcut(reasoning)); |
| 309 | } |
| 310 | for editor in [&editor_lower, &editor_upper] { |
| 311 | assert!(!is_reasoning_detail_shortcut(editor)); |
| 312 | assert!(!is_turn_inspector_shortcut(editor)); |
| 313 | assert!(is_external_editor_shortcut(editor)); |
| 314 | } |
| 315 | |
| 316 | let editor_legacy_fallback = KeyEvent::new(KeyCode::F(4), KeyModifiers::NONE); |
| 317 | assert!(is_external_editor_shortcut(&editor_legacy_fallback)); |
| 318 | } |
| 319 | |
| 320 | #[test] |
| 321 | fn turn_inspector_uses_collision_free_ctrl_alt_o() { |
| 322 | let turn_inspector = KeyEvent::new( |
| 323 | KeyCode::Char('o'), |
| 324 | KeyModifiers::CONTROL | KeyModifiers::ALT, |
| 325 | ); |
| 326 | let turn_inspector_caps = KeyEvent::new( |
| 327 | KeyCode::Char('O'), |
| 328 | KeyModifiers::CONTROL | KeyModifiers::ALT, |
| 329 | ); |
| 330 | for key in [&turn_inspector, &turn_inspector_caps] { |
| 331 | assert!(is_turn_inspector_shortcut(key)); |
| 332 | assert!(!is_reasoning_detail_shortcut(key)); |
| 333 | assert!(!is_external_editor_shortcut(key)); |
| 334 | } |
| 335 | |
| 336 | // Must not fire for bare Alt+O (would shadow typing) or Ctrl+Shift+O. |
| 337 | let alt_o = KeyEvent::new(KeyCode::Char('o'), KeyModifiers::ALT); |
| 338 | let ctrl_shift_o = KeyEvent::new( |
| 339 | KeyCode::Char('o'), |
| 340 | KeyModifiers::CONTROL | KeyModifiers::SHIFT, |
| 341 | ); |
| 342 | assert!(!is_turn_inspector_shortcut(&alt_o)); |
| 343 | assert!(!is_turn_inspector_shortcut(&ctrl_shift_o)); |
| 344 | } |
| 345 | |
| 346 | #[test] |
| 347 | fn ctrl_shift_u_routes_to_update_install_not_readline_clear() { |
| 348 | let ctrl_shift_lower = KeyEvent::new( |
| 349 | KeyCode::Char('u'), |
| 350 | KeyModifiers::CONTROL | KeyModifiers::SHIFT, |
| 351 | ); |
| 352 | let ctrl_shift_upper = KeyEvent::new( |
| 353 | KeyCode::Char('U'), |
| 354 | KeyModifiers::CONTROL | KeyModifiers::SHIFT, |
| 355 | ); |
| 356 | assert!(is_update_install_shortcut(&ctrl_shift_lower)); |
| 357 | assert!(is_update_install_shortcut(&ctrl_shift_upper)); |
| 358 | |
| 359 | // Readline Ctrl+U stays readline Ctrl+U (clear the composer line). |
| 360 | let readline_ctrl_u = KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL); |
| 361 | assert!(!is_update_install_shortcut(&readline_ctrl_u)); |
| 362 | // Bare Shift+U, plain `u`, and meta combos never install an update. |
| 363 | let shift_u = KeyEvent::new(KeyCode::Char('U'), KeyModifiers::SHIFT); |
| 364 | let plain_u = KeyEvent::new(KeyCode::Char('u'), KeyModifiers::NONE); |
| 365 | let ctrl_alt_u = KeyEvent::new( |
| 366 | KeyCode::Char('u'), |
| 367 | KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SHIFT, |
| 368 | ); |
| 369 | let super_shift_u = KeyEvent::new( |
| 370 | KeyCode::Char('u'), |
| 371 | KeyModifiers::SUPER | KeyModifiers::SHIFT, |
| 372 | ); |
| 373 | assert!(!is_update_install_shortcut(&shift_u)); |
| 374 | assert!(!is_update_install_shortcut(&plain_u)); |
| 375 | assert!(!is_update_install_shortcut(&ctrl_alt_u)); |
| 376 | assert!(!is_update_install_shortcut(&super_shift_u)); |
| 377 | } |
| 378 | |
| 379 | #[test] |
| 380 | fn select_all_accepts_ctrl_shift_a_and_cmd_a_but_not_readline_ctrl_a() { |
| 381 | let ctrl_shift_lower = KeyEvent::new( |
| 382 | KeyCode::Char('a'), |
| 383 | KeyModifiers::CONTROL | KeyModifiers::SHIFT, |
| 384 | ); |
| 385 | let ctrl_shift_upper = KeyEvent::new( |
| 386 | KeyCode::Char('A'), |
| 387 | KeyModifiers::CONTROL | KeyModifiers::SHIFT, |
| 388 | ); |
| 389 | let cmd_a = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::SUPER); |
| 390 | assert!(is_select_all_shortcut(&ctrl_shift_lower)); |
| 391 | assert!(is_select_all_shortcut(&ctrl_shift_upper)); |
| 392 | assert!(is_select_all_shortcut(&cmd_a)); |
| 393 | |
| 394 | // Readline home stays readline home. |
| 395 | let readline_ctrl_a = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL); |
| 396 | assert!(!is_select_all_shortcut(&readline_ctrl_a)); |
| 397 | // Alt combinations and plain typing never select-all. |
| 398 | let alt_a = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::ALT); |
| 399 | let plain_a = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE); |
| 400 | assert!(!is_select_all_shortcut(&alt_a)); |
| 401 | assert!(!is_select_all_shortcut(&plain_a)); |
| 402 | } |
| 403 | |
| 404 | #[test] |
| 405 | fn ctrl_backslash_matches_agent_board_shortcut_only() { |
| 406 | assert!(is_agent_board_shortcut(&KeyEvent::new( |
| 407 | KeyCode::Char('\\'), |
| 408 | KeyModifiers::CONTROL, |
| 409 | ))); |
| 410 | assert!(is_agent_board_shortcut(&KeyEvent::new( |
| 411 | KeyCode::Char('\u{1c}'), |
| 412 | KeyModifiers::NONE, |
| 413 | ))); |
| 414 | assert!(!is_agent_board_shortcut(&KeyEvent::new( |
| 415 | KeyCode::Char('\\'), |
| 416 | KeyModifiers::NONE, |
| 417 | ))); |
| 418 | assert!(!is_agent_board_shortcut(&KeyEvent::new( |
| 419 | KeyCode::Char('\\'), |
| 420 | KeyModifiers::ALT, |
| 421 | ))); |
| 422 | assert!(!is_agent_board_shortcut(&KeyEvent::new( |
| 423 | KeyCode::Char('\\'), |
| 424 | KeyModifiers::CONTROL | KeyModifiers::ALT, |
| 425 | ))); |
| 426 | assert!(!is_agent_board_shortcut(&KeyEvent::new( |
| 427 | KeyCode::Char('\\'), |
| 428 | KeyModifiers::CONTROL | KeyModifiers::SHIFT, |
| 429 | ))); |
| 430 | } |
| 431 | |
| 432 | #[test] |
| 433 | fn tool_details_hint_uses_the_routed_chord_not_plain_typing() { |
| 434 | let label = tool_details_shortcut_label(); |
| 435 | |
| 436 | assert_eq!(label, crate::tui::shell_key_routing::tool_details_chord()); |
| 437 | assert_ne!(label, "v"); |
| 438 | assert_eq!( |
| 439 | tool_details_shortcut_action_hint("output"), |
| 440 | format!("{label} output") |
| 441 | ); |
| 442 | } |
| 443 | |
| 444 | /// #3256: every surface that advertises tool details must name the chord |
| 445 | /// that `is_tool_details_shortcut` actually handles — the help catalog, |
| 446 | /// shell binding catalog, and in-transcript hint share one source of |
| 447 | /// truth so bare-`v` "details" copy cannot regress while bare `v` types `v`. |
| 448 | #[test] |
| 449 | fn tool_details_hint_tracks_keybinding_catalog_and_handler() { |
| 450 | use crate::tui::shell_key_routing::{ |
| 451 | ShellBindingId, binding, is_tool_details_shortcut, tool_details_chord, |
| 452 | }; |
| 453 | use codewhale_engine::keybinding_table::KEYBINDINGS; |
| 454 | use codewhale_localization::MessageId; |
| 455 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; |
| 456 | |
| 457 | let catalog_chords: Vec<&str> = KEYBINDINGS |
| 458 | .iter() |
| 459 | .filter(|entry| entry.description_id == MessageId::KbSelectedDetails) |
| 460 | .map(|entry| entry.chord) |
| 461 | .collect(); |
| 462 | assert_eq!(catalog_chords, vec!["Alt+V"]); |
| 463 | assert_eq!(binding(ShellBindingId::ToolDetails).catalog_chord, "Alt+V"); |
| 464 | assert_eq!(binding(ShellBindingId::ToolDetails).footer_chord, "Alt+V"); |
| 465 | |
| 466 | let label = tool_details_shortcut_label(); |
| 467 | assert_eq!(label, tool_details_chord()); |
| 468 | assert!( |
| 469 | label == "Alt+V" || label == "⌥V", |
| 470 | "details hint must advertise Alt+V / ⌥V, got {label}" |
| 471 | ); |
| 472 | assert!(!label.eq_ignore_ascii_case("v")); |
| 473 | let details_hint = tool_details_shortcut_action_hint("details"); |
| 474 | assert_eq!(details_hint, format!("{label} details")); |
| 475 | assert!(!details_hint.starts_with('v')); |
| 476 | |
| 477 | let plain_v = KeyEvent::new(KeyCode::Char('v'), KeyModifiers::NONE); |
| 478 | let alt_v = KeyEvent::new(KeyCode::Char('v'), KeyModifiers::ALT); |
| 479 | assert!(!is_tool_details_shortcut(&plain_v)); |
| 480 | assert!(is_tool_details_shortcut(&alt_v)); |
| 481 | } |
| 482 | |
| 483 | fn key_edge(code: char, modifiers: KeyModifiers, kind: KeyEventKind) -> KeyEvent { |
| 484 | KeyEvent::new_with_kind(KeyCode::Char(code), modifiers, kind) |
| 485 | } |
| 486 | |
| 487 | #[test] |
| 488 | fn paste_edges_paste_once_per_chord() { |
| 489 | let ctrl = KeyModifiers::CONTROL; |
| 490 | let press = key_edge('v', ctrl, KeyEventKind::Press); |
| 491 | let release = key_edge('v', ctrl, KeyEventKind::Release); |
| 492 | |
| 493 | let mut seen = false; |
| 494 | assert_eq!(paste_edge(&release, &mut seen), PasteEdge::ImageOnly); |
| 495 | assert_eq!(paste_edge(&release, &mut seen), PasteEdge::ImageOnly); |
| 496 | |
| 497 | assert_eq!(paste_edge(&press, &mut seen), PasteEdge::Route); |
| 498 | assert_eq!(paste_edge(&release, &mut seen), PasteEdge::Ignore); |
| 499 | assert_eq!(paste_edge(&release, &mut seen), PasteEdge::ImageOnly); |
| 500 | |
| 501 | assert_eq!(paste_edge(&press, &mut seen), PasteEdge::Route); |
| 502 | let repeat = key_edge('v', ctrl, KeyEventKind::Repeat); |
| 503 | assert_eq!(paste_edge(&repeat, &mut seen), PasteEdge::Ignore); |
| 504 | assert_eq!(paste_edge(&release, &mut seen), PasteEdge::Ignore); |
| 505 | } |
| 506 | |
| 507 | #[test] |
| 508 | fn a_paste_press_without_its_release_does_not_swallow_the_next_release_only_paste() { |
| 509 | let ctrl = KeyModifiers::CONTROL; |
| 510 | let press = key_edge('v', ctrl, KeyEventKind::Press); |
| 511 | let release = key_edge('v', ctrl, KeyEventKind::Release); |
| 512 | |
| 513 | let mut seen = false; |
| 514 | assert_eq!(paste_edge(&press, &mut seen), PasteEdge::Route); |
| 515 | let bare_v_release = key_edge('v', KeyModifiers::NONE, KeyEventKind::Release); |
| 516 | assert_eq!(paste_edge(&bare_v_release, &mut seen), PasteEdge::Ignore); |
| 517 | assert_eq!(paste_edge(&release, &mut seen), PasteEdge::ImageOnly); |
| 518 | |
| 519 | assert_eq!(paste_edge(&press, &mut seen), PasteEdge::Route); |
| 520 | let other = key_edge('a', KeyModifiers::NONE, KeyEventKind::Press); |
| 521 | assert_eq!(paste_edge(&other, &mut seen), PasteEdge::Route); |
| 522 | assert_eq!(paste_edge(&release, &mut seen), PasteEdge::ImageOnly); |
| 523 | |
| 524 | let other_release = key_edge('a', KeyModifiers::NONE, KeyEventKind::Release); |
| 525 | assert_eq!(paste_edge(&other_release, &mut seen), PasteEdge::Ignore); |
| 526 | } |
| 527 | } |
| 528 |