From 689f958d05e81bc50ee653928a9de3323d505749 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 22 Jul 2019 01:55:23 +0800 Subject: [PATCH] add print_color,print_color_and_flush --- src/lib.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c516b1e..cb86c85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,15 +120,26 @@ pub fn new_box_error(m: &str) -> Box { pub enum MessageType { INFO, OK, WARN, ERROR, } -pub fn print_message_ex(color: Option, h: &str, message: &str) { +pub fn print_color(color: Option, is_bold: bool, m: &str) { let mut t = term::stdout().unwrap(); match color { Some(c) => t.fg(c).unwrap(), None => (), } - t.attr(term::Attr::Bold).unwrap(); - write!(t, "{}", h).unwrap(); + if is_bold { + t.attr(term::Attr::Bold).unwrap(); + } + write!(t, "{}", m).unwrap(); t.reset().unwrap(); +} + +pub fn print_color_and_flush(color: Option, is_bold: bool, m: &str) { + print_color(color, is_bold, m); + flush_stdout(); +} + +pub fn print_message_ex(color: Option, h: &str, message: &str) { + print_color(color, true, h); println!(" {}", message); }