Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Honor configured keybindings before transcript navigation (#50389)

Eric Trautf88a6efe437e
diff --git a/codex-rs/tui/src/app/owned_transcript.rs b/codex-rs/tui/src/app/owned_transcript.rs
index 0893ad1791..26a570b4e0 100644
--- a/codex-rs/tui/src/app/owned_transcript.rs
+++ b/codex-rs/tui/src/app/owned_transcript.rs
@@ -9,6 +9,7 @@ use super::*;
use crate::history_cell::HistoryRenderMode;
use crate::keymap::KeymapContext;
use crate::keymap::bindings_for_action;
+use crate::keymap::configured_binding_for_action;
use crate::keymap::keymap_action_ids;
use crate::motion::MotionMode;
use crate::pager_overlay::TranscriptHistoryState;
@@ -472,15 +473,30 @@ impl App {
self.close_transcript_overlay(tui);
return Ok(true);
}
+ let empty_enter_returns_to_latest = matches!(
+ event,
+ TuiEvent::Key(KeyEvent {
+ code: KeyCode::Enter,
+ modifiers: KeyModifiers::NONE,
+ kind: KeyEventKind::Press,
+ ..
+ })
+ ) && self.enter_returns_to_latest()
+ && self.transcript_view.can_return_to_latest();
if let TuiEvent::Key(key) = event
- && ((key.modifiers.is_empty()
- && matches!(key.code, KeyCode::PageUp | KeyCode::PageDown))
- || crate::transcript_view::JumpTarget::from_key(*key).is_some())
&& !self.transcript_view.has_active_interaction()
&& keymap_action_ids().any(|action| {
action.context != KeymapContext::Pager
&& !matches!(action.action, "find_transcript" | "focus_activity")
&& self.active_keymap_contexts().contains_action(action)
+ && !(empty_enter_returns_to_latest
+ && action.context == KeymapContext::Composer
+ && action.action == "submit")
+ && ((key.modifiers.is_empty()
+ && matches!(key.code, KeyCode::PageUp | KeyCode::PageDown))
+ || crate::transcript_view::JumpTarget::from_key(*key).is_some()
+ || configured_binding_for_action(&self.local_settings.tui.keymap, action)
+ .is_some_and(std::option::Option::is_some))
&& bindings_for_action(
&self.keymap,
action.context.config_name(),
@@ -521,17 +537,7 @@ impl App {
| TuiEvent::FocusLost => None,
};
let Some(action) = action else {
- if matches!(
- event,
- TuiEvent::Key(KeyEvent {
- code: KeyCode::Enter,
- modifiers: KeyModifiers::NONE,
- kind: KeyEventKind::Press,
- ..
- })
- ) && self.enter_returns_to_latest()
- && self.transcript_view.can_return_to_latest()
- {
+ if empty_enter_returns_to_latest {
self.transcript_view.jump_to_latest();
tui.frame_requester().schedule_frame();
return Ok(true);
diff --git a/codex-rs/tui/src/app/owned_transcript_input_tests.rs b/codex-rs/tui/src/app/owned_transcript_input_tests.rs
index 0347fe45f5..26dce3a7a1 100644
--- a/codex-rs/tui/src/app/owned_transcript_input_tests.rs
+++ b/codex-rs/tui/src/app/owned_transcript_input_tests.rs
@@ -1,7 +1,9 @@
//! Composer input resumes after transcript selection without changing search or keymap ownership.
+use super::tests::attach_thread;
use super::*;
use crate::app::tests::make_test_app_with_channels;
+use crate::app_command::AppCommand as Op;
use crate::chatwidget::tests::helpers::normalize_snapshot_paths;
use crate::chatwidget::tests::helpers::render_bottom_popup;
use crate::history_cell::HistoryRenderMode;
@@ -11,6 +13,7 @@ use codex_app_server_protocol::Turn;
use codex_app_server_protocol::TurnCompletedNotification;
use codex_app_server_protocol::TurnItemsView;
use codex_app_server_protocol::TurnStartedNotification;
+use codex_app_server_protocol::UserInput;
use codex_protocol::config_types::ModeKind;
use crossterm::event::MouseButton;
use crossterm::event::MouseEvent;
@@ -64,6 +67,98 @@ async fn select_transcript(
Ok(())
}
+#[tokio::test]
+async fn configured_ctrl_space_submit_wins_over_transcript_selection() -> Result<()> {
+ for running in [false, true] {
+ let (mut app, mut events, mut operations) = make_test_app_with_channels().await;
+ let thread_id = ThreadId::new();
+ attach_thread(&mut app, thread_id);
+ if running {
+ app.chat_widget.handle_server_notification(
+ ServerNotification::TurnStarted(TurnStartedNotification {
+ thread_id: thread_id.to_string(),
+ turn: Turn {
+ id: "turn".into(),
+ items_view: TurnItemsView::Full,
+ items: Vec::new(),
+ status: TurnStatus::InProgress,
+ error: None,
+ started_at: None,
+ completed_at: None,
+ duration_ms: None,
+ },
+ }),
+ /*replay_kind*/ None,
+ );
+ }
+ let config = serde_json::from_value(serde_json::json!({
+ "composer": {"submit": ["ctrl-space", "f9"]}
+ }))?;
+ app.keymap = RuntimeKeymap::from_config(&config).expect("valid composer bindings");
+ app.local_settings.tui.keymap = config.clone();
+ app.chat_widget.apply_keymap_update(config, &app.keymap);
+ let mut app_server =
+ Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?;
+ let mut tui = crate::tui::test_support::make_test_tui()?;
+ tui.set_owned_screen(/*owned*/ true)?;
+ app.transcript_cells = vec![Arc::new(PlainHistoryCell::new(vec!["transcript".into()]))];
+ app.render_owned_transcript(&mut tui, Size::new(/*width*/ 80, /*height*/ 12))?;
+ let text = if running { "steer once" } else { "send once" };
+ app.chat_widget.apply_external_edit(text.to_string());
+ while events.try_recv().is_ok() {}
+
+ app.handle_tui_event(
+ &mut tui,
+ &mut app_server,
+ TuiEvent::Key(KeyEvent::new(KeyCode::Char(' '), KeyModifiers::CONTROL)),
+ )
+ .await?;
+
+ assert!(!app.transcript_view.has_active_interaction());
+ assert_eq!(app.chat_widget.composer_text_with_pending(), "");
+ let items = std::iter::from_fn(|| operations.try_recv().ok())
+ .find_map(|op| match op {
+ Op::UserTurn { items, .. } => Some(items),
+ _ => None,
+ })
+ .expect("expected submitted user turn");
+ assert_eq!(
+ items,
+ vec![UserInput::Text {
+ text: text.into(),
+ text_elements: Vec::new(),
+ }]
+ );
+ app_server.shutdown().await?;
+ tui.set_owned_screen(/*owned*/ false)?;
+ }
+ Ok(())
+}
+
+#[tokio::test]
+async fn default_page_up_still_scrolls_the_transcript() -> Result<()> {
+ let mut app = crate::app::test_support::make_test_app().await;
+ let mut app_server = Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?;
+ let mut tui = crate::tui::test_support::make_test_tui()?;
+ tui.set_owned_screen(/*owned*/ true)?;
+ app.transcript_cells = vec![Arc::new(PlainHistoryCell::new(
+ (0..30).map(|row| format!("row {row}").into()).collect(),
+ ))];
+ app.render_owned_transcript(&mut tui, Size::new(/*width*/ 80, /*height*/ 12))?;
+
+ app.handle_tui_event(
+ &mut tui,
+ &mut app_server,
+ TuiEvent::Key(KeyCode::PageUp.into()),
+ )
+ .await?;
+
+ assert!(!app.transcript_view.is_following());
+ app_server.shutdown().await?;
+ tui.set_owned_screen(/*owned*/ false)?;
+ Ok(())
+}
+
fn screen(tui: &tui::Tui) -> String {
let buffer = crate::custom_terminal::test_support::last_rendered_buffer(&tui.terminal);
let rendered = buffer
@@ -292,6 +387,10 @@ async fn empty_enter_returns_to_latest_with_contextual_hints() -> Result<()> {
let mut tui = crate::tui::test_support::make_test_tui()?;
tui.set_owned_screen(/*owned*/ true)?;
for (width, detailed) in [(80, false), (40, true)] {
+ let config = serde_json::from_value(serde_json::json!({}))?;
+ app.keymap = RuntimeKeymap::from_config(&config).expect("valid default bindings");
+ app.local_settings.tui.keymap = config.clone();
+ app.chat_widget.apply_keymap_update(config, &app.keymap);
let size = Size::new(width, /*height*/ 12);
tui.terminal.resize(size)?;
app.transcript_view = Default::default();
@@ -345,6 +444,33 @@ async fn empty_enter_returns_to_latest_with_contextual_hints() -> Result<()> {
assert!(events.try_recv().is_err());
assert!(operations.try_recv().is_err());
+ for config in [
+ serde_json::json!({"composer": {"submit": ["enter", "ctrl-space"]}}),
+ serde_json::json!({"global": {"submit": ["enter", "ctrl-space"]}}),
+ ] {
+ let config = serde_json::from_value(config)?;
+ app.keymap = RuntimeKeymap::from_config(&config).expect("valid submit bindings");
+ app.local_settings.tui.keymap = config.clone();
+ app.chat_widget.apply_keymap_update(config, &app.keymap);
+ app.transcript_view
+ .scroll(&app.transcript_cells, /*rows*/ -10);
+ app.render_owned_transcript(&mut tui, size)?;
+ assert!(screen(&tui).contains("enter/esc latest"));
+ while events.try_recv().is_ok() {}
+ app.handle_tui_event(
+ &mut tui,
+ &mut server,
+ TuiEvent::Key(KeyEvent::from(KeyCode::Enter)),
+ )
+ .await?;
+ app.render_owned_transcript(&mut tui, size)?;
+ assert!(app.transcript_view.is_following());
+ assert_eq!(app.chat_widget.composer_text_with_pending(), "");
+ assert!(!screen(&tui).contains("latest"));
+ assert!(events.try_recv().is_err());
+ assert!(operations.try_recv().is_err());
+ }
+
assert!(!app.handle_owned_transcript_event(
&mut tui,
&mut server,
diff --git a/codex-rs/tui/src/keymap.rs b/codex-rs/tui/src/keymap.rs
index 3628773dd5..62d60a271a 100644
--- a/codex-rs/tui/src/keymap.rs
+++ b/codex-rs/tui/src/keymap.rs
@@ -52,6 +52,7 @@ mod global_find_tests;
pub(crate) use bindings::KeymapActionId;
pub(crate) use bindings::KeymapContext;
pub(crate) use bindings::bindings_for_action;
+pub(crate) use bindings::configured_binding_for_action;
pub(crate) use bindings::keymap_action_id;
pub(crate) use bindings::keymap_action_ids;
use bindings::runtime_action_bindings;
diff --git a/codex-rs/tui/src/keymap/bindings.rs b/codex-rs/tui/src/keymap/bindings.rs
index 0613f0d1fa..52a8703dd2 100644
--- a/codex-rs/tui/src/keymap/bindings.rs
+++ b/codex-rs/tui/src/keymap/bindings.rs
@@ -164,7 +164,7 @@ macro_rules! define_runtime_action_bindings {
}
/// Return the configured slot for one runtime action, including global fallbacks.
- pub(super) fn configured_binding_for_action(
+ pub(crate) fn configured_binding_for_action(
keymap: &TuiKeymap,
action: KeymapActionId,
) -> Option<&Option<KeybindingsSpec>> {