Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Fix transcript viewer restoration and half-page scrolling (#43889)

Benjamin Carlsson095da4b7e8b7
diff --git a/codex-rs/tui/src/app/input.rs b/codex-rs/tui/src/app/input.rs
index 3b7dccb4ba..a599bc6186 100644
--- a/codex-rs/tui/src/app/input.rs
+++ b/codex-rs/tui/src/app/input.rs
@@ -226,6 +226,10 @@ impl App {
} else {
self.chat_widget.set_raw_output_mode(enabled);
}
+ if self.overlay.is_some() {
+ self.schedule_immediate_resize_reflow(tui);
+ return;
+ }
let terminal_width = tui.terminal.last_known_screen_size.into();
if let Err(err) = self.reflow_transcript_now(tui, terminal_width) {
tracing::warn!(error = %err, "failed to reflow transcript after raw output mode toggle");
diff --git a/codex-rs/tui/src/app/tests/snapshots/codex_tui__app__tests__transcript_composer__transcript_close_restores_inline_draft.snap b/codex-rs/tui/src/app/tests/snapshots/codex_tui__app__tests__transcript_composer__transcript_close_restores_inline_draft.snap
new file mode 100644
index 0000000000..9888ce81a5
--- /dev/null
+++ b/codex-rs/tui/src/app/tests/snapshots/codex_tui__app__tests__transcript_composer__transcript_close_restores_inline_draft.snap
@@ -0,0 +1,9 @@
+---
+source: tui/src/app/tests/transcript_composer.rs
+expression: "buffer_text(crate::custom_terminal::test_support::last_rendered_buffer(&tui.terminal))"
+---
+
+
+› EDGE-DRAFT-MUST-SURVIVE
+
+ gpt-5.6-sol default · /tmp/project
diff --git a/codex-rs/tui/src/app/tests/transcript_composer.rs b/codex-rs/tui/src/app/tests/transcript_composer.rs
index 7d0fce5446..7f71480da9 100644
--- a/codex-rs/tui/src/app/tests/transcript_composer.rs
+++ b/codex-rs/tui/src/app/tests/transcript_composer.rs
@@ -1,13 +1,29 @@
-//! Regression coverage for transcript viewer input and prompt selection.
+//! Regression coverage for transcript viewer input, prompt selection, and restoration.
//!
//! The default-off feature must leave the existing viewer and its draft intact.
use super::*;
+use crate::test_support::test_path_display;
use crossterm::event::KeyCode;
use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers;
use pretty_assertions::assert_eq;
+fn buffer_text(buffer: &ratatui::buffer::Buffer) -> String {
+ buffer
+ .content()
+ .chunks(usize::from(buffer.area.width))
+ .map(|row| {
+ row.iter()
+ .map(ratatui::buffer::Cell::symbol)
+ .collect::<String>()
+ .trim_end()
+ .to_string()
+ })
+ .collect::<Vec<_>>()
+ .join("\n")
+}
+
async fn press_key(
app: &mut App,
tui: &mut crate::tui::Tui,
@@ -73,19 +89,7 @@ async fn transcript_flag_off_preserves_viewer_and_backtracking() -> Result<()> {
panic!("viewer closed")
};
overlay.render(area, &mut buffer);
- let text = buffer
- .content()
- .chunks(usize::from(area.width))
- .map(|row| {
- row.iter()
- .map(ratatui::buffer::Cell::symbol)
- .collect::<String>()
- .trim_end()
- .to_string()
- })
- .collect::<Vec<_>>()
- .join("\n");
- insta::assert_snapshot!("transcript_flag_off_viewer", text);
+ insta::assert_snapshot!("transcript_flag_off_viewer", buffer_text(&buffer));
for (key, selected) in [
(KeyCode::Esc, 1),
(KeyCode::Esc, 0),
@@ -108,3 +112,57 @@ async fn transcript_flag_off_preserves_viewer_and_backtracking() -> Result<()> {
);
Ok(())
}
+
+async fn assert_transcript_close_repaints_inline_draft(mut app: App) -> Result<()> {
+ let mut app_server = start_config_write_test_app_server(&app).await?;
+ let mut tui = crate::tui::test_support::make_test_tui()?;
+ app.chat_widget.insert_str("EDGE-DRAFT-MUST-SURVIVE");
+ app.handle_tui_event(&mut tui, &mut app_server, TuiEvent::Draw)
+ .await?;
+ let inline_viewport = tui.terminal.viewport_area;
+ app.open_transcript_overlay(&mut tui);
+ tui.enter_alt_screen()?;
+ app.insert_history_cell(
+ &mut tui,
+ Box::new(PlainHistoryCell::new(vec!["arrived in transcript".into()])),
+ );
+ let deferred_history = app.deferred_history_lines.clone();
+ assert!(!deferred_history.is_empty());
+ app.apply_raw_output_mode(&mut tui, /*enabled*/ true, /*notify*/ false);
+ app.handle_tui_event(&mut tui, &mut app_server, TuiEvent::Draw)
+ .await?;
+ assert_eq!(app.deferred_history_lines, deferred_history);
+ assert!(app.transcript_reflow.has_pending_reflow());
+
+ app.handle_tui_event(
+ &mut tui,
+ &mut app_server,
+ TuiEvent::Key(KeyEvent::new(KeyCode::Char('t'), KeyModifiers::CONTROL)),
+ )
+ .await?;
+ assert_eq!(tui.terminal.viewport_area, inline_viewport);
+ app.handle_tui_event(&mut tui, &mut app_server, TuiEvent::Draw)
+ .await?;
+ assert!(!app.transcript_reflow.has_pending_reflow());
+ assert_eq!(
+ app.last_rendered_history_tail
+ .as_ref()
+ .expect("inline history reflowed")
+ .lines,
+ deferred_history
+ );
+
+ insta::assert_snapshot!(
+ "transcript_close_restores_inline_draft",
+ buffer_text(crate::custom_terminal::test_support::last_rendered_buffer(
+ &tui.terminal
+ ))
+ .replace(&test_path_display("/tmp/project"), "/tmp/project")
+ );
+ Ok(())
+}
+
+#[tokio::test]
+async fn transcript_viewer_close_repaints_preserved_inline_draft() -> Result<()> {
+ assert_transcript_close_repaints_inline_draft(make_test_app().await).await
+}
diff --git a/codex-rs/tui/src/custom_terminal.rs b/codex-rs/tui/src/custom_terminal.rs
index 64e163f18b..b667cca1b9 100644
--- a/codex-rs/tui/src/custom_terminal.rs
+++ b/codex-rs/tui/src/custom_terminal.rs
@@ -50,6 +50,10 @@ use ratatui::widgets::WidgetRef;
mod cursor;
+#[cfg(test)]
+#[path = "custom_terminal_test_support.rs"]
+pub(crate) mod test_support;
+
fn osc8_hyperlink_parts(symbol: &str) -> Option<(&str, &str)> {
let content = symbol.strip_prefix("\x1b]8;;")?;
let destination_end = content.find('\x07')?;
diff --git a/codex-rs/tui/src/custom_terminal_test_support.rs b/codex-rs/tui/src/custom_terminal_test_support.rs
new file mode 100644
index 0000000000..1ff3a153d8
--- /dev/null
+++ b/codex-rs/tui/src/custom_terminal_test_support.rs
@@ -0,0 +1,16 @@
+//! Inspect the completed frame without adding test helpers to the terminal implementation.
+
+use std::io;
+use std::io::Write;
+
+use ratatui::backend::Backend;
+use ratatui::buffer::Buffer;
+
+use super::Terminal;
+
+pub(crate) fn last_rendered_buffer<B>(terminal: &Terminal<B>) -> &Buffer
+where
+ B: Backend<Error = io::Error> + Write,
+{
+ terminal.previous_buffer()
+}
diff --git a/codex-rs/tui/src/pager_overlay.rs b/codex-rs/tui/src/pager_overlay.rs
index 9b6c763e3e..fb9cbf287f 100644
--- a/codex-rs/tui/src/pager_overlay.rs
+++ b/codex-rs/tui/src/pager_overlay.rs
@@ -307,13 +307,17 @@ impl PagerView {
self.scroll_offset = self.scroll_offset.saturating_add(page_height);
}
e if self.keymap.half_page_down.is_pressed(e) => {
- let area = self.content_area(tui.terminal.viewport_area);
- let half_page = (area.height as usize).saturating_add(1) / 2;
+ let half_page = self
+ .page_height(tui.terminal.viewport_area)
+ .saturating_add(1)
+ / 2;
self.scroll_offset = self.scroll_offset.saturating_add(half_page);
}
e if self.keymap.half_page_up.is_pressed(e) => {
- let area = self.content_area(tui.terminal.viewport_area);
- let half_page = (area.height as usize).saturating_add(1) / 2;
+ let half_page = self
+ .page_height(tui.terminal.viewport_area)
+ .saturating_add(1)
+ / 2;
self.scroll_offset = self.scroll_offset.saturating_sub(half_page);
}
e if self.keymap.jump_top.is_pressed(e) => {
@@ -1052,6 +1056,7 @@ mod tests {
use crate::history_cell::HistoryCell;
use crate::history_cell::new_patch_event;
use codex_protocol::parse_command::ParsedCommand;
+ use crossterm::event::KeyModifiers;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::text::Text;
@@ -1777,6 +1782,41 @@ mod tests {
);
}
+ #[tokio::test]
+ async fn half_page_uses_the_last_rendered_content_height() -> Result<()> {
+ let mut overlay = transcript_overlay(
+ (0..50)
+ .map(|i| {
+ Arc::new(TestCell {
+ lines: vec![Line::from(format!("line-{i:02}"))],
+ }) as Arc<dyn HistoryCell>
+ })
+ .collect(),
+ );
+ let transcript_area = Rect::new(
+ /*x*/ 0, /*y*/ 0, /*width*/ 40, /*height*/ 10,
+ );
+ let mut buf = Buffer::empty(transcript_area);
+ overlay.render(transcript_area, &mut buf);
+ let page_height = overlay.view.page_height(transcript_area);
+ let mut tui = crate::tui::test_support::make_test_tui()?;
+ tui.terminal.set_viewport_area(Rect::new(
+ /*x*/ 0, /*y*/ 0, /*width*/ 40, /*height*/ 24,
+ ));
+ overlay.view.scroll_offset = 10;
+
+ overlay.view.handle_key_event(
+ &mut tui,
+ KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL),
+ )?;
+
+ assert_eq!(
+ overlay.view.scroll_offset,
+ 10 + page_height.saturating_add(1) / 2
+ );
+ Ok(())
+ }
+
#[test]
fn static_overlay_wraps_long_lines() {
let mut overlay = static_overlay(
diff --git a/codex-rs/tui/src/tui.rs b/codex-rs/tui/src/tui.rs
index 26e1a97133..2e3b272d66 100644
--- a/codex-rs/tui/src/tui.rs
+++ b/codex-rs/tui/src/tui.rs
@@ -837,7 +837,7 @@ impl Tui {
/// Enter alternate screen and expand the viewport to full terminal size, saving the current
/// inline viewport for restoration when leaving.
pub fn enter_alt_screen(&mut self) -> Result<()> {
- if !self.alt_screen_enabled {
+ if !self.alt_screen_enabled || self.is_alt_screen_active() {
return Ok(());
}
let _ = execute!(self.terminal.backend_mut(), EnterAlternateScreen);