use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Color {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
BrightBlack,
BrightRed,
BrightGreen,
BrightYellow,
BrightBlue,
BrightMagenta,
BrightCyan,
BrightWhite,
Fixed(u8),
Rgb(u8, u8, u8),
}
impl Color {
pub(crate) fn fg_code(&self) -> String {
match self {
Color::Black => "30".to_string(),
Color::Red => "31".to_string(),
Color::Green => "32".to_string(),
Color::Yellow => "33".to_string(),
Color::Blue => "34".to_string(),
Color::Magenta => "35".to_string(),
Color::Cyan => "36".to_string(),
Color::White => "37".to_string(),
Color::BrightBlack => "90".to_string(),
Color::BrightRed => "91".to_string(),
Color::BrightGreen => "92".to_string(),
Color::BrightYellow => "93".to_string(),
Color::BrightBlue => "94".to_string(),
Color::BrightMagenta => "95".to_string(),
Color::BrightCyan => "96".to_string(),
Color::BrightWhite => "97".to_string(),
Color::Fixed(n) => format!("38;5;{}", n),
Color::Rgb(r, g, b) => format!("38;2;{};{};{}", r, g, b),
}
}
pub(crate) fn bg_code(&self) -> String {
match self {
Color::Black => "40".to_string(),
Color::Red => "41".to_string(),
Color::Green => "42".to_string(),
Color::Yellow => "43".to_string(),
Color::Blue => "44".to_string(),
Color::Magenta => "45".to_string(),
Color::Cyan => "46".to_string(),
Color::White => "47".to_string(),
Color::BrightBlack => "100".to_string(),
Color::BrightRed => "101".to_string(),
Color::BrightGreen => "102".to_string(),
Color::BrightYellow => "103".to_string(),
Color::BrightBlue => "104".to_string(),
Color::BrightMagenta => "105".to_string(),
Color::BrightCyan => "106".to_string(),
Color::BrightWhite => "107".to_string(),
Color::Fixed(n) => format!("48;5;{}", n),
Color::Rgb(r, g, b) => format!("48;2;{};{};{}", r, g, b),
}
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\x1b[{}m", self.fg_code())
}
}