diff --git a/src/color.rs b/src/color.rs index 67ba1b6..52dd93c 100644 --- a/src/color.rs +++ b/src/color.rs @@ -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() + } }; } diff --git a/src/lib.rs b/src/lib.rs index 59132f4..c575ce3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(); +}