simplify log color

This commit is contained in:
WangRunji 2019-04-02 17:53:20 +08:00
parent f711d20475
commit e5bc58d3f0

@ -22,11 +22,9 @@ impl Log for SimpleLogger {
fn log(&self, record: &Record) { fn log(&self, record: &Record) {
if self.enabled(record.metadata()) { if self.enabled(record.metadata()) {
let color = Color::from(record.level()); let color = Color::from(record.level());
let (show, code) = color.to_console_code();
println!( println!(
"\u{1B}[{};{}m[{:>5}] {}\u{1B}[0m", "\u{1B}[{}m[{:>5}] {}\u{1B}[0m",
show, color as u8,
code + 30,
record.level(), record.level(),
record.args() record.args()
); );
@ -42,7 +40,7 @@ impl From<Level> for Color {
Level::Warn => Color::Yellow, Level::Warn => Color::Yellow,
Level::Info => Color::Blue, Level::Info => Color::Blue,
Level::Debug => Color::Green, Level::Debug => Color::Green,
Level::Trace => Color::DarkGray, Level::Trace => Color::BrightBlack,
} }
} }
} }
@ -51,43 +49,20 @@ impl From<Level> for Color {
#[derive(Debug, Clone, Copy, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[repr(u8)] #[repr(u8)]
pub enum Color { pub enum Color {
Black = 0, Black = 30,
Blue = 1, Red = 31,
Green = 2, Green = 32,
Cyan = 3, Yellow = 33,
Red = 4, Blue = 34,
Magenta = 5, Magenta = 35,
Brown = 6, Cyan = 36,
LightGray = 7, White = 37,
DarkGray = 8, BrightBlack = 90,
LightBlue = 9, BrightRed = 91,
LightGreen = 10, BrightGreen = 92,
LightCyan = 11, BrightYellow = 93,
LightRed = 12, BrightBlue = 94,
Pink = 13, BrightMagenta = 95,
Yellow = 14, BrightCyan = 96,
White = 15, BrightWhite = 97,
}
impl Color {
fn to_console_code(&self) -> (u8, u8) {
match self {
Color::Black => (0, 0),
Color::Blue => (0, 4),
Color::Green => (0, 2),
Color::Cyan => (0, 6),
Color::Red => (0, 1),
Color::Magenta => (0, 5),
Color::Brown => (0, 3),
Color::LightGray => (1, 7),
Color::DarkGray => (0, 7),
Color::LightBlue => (1, 4),
Color::LightGreen => (1, 2),
Color::LightCyan => (1, 6),
Color::LightRed => (1, 1),
Color::Pink => (1, 5),
Color::Yellow => (1, 3),
Color::White => (1, 0),
}
}
} }