ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: parse --color option and reconfigure ui

I often redirect the jj output to pager, so I set ui.color = "always" in
config file. This patch allows me to remove such config, and instead specify
--color=always only when needed.
This commit is contained in:
Yuya Nishihara 2022-06-09 13:16:34 +09:00
parent 1f6a404646
commit 4dea1f9e16
3 changed files with 33 additions and 4 deletions

View file

@ -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

View file

@ -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<ColorChoice>,
}
#[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 {

View file

@ -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 <AT_OPERATION> Operation to load the repo at [default: @] [aliases: at-op]
--color <WHEN> 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 <REPOSITORY> Path to repository to operate on