mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
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:
1 parent
a1dd74b535
commit
1e85ca099e
61 files changed
+3503
-660
No files matched your search
@@ -24,6 +24,7 @@ use crate::history_cell::UserHistoryCell;
|
||||
use crate::key_hint;
|
||||
use crate::key_hint::KeyBinding;
|
||||
use crate::key_hint::KeyBindingListExt;
|
||||
use crate::key_hint::ShortcutHint;
|
||||
use crate::keymap::PagerKeymap;
|
||||
use crate::render::Insets;
|
||||
use crate::render::renderable::InsetRenderable;
|
||||
@@ -90,12 +91,16 @@ impl Overlay {
|
||||
}
|
||||
}
|
||||
|
||||
fn first_or_empty(bindings: &[KeyBinding]) -> Vec<KeyBinding> {
|
||||
bindings.first().copied().into_iter().collect()
|
||||
fn first_or_empty(
|
||||
keymap: &PagerKeymap,
|
||||
action: &'static str,
|
||||
bindings: &[KeyBinding],
|
||||
) -> Vec<ShortcutHint> {
|
||||
keymap.primary_hint(action, bindings).into_iter().collect()
|
||||
}
|
||||
|
||||
// Render a single line of key hints from (key(s), description) pairs.
|
||||
fn render_key_hints(area: Rect, buf: &mut Buffer, pairs: &[(Vec<KeyBinding>, &str)]) {
|
||||
fn render_key_hints(area: Rect, buf: &mut Buffer, pairs: &[(Vec<ShortcutHint>, &str)]) {
|
||||
let mut spans: Vec<Span<'static>> = vec![" ".into()];
|
||||
let mut first = true;
|
||||
for (keys, desc) in pairs {
|
||||
@@ -106,7 +111,7 @@ fn render_key_hints(area: Rect, buf: &mut Buffer, pairs: &[(Vec<KeyBinding>, &st
|
||||
if i > 0 {
|
||||
spans.push("/".into());
|
||||
}
|
||||
spans.push(Span::from(key));
|
||||
spans.push(Span::from(*key));
|
||||
}
|
||||
spans.push(" ".into());
|
||||
spans.push(Span::from(desc.to_string()));
|
||||
@@ -115,6 +120,31 @@ fn render_key_hints(area: Rect, buf: &mut Buffer, pairs: &[(Vec<KeyBinding>, &st
|
||||
Paragraph::new(vec![Line::from(spans).dim()]).render(area, buf);
|
||||
}
|
||||
|
||||
fn render_navigation_hints(area: Rect, buf: &mut Buffer, keymap: &PagerKeymap) {
|
||||
let actions = [
|
||||
("scroll_up", &keymap.scroll_up),
|
||||
("scroll_down", &keymap.scroll_down),
|
||||
("page_up", &keymap.page_up),
|
||||
("page_down", &keymap.page_down),
|
||||
("jump_top", &keymap.jump_top),
|
||||
("jump_bottom", &keymap.jump_bottom),
|
||||
];
|
||||
let hints = actions
|
||||
.chunks_exact(2)
|
||||
.zip(["to scroll", "to page", "to jump"])
|
||||
.map(|(actions, description)| {
|
||||
(
|
||||
actions
|
||||
.iter()
|
||||
.filter_map(|(action, bindings)| keymap.primary_hint(action, bindings))
|
||||
.collect(),
|
||||
description,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
render_key_hints(area, buf, &hints);
|
||||
}
|
||||
|
||||
/// Generic widget for rendering a pager view.
|
||||
struct PagerView {
|
||||
renderables: Vec<Box<dyn Renderable>>,
|
||||
@@ -732,48 +762,27 @@ impl TranscriptOverlay {
|
||||
fn render_hints(&self, area: Rect, buf: &mut Buffer) {
|
||||
let line1 = Rect::new(area.x, area.y, area.width, 1);
|
||||
let line2 = Rect::new(area.x, area.y.saturating_add(1), area.width, 1);
|
||||
render_key_hints(
|
||||
line1,
|
||||
buf,
|
||||
&[
|
||||
(
|
||||
first_or_empty(&self.view.keymap.scroll_up)
|
||||
.into_iter()
|
||||
.chain(first_or_empty(&self.view.keymap.scroll_down))
|
||||
.collect(),
|
||||
"to scroll",
|
||||
),
|
||||
(
|
||||
first_or_empty(&self.view.keymap.page_up)
|
||||
.into_iter()
|
||||
.chain(first_or_empty(&self.view.keymap.page_down))
|
||||
.collect(),
|
||||
"to page",
|
||||
),
|
||||
(
|
||||
first_or_empty(&self.view.keymap.jump_top)
|
||||
.into_iter()
|
||||
.chain(first_or_empty(&self.view.keymap.jump_bottom))
|
||||
.collect(),
|
||||
"to jump",
|
||||
),
|
||||
],
|
||||
);
|
||||
render_navigation_hints(line1, buf, &self.view.keymap);
|
||||
|
||||
let mut pairs: Vec<(Vec<KeyBinding>, &str)> =
|
||||
vec![(first_or_empty(&self.view.keymap.close), "to quit")];
|
||||
let mut pairs: Vec<(Vec<ShortcutHint>, &str)> = vec![(
|
||||
first_or_empty(&self.view.keymap, "close", &self.view.keymap.close),
|
||||
"to quit",
|
||||
)];
|
||||
if self.highlight_cell.is_some() {
|
||||
pairs.push((
|
||||
vec![
|
||||
key_hint::plain(KeyCode::Esc),
|
||||
key_hint::plain(KeyCode::Left),
|
||||
key_hint::plain(KeyCode::Esc).into(),
|
||||
key_hint::plain(KeyCode::Left).into(),
|
||||
],
|
||||
"to edit prev",
|
||||
));
|
||||
pairs.push((vec![key_hint::plain(KeyCode::Right)], "to edit next"));
|
||||
pairs.push((vec![key_hint::plain(KeyCode::Enter)], "to edit message"));
|
||||
pairs.push((vec![key_hint::plain(KeyCode::Right).into()], "to edit next"));
|
||||
pairs.push((
|
||||
vec![key_hint::plain(KeyCode::Enter).into()],
|
||||
"to edit message",
|
||||
));
|
||||
} else {
|
||||
pairs.push((vec![key_hint::plain(KeyCode::Esc)], "to edit prev"));
|
||||
pairs.push((vec![key_hint::plain(KeyCode::Esc).into()], "to edit prev"));
|
||||
}
|
||||
render_key_hints(line2, buf, &pairs);
|
||||
}
|
||||
@@ -846,35 +855,11 @@ impl StaticOverlay {
|
||||
fn render_hints(&self, area: Rect, buf: &mut Buffer) {
|
||||
let line1 = Rect::new(area.x, area.y, area.width, 1);
|
||||
let line2 = Rect::new(area.x, area.y.saturating_add(1), area.width, 1);
|
||||
render_key_hints(
|
||||
line1,
|
||||
buf,
|
||||
&[
|
||||
(
|
||||
first_or_empty(&self.view.keymap.scroll_up)
|
||||
.into_iter()
|
||||
.chain(first_or_empty(&self.view.keymap.scroll_down))
|
||||
.collect(),
|
||||
"to scroll",
|
||||
),
|
||||
(
|
||||
first_or_empty(&self.view.keymap.page_up)
|
||||
.into_iter()
|
||||
.chain(first_or_empty(&self.view.keymap.page_down))
|
||||
.collect(),
|
||||
"to page",
|
||||
),
|
||||
(
|
||||
first_or_empty(&self.view.keymap.jump_top)
|
||||
.into_iter()
|
||||
.chain(first_or_empty(&self.view.keymap.jump_bottom))
|
||||
.collect(),
|
||||
"to jump",
|
||||
),
|
||||
],
|
||||
);
|
||||
let pairs: Vec<(Vec<KeyBinding>, &str)> =
|
||||
vec![(first_or_empty(&self.view.keymap.close), "to quit")];
|
||||
render_navigation_hints(line1, buf, &self.view.keymap);
|
||||
let pairs: Vec<(Vec<ShortcutHint>, &str)> = vec![(
|
||||
first_or_empty(&self.view.keymap, "close", &self.view.keymap.close),
|
||||
"to quit",
|
||||
)];
|
||||
render_key_hints(line2, buf, &pairs);
|
||||
}
|
||||
|
||||
@@ -1035,6 +1020,27 @@ mod tests {
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn footer_hints_display_chords_without_internal_dispatch_keys() {
|
||||
use codex_config::types::KeybindingSpec;
|
||||
use codex_config::types::KeybindingsSpec;
|
||||
use codex_config::types::TuiKeymap;
|
||||
|
||||
let mut config = TuiKeymap::default();
|
||||
config.pager.page_up = Some(KeybindingsSpec::One(KeybindingSpec(
|
||||
"ctrl-x page-up".to_string(),
|
||||
)));
|
||||
let keymap = crate::keymap::RuntimeKeymap::from_config(&config).expect("valid pager chord");
|
||||
|
||||
assert_eq!(
|
||||
first_or_empty(&keymap.pager, "page_up", &keymap.pager.page_up),
|
||||
vec![ShortcutHint::Chord {
|
||||
prefix: key_hint::ctrl(KeyCode::Char('x')),
|
||||
completion: key_hint::plain(KeyCode::PageUp),
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_prev_hint_is_visible() {
|
||||
let mut overlay = transcript_overlay(vec![Arc::new(TestCell {
|
||||
|
||||
Reference in new issue
Block a user