fix: keep explorer selections readable on light terminals

This commit is contained in:
2026-10-23 11:59:06 +02:00
parent c12d98e348
commit 6b04b6f0ac
4 changed files with 94 additions and 5 deletions
+68
View File
@@ -32,6 +32,74 @@ impl Drop for Directory {
}
}
#[test]
fn explorer_selection_follows_light_background_and_restores_dark_styles() {
let directory = Directory::new();
directory.file("arguments.hon", "return 0\n");
let mut picker =
FilePicker::from_paths(directory.0.clone(), [(0, PathBuf::from("arguments.hon"))]);
let light = ThemeColors {
background: [255, 246, 230],
red: [177, 52, 37],
green: [82, 101, 31],
};
let dark = ThemeColors {
background: [31, 23, 20],
red: [224, 120, 100],
green: [169, 182, 101],
};
let mut views = BufferViewports::default();
let mut terminal = Terminal::new(ratatui::backend::TestBackend::new(70, 12)).unwrap();
for theme in [Some(light), Some(dark), None, Some(light)] {
views.set_theme(theme);
for focused in [false, true] {
picker.focused = focused;
terminal
.draw(|frame| {
draw_file_list(frame, &picker, frame.area(), "", &mut views.panels, None);
})
.unwrap();
let buffer = terminal.backend().buffer();
let cell = buffer
.content
.iter()
.find(|cell| cell.symbol() == "a" && cell.modifier.contains(Modifier::BOLD))
.expect("selected arguments.hon row");
if theme == Some(light) {
let Color::Rgb(r, g, b) = cell.bg else {
panic!("light selection must not use a dark ANSI background");
};
let luminance = |rgb: [u8; 3]| {
rgb.into_iter()
.zip([0.2126, 0.7152, 0.0722])
.map(|(c, w)| {
let c = f64::from(c) / 255.0;
w * if c <= 0.04045 {
c / 12.92
} else {
((c + 0.055) / 1.055).powf(2.4)
}
})
.sum::<f64>()
};
for foreground in [[69, 49, 38], [138, 98, 28], [153, 91, 43]] {
assert!((luminance([r, g, b]) + 0.05) / (luminance(foreground) + 0.05) >= 4.5);
}
assert_ne!([r, g, b], light.background, "selection must remain visible");
} else {
assert_eq!(
cell.bg,
if focused {
Color::Blue
} else {
Color::DarkGray
}
);
}
}
}
}
#[allow(clippy::too_many_arguments)]
fn rendered_whitespace_line(
source: &str,