Skip to content
6 changes: 4 additions & 2 deletions 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", "color"]
tty = ["dep:atty"]
# with this feature, no color will ever be written
no-color = []
color = []

[dependencies]
atty = "0.2"
atty = { version = "0.2", optional = true }
lazy_static = "1"

[target.'cfg(windows)'.dependencies.winapi]
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ using the `no-color` feature.
For example, you can do this in your `Cargo.toml` to disable color in tests:

```toml
[dependencies]
colored = { version = "2", default-features = false, features = [ "tty" ] }

[features]
# this effectively enable the feature `no-color` of colored when testing with
# this effectively disables the feature `color` of colored when testing with
# `cargo test --feature dumb_terminal`
dumb_terminal = ["colored/no-color"]
dumb_terminal = ["colored"]
color_terminal = ["colored/color"]
```

You can use have even finer control by using the
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)));
}
12 changes: 10 additions & 2 deletions src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,17 @@ impl ShouldColorize {
/// `CLICOLOR_FORCE` takes highest priority, followed by `NO_COLOR`,
/// followed by `CLICOLOR` combined with tty check.
pub fn from_env() -> Self {
let tty = {
#[cfg(feature = "tty")]
{
atty::is(atty::Stream::Stdout)
}
#[cfg(not(feature = "tty"))]
false
};
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