From 2375c721a32bb2be613ad97c60b680299bc5f75f Mon Sep 17 00:00:00 2001 From: canvrno-oai Date: Wed, 8 Apr 2026 15:29:22 -0700 Subject: [PATCH] testing --- codex-rs/tui/src/insert_history.rs | 32 +++++++++------- codex-rs/tui/src/tui.rs | 61 +++++++++++++++++++++++++----- 2 files changed, 70 insertions(+), 23 deletions(-) diff --git a/codex-rs/tui/src/insert_history.rs b/codex-rs/tui/src/insert_history.rs index 76cd699e86..1ccb388057 100644 --- a/codex-rs/tui/src/insert_history.rs +++ b/codex-rs/tui/src/insert_history.rs @@ -32,23 +32,29 @@ use ratatui::text::Span; /// Selects the terminal escape strategy for inserting history lines above the viewport. /// /// Standard terminals support `DECSTBM` scroll regions and Reverse Index (`ESC M`), -/// which let us slide existing content down without redrawing it. Zellij silently -/// drops or mishandles those sequences, so `Zellij` mode falls back to emitting -/// newlines at the bottom of the screen and writing lines at absolute positions. +/// which let us slide existing content down without redrawing it. Some terminal +/// stacks either mishandle those sequences or do not promote content that scrolls +/// out of a bounded scroll region into native scrollback; `BottomNewline` mode +/// falls back to emitting newlines at the bottom of the screen and writing lines +/// at absolute positions. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum InsertHistoryMode { Standard, - Zellij, + BottomNewline, } impl InsertHistoryMode { - pub fn new(is_zellij: bool) -> Self { - if is_zellij { - Self::Zellij + pub fn from_bottom_newline_preference(prefers_bottom_newline: bool) -> Self { + if prefers_bottom_newline { + Self::BottomNewline } else { Self::Standard } } + + pub fn requires_full_repaint(self) -> bool { + matches!(self, Self::BottomNewline) + } } /// Insert `lines` above the viewport using the terminal's backend writer @@ -66,9 +72,9 @@ where /// Insert `lines` above the viewport, using the escape strategy selected by `mode`. /// /// In `Standard` mode this manipulates DECSTBM scroll regions to slide existing -/// scrollback down and writes new lines into the freed space. In `Zellij` mode it -/// emits newlines at the screen bottom to create space (since Zellij ignores scroll -/// region escapes) and writes lines at computed absolute positions. Both modes +/// scrollback down and writes new lines into the freed space. In `BottomNewline` +/// mode it emits newlines at the screen bottom to create space and writes lines +/// at computed absolute positions. Both modes /// update `terminal.viewport_area` so subsequent draw passes know where the /// viewport moved to. pub fn insert_history_lines_with_mode( @@ -116,7 +122,7 @@ where } let wrapped_lines = wrapped_rows as u16; - if matches!(mode, InsertHistoryMode::Zellij) { + if matches!(mode, InsertHistoryMode::BottomNewline) { let space_below = screen_size.height.saturating_sub(area.bottom()); let shift_down = wrapped_lines.min(space_below); let scroll_up_amount = wrapped_lines.saturating_sub(shift_down); @@ -801,7 +807,7 @@ mod tests { } #[test] - fn vt100_zellij_mode_inserts_history_and_updates_viewport() { + fn vt100_bottom_newline_mode_inserts_history_and_updates_viewport() { let width: u16 = 32; let height: u16 = 8; let backend = VT100Backend::new(width, height); @@ -810,7 +816,7 @@ mod tests { term.set_viewport_area(viewport); let line: Line<'static> = Line::from("zellij history"); - insert_history_lines_with_mode(&mut term, vec![line], InsertHistoryMode::Zellij) + insert_history_lines_with_mode(&mut term, vec![line], InsertHistoryMode::BottomNewline) .expect("insert zellij history"); let rows: Vec = term.backend().vt100().screen().rows(0, width).collect(); diff --git a/codex-rs/tui/src/tui.rs b/codex-rs/tui/src/tui.rs index 9fc67d0a52..ced2957381 100644 --- a/codex-rs/tui/src/tui.rs +++ b/codex-rs/tui/src/tui.rs @@ -40,6 +40,7 @@ use tokio_stream::Stream; pub use self::frame_requester::FrameRequester; use crate::custom_terminal; use crate::custom_terminal::Terminal as CustomTerminal; +use crate::insert_history::InsertHistoryMode; use crate::notifications::DesktopNotificationBackend; use crate::notifications::detect_backend; use crate::tui::event_stream::EventBroker; @@ -47,6 +48,8 @@ use crate::tui::event_stream::TuiEventStream; #[cfg(unix)] use crate::tui::job_control::SuspendContext; use codex_config::types::NotificationMethod; +use codex_terminal_detection::TerminalInfo; +use codex_terminal_detection::TerminalName; mod event_stream; mod frame_rate_limiter; @@ -255,10 +258,25 @@ pub struct Tui { enhanced_keys_supported: bool, notification_backend: Option, is_zellij: bool, + history_insertion_mode: InsertHistoryMode, // When false, enter_alt_screen() becomes a no-op (for Zellij scrollback support) alt_screen_enabled: bool, } +fn history_insertion_mode_for_terminal(terminal_info: &TerminalInfo) -> InsertHistoryMode { + InsertHistoryMode::from_bottom_newline_preference( + terminal_info.is_zellij() + || terminal_prefers_bottom_newline_history_insertion(terminal_info.name), + ) +} + +fn terminal_prefers_bottom_newline_history_insertion(terminal_name: TerminalName) -> bool { + matches!( + terminal_name, + TerminalName::VsCode | TerminalName::WindowsTerminal + ) +} + impl Tui { pub fn new(terminal: Terminal) -> Self { let (draw_tx, _) = broadcast::channel(1); @@ -270,10 +288,9 @@ impl Tui { // Cache this to avoid contention with the event reader. supports_color::on_cached(supports_color::Stream::Stdout); let _ = crate::terminal_palette::default_colors(); - let is_zellij = matches!( - codex_terminal_detection::terminal_info().multiplexer, - Some(codex_terminal_detection::Multiplexer::Zellij {}) - ); + let terminal_info = codex_terminal_detection::terminal_info(); + let is_zellij = terminal_info.is_zellij(); + let history_insertion_mode = history_insertion_mode_for_terminal(&terminal_info); Self { frame_requester, @@ -289,6 +306,7 @@ impl Tui { enhanced_keys_supported, notification_backend: Some(detect_backend(NotificationMethod::default())), is_zellij, + history_insertion_mode, alt_screen_enabled: true, } } @@ -512,12 +530,13 @@ impl Tui { } /// Write any buffered history lines above the viewport and clear the buffer. - /// Returns `true` when Zellij mode was used, signaling that the caller must - /// invalidate the diff buffer for a full repaint. + /// Returns `true` when the selected insertion mode moved screen content + /// outside ratatui's diff model, signaling that the caller must invalidate + /// the diff buffer for a full repaint. fn flush_pending_history_lines( terminal: &mut Terminal, pending_history_lines: &mut Vec>, - is_zellij: bool, + history_insertion_mode: InsertHistoryMode, ) -> Result { if pending_history_lines.is_empty() { return Ok(false); @@ -526,10 +545,10 @@ impl Tui { crate::insert_history::insert_history_lines_with_mode( terminal, pending_history_lines.clone(), - crate::insert_history::InsertHistoryMode::new(is_zellij), + history_insertion_mode, )?; pending_history_lines.clear(); - Ok(is_zellij) + Ok(history_insertion_mode.requires_full_repaint()) } pub fn draw( @@ -565,7 +584,7 @@ impl Tui { needs_full_repaint |= Self::flush_pending_history_lines( terminal, &mut self.pending_history_lines, - self.is_zellij, + self.history_insertion_mode, )?; if needs_full_repaint { @@ -614,3 +633,25 @@ impl Tui { Ok(None) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn bottom_newline_history_insertion_is_used_for_known_scrollback_problem_terminals() { + assert!(terminal_prefers_bottom_newline_history_insertion( + TerminalName::VsCode + )); + assert!(terminal_prefers_bottom_newline_history_insertion( + TerminalName::WindowsTerminal + )); + + assert!(!terminal_prefers_bottom_newline_history_insertion( + TerminalName::Iterm2 + )); + assert!(!terminal_prefers_bottom_newline_history_insertion( + TerminalName::Unknown + )); + } +}