add NO_COLOR support — https://no-color.org/

pull/22/head
chris west 4 years ago
parent 5f232d8862
commit ffa2083740

@ -11,12 +11,16 @@ use std::fmt;
/// let x = color_string!("Hyperlink-ish", Blue, Underline);
macro_rules! color_string {
($s:expr, $( $color:ident ),+) => {{
let mut out = String::from("\x1b[");
$( out.push_str(crate::color::$color::code()); out.push_str(";"); )+
out.push('m');
out.push_str(&$s);
out.push_str(crate::color::Reset.as_ref());
out.replace(";m", "m")
if *crate::NO_COLOR {
$s.to_string()
} else {
let mut out = String::from("\x1b[");
$( out.push_str(crate::color::$color::code()); out.push_str(";"); )+
out.push('m');
out.push_str(&$s);
out.push_str(crate::color::Reset.as_ref());
out.replace(";m", "m")
}
}};
}
@ -29,7 +33,11 @@ macro_rules! color_string {
/// o.push_str(color!(Reset));
macro_rules! color {
($color:ident) => {
crate::color::$color.as_ref()
if *crate::NO_COLOR {
""
} else {
crate::color::$color.as_ref()
}
};
}

@ -77,3 +77,8 @@ pub const TOR_SUPPORT: bool = true;
#[cfg(not(feature = "tor"))]
/// Whether we compiled with Tor support.
pub const TOR_SUPPORT: bool = false;
lazy_static! {
/// Is the NO_COLOR env variable set? https://no-color.org/
pub static ref NO_COLOR: bool = std::env::var("NO_COLOR").is_ok();
}

Loading…
Cancel
Save