1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 23:40:05 +08:00

fix print_color unwarp issue

This commit is contained in:
2020-05-07 08:21:19 +08:00
parent ef49c85cf2
commit 0ebd4e5630
2 changed files with 17 additions and 13 deletions

View File

@@ -17,18 +17,22 @@ pub fn is_atty() -> bool {
}
pub fn print_color(color: Option<term::color::Color>, is_bold: bool, m: &str) {
let mut t = term::stdout().unwrap();
if *IS_ATTY {
if let Some(c) = color {
t.fg(c).unwrap();
}
if is_bold {
t.attr(term::Attr::Bold).unwrap();
}
write!(t, "{}", m).unwrap();
t.reset().unwrap();
} else {
write!(t, "{}", m).unwrap();
match term::stdout() {
Some(mut t) => {
if *IS_ATTY {
if let Some(c) = color {
t.fg(c).ok();
}
if is_bold {
t.attr(term::Attr::Bold).ok();
}
write!(t, "{}", m).ok();
t.reset().ok();
} else {
write!(t, "{}", m).ok();
}
},
None => print!("{}", m),
}
}