#[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 {
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")
}
pub fn reset() -> String {
style_esc(0)
}
pub fn color_bg(color: Color) -> String {
style_esc(color.number())
}
pub fn color_fg(color: Color) -> String {
style_esc(color.number() - 10)
}
pub fn bold() -> String {
style_esc(1)
}
pub fn italic() -> String {
style_esc(3)
}
pub fn underline() -> String {
style_esc(4)
}
pub fn invert() -> String {
style_esc(7)
}
pub fn strikethrough() -> String {
style_esc(9)
}