From f942c5069dff4b7874ee1a7ce7c796a0c584e5fa Mon Sep 17 00:00:00 2001 From: mateuscmtropical Date: Sat, 21 Nov 2026 11:59:46 -0300 Subject: [PATCH] Respect NO_COLOR in the auto color mode --- CHANGELOG.md | 4 ++++ docs/manual.md | 2 ++ src/main.rs | 46 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 38 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6abc8e9..6b62d6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] - Next Date +### Added + +- The `auto` color mode respects the `NO_COLOR` environment variable (see https://no-color.org/). + ### Changed - Renamed CLI option `--reverse` to `--rotate` (`-r`). `--reverse` remains available as a hidden alias. diff --git a/docs/manual.md b/docs/manual.md index c6bf71c..5db8646 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -105,6 +105,8 @@ FLAGS: OPTIONS: --color Specify when colors should be used. One of [auto|always|never]. Default: auto. + The auto mode also prints no colors when + the NO_COLOR environment variable is set. -f, --format Commit format. One of [oneline|short|medium|full|""]. (First character can be used as abbreviation, e.g. '-f m') Default: oneline. diff --git a/src/main.rs b/src/main.rs index bd5a20c..a25c492 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ use gleisbau::settings::{ use itertools::enumerate; use platform_dirs::AppDirs; use std::borrow::Borrow; +use std::ffi::OsString; use std::io; use std::io::Write; use std::path::PathBuf; @@ -426,7 +427,8 @@ fn add_color_args(app: Command) -> Command { .long("color") .help( "Specify when colors should be used. One of [auto|always|never].\n\ - Default: auto.", + Default: auto. The auto mode also prints no colors when\n\ + the NO_COLOR environment variable is set.", ) .required(false) .num_args(1), @@ -449,13 +451,7 @@ fn match_color_args(_ses: &mut Session, matches: &ArgMatches) -> Result("color") { match &mode[..] { - "auto" => { - atty::is(atty::Stream::Stdout) - && (!cfg!(windows) || { - yansi::enable(); - yansi::is_enabled() - }) - } + "auto" => auto_color(), "always" => { if cfg!(windows) { yansi::enable(); @@ -471,16 +467,29 @@ fn match_color_args(_ses: &mut Session, matches: &ArgMatches) -> Result bool { + !no_color_requested(std::env::var_os("NO_COLOR")) + && atty::is(atty::Stream::Stdout) + && (!cfg!(windows) || { + yansi::enable(); + yansi::is_enabled() + }) +} + +/// `NO_COLOR` set to anything but an empty string turns colors off. +/// See . +fn no_color_requested(value: Option) -> bool { + value.is_some_and(|value| !value.is_empty()) +} + // // wrap flag // @@ -713,7 +722,16 @@ fn io_err_to_string(result: Result<(), io::Error>) -> Result<(), String> { #[cfg(test)] mod cli_tests { - use super::build_cli; + use super::{build_cli, no_color_requested}; + use std::ffi::OsString; + + #[test] + fn no_color_env_turns_colors_off_when_not_empty() { + assert!(no_color_requested(Some(OsString::from("1")))); + assert!(no_color_requested(Some(OsString::from("false")))); + assert!(!no_color_requested(Some(OsString::new()))); + assert!(!no_color_requested(None)); + } #[test] fn rotate_flag_sets_rotate() {