From a6f335d8a1d0db219f3b913ba4e1b254657adcae Mon Sep 17 00:00:00 2001 From: Techcable Date: Wed, 20 Aug 2025 18:00:55 -0700 Subject: [PATCH] Respect the NO_COLOR environment variable See Support for CLICOLOR and CLICOLOR_FORCE is a bigger change, and will have to wait till v2.10. See for more details. --- CHANGELOG.md | 5 +++++ src/lib.rs | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 940654e..28c76b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). [slog#267]: https://github.com/slog-rs/slog/issues/267 +### Changed +* Respect the [`NO_COLOR`] environment variable. + +[`NO_COLOR`]: https://no-color.org/ + ## 2.9.1 - 2024-02-18 ### Fixed * Switch from `atty` to `is_terminal` diff --git a/src/lib.rs b/src/lib.rs index 017ed24..77a759b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1404,6 +1404,10 @@ enum AnyTerminal { impl AnyTerminal { fn should_use_color(&self) -> bool { + // Respect NO_COLOR + if std::env::var_os("NO_COLOR").map_or(false, |x| !x.is_empty()) { + return false; + } match *self { AnyTerminal::Stdout { .. } => std::io::stdout().is_terminal(), AnyTerminal::Stderr { .. } => std::io::stderr().is_terminal(),