use irodori::{Color, NordPalette, NORD};
use katsuji::{Attr, Crisp, Ink, Line, Piece};
pub const SHIFT: [Ink; 4] = [Ink::BrightCyan, Ink::Cyan, Ink::BrightBlue, Ink::Blue];
pub const CELL: Crisp = Crisp::Full;
#[must_use]
pub fn mark() -> String {
core::iter::repeat_n(CELL.ch(), SHIFT.len()).collect()
}
#[must_use]
pub fn mark_line() -> Line {
SHIFT
.iter()
.fold(Line::new(), |l, ink| l.piece(Piece::glyph(CELL).ink(*ink)))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Theme {
pub primary: Color,
pub accent: Color,
pub background: Color,
pub foreground: Color,
pub dim: Color,
}
const NORD_PRIMARY: usize = 10;
const NORD_ACCENT: usize = 8;
const NORD_BACKGROUND: usize = 0;
const NORD_FOREGROUND: usize = 6;
const NORD_DIM: usize = 3;
impl Theme {
#[must_use]
pub fn from_palette(p: &NordPalette) -> Self {
let at = |i: usize| {
p.get(i)
.or_else(|| NORD.get(i))
.unwrap_or(Color::new(0, 0, 0))
};
Self {
primary: at(NORD_PRIMARY),
accent: at(NORD_ACCENT),
background: at(NORD_BACKGROUND),
foreground: at(NORD_FOREGROUND),
dim: at(NORD_DIM),
}
}
#[must_use]
pub fn fleet() -> Self {
Self::from_palette(&NORD)
}
}
impl Default for Theme {
fn default() -> Self {
Self::fleet()
}
}
#[must_use]
pub fn wordmark(version: &str) -> Vec<Line> {
vec![
SHIFT
.iter()
.fold(Line::new(), |l, ink| l.piece(Piece::glyph(CELL).ink(*ink)))
.piece(
Piece::text(" blue ")
.ink(Ink::BrightWhite)
.attr(Attr::Bold),
)
.piece(Piece::text(version).ink(Ink::BrightBlack)),
Line::new().piece(Piece::glyphs(Crisp::Horizontal, 20).ink(Ink::BrightBlack)),
Line::new().piece(
Piece::text("a Ruby/Elixir surface on tatara-lisp and Rust").ink(Ink::BrightBlack),
),
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_mark_is_four_solid_cells() {
assert_eq!(mark(), "████");
}
#[test]
fn the_mark_uses_no_dither_glyph() {
for bad in [Crisp::ShadeLight, Crisp::ShadeMedium, Crisp::ShadeDark] {
assert!(
!mark().contains(bad.ch()),
"{bad:?} reads as fuzz at cell size — the mark must be solid"
);
}
}
#[test]
fn every_cell_of_the_shift_is_a_different_ink() {
let mut seen: Vec<Ink> = Vec::new();
for ink in SHIFT {
assert!(
!seen.contains(&ink),
"{ink:?} repeats — a shift with a repeated colour is not shifting"
);
seen.push(ink);
}
assert_eq!(seen.len(), 4);
}
#[test]
fn the_shift_walks_the_frost_band_toward_blue() {
assert_eq!(
SHIFT,
[Ink::BrightCyan, Ink::Cyan, Ink::BrightBlue, Ink::Blue],
"the direction is the meaning: toward blue, not away from it"
);
assert_eq!(
*SHIFT.last().expect("non-empty"),
Ink::Blue,
"it ends on blue"
);
}
#[test]
fn the_mark_emits_no_truecolor_escape() {
let rendered = mark_line().render();
assert!(
!rendered.contains("38;2;"),
"a truecolor escape would stop tracking the reader's theme: {rendered:?}"
);
}
#[test]
fn the_styled_mark_paints_four_runs() {
let rendered = mark_line().render();
for ink in SHIFT {
let param = match ink {
Ink::BrightCyan => "96",
Ink::Cyan => "36",
Ink::BrightBlue => "94",
Ink::Blue => "34",
other => panic!("unexpected ink in SHIFT: {other:?}"),
};
assert!(
rendered.contains(param),
"expected SGR {param} for {ink:?} in {rendered:?}"
);
}
}
#[test]
fn the_primary_is_nord10_deep_blue() {
let t = Theme::fleet();
assert_eq!(t.primary, Color::new(0x5E, 0x81, 0xAC), "nord10");
}
#[test]
fn the_accent_is_the_fleets_nord8_frost_cyan() {
let t = Theme::fleet();
assert_eq!(t.accent, Color::new(0x88, 0xC0, 0xD0), "nord8");
assert_ne!(
t.primary, t.accent,
"identity and interaction are different roles and must be different colours"
);
}
#[test]
fn every_theme_colour_comes_from_the_nord_palette() {
let t = Theme::fleet();
let nord: Vec<Color> = (0..16).filter_map(|i| NORD.get(i)).collect();
for (name, c) in [
("primary", t.primary),
("accent", t.accent),
("background", t.background),
("foreground", t.foreground),
("dim", t.dim),
] {
assert!(
nord.contains(&c),
"{name} = {c:?} is not a Nord colour — blue must not carry its own hex"
);
}
}
#[test]
fn the_theme_follows_a_customised_palette() {
let mut custom = NORD;
custom.frost[3] = Color::new(1, 2, 3);
assert_eq!(
Theme::from_palette(&custom).primary,
Color::new(1, 2, 3),
"a consumer's palette must drive the value; blue supplies the role"
);
}
#[test]
fn the_wordmark_carries_the_name_the_mark_and_the_version() {
let lines = wordmark("9.9.9");
let rendered: String = lines.iter().map(Line::render).collect();
assert!(rendered.contains("blue"), "the name: {rendered:?}");
assert!(rendered.contains("9.9.9"), "the version: {rendered:?}");
assert!(
rendered.contains(CELL.ch()),
"the mark must appear: {rendered:?}"
);
}
#[test]
fn the_wordmark_takes_its_version_from_the_caller() {
let a: String = wordmark("1.2.3").iter().map(Line::render).collect();
let b: String = wordmark("4.5.6").iter().map(Line::render).collect();
assert!(a.contains("1.2.3") && !a.contains("4.5.6"));
assert!(b.contains("4.5.6") && !b.contains("1.2.3"));
}
}