paperos 0.2.0

A programmable editor built on the Runact actor runtime
Documentation
//! Theme: named colors for every UI element, customizable via
//! `paper.theme.set(name, color)` (stored as `theme.<name>` config keys).
//!
//! Defaults match the classic paperOS look: yellow cursor in Normal/Visual,
//! white cursor in Insert.

use ratatui::style::Color;
use std::collections::HashMap;

#[allow(dead_code)]
#[derive(Clone, Debug, PartialEq)]
pub struct Theme {
    /// Cursor block in Normal/Visual mode.
    pub cursor_normal: Color,
    /// Cursor block in Insert mode.
    pub cursor_insert: Color,
    /// Cursor block in Visual mode (distinct selection cursor).
    pub cursor_visual: Color,
    /// Editor panel border.
    pub border: Color,
    /// Line-number gutter text.
    pub line_number_fg: Color,
    /// Statusbar foreground.
    pub statusbar_fg: Color,
    /// Statusbar background.
    pub statusbar_bg: Color,
    /// Minibuffer candidate text.
    pub candidate_fg: Color,
    /// Minibuffer candidate background.
    pub candidate_bg: Color,
    /// Selected candidate background.
    pub candidate_sel_bg: Color,
    /// Echo line text.
    pub echo_fg: Color,
}

impl Default for Theme {
    fn default() -> Self {
        Self {
            cursor_normal: Color::Yellow,
            cursor_insert: Color::White,
            cursor_visual: Color::Yellow,
            border: Color::Cyan,
            line_number_fg: Color::DarkGray,
            statusbar_fg: Color::Black,
            statusbar_bg: Color::Cyan,
            candidate_fg: Color::Black,
            candidate_bg: Color::White,
            candidate_sel_bg: Color::DarkGray,
            echo_fg: Color::White,
        }
    }
}

impl Theme {
    /// Build a Theme from config map entries (`theme.<name>` keys).
    /// Unknown names / invalid colors are ignored (fall back to default).
    pub fn from_config(config: &HashMap<String, String>) -> Self {
        let mut theme = Self::default();
        let get = |name: &str| config.get(name).and_then(|v| parse_color(v));
        if let Some(c) = get("theme.cursor-normal") {
            theme.cursor_normal = c;
        }
        if let Some(c) = get("theme.cursor-insert") {
            theme.cursor_insert = c;
        }
        if let Some(c) = get("theme.cursor-visual") {
            theme.cursor_visual = c;
        }
        if let Some(c) = get("theme.border") {
            theme.border = c;
        }
        if let Some(c) = get("theme.line-number-fg") {
            theme.line_number_fg = c;
        }
        if let Some(c) = get("theme.statusbar-fg") {
            theme.statusbar_fg = c;
        }
        if let Some(c) = get("theme.statusbar-bg") {
            theme.statusbar_bg = c;
        }
        if let Some(c) = get("theme.candidate-fg") {
            theme.candidate_fg = c;
        }
        if let Some(c) = get("theme.candidate-bg") {
            theme.candidate_bg = c;
        }
        if let Some(c) = get("theme.candidate-sel-bg") {
            theme.candidate_sel_bg = c;
        }
        if let Some(c) = get("theme.echo-fg") {
            theme.echo_fg = c;
        }
        theme
    }
}

/// Parse a color name or `#rrggbb` hex string.
pub fn parse_color(s: &str) -> Option<Color> {
    let s = s.trim();
    match s.to_ascii_lowercase().as_str() {
        "black" => Some(Color::Black),
        "red" => Some(Color::Red),
        "green" => Some(Color::Green),
        "yellow" => Some(Color::Yellow),
        "blue" => Some(Color::Blue),
        "magenta" => Some(Color::Magenta),
        "cyan" => Some(Color::Cyan),
        "gray" | "grey" => Some(Color::Gray),
        "darkgray" | "dark-grey" => Some(Color::DarkGray),
        "white" => Some(Color::White),
        "reset" => Some(Color::Reset),
        _ => {
            // #rrggbb
            let hex = s.strip_prefix('#')?;
            if hex.len() == 6 {
                let r = u8::from_str_radix(&hex[0..2], 16).ok()?;
                let g = u8::from_str_radix(&hex[2..4], 16).ok()?;
                let b = u8::from_str_radix(&hex[4..6], 16).ok()?;
                Some(Color::Rgb(r, g, b))
            } else {
                None
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_named_colors() {
        assert_eq!(parse_color("yellow"), Some(Color::Yellow));
        assert_eq!(parse_color("MAGENTA"), Some(Color::Magenta));
        assert_eq!(parse_color("cyan"), Some(Color::Cyan));
    }

    #[test]
    fn parse_hex_colors() {
        assert_eq!(parse_color("#ff0000"), Some(Color::Rgb(255, 0, 0)));
        assert_eq!(parse_color("#00ff00"), Some(Color::Rgb(0, 255, 0)));
        assert_eq!(parse_color("#000000"), Some(Color::Rgb(0, 0, 0)));
    }

    #[test]
    fn parse_invalid_returns_none() {
        assert_eq!(parse_color("notacolor"), None);
        assert_eq!(parse_color("#ff00"), None);
        assert_eq!(parse_color("#zzzzzz"), None);
    }

    #[test]
    fn from_config_overrides_defaults() {
        let mut config = HashMap::new();
        config.insert("theme.cursor-normal".to_string(), "magenta".to_string());
        let theme = Theme::from_config(&config);
        assert_eq!(theme.cursor_normal, Color::Magenta);
        // Untouched fields keep defaults.
        assert_eq!(theme.cursor_insert, Color::White);
    }

    #[test]
    fn defaults_match_classic_look() {
        let t = Theme::default();
        assert_eq!(t.cursor_normal, Color::Yellow);
        assert_eq!(t.cursor_visual, Color::Yellow);
        assert_eq!(t.cursor_insert, Color::White);
    }
}