Keep terminal background colors when foreground replies are missing (#63191)

## Why

On Unix, terminals that report a background color without a foreground
color would lose both from the TUI palette. This left the composer
without shading based on the terminal background.

## What changed

Keep the background reply from startup and standalone color probes while
giving the foreground reply the rest of the existing probe window.
Use the reported background for composer shading. When either color is
missing, render the empty-state logo in the terminal's default foreground
and use the fallback color pair for shimmer.

## Testing

Cover background retention at the startup deadline, a foreground reply
arriving after the background, and rendering a shaded composer with the
logo in the default foreground.

GitOrigin-RevId: a3ce859eb04a21cfa9742d28b57fa98d4c26e0cc
This commit is contained in:
Eric Traut
2026-11-22 21:04:17 +00:00
committed by copyberry
parent ba1c17f960
commit b087609f83
6 changed files with 108 additions and 58 deletions
+3 -5
View File
@@ -234,16 +234,14 @@ impl EmptyStateAnimation {
fn paint_frame(&mut self, area: Rect, buffer: &mut Buffer, phase: f64, opacity: f32) {
let background = terminal_palette::default_bg();
let color_level = if background.is_some() {
let foreground = terminal_palette::default_fg();
let color_level = if background.is_some() && foreground.is_some() {
terminal_palette::effective_stdout_color_level()
} else {
terminal_palette::StdoutColorLevel::Unknown
};
let background = background.unwrap_or((15, 20, 37));
let light = Lighting::terminal(
terminal_palette::default_fg().unwrap_or((210, 221, 235)),
background,
);
let light = Lighting::terminal(foreground.unwrap_or((210, 221, 235)), background);
let cells = self.renderer.get_or_insert_with(Renderer::default).frame(
area.width,
area.height,
@@ -244,6 +244,31 @@ fn loop_closes_and_light_themes_preserve_coverage() {
}
}
#[test]
fn background_only_keeps_logo_visible_and_composer_shaded() {
let colors = terminal_palette::DefaultColors {
fg: None,
bg: (210, 221, 235),
};
terminal_palette::with_test_palette(Some(colors), || {
let area = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 48, /*height*/ 17,
);
let mut buffer = Buffer::empty(area);
let mut animation = EmptyStateAnimation::default();
animation.start_fresh();
animation.render_in(area, &mut buffer, Presentation::Animated);
let ink = buffer
.content
.iter()
.filter(|cell| cell.symbol() != " ")
.map(|cell| cell.fg)
.collect::<std::collections::HashSet<_>>();
let fill = crate::style::user_message_style().bg;
insta::assert_snapshot!(format!("Logo {ink:?}, composer {fill:?}"), @"Logo {Reset}, composer Some(Rgb(201, 212, 225))");
});
}
#[test]
fn faded_presentation_settles_without_redraws_then_resumes() {
let area = Rect::new(
+3 -2
View File
@@ -36,8 +36,9 @@ pub(super) fn shimmer_spans(text: &str) -> Vec<Span<'static>> {
let band_half_width = 5.0;
let mut spans: Vec<Span<'static>> = Vec::with_capacity(chars.len());
let base_color = default_fg().unwrap_or((128, 128, 128));
let highlight_color = default_bg().unwrap_or((255, 255, 255));
let (base_color, highlight_color) = default_fg()
.zip(default_bg())
.unwrap_or(((128, 128, 128), (255, 255, 255)));
for (i, ch) in chars.iter().enumerate() {
let i_pos = i as isize + padding as isize;
let pos = pos as isize;
+23 -26
View File
@@ -89,10 +89,20 @@ fn best_color_for_color_level(target: (u8, u8, u8), color_level: StdoutColorLeve
}
}
#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct DefaultColors {
fg: (u8, u8, u8),
bg: (u8, u8, u8),
pub(crate) fg: Option<(u8, u8, u8)>,
pub(crate) bg: (u8, u8, u8),
}
#[cfg(any(windows, test))]
impl From<crate::terminal_probe::DefaultColors> for DefaultColors {
fn from(colors: crate::terminal_probe::DefaultColors) -> Self {
Self {
fg: Some(colors.fg),
bg: colors.bg,
}
}
}
#[cfg(test)]
@@ -117,11 +127,12 @@ pub(crate) fn with_test_default_colors<T>(
colors: impl Into<Option<crate::terminal_probe::DefaultColors>>,
render: impl FnOnce() -> T,
) -> T {
with_test_palette(colors.into().map(DefaultColors::from), render)
}
#[cfg(test)]
pub(crate) fn with_test_palette<T>(colors: Option<DefaultColors>, render: impl FnOnce() -> T) -> T {
TEST_DEFAULT_COLORS.with(|override_colors| {
let colors = colors.into().map(|colors| DefaultColors {
fg: colors.fg,
bg: colors.bg,
});
let previous = override_colors.replace(Some(colors));
let result = render();
override_colors.set(previous);
@@ -130,7 +141,7 @@ pub(crate) fn with_test_default_colors<T>(
}
pub fn default_fg() -> Option<(u8, u8, u8)> {
default_colors().map(|c| c.fg)
default_colors().and_then(|c| c.fg)
}
pub fn default_bg() -> Option<(u8, u8, u8)> {
@@ -138,9 +149,7 @@ pub fn default_bg() -> Option<(u8, u8, u8)> {
}
#[cfg(any(unix, windows))]
pub(crate) fn set_default_colors_from_startup_probe(
colors: Option<crate::terminal_probe::DefaultColors>,
) {
pub(crate) fn set_default_colors_from_startup_probe(colors: Option<DefaultColors>) {
imp::set_default_colors_from_startup_probe(colors);
}
@@ -185,14 +194,9 @@ mod imp {
cache.get_or_init_with(query_default_colors)
}
pub(super) fn set_default_colors_from_startup_probe(
colors: Option<crate::terminal_probe::DefaultColors>,
) {
pub(super) fn set_default_colors_from_startup_probe(colors: Option<DefaultColors>) {
if let Ok(mut cache) = default_colors_cache().lock() {
cache.value = colors.map(|colors| DefaultColors {
fg: colors.fg,
bg: colors.bg,
});
cache.value = colors;
cache.attempted = true;
}
}
@@ -206,10 +210,6 @@ mod imp {
crate::terminal_probe::default_colors(crate::terminal_probe::DEFAULT_TIMEOUT)
.ok()
.flatten()
.map(|colors| DefaultColors {
fg: colors.fg,
bg: colors.bg,
})
}
}
@@ -222,10 +222,7 @@ mod imp {
}
#[cfg(any(unix, windows))]
pub(super) fn set_default_colors_from_startup_probe(
_colors: Option<crate::terminal_probe::DefaultColors>,
) {
}
pub(super) fn set_default_colors_from_startup_probe(_colors: Option<DefaultColors>) {}
}
/// The subset of Xterm colors that are usually consistent across terminals.
+49 -22
View File
@@ -26,7 +26,8 @@ mod windows_replay;
/// Default wall-clock budget for each terminal probe or startup probe group.
pub(crate) const DEFAULT_TIMEOUT: Duration = Duration::from_millis(/*millis*/ 250);
/// Default terminal foreground and background colors reported by OSC 10 and OSC 11.
/// A complete foreground/background pair from the Windows probe or a test fixture.
#[cfg(any(windows, test))]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub(crate) struct DefaultColors {
/// Default foreground color as an 8-bit RGB tuple.
@@ -47,10 +48,10 @@ fn alternate_scroll_report(input: &[u8]) -> Option<bool> {
#[cfg(unix)]
#[cfg_attr(test, allow(dead_code))]
mod imp {
use super::DefaultColors;
use super::parse_default_colors;
use super::startup_replay::parse_startup_input;
use super::terminal_identity;
use crate::terminal_palette::DefaultColors;
use std::fs::File;
use std::fs::OpenOptions;
use std::io;
@@ -265,14 +266,16 @@ mod imp {
/// Queries OSC 10 and OSC 11 default colors under one shared deadline.
///
/// Foreground and background are only useful as a pair for palette calculations, so a missing
/// response from either slot returns `Ok(None)`. Both queries are sent before reading so a
/// terminal that supports palette replies gets the full bounded window to return both values,
/// while unsupported terminals still pay one bounded wait instead of one wait per slot.
/// Wait for both replies, retaining a lone background when the probe ends.
pub(crate) fn default_colors(timeout: Duration) -> io::Result<Option<DefaultColors>> {
let mut tty = Tty::open()?;
tty.write_all(b"\x1B]10;?\x1B\\\x1B]11;?\x1B\\")?;
read_until(&mut tty, timeout, |buffer| parse_default_colors(buffer))
let mut colors = None;
read_until(&mut tty, timeout, |buffer| {
colors = parse_default_colors(buffer);
colors?.fg
})?;
Ok(colors)
}
/// Queries the terminal cursor position while normal input polling is paused.
@@ -435,7 +438,10 @@ mod imp {
if probe.cursor_position.is_none() {
probe.cursor_position = parse_cursor_position(buffer);
}
if probe.default_colors.is_none() {
if probe
.default_colors
.is_none_or(|colors| colors.fg.is_none())
{
probe.default_colors = parse_default_colors(buffer);
}
if probe.terminal_app_over_ssh.is_none() {
@@ -465,7 +471,9 @@ mod imp {
keyboard_probe: StartupKeyboardEnhancementProbe,
) -> bool {
probe.cursor_position.is_some()
&& probe.default_colors.is_some()
&& probe
.default_colors
.is_some_and(|colors| colors.fg.is_some())
&& probe.terminal_app_over_ssh.is_some()
&& (keyboard_probe == StartupKeyboardEnhancementProbe::Skip
|| probe.keyboard_enhancement_supported.is_some())
@@ -716,10 +724,24 @@ mod imp {
kitty_graphics: false,
};
let mut saw_supported_keyboard = false;
let mut input =
b"\x1B[?1007;1$y\x1B[20;10R\x1B]11;rgb:1111/1111/1111\x07\x1B[?64;1;2c\x1B[?7u"
.to_vec();
update_startup_probe(
&mut probe,
&mut saw_supported_keyboard,
b"\x1B[?1007;1$y\x1B[20;10R\x1B]11;rgb:1111/1111/1111\x07\x1B[?64;1;2c\x1B]10;rgb:eeee/eeee/eeee\x1B\\\x1B[?7u",
&input,
StartupKeyboardEnhancementProbe::Query,
);
assert!(!startup_probe_complete(
&probe,
StartupKeyboardEnhancementProbe::Query
));
input.extend_from_slice(b"\x1B]10;rgb:eeee/eeee/eeee\x1B\\");
update_startup_probe(
&mut probe,
&mut saw_supported_keyboard,
&input,
StartupKeyboardEnhancementProbe::Query,
);
@@ -729,7 +751,7 @@ mod imp {
alternate_scroll_enabled: Some(true),
cursor_position: Some(Position { x: 9, y: 19 }),
default_colors: Some(DefaultColors {
fg: (238, 238, 238),
fg: Some((238, 238, 238)),
bg: (17, 17, 17),
}),
keyboard_enhancement_supported: Some(true),
@@ -782,12 +804,12 @@ mod imp {
}
#[test]
fn missing_identity_remains_unknown_at_the_deadline() {
let mut tty = tty_with_buffered_input(b"\x1b[?1;2c");
let mut buffer = Vec::new();
fn startup_deadline_preserves_background_without_foreground_or_identity() {
let mut tty = tty_with_buffered_input(b"");
let mut buffer = b"\x1b[?1;2c\x1b]11;rgb:1b1b/1b1b/1b1b\x07".to_vec();
let probe = read_startup_probe(
&mut tty,
Duration::from_millis(/*millis*/ 5),
Duration::ZERO,
StartupKeyboardEnhancementProbe::Skip,
/*query_identity*/ true,
/*probe_herdr*/ false,
@@ -795,6 +817,10 @@ mod imp {
)
.expect("finish within probe deadline");
assert_eq!(probe.terminal_app_over_ssh, None);
let colors = probe.default_colors.expect("OSC 11 background");
assert_eq!(colors.fg, None);
let fill = crate::style::user_message_bg_rgb(colors.bg);
insta::assert_snapshot!(format!("{fill:?}"), @"(54, 54, 54)");
}
#[test]
@@ -822,7 +848,7 @@ mod imp {
alternate_scroll_enabled: Some(false),
cursor_position: Some(Position { x: 9, y: 19 }),
default_colors: Some(DefaultColors {
fg: (238, 238, 238),
fg: Some((238, 238, 238)),
bg: (17, 17, 17),
}),
keyboard_enhancement_supported: Some(true),
@@ -874,10 +900,10 @@ fn parse_osc_color(buffer: &[u8], slot: u8) -> Option<(u8, u8, u8)> {
}
#[cfg(any(unix, test))]
fn parse_default_colors(buffer: &[u8]) -> Option<DefaultColors> {
let fg = parse_osc_color(buffer, /*slot*/ 10)?;
fn parse_default_colors(buffer: &[u8]) -> Option<crate::terminal_palette::DefaultColors> {
let bg = parse_osc_color(buffer, /*slot*/ 11)?;
Some(DefaultColors { fg, bg })
let fg = parse_osc_color(buffer, /*slot*/ 10);
Some(crate::terminal_palette::DefaultColors { fg, bg })
}
#[cfg(any(unix, windows, test))]
@@ -934,6 +960,7 @@ pub(crate) use imp::*;
#[cfg(test)]
mod tests {
use super::*;
use crate::terminal_palette::DefaultColors;
use pretty_assertions::assert_eq;
#[test]
@@ -965,14 +992,14 @@ mod tests {
assert_eq!(
parse_default_colors(b"\x1B]10;rgb:eeee/eeee/eeee\x1B\\\x1B]11;rgb:1111/1111/1111\x07"),
Some(DefaultColors {
fg: (238, 238, 238),
fg: Some((238, 238, 238)),
bg: (17, 17, 17)
})
);
assert_eq!(
parse_default_colors(b"\x1B]11;rgb:1111/1111/1111\x07\x1B]10;rgb:eeee/eeee/eeee\x1B\\"),
Some(DefaultColors {
fg: (238, 238, 238),
fg: Some((238, 238, 238)),
bg: (17, 17, 17)
})
);
@@ -1005,7 +1032,7 @@ mod tests {
b"typed\x1B]10;rgb:eeee/eeee/eeee\x1B\\noise\x1B]11;rgb:1111/1111/1111\x07"
),
Some(DefaultColors {
fg: (238, 238, 238),
fg: Some((238, 238, 238)),
bg: (17, 17, 17),
})
);
+5 -3
View File
@@ -34,7 +34,9 @@ const WINDOWS_PROBE_READ_RECORDS: usize = 64;
/// Windows Terminal and xterm.js can render a theme unrelated to ConPTY's console color table.
/// Their OSC responses share the input queue with user input, so the probe preserves and restores
/// the original console records without changing Windows' input-record mode.
pub(crate) fn default_colors(timeout: Duration) -> io::Result<Option<DefaultColors>> {
pub(crate) fn default_colors(
timeout: Duration,
) -> io::Result<Option<crate::terminal_palette::DefaultColors>> {
let Ok(output) = std_handle(STD_OUTPUT_HANDLE) else {
return Ok(None);
};
@@ -47,7 +49,7 @@ pub(crate) fn default_colors(timeout: Duration) -> io::Result<Option<DefaultColo
background = ?colors.bg,
"using Windows terminal OSC default colors"
);
return Ok(Some(colors));
return Ok(Some(colors.into()));
}
// ConPTY's PseudoConsoleWindow can report itself as visible, but its palette does not
@@ -76,7 +78,7 @@ pub(crate) fn default_colors(timeout: Duration) -> io::Result<Option<DefaultColo
"using Windows console palette default colors"
);
}
Ok(colors)
Ok(colors.map(Into::into))
}
fn query_osc_default_colors(