arblang 0.1.24

Visualize and modify Unix pipelines: pipe a stream in, arb spawns a dynamic TUI
Documentation
//! Color themes — the 31 storageshower palettes shared by the sibling `iftoprs`
//! and `htoprs` HUD apps, ported verbatim, plus a user `custom` palette.
//!
//! A theme is a **6-color palette** of 256-color terminal indices
//! `(c1..c6) = (primary, accent, alt, mid, dim, bg)`, exactly as the sibling apps
//! store it. A [`Palette`] exposes those slots by role so arb's widgets recolor
//! as one system when the active `theme` changes; a widget's `-color <slot>`
//! (accent/primary/alt/mid/dim/bg) resolves through it. With no `theme` directive
//! the palette is `None` and color resolution stays exactly as before (cyan
//! default + the fixed named colors) — themes are purely additive.

use ratatui::style::Color;

/// A resolved 6-color theme palette (256-color terminal indices). `Copy` — 6
/// bytes, passed by value into the render functions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Palette {
    /// `(c1, c2, c3, c4, c5, c6)` = (primary, accent, alt, mid, dim, bg).
    pub c: [u8; 6],
}

impl Palette {
    /// `c2` — the bright accent (default widget border/fill, focused controls).
    pub fn accent(&self) -> Color {
        Color::Indexed(self.c[1])
    }
    /// `c1` — the primary label color.
    pub fn primary(&self) -> Color {
        Color::Indexed(self.c[0])
    }
    /// `c3` — a distinct alternate hue.
    pub fn alt(&self) -> Color {
        Color::Indexed(self.c[2])
    }
    /// `c4` — a mid tone.
    pub fn mid(&self) -> Color {
        Color::Indexed(self.c[3])
    }
    /// `c5` — a dim tone.
    pub fn dim(&self) -> Color {
        Color::Indexed(self.c[4])
    }
    /// `c6` — the darkest (backgrounds).
    pub fn bg(&self) -> Color {
        Color::Indexed(self.c[5])
    }
    /// Resolve a `-color <slot>` name to a palette color, if it names a slot.
    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,
        })
    }
}

/// The 31 built-in themes, in display order — `(kebab-name, [c1..c6])`. Palettes
/// are the storageshower values ported verbatim from `iftoprs`/`htoprs`.
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]),
];

/// Normalize a theme name for lenient matching: lowercase, drop `-`/`_`/space so
/// `neon-noir`, `NeonNoir`, `neon_noir` and `neonnoir` all resolve.
fn norm(s: &str) -> String {
    s.chars()
        .filter(|c| c.is_alphanumeric())
        .flat_map(char::to_lowercase)
        .collect()
}

/// Resolve a theme name to its palette (lenient matching); `None` if unknown.
pub fn by_name(name: &str) -> Option<Palette> {
    let want = norm(name);
    THEMES
        .iter()
        .find(|(n, _)| norm(n) == want)
        .map(|&(_, c)| Palette { c })
}

/// A `theme custom c1 c2 c3 c4 c5 c6` palette from six 256-color indices.
pub fn custom(c: [u8; 6]) -> Palette {
    Palette { c }
}

/// The default palette when a spec sets no `theme` — `neon-sprawl`, matching the
/// `#[default]` of the sibling `iftoprs`/`htoprs` apps. `theme off` opts out to
/// the classic cyan look (`Spec.theme = None`).
pub fn default_palette() -> Palette {
    Palette { c: THEMES[0].1 }
}

/// The baked default theme's kebab name (`neon-sprawl`).
pub fn default_name() -> &'static str {
    THEMES[0].0
}

/// Index of a palette in [`THEMES`] (for the `c`/Ctrl-T cycle start point); `0`
/// for a custom palette that matches no built-in.
pub fn index_of(p: Palette) -> usize {
    THEMES.iter().position(|&(_, c)| c == p.c).unwrap_or(0)
}

/// `~/.arb/config.toml` (honoring `HOME`; `ARB_LIB`-style relocation isn't used
/// for config).
fn config_path() -> Option<std::path::PathBuf> {
    std::env::var_os("HOME").map(|h| std::path::PathBuf::from(h).join(".arb/config.toml"))
}

/// The user's global default theme name from `~/.arb/config.toml` `[ui] theme`
/// (or a top-level `theme`), if set to a valid theme. `None` when unset/invalid
/// or `ARB_NO_CONFIG` is set — the caller then falls back to [`default_palette`].
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)
}

/// Persist `name` as the global default theme in `~/.arb/config.toml` `[ui]
/// theme` (used by `arb --set-theme NAME`). Rewrites an existing `theme =` line
/// in place or appends a `[ui]` section; validates the name first.
pub fn set_config_default(name: &str) -> Result<(), String> {
    if std::env::var_os("ARB_NO_CONFIG").is_some() {
        return Ok(()); // config writes disabled (tests / opt-out)
    }
    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()))
}

/// Rewrite the `theme =` line under `[ui]` (or append the section) to `name`.
fn upsert_ui_theme(existing: &str, name: &str) -> String {
    let line = format!("theme = \"{name}\"");
    // Replace an existing active `theme = ...` (commented or not) if present.
    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";
    }
    // Insert after an existing `[ui]` header, else append a new section.
    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
}

/// Every theme's kebab-name, in display order (for `--list-themes` / cycling).
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() {
        // neon-noir = [201, 231, 93, 219, 57, 53]
        let p = by_name("neon-noir").unwrap();
        assert_eq!(p.accent(), Color::Indexed(231)); // c2
        assert_eq!(p.primary(), Color::Indexed(201)); // c1
        assert_eq!(p.bg(), Color::Indexed(53)); // c6
        assert_eq!(p.slot("dim"), Some(Color::Indexed(57))); // c5
        assert_eq!(p.slot("bogus"), None);
    }

    #[test]
    fn upsert_ui_theme_appends_or_replaces() {
        // Empty config -> a new [ui] section.
        let out = upsert_ui_theme("", "neon-noir");
        assert!(out.contains("[ui]") && out.contains("theme = \"neon-noir\""));
        // An existing commented `# theme = ...` line is replaced in place.
        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"));
        // Existing [repl] section is preserved when appending [ui].
        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));
    }
}