Support two-stroke TUI key chords (#36511)

## What changed

- Accept two-stroke bindings such as `ctrl-x ctrl-s` in TUI keymap configuration while preserving arrays as alternative bindings.
- Route chords through the active TUI context, show pending and configured chord hints, and cancel pending chords on `esc`, context changes, or a one-second timeout.
- Extend `/keymap` to capture, add, replace, and display key chords.
- Reject chords that conflict with active single-key bindings, overlapping-context chords, or reserved terminal shortcuts.

## Testing

- Add coverage for chord parsing, validation, matching, timeout and cancellation behavior, context dispatch, key capture, and rendered shortcut hints.

GitOrigin-RevId: dfdede03f518d4936ad8621ebf5537026f2b282e
This commit is contained in:
Felipe Coury authored and copyberry committed 2026-08-01 17:43:03 +00:00
1 parent a1dd74b535
commit 1e85ca099e
61 files changed
+3503 -660

No files matched your search

@@ -0,0 +1,76 @@
use super::KeybindingSpec;
use super::KeybindingsSpec;
use super::TuiKeymap;
use super::normalize_keybinding_spec;
use pretty_assertions::assert_eq;
#[test]
fn normalizes_each_stroke_in_a_key_chord() {
assert_eq!(
normalize_keybinding_spec(" CONTROL-X Option-Return "),
Ok("ctrl-x alt-enter".to_string())
);
}
#[test]
fn deserializes_a_two_stroke_global_key_chord() {
let actual = toml::from_str::<TuiKeymap>(
r#"
[global]
open_transcript = "ctrl-x ctrl-t"
"#,
)
.expect("two-stroke key chords should deserialize");
let expected = TuiKeymap {
global: super::TuiGlobalKeymap {
open_transcript: Some(KeybindingsSpec::One(KeybindingSpec(
"ctrl-x ctrl-t".to_string(),
))),
..Default::default()
},
..Default::default()
};
assert_eq!(actual, expected);
}
#[test]
fn keeps_single_keys_and_chords_as_alternative_bindings() {
let actual = toml::from_str::<TuiKeymap>(
r#"
[global]
open_transcript = ["ctrl-t", "ctrl-x ctrl-t"]
"#,
)
.expect("single keys and key chords should coexist");
let expected = TuiKeymap {
global: super::TuiGlobalKeymap {
open_transcript: Some(KeybindingsSpec::Many(vec![
KeybindingSpec("ctrl-t".to_string()),
KeybindingSpec("ctrl-x ctrl-t".to_string()),
])),
..Default::default()
},
..Default::default()
};
assert_eq!(actual, expected);
}
#[test]
fn rejects_chords_longer_than_two_strokes() {
let error = normalize_keybinding_spec("ctrl-x ctrl-s ctrl-t")
.expect_err("key chords should have a bounded length");
assert!(error.contains("at most 2 strokes"), "{error}");
}
#[test]
fn rejects_an_invalid_second_chord_stroke() {
let error = normalize_keybinding_spec("ctrl-x ctrl-unknown")
.expect_err("every key chord stroke should be valid");
assert!(error.contains("unknown key"), "{error}");
}