mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 07:30:05 +08:00
add terminal check
This commit is contained in:
@@ -5,5 +5,7 @@ authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2.65"
|
||||
term = "0.5.2"
|
||||
term_size = "0.3.1"
|
||||
lazy_static = "1.3.0"
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate term;
|
||||
|
||||
use std::{
|
||||
|
||||
@@ -2,19 +2,33 @@ use std::{
|
||||
io::{self, Write},
|
||||
};
|
||||
|
||||
lazy_static! {
|
||||
pub static ref IS_ATTY: bool = is_atty();
|
||||
}
|
||||
|
||||
pub enum MessageType { INFO, OK, WARN, ERROR, DEBUG, }
|
||||
|
||||
pub fn is_atty() -> bool{
|
||||
let isatty = unsafe { libc::isatty(libc::STDOUT_FILENO as i32) } != 0;
|
||||
isatty
|
||||
}
|
||||
|
||||
pub fn print_color(color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
||||
let mut t = term::stdout().unwrap();
|
||||
match color {
|
||||
Some(c) => t.fg(c).unwrap(),
|
||||
None => (),
|
||||
}
|
||||
if is_bold {
|
||||
t.attr(term::Attr::Bold).unwrap();
|
||||
}
|
||||
write!(t, "{}", m).unwrap();
|
||||
t.reset().unwrap();
|
||||
match *IS_ATTY {
|
||||
true => {
|
||||
match color {
|
||||
Some(c) => t.fg(c).unwrap(),
|
||||
None => (),
|
||||
}
|
||||
if is_bold {
|
||||
t.attr(term::Attr::Bold).unwrap();
|
||||
}
|
||||
write!(t, "{}", m).unwrap();
|
||||
t.reset().unwrap();
|
||||
},
|
||||
false => write!(t, "{}", m).unwrap(),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn print_color_and_flush(color: Option<term::color::Color>, is_bold: bool, m: &str) {
|
||||
|
||||
Reference in New Issue
Block a user