Skip to content
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
6 changes: 3 additions & 3 deletions examples/dynamic_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use colored::*;

fn main() {
// the easy way
"blue string yo".color("blue");
println!("{}", "blue string yo".color("blue"));

// this will default to white
"white string".color("zorglub");
println!("{}", "white string".color("zorglub"));

// the safer way via a Result
let color_res = "zorglub".parse(); // <- this returns a Result<Color, ()>
"red string".color(color_res.unwrap_or(Color::Red));
println!("{}", "red string".color(color_res.unwrap_or(Color::Red)));
}
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)]
#[allow(unused_assignments)]
let mut tty = false;
#[cfg(feature = "tty")]
{
tty = atty::is(atty::Stream::Stdout);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[allow(unused_mut)]
#[allow(unused_assignments)]
let mut tty = false;
#[cfg(feature = "tty")]
{
tty = atty::is(atty::Stream::Stdout);
}
let tty = if cfg!(feature = "tty") {
atty::is(atty::Stream::Stdout)
} else {
false
};

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I like this better than mine but it doesn't compile. This compiles:

        let tty = if cfg!(feature = "tty") {
            #[cfg(feature = "tty")]
            {
            atty::is(atty::Stream::Stdout)
            }
            #[cfg(not(feature = "tty"))]
            false
        } else {
            false
        };

But we can clean it up further to this:

        let tty = {
            #[cfg(feature = "tty")]
            {
                atty::is(atty::Stream::Stdout)
            }
            #[cfg(not(feature = "tty"))]
            false
        };

And sorry there are some commits that I didn't mean to put into this PR.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also use

use std::io::{self, IsTerminal};
let tty = io::stdout().is_terminal();

that was very recently introduced in the Rust 1.70.0 std library.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use that and drop the atty dependency.

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,
clicolor_force: ShouldColorize::resolve_clicolor_force(
env::var("NO_COLOR"),
env::var("CLICOLOR_FORCE"),
Expand Down
Loading