1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-29 16: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

@@ -1,6 +1,6 @@
[package] [package]
name = "rust_util" name = "rust_util"
version = "0.2.5" version = "0.2.6"
authors = ["Hatter Jiang <jht5945@gmail.com>"] authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018" edition = "2018"
description = "Hatter's Rust Util" description = "Hatter's Rust Util"

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