tui: hardcode xterm palette, shimmer blends between fg and bg (#4957)

Instead of querying all 256 terminal colors on startup, which was slow
in some terminals, hardcode the default xterm palette.

Additionally, tweak the shimmer so that it blends between default_fg and
default_bg, instead of "dark gray" (according to the terminal) and pure
white (regardless of terminal theme).
This commit is contained in:
Jeremy Rose
2025-10-08 13:23:13 -07:00
committed by GitHub
parent 96acb8a74e
commit e896db1180
4 changed files with 300 additions and 268 deletions

View File

@@ -1,8 +1,7 @@
use crate::color::blend;
use crate::color::is_light;
use crate::color::perceptual_distance;
use crate::terminal_palette::best_color;
use crate::terminal_palette::default_bg;
use crate::terminal_palette::terminal_palette;
use ratatui::style::Color;
use ratatui::style::Style;
@@ -25,25 +24,5 @@ pub fn user_message_bg(terminal_bg: (u8, u8, u8)) -> Color {
} else {
(255, 255, 255)
};
let bottom = terminal_bg;
let Some(color_level) = supports_color::on_cached(supports_color::Stream::Stdout) else {
return Color::default();
};
let target = blend(top, bottom, 0.1);
if color_level.has_16m {
let (r, g, b) = target;
Color::Rgb(r, g, b)
} else if color_level.has_256
&& let Some(palette) = terminal_palette()
&& let Some((i, _)) = palette.into_iter().enumerate().min_by(|(_, a), (_, b)| {
perceptual_distance(*a, target)
.partial_cmp(&perceptual_distance(*b, target))
.unwrap_or(std::cmp::Ordering::Equal)
})
{
Color::Indexed(i as u8)
} else {
Color::default()
}
best_color(blend(top, terminal_bg, 0.1))
}