Files
hx/crates/hx-tui/src/theme.rs
T
hermes b0b22b4061
ci / check (push) Canceled after 0s
ci / check (pull_request) Canceled after 0s
M9: theme, turn layout, footer, input, activity line
Closes #76, closes #77, closes #79, closes #80, closes #81
2026-10-01 08:56:27 +00:00

143 lines
3.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Styles and symbols. `NO_COLOR` drops colours (modifiers stay); `HX_ASCII=1` swaps in ASCII symbols.
use ratatui::style::{Color, Modifier, Style};
use std::sync::OnceLock;
pub struct Sym {
pub user: &'static str,
pub asst: &'static str,
pub ok: &'static str,
pub err: &'static str,
pub pending: &'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,
}
const UNICODE: Sym = Sym {
user: "❯",
asst: "◆",
ok: "✓",
err: "✗",
pending: "○",
more: "▸",
gutter: "│",
bar_full: "▰",
bar_empty: "▱",
info: "·",
ellipsis: "…",
minus: "−",
};
const ASCII: Sym = Sym {
user: ">",
asst: "*",
ok: "+",
err: "x",
pending: "o",
more: ">",
gutter: "|",
bar_full: "#",
bar_empty: "-",
info: "-",
ellipsis: "...",
minus: "-",
};
pub struct Theme {
color: bool,
pub sym: &'static Sym,
}
impl Theme {
pub fn new(color: bool, ascii: bool) -> Self {
Self {
color,
sym: if ascii { &ASCII } else { &UNICODE },
}
}
fn fg(&self, c: Color) -> Style {
if self.color {
Style::default().fg(c)
} else {
Style::default()
}
}
pub fn plain(&self) -> Style {
Style::default()
}
pub fn accent(&self) -> Style {
self.fg(Color::Cyan)
}
pub fn dim(&self) -> Style {
if self.color {
self.fg(Color::DarkGray)
} else {
Style::default().add_modifier(Modifier::DIM)
}
}
pub fn ok(&self) -> Style {
self.fg(Color::Green)
}
pub fn warn(&self) -> Style {
self.fg(Color::Yellow)
}
pub fn err(&self) -> Style {
self.fg(Color::Red)
}
pub fn add(&self) -> Style {
self.fg(Color::Green)
}
pub fn del(&self) -> Style {
self.fg(Color::Red)
}
pub fn hunk(&self) -> Style {
self.fg(Color::Cyan)
}
pub fn code(&self) -> Style {
self.fg(Color::Yellow)
}
pub fn block_code(&self) -> Style {
self.fg(Color::Cyan)
}
/// Selected row in lists.
pub fn sel(&self) -> Style {
if self.color {
Style::default().bg(Color::DarkGray).fg(Color::White)
} else {
Style::default().add_modifier(Modifier::REVERSED)
}
}
}
/// The process-wide theme, read from the environment once.
pub fn t() -> &'static Theme {
static T: OnceLock<Theme> = OnceLock::new();
T.get_or_init(|| {
let set = |k| std::env::var_os(k).is_some_and(|v| !v.is_empty() && v != "0");
Theme::new(!set("NO_COLOR"), 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::Red));
assert_eq!(c.sym.ok, "✓");
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), ("+", ">"));
}
}