beautiful_terminal 0.1.2

A tool on terminal.
Documentation
use std::fmt::Display;

/// 控制颜色的结构体  
/// A struct to control colors in terminal.
///
/// HINT:__本结构体不包括文本__,只是设置  
/// HINT: This struct __didn't__ contains context, this is a __setting__ only.
///
/// # Arguments
///
/// * fore: [`Fore`] 控制前景色  
/// * fore: [`Fore`] control color of fore  
/// * back: [`Back`] 控制背景色  
/// * back: [`Back`] control color of back  
/// * style: [`Style`] 控制样式  
/// * style: [`Style`] control style  
///
/// # Example
///
/// ```rust
/// use beautiful_terminal::colored::*;
/// let setting = ColorSettings::new(Fore::Red, Back::White);
/// println!("{}This text is red, on white.{}", setting, ColorSettings::RESET)
/// ```
///
/// # Others
///
/// 本结构体有点像`Python`中的`colorama.Fore`+`colorama.Back`😅  
/// This struct like `colorama.Fore`+`colorama.Back` in `python`😅
///
/// <del>没错,我就是抄了`colorama`  
/// <del>Yeah, I'm copy `colorama` package to here.
#[derive(Clone, Copy, Debug, Default)]
#[must_use]
pub struct ColorSettings {
    /// 前景色设置
    pub fore: Fore,
    /// 背景色设置
    pub back: Back,
    /// 样式设置
    pub style: Style,
}
impl ColorSettings {
    /// 颜色重置
    pub const RESET: Self = Self {
        fore: Fore::Reset,
        back: Back::Reset,
        style: Style::Default,
    };
    /// 新建设置(只设置前景色)
    pub const fn from_fore(fore: Fore) -> Self {
        Self {
            fore,
            back: Back::Reset,
            style: Style::Default,
        }
    }
    /// 新建设置(只设置背景色)
    pub const fn from_back(back: Back) -> Self {
        Self {
            fore: Fore::Reset,
            back,
            style: Style::Default,
        }
    }
    /// 新建设置
    pub const fn new(fore: Fore, back: Back) -> Self {
        Self {
            fore,
            back,
            style: Style::Default,
        }
    }
    /// 更改设置(设置前景色)
    pub const fn with_fore(self, fore: Fore) -> Self {
        Self {
            fore,
            back: self.back,
            style: self.style,
        }
    }
    /// 更改设置(设置背景色)
    pub const fn with_back(self, back: Back) -> Self {
        Self {
            fore: self.fore,
            back,
            style: self.style,
        }
    }
    /// 更改设置(设置样式)
    pub const fn with_style(self, style: Style) -> Self {
        Self {
            fore: self.fore,
            back: self.back,
            style,
        }
    }
}

/// 控制前景色  
/// This struct control the fore color of terminal.
///
/// 更多有关见[`ColorSettings`]
///
/// # Example
///
/// ```rust
/// use beautiful_terminal::colored::*;
/// let setting = Fore::Red;
/// println!("{}This text is red.{}", setting, Fore::Reset)
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[must_use]
pub enum Fore {
    #[default]
    /// 重置颜色
    Reset,
    /// 黑色
    Black,
    /// 红色
    Red,
    /// 绿色
    Green,
    /// 黄色
    Yellow,
    /// 蓝色
    Blue,
    /// 品红色
    Magenta,
    /// 青色
    Cyan,
    /// 白色
    White,
    /// 黑色
    BrightBlack,
    /// 红色
    BrightRed,
    /// 绿色
    BrightGreen,
    /// 黄色
    BrightYellow,
    /// 蓝色
    BrightBlue,
    /// 品红色
    BrightMagenta,
    /// 青色
    BrightCyan,
    /// 白色
    BrightWhite,
}
/// 控制背景色  
/// This struct control the back color of terminal.
///
/// 更多有关见[`ColorSettings`]
///
/// # Example
///
/// ```rust
/// use beautiful_terminal::colored::*;
/// let setting = Back::White;
/// println!("{}This text is on white.{}", setting, Back::Reset)
#[derive(Clone, Copy, Debug, Default)]
#[must_use]
pub enum Back {
    #[default]
    /// 重置颜色
    Reset,
    /// 黑色
    Black,
    /// 红色
    Red,
    /// 绿色
    Green,
    /// 黄色
    Yellow,
    /// 蓝色
    Blue,
    /// 品红色
    Magenta,
    /// 青色
    Cyan,
    /// 白色
    White,
    /// 黑色
    BrightBlack,
    /// 红色
    BrightRed,
    /// 绿色
    BrightGreen,
    /// 黄色
    BrightYellow,
    /// 蓝色
    BrightBlue,
    /// 品红色
    BrightMagenta,
    /// 青色
    BrightCyan,
    /// 白色
    BrightWhite,
}

