rustom 0.1.0

A library for building rich, stateful terminal user interfaces and dashboards.
Documentation
//! Styling primitives: colors, text modifiers, and the `Style` type used
//! throughout the buffer and widget rendering pipeline.

use bitflags::bitflags;

/// A foreground or background color for a terminal cell.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Color {
    #[default]
    Reset,
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    Gray,
    DarkGray,
    LightRed,
    LightGreen,
    LightYellow,
    LightBlue,
    LightMagenta,
    LightCyan,
    White,
    Rgb(u8, u8, u8),
    Indexed(u8),
}

bitflags! {
    /// Text modifiers such as bold or underline, stored as bit flags so
    /// multiple modifiers can be combined on a single style.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub struct Modifier: u16 {
        const BOLD              = 0b0000_0000_0001;
        const DIM                = 0b0000_0000_0010;
        const ITALIC             = 0b0000_0000_0100;
        const UNDERLINED         = 0b0000_0000_1000;
        const SLOW_BLINK         = 0b0000_0001_0000;
        const RAPID_BLINK        = 0b0000_0010_0000;
        const REVERSED           = 0b0000_0100_0000;
        const HIDDEN             = 0b0000_1000_0000;
        const CROSSED_OUT        = 0b0001_0000_0000;
    }
}

impl Default for Modifier {
    fn default() -> Self {
        Modifier::empty()
    }
}

/// A combination of foreground color, background color, and text modifiers
/// that can be applied to a span of text or a single buffer cell.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Style {
    pub fg: Option<Color>,
    pub bg: Option<Color>,
    pub add_modifier: Modifier,
    pub sub_modifier: Modifier,
}

impl Style {
    pub const fn new() -> Style {
        Style {
            fg: None,
            bg: None,
            add_modifier: Modifier::empty(),
            sub_modifier: Modifier::empty(),
        }
    }

    pub fn fg(mut self, color: Color) -> Style {
        self.fg = Some(color);
        self
    }

    pub fn bg(mut self, color: Color) -> Style {
        self.bg = Some(color);
        self
    }

    pub fn add_modifier(mut self, modifier: Modifier) -> Style {
        self.sub_modifier.remove(modifier);
        self.add_modifier.insert(modifier);
        self
    }

    pub fn remove_modifier(mut self, modifier: Modifier) -> Style {
        self.add_modifier.remove(modifier);
        self.sub_modifier.insert(modifier);
        self
    }

    pub fn bold(self) -> Style {
        self.add_modifier(Modifier::BOLD)
    }

    pub fn italic(self) -> Style {
        self.add_modifier(Modifier::ITALIC)
    }

    pub fn underlined(self) -> Style {
        self.add_modifier(Modifier::UNDERLINED)
    }

    /// Layers `other` on top of `self`, letting `other` override any field
    /// it explicitly sets while leaving unset fields untouched.
    pub fn patch(mut self, other: Style) -> Style {
        self.fg = other.fg.or(self.fg);
        self.bg = other.bg.or(self.bg);
        self.add_modifier.remove(other.sub_modifier);
        self.add_modifier.insert(other.add_modifier);
        self.sub_modifier.remove(other.add_modifier);
        self.sub_modifier.insert(other.sub_modifier);
        self
    }
}