diff --git a/CHANGELOG.md b/CHANGELOG.md index d39c8e12a..d00c4195e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -109,6 +109,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * The "(no name/email configured)" placeholder value for name/email will now be replaced if once you modify a commit after having configured your name/email. +* Color setting can now be overridden by `--color=always|never|auto` option. + ### Fixed bugs * When rebasing a conflict where one side modified a file and the other side diff --git a/src/commands.rs b/src/commands.rs index 244daa3d4..6b1bc2a3e 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -74,7 +74,7 @@ use crate::graphlog::{AsciiGraphDrawer, Edge}; use crate::template_parser::TemplateParser; use crate::templater::Template; use crate::ui; -use crate::ui::{FilePathParseError, Ui}; +use crate::ui::{ColorChoice, FilePathParseError, Ui}; pub enum CommandError { UserError(String), @@ -1101,6 +1101,14 @@ struct GlobalArgs { default_value = "@" )] at_operation: String, + /// When to colorize output (always, never, auto) + #[clap( + long, + value_name = "WHEN", + global = true, + help_heading = "GLOBAL OPTIONS" + )] + color: Option, } #[derive(Subcommand, Clone, Debug)] @@ -5207,6 +5215,10 @@ where } let args = parse_args(ui.settings(), &string_args)?; + if let Some(choice) = args.global_args.color { + // Here we assume ui was created for_terminal(). + ui.reset_color_for_terminal(choice); + } let app = Args::command(); let command_helper = CommandHelper::new(app, string_args, args.global_args.clone()); match &args.command { diff --git a/tests/test_global_opts.rs b/tests/test_global_opts.rs index 09d66461b..215b06ab1 100644 --- a/tests/test_global_opts.rs +++ b/tests/test_global_opts.rs @@ -95,20 +95,34 @@ fn test_repo_arg_with_git_clone() { fn test_color_config() { let mut test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + // Test that --color=always is respected. + let stdout = test_env.jj_cmd_success(&repo_path, &["--color=always", "log", "-T", "commit_id"]); + insta::assert_snapshot!(stdout, @r###" + @ 230dd059e1b059aefc0da06a2e5a7dbf22362f22 + o 0000000000000000000000000000000000000000 + "###); + // Test that color is used if it's requested in the config file test_env.add_config( br#"[ui] color="always""#, ); - test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); - - let repo_path = test_env.env_root().join("repo"); let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]); insta::assert_snapshot!(stdout, @r###" @ 230dd059e1b059aefc0da06a2e5a7dbf22362f22 o 0000000000000000000000000000000000000000 "###); + // Test that --color=never overrides the config. + let stdout = test_env.jj_cmd_success(&repo_path, &["--color=never", "log", "-T", "commit_id"]); + insta::assert_snapshot!(stdout, @r###" + @ 230dd059e1b059aefc0da06a2e5a7dbf22362f22 + o 0000000000000000000000000000000000000000 + "###); + // Test that NO_COLOR does NOT override the request for color in the config file test_env.add_env_var("NO_COLOR", ""); let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]); @@ -148,6 +162,7 @@ fn test_help() { GLOBAL OPTIONS: --at-operation Operation to load the repo at [default: @] [aliases: at-op] + --color When to colorize output (always, never, auto) -h, --help Print help information, more help with --help than with -h --no-commit-working-copy Don't commit the working copy -R, --repository Path to repository to operate on