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
This commit is contained in:
Felipe Coury
2026-11-26 18:19:55 +00:00
committed by copyberry
parent f229915597
commit bdc0764660
3 changed files with 49 additions and 16 deletions
+14 -16
View File
@@ -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<DrawCommand> {
@@ -720,14 +720,10 @@ fn diff_buffers(a: &Buffer, b: &Buffer) -> Vec<DrawCommand> {
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<DrawCommand> {
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<DrawCommand> {
});
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<u8>,
@@ -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:?}"
);
}
@@ -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::<String>();
assert_snapshot!(format!("[{shaded}]"), @"[Show the two demo files. ]");
}
@@ -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::<Vec<_>>()
.join("\n"),
);