use ratatui::style::Color;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Palette {
pub c: [u8; 6],
}
impl Palette {
pub fn accent(&self) -> Color {
Color::Indexed(self.c[1])
}
pub fn primary(&self) -> Color {
Color::Indexed(self.c[0])
}
pub fn alt(&self) -> Color {
Color::Indexed(self.c[2])
}
pub fn mid(&self) -> Color {
Color::Indexed(self.c[3])
}
pub fn dim(&self) -> Color {
Color::Indexed(self.c[4])
}
pub fn bg(&self) -> Color {
Color::Indexed(self.c[5])
}
pub fn slot(&self, name: &str) -> Option<Color> {
Some(match name {
"accent" => self.accent(),
"primary" => self.primary(),
"alt" => self.alt(),
"mid" => self.mid(),
"dim" => self.dim(),
"bg" | "dark" | "background" => self.bg(),
_ => return None,
})
}
}
pub const THEMES: &[(&str, [u8; 6])] = &[
("neon-sprawl", [27, 48, 135, 141, 63, 99]),
("acid-rain", [28, 46, 34, 40, 22, 35]),
("ice-breaker", [19, 39, 25, 33, 21, 32]),
("synth-wave", [91, 177, 128, 134, 93, 97]),
("rust-belt", [172, 214, 178, 220, 166, 130]),
("ghost-wire", [37, 50, 44, 87, 30, 23]),
("red-sector", [160, 203, 196, 210, 124, 88]),
("sakura-den", [175, 218, 182, 225, 169, 132]),
("data-stream", [22, 46, 28, 119, 34, 22]),
("solar-flare", [202, 220, 196, 213, 160, 125]),
("neon-noir", [201, 231, 93, 219, 57, 53]),
("chrome-heart", [250, 255, 246, 253, 243, 239]),
("blade-runner", [208, 37, 166, 73, 130, 23]),
("void-walker", [55, 99, 54, 141, 92, 17]),
("toxic-waste", [118, 190, 154, 226, 82, 58]),
("cyber-frost", [159, 195, 153, 189, 111, 67]),
("plasma-core", [199, 213, 163, 207, 126, 89]),
("steel-nerve", [68, 110, 60, 146, 24, 236]),
("dark-signal", [30, 43, 23, 79, 29, 16]),
("glitch-pop", [201, 51, 226, 47, 196, 21]),
("holo-shift", [123, 219, 159, 183, 87, 133]),
("night-city", [214, 227, 209, 223, 172, 94]),
("deep-net", [19, 33, 17, 75, 26, 16]),
("laser-grid", [46, 201, 51, 226, 196, 21]),
("quantum-flux", [135, 75, 171, 111, 98, 61]),
("bio-hazard", [148, 184, 106, 192, 64, 22]),
("darkwave", [53, 140, 89, 176, 127, 52]),
("overlock", [196, 208, 160, 214, 124, 52]),
("megacorp", [252, 39, 245, 81, 242, 236]),
("zaibatsu", [167, 216, 131, 224, 95, 52]),
("iftopcolor", [21, 46, 28, 48, 33, 19]),
];
fn norm(s: &str) -> String {
s.chars()
.filter(|c| c.is_alphanumeric())
.flat_map(char::to_lowercase)
.collect()
}
pub fn by_name(name: &str) -> Option<Palette> {
let want = norm(name);
THEMES
.iter()
.find(|(n, _)| norm(n) == want)
.map(|&(_, c)| Palette { c })
}
pub fn custom(c: [u8; 6]) -> Palette {
Palette { c }
}
pub fn default_palette() -> Palette {
Palette { c: THEMES[0].1 }
}
pub fn default_name() -> &'static str {
THEMES[0].0
}
pub fn index_of(p: Palette) -> usize {
THEMES.iter().position(|&(_, c)| c == p.c).unwrap_or(0)
}
fn config_path() -> Option<std::path::PathBuf> {
std::env::var_os("HOME").map(|h| std::path::PathBuf::from(h).join(".arb/config.toml"))
}
pub fn config_default() -> Option<Palette> {
if std::env::var_os("ARB_NO_CONFIG").is_some() {
return None;
}
let text = std::fs::read_to_string(config_path()?).ok()?;
let v: toml::Value = text.parse().ok()?;
let name = v
.get("ui")
.and_then(|u| u.get("theme"))
.and_then(|t| t.as_str())
.or_else(|| v.get("theme").and_then(|t| t.as_str()))?;
by_name(name)
}
pub fn set_config_default(name: &str) -> Result<(), String> {
if std::env::var_os("ARB_NO_CONFIG").is_some() {
return Ok(()); }
let canon = THEMES
.iter()
.find(|(n, _)| norm(n) == norm(name))
.map(|&(n, _)| n)
.ok_or_else(|| format!("unknown theme `{name}` (see `arb --list-themes`)"))?;
let path = config_path().ok_or("no HOME for ~/.arb/config.toml")?;
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let existing = std::fs::read_to_string(&path).unwrap_or_default();
let out = upsert_ui_theme(&existing, canon);
std::fs::write(&path, out).map_err(|e| format!("{}: {e}", path.display()))
}
fn upsert_ui_theme(existing: &str, name: &str) -> String {
let line = format!("theme = \"{name}\"");
let mut lines: Vec<String> = existing.lines().map(String::from).collect();
if let Some(i) = lines.iter().position(|l| {
l.trim_start()
.trim_start_matches('#')
.trim_start()
.starts_with("theme =")
}) {
lines[i] = line;
return lines.join("\n") + "\n";
}
if let Some(i) = lines.iter().position(|l| l.trim() == "[ui]") {
lines.insert(i + 1, line);
return lines.join("\n") + "\n";
}
let mut out = existing.trim_end().to_string();
if !out.is_empty() {
out.push('\n');
}
out.push_str(&format!("\n[ui]\n{line}\n"));
out
}
pub fn names() -> impl Iterator<Item = &'static str> {
THEMES.iter().map(|&(n, _)| n)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn thirty_one_themes_all_distinct() {
assert_eq!(THEMES.len(), 31);
let mut seen = std::collections::HashSet::new();
for (n, _) in THEMES {
assert!(seen.insert(*n), "duplicate theme name {n}");
}
}
#[test]
fn lenient_name_matching() {
let p = by_name("neon-noir").unwrap();
assert_eq!(by_name("NeonNoir"), Some(p));
assert_eq!(by_name("neon_noir"), Some(p));
assert_eq!(by_name("neonnoir"), Some(p));
assert_eq!(by_name("NEON NOIR"), Some(p));
assert_eq!(by_name("nope"), None);
}
#[test]
fn palette_slots_map_to_indices() {
let p = by_name("neon-noir").unwrap();
assert_eq!(p.accent(), Color::Indexed(231)); assert_eq!(p.primary(), Color::Indexed(201)); assert_eq!(p.bg(), Color::Indexed(53)); assert_eq!(p.slot("dim"), Some(Color::Indexed(57))); assert_eq!(p.slot("bogus"), None);
}
#[test]
fn upsert_ui_theme_appends_or_replaces() {
let out = upsert_ui_theme("", "neon-noir");
assert!(out.contains("[ui]") && out.contains("theme = \"neon-noir\""));
let cfg = "[ui]\n# theme = \"neon-sprawl\"\n";
let out = upsert_ui_theme(cfg, "blade-runner");
assert!(out.contains("theme = \"blade-runner\""));
assert!(!out.contains("neon-sprawl"));
let out = upsert_ui_theme("[repl]\nmode = \"vi\"\n", "megacorp");
assert!(out.contains("[repl]") && out.contains("mode = \"vi\""));
assert!(out.contains("[ui]") && out.contains("megacorp"));
}
#[test]
fn default_name_is_neon_sprawl() {
assert_eq!(default_name(), "neon-sprawl");
assert_eq!(default_palette(), by_name("neon-sprawl").unwrap());
}
#[test]
fn custom_palette_from_six_indices() {
let p = custom([1, 2, 3, 4, 5, 6]);
assert_eq!(p.accent(), Color::Indexed(2));
assert_eq!(p.bg(), Color::Indexed(6));
}
}