Skip to content

Commit

Permalink
feature: Make usable without atty.
Browse files Browse the repository at this point in the history
  • Loading branch information
shanecelis committed Jun 8, 2023
1 parent 11ffd20 commit ea43692
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 9 additions & 2 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> = 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"),
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//!
#![warn(missing_docs)]

#[cfg(feature = "tty")]
extern crate atty;
#[macro_use]
extern crate lazy_static;
Expand Down

0 comments on commit ea43692

Please sign in to comment.