Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions docs/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ FLAGS:
OPTIONS:
--color <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 <format> Commit format. One of [oneline|short|medium|full|"<string>"].
(First character can be used as abbreviation, e.g. '-f m')
Default: oneline.
Expand Down
46 changes: 32 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand All @@ -449,13 +451,7 @@ fn match_color_args(_ses: &mut Session, matches: &ArgMatches) -> Result<bool, St
false
} else if let Some(mode) = matches.get_one::<String>("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();
Expand All @@ -471,16 +467,29 @@ fn match_color_args(_ses: &mut Session, matches: &ArgMatches) -> Result<bool, St
}
}
} else {
atty::is(atty::Stream::Stdout)
&& (!cfg!(windows) || {
yansi::enable();
yansi::is_enabled()
})
auto_color()
};

Ok(colored)
}

/// Whether the `auto` color mode prints colors: not when `NO_COLOR` asks for
/// none, nor when stdout is not a terminal.
fn auto_color() -> 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 <https://no-color.org/>.
fn no_color_requested(value: Option<OsString>) -> bool {
value.is_some_and(|value| !value.is_empty())
}

//
// wrap flag
//
Expand Down Expand Up @@ -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() {
Expand Down
Loading