Lines
99.65 %
Functions
100 %
Branches
use super::*;
const MINIMAL: &str = "
# a comment
menu = black on lightgray
menu-selected = black on cyan
menu-hotkey = red on lightgray
menu-selected-hotkey = red on cyan
menu-disabled = darkgray on lightgray
menu-selected-disabled = darkgray on cyan
panel = lightcyan on darkgray
panel-frame = lightcyan on darkgray
panel-frame-passive = lightgray on darkgray
panel-title = black on cyan
panel-header = yellow on darkgray bold underline
panel-cursor = black on cyan
panel-dim = lightgray on darkgray
frame-icon = lightgreen on darkgray
desktop = darkgray on black
scrollbar = lightcyan on darkgray
scrollbar-thumb = black on cyan
status = black on lightgray
status-hotkey = red on lightgray
dialog = black on lightgray
dialog-frame = black on lightgray
dialog-title = white on lightgray
dialog-icon = lightgreen on lightgray
dialog-focus = white on lightblue reverse
label = black on lightgray
label-lit = white on lightgray
button = lightcyan on magenta
button-default = white on brown
button-hotkey = lightmagenta on magenta
button-focus = white on lightred
button-shadow = black on lightgray
input-cursor = black on cyan
input = white on black
input-focused = white on lightblue
input-arrow = lightgreen on lightblue
history = green on lightgray
history-arrow = black on green
prompt = white on darkgray bold
list = black on cyan
list-selected = white on black
shadow = darkgray on black
";
fn without(role: &str) -> String {
MINIMAL
.lines()
.filter(|line| !line.starts_with(&format!("{role} =")))
.collect::<Vec<_>>()
.join("\n")
}
fn error_of(text: &str) -> ParseError {
Theme::parse(text).expect_err("the text must not parse")
#[test]
fn cga_names_map_to_the_exact_rgb_values() {
let table = [
("black", 0x000000),
("blue", 0x0000AA),
("green", 0x00AA00),
("cyan", 0x00AAAA),
("red", 0xAA0000),
("magenta", 0xAA00AA),
("brown", 0xAA5500),
("lightgray", 0xAAAAAA),
("darkgray", 0x555555),
("lightblue", 0x5555FF),
("lightgreen", 0x55FF55),
("lightcyan", 0x55FFFF),
("lightred", 0xFF5555),
("lightmagenta", 0xFF55FF),
("yellow", 0xFFFF55),
("white", 0xFFFFFF),
];
for (name, rgb) in table {
let cga = Cga::from_name(name).expect(name);
assert_eq!(cga.color(ColorDepth::Rgb), Color::from_u32(rgb), "{name}");
assert_eq!(Cga::from_name("grey"), None);
fn roles_are_listed_in_declaration_order() {
for (index, role) in Role::ALL.iter().enumerate() {
assert_eq!(role.index(), index, "{role:?}");
assert_eq!(Role::from_name(role.name()), Some(*role));
fn a_valid_file_gives_each_role_its_style() {
let theme = Theme::parse(MINIMAL).expect("valid");
let header = theme.style(Role::PanelHeader);
assert_eq!(header.fg, Some(Cga::Yellow.color(ColorDepth::Rgb)));
assert_eq!(header.bg, Some(Cga::DarkGray.color(ColorDepth::Rgb)));
assert_eq!(header.add_modifier, Modifier::BOLD | Modifier::UNDERLINED);
assert_eq!(
theme.style(Role::DialogFocus).add_modifier,
Modifier::REVERSED
);
assert_eq!(theme.style(Role::Menu).add_modifier, Modifier::empty());
assert_eq!(theme.bg(Role::Panel), Cga::DarkGray.color(ColorDepth::Rgb));
fn the_dn_preset_maps_every_role_to_the_dos_navigator_colours() {
use Cga::{
Black, Brown, Cyan, DarkGray, Green, LightBlue, LightCyan, LightGray, LightGreen,
LightMagenta, LightRed, Magenta, White, Yellow,
};
let bold = Modifier::BOLD;
let none = Modifier::empty();
let expected = [
(Role::Menu, Black, LightGray, none),
(Role::MenuSelected, Black, Cyan, none),
(Role::MenuHotkey, Magenta, LightGray, none),
(Role::MenuSelectedHotkey, Magenta, Cyan, none),
(Role::MenuDisabled, DarkGray, LightGray, none),
(Role::MenuSelectedDisabled, DarkGray, Cyan, none),
(Role::Panel, LightCyan, DarkGray, none),
(Role::PanelFrame, LightCyan, DarkGray, none),
(Role::PanelFramePassive, LightGray, DarkGray, none),
(Role::PanelTitle, Black, Cyan, none),
(Role::PanelHeader, Yellow, DarkGray, bold),
(Role::PanelCursor, Black, Cyan, none),
(Role::PanelDim, LightGray, DarkGray, none),
(Role::FrameIcon, LightGreen, DarkGray, none),
(Role::Desktop, DarkGray, Black, none),
(Role::Scrollbar, LightCyan, DarkGray, none),
(Role::ScrollbarThumb, Black, Cyan, none),
(Role::Status, Black, LightGray, none),
(Role::StatusHotkey, Magenta, LightGray, none),
(Role::Dialog, Black, LightGray, none),
(Role::DialogFrame, White, LightGray, none),
(Role::DialogTitle, White, LightGray, none),
(Role::DialogIcon, LightGreen, LightGray, none),
(Role::DialogFocus, White, LightBlue, none),
(Role::Label, Black, LightGray, none),
(Role::LabelLit, White, LightGray, none),
(Role::Button, LightCyan, Magenta, none),
(Role::ButtonDefault, White, Brown, none),
(Role::ButtonHotkey, LightMagenta, Magenta, none),
(Role::ButtonFocus, White, LightRed, none),
(Role::ButtonShadow, Black, LightGray, none),
(Role::InputCursor, Black, Cyan, none),
(Role::Input, White, Black, none),
(Role::InputFocused, White, LightBlue, none),
(Role::InputArrow, LightGreen, LightBlue, none),
(Role::History, Green, LightGray, none),
(Role::HistoryArrow, Black, Green, none),
(Role::Prompt, White, DarkGray, bold),
(Role::List, Black, Cyan, none),
(Role::ListSelected, White, Black, none),
(Role::Shadow, DarkGray, Black, none),
assert_eq!(expected.len(), Role::ALL.len());
let theme = Theme::preset("dn").expect("the preset parses");
for (role, fg, bg, modifiers) in expected {
let style = theme.style(role);
assert_eq!(style.fg, Some(fg.color(ColorDepth::Rgb)), "{role:?} fg");
assert_eq!(style.bg, Some(bg.color(ColorDepth::Rgb)), "{role:?} bg");
assert_eq!(style.add_modifier, modifiers, "{role:?} modifiers");
assert_eq!(theme.bg(role), bg.color(ColorDepth::Rgb), "{role:?} bg()");
fn errors_carry_the_line_number() {
let err = error_of(&format!("{MINIMAL}\nmenu-selected = black on cyan"));
assert_eq!(err.line, Some(45));
assert!(
err.message.contains("duplicate role `menu-selected`"),
"{err}"
let err = error_of("# one\nmystery = black on cyan");
assert_eq!(err.line, Some(2));
assert!(err.message.contains("unknown role `mystery`"), "{err}");
let err = error_of("menu = black on pink");
assert_eq!(err.line, Some(1));
assert!(err.message.contains("unknown colour `pink`"), "{err}");
let err = error_of("\n\nmenu black on cyan");
assert_eq!(err.line, Some(3));
let err = error_of("menu = black cyan");
assert!(err.message.contains("`on`"), "{err}");
let err = error_of("menu = black on");
let err = error_of("menu = black on cyan blink");
assert!(err.message.contains("unknown modifier `blink`"), "{err}");
fn a_missing_role_is_named_without_a_line() {
let err = error_of(&without("status"));
assert_eq!(err.line, None);
assert!(err.message.contains("missing role `status`"), "{err}");
fn the_error_display_names_the_line() {
error_of("menu = black on pink").to_string(),
"line 1: unknown colour `pink`"
error_of(&without("menu")).to_string(),
"missing role `menu`"
fn detect_reads_colorterm_and_term() {
use ColorDepth::{Ansi16, Indexed256, Rgb};
let cases = [
(Some("xterm"), Some("truecolor"), Rgb),
(Some("xterm"), Some("24BIT"), Rgb),
(None, Some("TrueColor"), Rgb),
(Some("xterm-kitty"), None, Rgb),
(Some("ghostty"), None, Rgb),
(Some("wezterm"), None, Rgb),
(Some("foot"), None, Rgb),
(Some("alacritty"), None, Rgb),
(Some("ALACRITTY"), None, Rgb),
(Some("eat-truecolor"), None, Rgb),
(Some("xterm-truecolor"), None, Rgb),
(Some("xterm-direct"), None, Rgb),
(Some("xterm-256color"), None, Indexed256),
(Some("XTERM-256COLOR"), Some("other"), Indexed256),
(Some("screen"), None, Ansi16),
(Some("linux"), None, Ansi16),
(Some("vt100"), None, Ansi16),
(None, None, Ansi16),
for (term, colorterm, depth) in cases {
ColorDepth::detect(term, colorterm),
depth,
"{term:?} {colorterm:?}"
fn xterm_rgb(index: u8) -> (i32, i32, i32) {
const LEVELS: [i32; 6] = [0, 95, 135, 175, 215, 255];
let n = i32::from(index);
if n >= 232 {
let grey = 8 + 10 * (n - 232);
return (grey, grey, grey);
let cube = n - 16;
(
LEVELS[(cube / 36) as usize],
LEVELS[(cube / 6 % 6) as usize],
LEVELS[(cube % 6) as usize],
)
fn each_cga_colour_maps_to_its_nearest_cube_or_grey_index() {
for cga in Cga::ALL {
let Color::Rgb(r, g, b) = cga.color(ColorDepth::Rgb) else {
panic!("{cga:?} is not RGB");
let distance = |index: u8| {
let (pr, pg, pb) = xterm_rgb(index);
(pr - i32::from(r)).pow(2) + (pg - i32::from(g)).pow(2) + (pb - i32::from(b)).pow(2)
let nearest = (16..=255u8)
.min_by_key(|index| distance(*index))
.expect("indices");
cga.color(ColorDepth::Indexed256),
Color::Indexed(nearest),
"{cga:?}"
fn ansi16_follows_the_ansi_order_not_the_cga_order() {
Color::Black,
Color::Blue,
Color::Green,
Color::Cyan,
Color::Red,
Color::Magenta,
Color::Yellow,
Color::Gray,
Color::DarkGray,
Color::LightBlue,
Color::LightGreen,
Color::LightCyan,
Color::LightRed,
Color::LightMagenta,
Color::LightYellow,
Color::White,
for (cga, color) in Cga::ALL.into_iter().zip(expected) {
assert_eq!(cga.color(ColorDepth::Ansi16), color, "{cga:?}");
fn ansi16_backgrounds_fold_bright_colours_to_dark() {
let dark = match cga {
Cga::DarkGray => Cga::Black,
Cga::LightBlue => Cga::Blue,
Cga::LightGreen => Cga::Green,
Cga::LightCyan => Cga::Cyan,
Cga::LightRed => Cga::Red,
Cga::LightMagenta => Cga::Magenta,
Cga::Yellow => Cga::Brown,
Cga::White => Cga::LightGray,
other => other,
cga.background(ColorDepth::Ansi16),
dark.color(ColorDepth::Ansi16),
Cga::White.background(ColorDepth::Rgb),
Cga::White.color(ColorDepth::Rgb)
fn a_theme_renders_each_role_at_its_depth() {
let focus = Role::DialogFocus;
let rgb = theme.style(focus);
assert_eq!(rgb.fg, Some(Color::Rgb(255, 255, 255)));
assert_eq!(rgb.bg, Some(Color::Rgb(0x55, 0x55, 0xFF)));
let indexed = theme.clone().with_depth(ColorDepth::Indexed256);
assert_eq!(indexed.style(focus).fg, Some(Color::Indexed(231)));
assert_eq!(indexed.style(focus).bg, Some(Color::Indexed(63)));
assert_eq!(indexed.bg(focus), Color::Indexed(63));
let ansi = theme.with_depth(ColorDepth::Ansi16);
assert_eq!(ansi.style(focus).fg, Some(Color::White));
assert_eq!(ansi.style(focus).bg, Some(Color::Blue));
assert_eq!(ansi.style(Role::Panel).bg, Some(Color::Black));
assert_eq!(ansi.bg(Role::Panel), Color::Black);
fn shown_rgb(color: Color) -> [u8; 3] {
let channel = |v: i32| u8::try_from(v).expect("a channel");
match color {
Color::Rgb(r, g, b) => [r, g, b],
Color::Indexed(index) => {
let (r, g, b) = xterm_rgb(index);
[channel(r), channel(g), channel(b)]
named => Cga::ALL
.into_iter()
.find(|cga| cga.color(ColorDepth::Ansi16) == named)
.expect("an ANSI colour")
.rgb(),
const DEPTHS: [ColorDepth; 3] = [ColorDepth::Rgb, ColorDepth::Indexed256, ColorDepth::Ansi16];
fn bg_rgb_is_the_colour_the_cells_show_at_every_depth() {
for name in ["dn", "turbo"] {
for depth in DEPTHS {
let theme = Theme::preset(name)
.expect("the preset parses")
.with_depth(depth);
for role in Role::ALL {
theme.bg_rgb(role),
shown_rgb(theme.bg(role)),
"{name} {depth:?} {role:?}"
fn the_dn_panel_is_black_in_the_picture_at_ansi16() {
let dn = Theme::preset("dn").expect("the preset parses");
assert_eq!(dn.bg_rgb(Role::Panel), [0x55; 3]);
dn.with_depth(ColorDepth::Ansi16).bg_rgb(Role::Panel),
[0, 0, 0]
fn preset_names_are_the_presets_that_parse() {
let names: Vec<_> = Theme::preset_names().collect();
assert_eq!(names, ["dn", "turbo"]);
for name in names {
assert!(Theme::preset(name).is_ok(), "{name}");
fn a_depth_name_parses_back_to_its_depth() {
for depth in [ColorDepth::Rgb, ColorDepth::Indexed256, ColorDepth::Ansi16] {
assert_eq!(depth.name().parse(), Ok(depth));