Lightweight coding agent that runs in your terminal
Rust
Restore the inline TUI after full-screen overlays (#42641)
Felipe Coury956aa3f6372f
diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rsindex 76944a3a4a..493d281f9b 100644--- a/codex-rs/tui/src/app.rs+++ b/codex-rs/tui/src/app.rs@@ -986,11 +986,17 @@ impl App {.chat_widget.selected_index_for_present_view(AGENTS_OVERVIEW_VIEW_ID).is_some();- if std::mem::replace(+ let dashboard_was_visible = std::mem::replace(&mut self.agents_overview.rendered_full_screen,dashboard_visible,- ) && !dashboard_visible- {+ );+ // Full-height inline overlays scroll history off screen without a terminal resize.+ // Rebuild it once when returning to a content-height chat viewport.+ let restoring_inline_viewport = !tui.is_alt_screen_active()+ && tui.terminal.viewport_area.height == screen_size.height+ && self.with_chat_widget_frame(screen_size.width, |height, _| height)+ < screen_size.height;+ if !dashboard_visible && (dashboard_was_visible || restoring_inline_viewport) {self.schedule_immediate_resize_reflow(tui);self.maybe_run_resize_reflow(tui, screen_size)?;}diff --git a/codex-rs/tui/src/app/tests.rs b/codex-rs/tui/src/app/tests.rsindex bd29376141..3737f1cf27 100644--- a/codex-rs/tui/src/app/tests.rs+++ b/codex-rs/tui/src/app/tests.rs@@ -5947,6 +5947,49 @@ async fn resize_reflow_wraps_transcript_early_when_pet_is_enabled() {);}+#[tokio::test]+async fn closing_fullscreen_inline_overlay_restores_history_once() -> Result<()> {+ let (mut app, _rx, _op_rx) = make_test_app_with_channels().await;+ app.transcript_cells = vec![plain_line_cell("Previous conversation")];+ let mut tui = crate::tui::test_support::make_test_tui()?;+ tui.set_alt_screen_enabled(/*enabled*/ false);+ let screen_size = tui.terminal.last_known_screen_size;+ tui.insert_history_lines(vec![Line::from("Previous conversation")]);+ app.render_chat_widget_frame(&mut tui, screen_size)?;++ // Content-height popups keep the existing history without rebuilding scrollback.+ let popup_height = screen_size.height - 1;+ tui.draw(popup_height, |_| {})?;+ app.render_chat_widget_frame(&mut tui, screen_size)?;+ assert!(app.last_rendered_history_tail.is_none());++ tui.enter_alt_screen()?;+ tui.draw(screen_size.height, |_| {})?;+ tui.leave_alt_screen()?;+ app.render_chat_widget_frame(&mut tui, screen_size)?;++ assert!(tui.terminal.viewport_area.height < screen_size.height);+ assert!(tui.terminal.viewport_area.y > 0);+ let tail = app+ .last_rendered_history_tail+ .as_ref()+ .expect("fullscreen exit rebuilt history");+ assert_snapshot!(+ tail.lines+ .iter()+ .map(rendered_line_text)+ .collect::<Vec<_>>()+ .join("\n"),+ @"Previous conversation"+ );++ let deferred = vec![Line::from("Pending history").into()];+ app.deferred_history_lines = deferred.clone();+ app.render_chat_widget_frame(&mut tui, screen_size)?;+ assert_eq!(app.deferred_history_lines, deferred);+ Ok(())+}+#[tokio::test]async fn copy_picker_opening_preserves_terminal_scrollback_without_reflow() {let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;diff --git a/codex-rs/tui/src/custom_terminal.rs b/codex-rs/tui/src/custom_terminal.rsindex 39eb5f1b08..64e163f18b 100644--- a/codex-rs/tui/src/custom_terminal.rs+++ b/codex-rs/tui/src/custom_terminal.rs@@ -1009,6 +1009,79 @@ mod tests {");}+ #[tokio::test]+ async fn leaving_alternate_screen_repaints_restored_inline_viewport() {+ let mut tui = crate::tui::test_support::make_test_tui().expect("test tui");+ let size = tui.terminal.last_known_screen_size;+ let inline = Rect::new(/*x*/ 0, /*y*/ 3, size.width, /*height*/ 6);+ tui.terminal.set_viewport_area(inline);+ tui.enter_alt_screen().expect("enter alternate screen");+ tui.terminal+ .draw(|frame| {+ Paragraph::new(vec![+ Line::from(" Resume a previous session"),+ Line::default(),+ Line::from(" Type to search".dim()),+ ])+ .render(frame.area(), frame.buffer_mut());+ })+ .expect("draw picker");+ tui.leave_alt_screen().expect("leave alternate screen");++ let mut terminal = Terminal::with_options(VT100Backend::new(size.width, size.height))+ .expect("physical terminal");+ write!(terminal.backend_mut(), "Previous conversation").expect("write history");+ terminal.set_viewport_area(inline);+ terminal+ .draw(|frame| {+ Paragraph::new(vec![+ Line::default(),+ Line::default(),+ Line::from(vec!["›".bold(), " ".into(), "/resume".dim()]),+ ])+ .render(frame.area(), frame.buffer_mut());+ })+ .expect("draw submitted command");++ // Apply the real Tui screen-return baseline to the unchanged physical main screen.+ // Matching spaces and letters in the picker must not leave /resume cells behind.+ *terminal.previous_buffer_mut() = tui.terminal.previous_buffer().clone();+ for _ in 0..2 {+ terminal+ .draw(|frame| {+ Paragraph::new(vec![+ Line::default(),+ Line::default(),+ Line::from(vec![+ "›".bold(),+ " ".into(),+ "Ask Codex to do anything".dim(),+ ]),+ ])+ .render(frame.area(), frame.buffer_mut());+ })+ .expect("restore composer");+ }++ let visible = terminal+ .backend()+ .vt100()+ .screen()+ .contents()+ .lines()+ .map(str::trim_end)+ .collect::<Vec<_>>()+ .join("\n");+ assert_snapshot!(visible.trim_end(), @r"+ Previous conversation+++++ › Ask Codex to do anything+ ");+ }+#[test]fn ordinary_redraws_with_known_size_do_not_query_backend_size() {let mut terminal =diff --git a/codex-rs/tui/src/tui.rs b/codex-rs/tui/src/tui.rsindex 814e01d844..96b3fbcbb9 100644--- a/codex-rs/tui/src/tui.rs+++ b/codex-rs/tui/src/tui.rs@@ -861,6 +861,8 @@ impl Tui {if let Some(saved) = self.alt_saved_viewport.take() {self.terminal.set_viewport_area(saved);}+ // The restored main screen does not contain the alternate screen's diff baseline.+ self.terminal.invalidate_viewport();self.alt_screen_active.store(false, Ordering::Relaxed);Ok(())}