urs 0.5.1

Rust utility library
Documentation
//! # console
//!
//! Terminal console outputing library

/// Color
#[derive(Clone, Hash, PartialEq, Eq)]
pub enum Color {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
    Gray,
    BrightRed,
    BrightGreen,
    BrightYellow,
    BrightBlue,
    BrightMagenta,
    BrightCyan,
    BrightWhite,
}

impl Color {
    /// Returns background ANSI escape code number
    pub fn number(&self) -> u8 {
        use Color::*;
        match self {
            Black => 40,
            Red => 41,
            Green => 42,
            Yellow => 43,
            Blue => 44,
            Magenta => 45,
            Cyan => 46,
            White => 47,
            Gray => 100,
            BrightRed => 101,
            BrightGreen => 102,
            BrightYellow => 103,
            BrightBlue => 104,
            BrightMagenta => 105,
            BrightCyan => 106,
            BrightWhite => 107,
        }
    }
}

impl std::fmt::Debug for Color {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(format!("{}", self.number()).as_str())?;
        Ok(())
    }
}

fn style_esc(c: u8) -> String {
    format!("\x1b[{c}m")
}

/// Reset style settings
pub fn reset() -> String {
    style_esc(0)
}

/// Returns background color escape code for `color`
pub fn color_bg(color: Color) -> String {
    style_esc(color.number())
}

/// Returns foreground color escape code for `color`
pub fn color_fg(color: Color) -> String {
    // Foreground code is always 10 less than background
    style_esc(color.number() - 10)
}

/// **Boldens** text
pub fn bold() -> String {
    style_esc(1)
}

/// Makes text *italic*
pub fn italic() -> String {
    style_esc(3)
}

/// Makes text underlined
pub fn underline() -> String {
    style_esc(4)
}

/// Inverts the text
pub fn invert() -> String {
    style_esc(7)
}

/// Gives the text a strikethrough
pub fn strikethrough() -> String {
    style_esc(9)
}