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

+10 -2
View File
@@ -2349,9 +2349,15 @@ impl App {
context,
action,
intent,
capture_mode,
} => {
self.chat_widget
.open_keymap_capture(context, action, intent, &self.keymap);
self.chat_widget.open_keymap_capture(
context,
action,
intent,
capture_mode,
&self.keymap,
);
}
AppEvent::OpenKeymapDebug => {
self.chat_widget.open_keymap_debug(&self.keymap);
@@ -2424,6 +2430,7 @@ impl App {
.await
{
Ok(()) => {
self.cancel_pending_key_chord();
self.config.tui_keymap = keymap_config.clone();
self.keymap = runtime_keymap.clone();
self.chat_widget
@@ -2475,6 +2482,7 @@ impl App {
.await
{
Ok(()) => {
self.cancel_pending_key_chord();
self.config.tui_keymap = keymap_config.clone();
self.keymap = runtime_keymap.clone();
self.chat_widget
+77
View File
@@ -7,6 +7,83 @@ use super::*;
use crate::app_backtrack::SIDE_EDIT_PREVIOUS_UNAVAILABLE_MESSAGE;
impl App {
pub(super) fn route_key_chord_event(
&mut self,
tui: &mut tui::Tui,
key_event: KeyEvent,
) -> Option<KeyEvent> {
let contexts = self.active_keymap_contexts();
let was_pending = self.key_chord_matcher.is_pending();
match self.key_chord_matcher.advance(
key_event,
&self.keymap.chords,
contexts,
tokio::time::Instant::now(),
) {
crate::keymap::KeyChordMatch::PassThrough => {
if was_pending && !self.key_chord_matcher.is_pending() {
self.chat_widget.set_footer_hint_override(/*items*/ None);
}
Some(key_event)
}
crate::keymap::KeyChordMatch::Pending(prefix) => {
if self.backtrack.primed {
self.reset_backtrack_state();
}
self.chat_widget.set_footer_hint_override(Some(vec![
(
format!("{} …", prefix.display_label()),
"waiting for next key".to_string(),
),
("esc".to_string(), "cancel".to_string()),
]));
tui.frame_requester()
.schedule_frame_in(crate::keymap::KEY_CHORD_TIMEOUT);
None
}
crate::keymap::KeyChordMatch::Completed(dispatch_event) => {
self.chat_widget.set_footer_hint_override(/*items*/ None);
Some(dispatch_event)
}
crate::keymap::KeyChordMatch::Cancelled => {
self.chat_widget.set_footer_hint_override(/*items*/ None);
None
}
crate::keymap::KeyChordMatch::Ignored => None,
}
}
pub(super) fn expire_pending_key_chord(&mut self) {
let contexts = self.active_keymap_contexts();
if self
.key_chord_matcher
.expire(contexts, tokio::time::Instant::now())
{
self.chat_widget.set_footer_hint_override(/*items*/ None);
}
}
pub(super) fn cancel_pending_key_chord(&mut self) {
if self.key_chord_matcher.cancel() {
self.chat_widget.set_footer_hint_override(/*items*/ None);
}
}
fn active_keymap_contexts(&self) -> crate::keymap::KeymapContextSet {
if self.overlay.is_some() {
return crate::keymap::KeymapContextSet::new(crate::keymap::KeymapContext::Pager);
}
let contexts = self.chat_widget.keymap_contexts();
if self.chat_widget.no_modal_or_popup_active() {
contexts
.with(crate::keymap::KeymapContext::Global)
.with(crate::keymap::KeymapContext::Chat)
} else {
contexts
}
}
pub(super) async fn launch_external_editor(&mut self, tui: &mut tui::Tui) {
let editor_cmd = match external_editor::resolve_editor_command() {
Ok(cmd) => cmd,
+8 -5
View File
@@ -244,8 +244,10 @@ impl App {
.side_threads
.values()
.any(|state| state.parent_thread_id == active_thread_id)
&& let Some(binding) =
crate::keymap::primary_binding(&self.keymap.app.toggle_side_conversation)
&& let Some(binding) = self.keymap.primary_hint(
crate::keymap::KeymapContext::Global,
"toggle_side_conversation",
)
{
self.chat_widget
.set_side_conversation_context_label(Some(format!(
@@ -273,9 +275,10 @@ impl App {
if let Some(parent_status) = parent_status {
label_parts.push(parent_status.label(parent_is_main).to_string());
}
if let Some(binding) =
crate::keymap::primary_binding(&self.keymap.app.toggle_side_conversation)
{
if let Some(binding) = self.keymap.primary_hint(
crate::keymap::KeymapContext::Global,
"toggle_side_conversation",
) {
label_parts.push(format!("{} to switch", binding.display_label()));
}
label_parts.push("ctrl + c to close".to_string());
+1
View File
@@ -40,6 +40,7 @@ pub(super) async fn make_test_app() -> App {
initial_history_replay_buffer: None,
enhanced_keys_supported: false,
keymap: crate::keymap::RuntimeKeymap::defaults(),
key_chord_matcher: crate::keymap::KeyChordMatcher::default(),
commit_anim_running: Arc::new(AtomicBool::new(false)),
status_line_invalid_items_warned: Arc::new(AtomicBool::new(false)),
terminal_title_invalid_items_warned: Arc::new(AtomicBool::new(false)),
+4
View File
@@ -2,6 +2,8 @@
#[path = "tests/advanced_reasoning_tests.rs"]
mod advanced_reasoning_tests;
#[path = "tests/key_chords.rs"]
mod key_chords;
mod model_catalog;
mod plugin_catalog;
mod rate_limits;
@@ -4740,6 +4742,7 @@ async fn make_test_app() -> App {
initial_history_replay_buffer: None,
enhanced_keys_supported: false,
keymap: crate::keymap::RuntimeKeymap::defaults(),
key_chord_matcher: crate::keymap::KeyChordMatcher::default(),
commit_anim_running: Arc::new(AtomicBool::new(false)),
status_line_invalid_items_warned: Arc::new(AtomicBool::new(false)),
terminal_title_invalid_items_warned: Arc::new(AtomicBool::new(false)),
@@ -4808,6 +4811,7 @@ async fn make_test_app_with_channels() -> (
initial_history_replay_buffer: None,
enhanced_keys_supported: false,
keymap: crate::keymap::RuntimeKeymap::defaults(),
key_chord_matcher: crate::keymap::KeyChordMatcher::default(),
commit_anim_running: Arc::new(AtomicBool::new(false)),
status_line_invalid_items_warned: Arc::new(AtomicBool::new(false)),
terminal_title_invalid_items_warned: Arc::new(AtomicBool::new(false)),
+217
View File
@@ -0,0 +1,217 @@
use super::App;
use super::AppServerSession;
use super::Result;
use super::RuntimeKeymap;
use super::TuiEvent;
use super::make_test_app;
use super::start_config_write_test_app_server;
use crate::bottom_pane::SelectionItem;
use crate::bottom_pane::SelectionViewParams;
use crate::chatwidget::tests::helpers::render_bottom_popup;
use crate::keymap::KeymapContext;
use crate::test_support::test_path_display;
use crate::tui::Tui;
use codex_app_server_protocol::ToolRequestUserInputOption;
use codex_app_server_protocol::ToolRequestUserInputParams;
use codex_app_server_protocol::ToolRequestUserInputQuestion;
use codex_config::types::KeybindingSpec;
use codex_config::types::KeybindingsSpec;
use codex_config::types::TuiKeymap;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers;
use pretty_assertions::assert_eq;
async fn chord_app() -> Result<(App, Tui, AppServerSession)> {
let mut app = make_test_app().await;
let mut config = TuiKeymap::default();
config.global.open_transcript = Some(KeybindingsSpec::One(KeybindingSpec(
"ctrl-x ctrl-t".to_string(),
)));
config.chat.interrupt_turn = Some(KeybindingsSpec::One(KeybindingSpec(
"ctrl-x ctrl-u".to_string(),
)));
config.list.move_down = Some(KeybindingsSpec::One(KeybindingSpec("ctrl-x j".to_string())));
config.list.accept = Some(KeybindingsSpec::One(KeybindingSpec(
"ctrl-x enter".to_string(),
)));
let runtime =
RuntimeKeymap::from_config(&config).map_err(|error| color_eyre::eyre::eyre!(error))?;
app.chat_widget.apply_keymap_update(config, &runtime);
app.keymap = runtime;
let app_server = start_config_write_test_app_server(&app).await?;
let tui = crate::tui::test_support::make_test_tui()?;
Ok((app, tui, app_server))
}
async fn press(
app: &mut App,
tui: &mut Tui,
app_server: &mut AppServerSession,
key: KeyEvent,
) -> Result<()> {
app.handle_tui_event(tui, app_server, TuiEvent::Key(key))
.await?;
Ok(())
}
fn ctrl(ch: char) -> KeyEvent {
KeyEvent::new(KeyCode::Char(ch), KeyModifiers::CONTROL)
}
#[tokio::test]
async fn completed_global_chord_reuses_the_existing_action_handler() -> Result<()> {
let (mut app, mut tui, mut app_server) = chord_app().await?;
press(&mut app, &mut tui, &mut app_server, ctrl('x')).await?;
assert!(app.key_chord_matcher.is_pending());
assert!(app.overlay.is_none());
insta::assert_snapshot!(
render_bottom_popup(&app.chat_widget, /*width*/ 80)
.replace(&test_path_display("/tmp/project"), "/tmp/project"),
@r"
› Ask Codex to do anything
ctrl + x … waiting for next key esc cancel
"
);
press(&mut app, &mut tui, &mut app_server, ctrl('t')).await?;
assert!(!app.key_chord_matcher.is_pending());
assert!(app.overlay.is_some());
Ok(())
}
#[tokio::test]
async fn wrong_second_stroke_passes_through_but_escape_is_consumed() -> Result<()> {
let (mut app, mut tui, mut app_server) = chord_app().await?;
press(&mut app, &mut tui, &mut app_server, ctrl('x')).await?;
let wrong_second = KeyEvent::new(KeyCode::Char('q'), KeyModifiers::NONE);
assert_eq!(
app.route_key_chord_event(&mut tui, wrong_second),
Some(wrong_second)
);
press(&mut app, &mut tui, &mut app_server, ctrl('x')).await?;
let escape = KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE);
assert_eq!(app.route_key_chord_event(&mut tui, escape), None);
assert!(!app.key_chord_matcher.is_pending());
assert!(!app.backtrack.primed);
Ok(())
}
#[tokio::test]
async fn physical_dispatch_band_events_are_dropped() -> Result<()> {
let (mut app, mut tui, mut app_server) = chord_app().await?;
let (code, modifiers) = app
.keymap
.app
.open_transcript
.last()
.expect("configured chord appends a dispatch token")
.parts();
press(
&mut app,
&mut tui,
&mut app_server,
KeyEvent::new(code, modifiers),
)
.await?;
assert!(app.overlay.is_none());
Ok(())
}
#[tokio::test]
async fn physical_chords_route_list_and_mixed_request_input_modals() -> Result<()> {
let (mut app, mut tui, mut app_server) = chord_app().await?;
app.chat_widget.show_selection_view(SelectionViewParams {
view_id: Some("list"),
items: ["First", "Second"]
.into_iter()
.map(|name| SelectionItem {
name: name.to_string(),
dismiss_on_select: true,
..Default::default()
})
.collect(),
..Default::default()
});
assert_eq!(
app.chat_widget.selected_index_for_present_view("list"),
Some(0)
);
press(&mut app, &mut tui, &mut app_server, ctrl('x')).await?;
assert!(app.key_chord_matcher.is_pending());
assert_eq!(
app.chat_widget.selected_index_for_present_view("list"),
Some(0)
);
press(
&mut app,
&mut tui,
&mut app_server,
KeyCode::Char('j').into(),
)
.await?;
assert_eq!(
app.chat_widget.selected_index_for_present_view("list"),
Some(1)
);
press(&mut app, &mut tui, &mut app_server, ctrl('x')).await?;
press(&mut app, &mut tui, &mut app_server, KeyCode::Enter.into()).await?;
assert_eq!(
app.chat_widget.selected_index_for_present_view("list"),
None
);
app.chat_widget
.handle_request_user_input_now(ToolRequestUserInputParams {
thread_id: "thread-1".to_string(),
turn_id: "turn-1".to_string(),
item_id: "call-1".to_string(),
questions: vec![ToolRequestUserInputQuestion {
id: "choice".to_string(),
header: "Pick one".to_string(),
question: "Choose an option.".to_string(),
is_other: false,
is_secret: false,
options: Some(
["First", "Second"]
.into_iter()
.map(|label| ToolRequestUserInputOption {
label: label.to_string(),
description: label.to_string(),
})
.collect(),
),
}],
is_blocking: true,
auto_resolution_ms: None,
});
let contexts = app.chat_widget.keymap_contexts();
assert!(contexts.contains(KeymapContext::Chat));
assert!(contexts.contains(KeymapContext::List));
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 80).contains("› 1. First"));
press(&mut app, &mut tui, &mut app_server, ctrl('x')).await?;
assert!(app.key_chord_matcher.is_pending());
press(
&mut app,
&mut tui,
&mut app_server,
KeyCode::Char('j').into(),
)
.await?;
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 80).contains("› 2. Second"));
press(&mut app, &mut tui, &mut app_server, ctrl('x')).await?;
press(&mut app, &mut tui, &mut app_server, ctrl('u')).await?;
assert!(app.chat_widget.can_launch_external_editor());
Ok(())
}