/// 控制背景色  
/// This struct control the back color of terminal.
///
/// 更多有关见[`ColorSettings`]
///
/// # Example
///
/// ```rust
/// use beautiful_terminal::colored::*;
/// let setting = Style::Blink;
/// println!("{}This text is blinking.{}", setting, Style::Reset)
#[derive(Clone, Copy, Debug, Default)]
pub enum Style {
    #[default]
    Default,
    Wide,
    Underline,
    Reserve,
    Blink,
    Hide,
}

impl Display for Fore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Reset => "\x1b[0m",
                Self::Black => "\x1b[30m",
                Self::Red => "\x1b[31m",
                Self::Green => "\x1b[32m",
                Self::Yellow => "\x1b[33m",
                Self::Blue => "\x1b[34m",
                Self::Magenta => "\x1b[35m",
                Self::Cyan => "\x1b[36m",
                Self::White => "\x1b[37m",
                Self::BrightBlack => "\x1b[90m",
                Self::BrightRed => "\x1b[91m",
                Self::BrightGreen => "\x1b[92m",
                Self::BrightYellow => "\x1b[93m",
                Self::BrightBlue => "\x1b[94m",
                Self::BrightMagenta => "\x1b[95m",
                Self::BrightCyan => "\x1b[96m",
                Self::BrightWhite => "\x1b[97m",
            }
        )
    }
}
impl Display for Back {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Reset => "\x1b[0m",
                Self::Black => "\x1b[40m",
                Self::Red => "\x1b[41m",
                Self::Green => "\x1b[42m",
                Self::Yellow => "\x1b[43m",
                Self::Blue => "\x1b[44m",
                Self::Magenta => "\x1b[45m",
                Self::Cyan => "\x1b[46m",
                Self::White => "\x1b[47m",
                Self::BrightBlack => "\x1b[100m",
                Self::BrightRed => "\x1b[101m",
                Self::BrightGreen => "\x1b[102m",
                Self::BrightYellow => "\x1b[103m",
                Self::BrightBlue => "\x1b[104m",
                Self::BrightMagenta => "\x1b[105m",
                Self::BrightCyan => "\x1b[106m",
                Self::BrightWhite => "\x1b[107m",
            }
        )
    }
}
impl Display for Style {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Default => "\x1b0m",
                Self::Wide => "\x1b1m",
                Self::Underline => "\x1b4m",
                Self::Blink => "\x1b5m",
                Self::Reserve => "\x1b[7m",
                Self::Hide => "\x1b[8m",
            }
        )
    }
}
impl Display for ColorSettings {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match (self.fore, self.back, self.style) {
            (Fore::Reset, Back::Reset, Style::Default) => write!(f, "\x1b[0m"),
            (fr, Back::Reset, Style::Default) => write!(f, "\x1b[0m{fr}"),
            (fr, Back::Reset, st) => write!(f, "\x1b[0m{fr}{st}"),
            (Fore::Reset, bg, Style::Default) => write!(f, "\x1b[0m{bg}"),
            (Fore::Reset, bg, st) => write!(f, "\x1b[0m{bg}{st}"),
            (fr, bg, Style::Default) => write!(f, "\x1b[0m{fr}{bg}"),
            (fr, bg, st) => write!(f, "{fr}{bg}{st}"),
        }
    }
}