From bdc07646607cd9c30b65655525188e106ae9b343 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Thu, 26 Nov 2026 18:07:45 +0000 Subject: [PATCH] Preserve TUI row shading across redraws in Apple Terminal (#64195) ## Why Apple Terminal can lose the background color of spaces painted by an erase sequence when a later redraw clears part of the same row. ## What changed Print spaces with a non-default background as styled cells. Use clear-to-end-of-line only for trailing blanks with the default background. ## Testing Add a VT100 regression test that narrows a shaded row and checks that the remaining shading includes the message's trailing spaces. GitOrigin-RevId: 5c9ba7b7ad79f827d74cc4cc618142375cb6abb6 --- codex-rs/tui/src/custom_terminal.rs | 30 ++++++++-------- .../custom_terminal/tests/shading_tests.rs | 34 +++++++++++++++++++ .../tui/tests/suite/external_editor_tests.rs | 1 + 3 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 codex-rs/tui/src/custom_terminal/tests/shading_tests.rs diff --git a/codex-rs/tui/src/custom_terminal.rs b/codex-rs/tui/src/custom_terminal.rs index 3e70802834..e5fdd5bffb 100644 --- a/codex-rs/tui/src/custom_terminal.rs +++ b/codex-rs/tui/src/custom_terminal.rs @@ -485,7 +485,7 @@ where } else if !moved.contains(&index) && updates.iter().any(|update| match update { DrawCommand::Put { x, y, .. } => image.area.contains((*x, *y).into()), - DrawCommand::ClearToEnd { x, y, .. } => { + DrawCommand::ClearToEnd { x, y } => { (image.area.y..image.area.bottom()).contains(y) && *x < image.area.right() } }) @@ -707,7 +707,7 @@ use ratatui::buffer::Cell; #[derive(Debug, IsVariant)] enum DrawCommand { Put { x: u16, y: u16, cell: Cell }, - ClearToEnd { x: u16, y: u16, bg: Color }, + ClearToEnd { x: u16, y: u16 }, } fn diff_buffers(a: &Buffer, b: &Buffer) -> Vec { @@ -720,14 +720,10 @@ fn diff_buffers(a: &Buffer, b: &Buffer) -> Vec { let row_end = row_start + a.area.width as usize; let previous_row = &a.content[row_start..row_end]; let row = &next_buffer[row_start..row_end]; - let bg = row.last().map(|cell| cell.bg).unwrap_or(Color::Reset); - // Scan the row to find the rightmost column that still matters: any non-space glyph, - // any cell whose bg differs from the row’s trailing bg, any cell with modifiers, - // or any cell explicitly marked for updating. - // Multi-width glyphs extend that region through their full displayed width. - // After that point the rest of the row can be cleared with a single ClearToEnd, a perf win - // versus emitting multiple space Put commands. + // Only erase a tail of blanks with the default background. Apple Terminal can + // lose a background applied by an erase when a later frame clears part of + // the row. Printed spaces keep their color when the diff skips them. let mut last_nonblank_column = 0usize; let mut column = 0usize; while column < row.len() { @@ -735,7 +731,7 @@ fn diff_buffers(a: &Buffer, b: &Buffer) -> Vec { let width = usize::from(cell.cell_width()); // A trailing clear must neither swallow a forced repaint nor erase skipped cells. if cell.symbol() != " " - || cell.bg != bg + || cell.bg != Color::Reset || cell.modifier != Modifier::empty() || matches!( cell.diff_option, @@ -765,7 +761,7 @@ fn diff_buffers(a: &Buffer, b: &Buffer) -> Vec { }); if tail_changed || wide_char_overlaps_tail { let (x, y) = a.pos_of(row_start + clear_start); - updates.push(DrawCommand::ClearToEnd { x, y, bg }); + updates.push(DrawCommand::ClearToEnd { x, y }); } } @@ -837,7 +833,7 @@ where for command in commands { let (x, y) = match &command { DrawCommand::Put { x, y, .. } => (x, y), - DrawCommand::ClearToEnd { x, y, .. } => (x, y), + DrawCommand::ClearToEnd { x, y } => (x, y), }; let hyperlink = match &command { DrawCommand::Put { cell, .. } => osc8_hyperlink_parts(cell.symbol()), @@ -881,11 +877,11 @@ where let symbol = hyperlink.map_or_else(|| cell.symbol(), |(_, visible)| visible); queue!(writer, Print(symbol))?; } - DrawCommand::ClearToEnd { bg: clear_bg, .. } => { + DrawCommand::ClearToEnd { .. } => { queue!(writer, SetAttribute(crossterm::style::Attribute::Reset))?; modifier = Modifier::empty(); - queue!(writer, SetBackgroundColor((*clear_bg).into_crossterm()))?; - bg = *clear_bg; + fg = Color::Reset; + bg = Color::Reset; queue!(writer, Clear(crossterm::terminal::ClearType::UntilNewLine))?; } } @@ -992,6 +988,8 @@ mod tests { #[path = "cursor_tests.rs"] mod cursor; + #[path = "shading_tests.rs"] + mod shading; struct CaptureBackend { output: Vec, @@ -1504,7 +1502,7 @@ mod tests { assert!( commands .iter() - .any(|command| matches!(command, DrawCommand::ClearToEnd { x: 2, y: 0, .. })), + .any(|command| matches!(command, DrawCommand::ClearToEnd { x: 2, y: 0 })), "expected clear-to-end after {before:?} became {after:?}; commands: {commands:?}" ); } diff --git a/codex-rs/tui/src/custom_terminal/tests/shading_tests.rs b/codex-rs/tui/src/custom_terminal/tests/shading_tests.rs new file mode 100644 index 0000000000..c4d5a68be0 --- /dev/null +++ b/codex-rs/tui/src/custom_terminal/tests/shading_tests.rs @@ -0,0 +1,34 @@ +#![cfg(test)] +//! Regression coverage for shaded spaces across terminal redraws. + +use super::*; +use crate::terminal_palette::indexed_color; + +#[test] +fn shaded_spaces_survive_narrowing_a_shaded_row() { + let area = Rect::new( + /*x*/ 0, /*y*/ 0, /*width*/ 40, /*height*/ 1, + ); + let mut terminal = Terminal::with_options(VT100Backend::new(area.width, area.height)).unwrap(); + terminal.set_viewport_area(area); + + // Restoring the sidebar moves the message up onto its full-width top padding. + for (width, text) in [(area.width, ""), (28, "Show the two demo files.")] { + terminal + .draw(|frame| { + Paragraph::new(text) + .style(Style::default().bg(indexed_color(/*index*/ 238))) + .render(Rect { width, ..area }, frame.buffer_mut()); + }) + .unwrap(); + } + + // Erased cells have empty contents in vt100, while printed spaces retain " ". + let screen = terminal.backend().vt100().screen(); + let shaded = (0..area.width) + .map(|x| screen.cell(/*row*/ 0, x).unwrap()) + .filter(|cell| cell.bgcolor() == vt100::Color::Idx(238)) + .map(vt100::Cell::contents) + .collect::(); + assert_snapshot!(format!("[{shaded}]"), @"[Show the two demo files. ]"); +} diff --git a/codex-rs/tui/tests/suite/external_editor_tests.rs b/codex-rs/tui/tests/suite/external_editor_tests.rs index c4ac0b6ea0..2279f6b56d 100644 --- a/codex-rs/tui/tests/suite/external_editor_tests.rs +++ b/codex-rs/tui/tests/suite/external_editor_tests.rs @@ -91,6 +91,7 @@ fn external_editors_keep_the_screen_and_return_the_draft() -> Result<()> { line.contains("initial draft") || line.contains("Save and close external editor to continue.") }) + .map(str::trim_end) .collect::>() .join("\n"), );