//! Styles and symbols: Catppuccin Mocha, colour marks status only. //! `NO_COLOR` drops colours (modifiers stay); `HX_ASCII=1` swaps in ASCII symbols; //! without `COLORTERM=truecolor|24bit` colours map to the nearest xterm-256 entry. //! The background is never painted, so the terminal's own ground shows through. use ratatui::style::{Color, Modifier, Style}; use std::sync::OnceLock; /// Catppuccin Mocha. pub mod mocha { pub const TEXT: u32 = 0xcdd6f4; pub const SUBTEXT0: u32 = 0xa6adc8; pub const OVERLAY0: u32 = 0x6c7086; pub const SURFACE1: u32 = 0x45475a; pub const SURFACE0: u32 = 0x313244; pub const PEACH: u32 = 0xfab387; pub const YELLOW: u32 = 0xf9e2af; pub const GREEN: u32 = 0xa6e3a1; pub const RED: u32 = 0xf38ba8; pub const BLUE: u32 = 0x89b4fa; pub const MAUVE: u32 = 0xcba6f7; pub const TEAL: u32 = 0x94e2d5; /// Diff line backgrounds (green/red mixed into base). pub const ADD_BG: u32 = 0x2b3a33; pub const DEL_BG: u32 = 0x3d2a35; } pub struct Sym { pub user: &'static str, pub ok: &'static str, pub done: &'static str, pub err: &'static str, pub warn: &'static str, pub pending: &'static str, pub off: &'static str, pub more: &'static str, pub gutter: &'static str, pub bar_full: &'static str, pub bar_empty: &'static str, pub info: &'static str, pub ellipsis: &'static str, pub minus: &'static str, pub reset: &'static str, pub queued: &'static str, pub up: &'static str, pub down: &'static str, pub rule: &'static str, pub branch: &'static str, pub dirty: &'static str, pub vrule: &'static str, pub spin: &'static [&'static str], } const UNICODE: Sym = Sym { user: "❯", ok: "✓", done: "●", err: "✗", warn: "⚠", pending: "○", off: "○", more: "▸", gutter: "│", bar_full: "▰", bar_empty: "▱", info: "·", ellipsis: "…", minus: "−", reset: "↻", queued: "⏎", up: "↑", down: "↓", rule: "┈", branch: "⎇ ", dirty: "●", vrule: "│", spin: &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"], }; const ASCII: Sym = Sym { user: ">", ok: "v", done: "*", err: "x", warn: "!", pending: "o", off: "o", more: ">", gutter: "|", bar_full: "#", bar_empty: "-", info: "-", ellipsis: "...", minus: "-", reset: "in ", queued: ">>", up: "^", down: "v", rule: "~", branch: "@", dirty: "*", vrule: "|", spin: &["-", "\\", "|", "/"], }; pub struct Theme { color: bool, truecolor: bool, pub sym: &'static Sym, } /// Nearest xterm-256 colour (6x6x6 cube or grey ramp). fn to_256(rgb: u32) -> u8 { let (r, g, b) = ( (rgb >> 16) as i32 & 255, (rgb >> 8) as i32 & 255, rgb as i32 & 255, ); let lv = [0, 95, 135, 175, 215, 255]; let near = |v: i32| (0..6).min_by_key(|&i| (lv[i] - v).abs()).unwrap(); let (ri, gi, bi) = (near(r), near(g), near(b)); let cube = (lv[ri], lv[gi], lv[bi]); let avg = (r + g + b) / 3; let gi_ = ((avg - 8).max(0) / 10).min(23); let grey = 8 + gi_ * 10; let d = |(x, y, z): (i32, i32, i32)| (x - r).pow(2) + (y - g).pow(2) + (z - b).pow(2); if d((grey, grey, grey)) < d(cube) { 232 + gi_ as u8 } else { 16 + (36 * ri + 6 * gi + bi) as u8 } } impl Theme { pub fn new(color: bool, ascii: bool) -> Self { Self::with(color, true, ascii) } pub fn with(color: bool, truecolor: bool, ascii: bool) -> Self { Self { color, truecolor, sym: if ascii { &ASCII } else { &UNICODE }, } } pub fn ascii(&self) -> bool { std::ptr::eq(self.sym, &ASCII) } pub fn rgb(&self, c: u32) -> Color { if self.truecolor { Color::Rgb((c >> 16) as u8, (c >> 8) as u8, c as u8) } else { Color::Indexed(to_256(c)) } } fn fg(&self, c: u32) -> Style { if self.color { Style::default().fg(self.rgb(c)) } else { Style::default() } } fn bg(&self, s: Style, c: u32) -> Style { if self.color { s.bg(self.rgb(c)) } else { s } } /// Body text. pub fn plain(&self) -> Style { self.fg(mocha::TEXT) } /// Secondary: paths, metadata, assistant-adjacent detail. pub fn sec(&self) -> Style { self.fg(mocha::SUBTEXT0) } /// Prompt, focus, working. pub fn accent(&self) -> Style { self.fg(mocha::PEACH) } /// Decoration only: rules, hints. pub fn dim(&self) -> Style { if self.color { self.fg(mocha::OVERLAY0) } else { Style::default().add_modifier(Modifier::DIM) } } pub fn ok(&self) -> Style { self.fg(mocha::GREEN) } pub fn warn(&self) -> Style { self.fg(mocha::YELLOW) } pub fn err(&self) -> Style { self.fg(mocha::RED) } /// Links, other-origin tags. pub fn link(&self) -> Style { self.fg(mocha::BLUE) } pub fn add(&self) -> Style { self.bg(self.fg(mocha::GREEN), mocha::ADD_BG) } pub fn del(&self) -> Style { self.bg(self.fg(mocha::RED), mocha::DEL_BG) } pub fn hunk(&self) -> Style { self.fg(mocha::MAUVE) } pub fn code(&self) -> Style { self.fg(mocha::TEAL) } pub fn block_code(&self) -> Style { self.fg(mocha::TEAL) } /// Selected row in lists: a band, text colours kept. pub fn sel(&self) -> Style { if self.color { self.bg(self.plain(), mocha::SURFACE0) } else { Style::default().add_modifier(Modifier::REVERSED) } } /// Border of boxes that carry input. pub fn frame(&self) -> Style { self.fg(mocha::SURFACE1) } /// `⠋`… spinner frame. pub fn spin(&self, frame: usize) -> &'static str { self.sym.spin[frame % self.sym.spin.len()] } /// `┈ label ┈┈┈` to `width` columns. pub fn rule(&self, label: &str, width: usize) -> String { let head = if label.is_empty() { String::new() } else { format!("{} {label} ", self.sym.rule) }; let n = width.saturating_sub(unicode_w(&head)); format!("{head}{}", self.sym.rule.repeat(n)) } } fn unicode_w(s: &str) -> usize { ratatui::text::Span::raw(s).width() } /// The process-wide theme, read from the environment once. pub fn t() -> &'static Theme { static T: OnceLock = OnceLock::new(); T.get_or_init(|| { let set = |k| std::env::var_os(k).is_some_and(|v| !v.is_empty() && v != "0"); let tc = std::env::var("COLORTERM").is_ok_and(|v| v == "truecolor" || v == "24bit"); Theme::with(!set("NO_COLOR"), tc, set("HX_ASCII")) }) } #[cfg(test)] mod tests { use super::*; #[test] fn fallbacks() { let c = Theme::new(true, false); assert_eq!(c.err().fg, Some(Color::Rgb(0xf3, 0x8b, 0xa8))); assert_eq!(c.sym.done, "●"); let n = Theme::new(false, true); assert_eq!(n.err().fg, None); assert!(n.dim().add_modifier.contains(Modifier::DIM)); assert!(n.sel().add_modifier.contains(Modifier::REVERSED)); assert_eq!((n.sym.ok, n.sym.user, n.sym.warn), ("v", ">", "!")); assert!(n.ascii() && !c.ascii()); assert_eq!(c.rule("lens", 10), "┈ lens ┈┈┈"); } #[test] fn indexed_fallback() { assert_eq!(to_256(0x000000), 16); assert_eq!(to_256(0xffffff), 231); assert_eq!(to_256(0xff0000), 196); assert_eq!(to_256(0x303030), 236); let t = Theme::with(true, false, false); assert!(matches!(t.ok().fg, Some(Color::Indexed(_)))); } }