From ea43692918c8742a982eaf0701c751661a263890 Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Thu, 8 Jun 2023 05:54:14 -0400 Subject: [PATCH] feature: Make usable without atty. --- Cargo.toml | 4 +++- src/control.rs | 11 +++++++++-- src/lib.rs | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 170c690..cdd3062 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,11 +11,13 @@ readme = "README.md" keywords = ["color", "string", "term", "ansi_term", "term-painter"] [features] +default = ["tty"] +tty = ["dep:atty"] # with this feature, no color will ever be written no-color = [] [dependencies] -atty = "0.2" +atty = { version = "0.2", optional = true } lazy_static = "1" [target.'cfg(windows)'.dependencies.winapi] diff --git a/src/control.rs b/src/control.rs index 7ad6e62..c229445 100644 --- a/src/control.rs +++ b/src/control.rs @@ -103,9 +103,16 @@ impl ShouldColorize { /// `CLICOLOR_FORCE` takes highest priority, followed by `NO_COLOR`, /// followed by `CLICOLOR` combined with tty check. pub fn from_env() -> Self { + #[allow(unused_mut)] + let mut tty: Option = None; + + #[cfg(feature = "tty")] + { + tty = Some(atty::is(atty::Stream::Stdout)); + } ShouldColorize { - clicolor: ShouldColorize::normalize_env(env::var("CLICOLOR")).unwrap_or_else(|| true) - && atty::is(atty::Stream::Stdout), + clicolor: ShouldColorize::normalize_env(env::var("CLICOLOR")).unwrap_or(true) + && tty.unwrap_or(false), clicolor_force: ShouldColorize::resolve_clicolor_force( env::var("NO_COLOR"), env::var("CLICOLOR_FORCE"), diff --git a/src/lib.rs b/src/lib.rs index 1158c2a..308992e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ //! #![warn(missing_docs)] +#[cfg(feature = "tty")] extern crate atty; #[macro_use] extern crate lazy_static;