mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-24 13:12:31 +00:00
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:
parent
1f6a404646
commit
4dea1f9e16
3 changed files with 33 additions and 4 deletions
|
@ -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
|
* 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.
|
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
|
### Fixed bugs
|
||||||
|
|
||||||
* When rebasing a conflict where one side modified a file and the other side
|
* When rebasing a conflict where one side modified a file and the other side
|
||||||
|
|
|
@ -74,7 +74,7 @@ use crate::graphlog::{AsciiGraphDrawer, Edge};
|
||||||
use crate::template_parser::TemplateParser;
|
use crate::template_parser::TemplateParser;
|
||||||
use crate::templater::Template;
|
use crate::templater::Template;
|
||||||
use crate::ui;
|
use crate::ui;
|
||||||
use crate::ui::{FilePathParseError, Ui};
|
use crate::ui::{ColorChoice, FilePathParseError, Ui};
|
||||||
|
|
||||||
pub enum CommandError {
|
pub enum CommandError {
|
||||||
UserError(String),
|
UserError(String),
|
||||||
|
@ -1101,6 +1101,14 @@ struct GlobalArgs {
|
||||||
default_value = "@"
|
default_value = "@"
|
||||||
)]
|
)]
|
||||||
at_operation: String,
|
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)]
|
#[derive(Subcommand, Clone, Debug)]
|
||||||
|
@ -5207,6 +5215,10 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
let args = parse_args(ui.settings(), &string_args)?;
|
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 app = Args::command();
|
||||||
let command_helper = CommandHelper::new(app, string_args, args.global_args.clone());
|
let command_helper = CommandHelper::new(app, string_args, args.global_args.clone());
|
||||||
match &args.command {
|
match &args.command {
|
||||||
|
|
|
@ -95,20 +95,34 @@ fn test_repo_arg_with_git_clone() {
|
||||||
fn test_color_config() {
|
fn test_color_config() {
|
||||||
let mut test_env = TestEnvironment::default();
|
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###"
|
||||||
|
@ [1;34m230dd059e1b059aefc0da06a2e5a7dbf22362f22[0m
|
||||||
|
o [34m0000000000000000000000000000000000000000[0m
|
||||||
|
"###);
|
||||||
|
|
||||||
// Test that color is used if it's requested in the config file
|
// Test that color is used if it's requested in the config file
|
||||||
test_env.add_config(
|
test_env.add_config(
|
||||||
br#"[ui]
|
br#"[ui]
|
||||||
color="always""#,
|
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"]);
|
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
|
||||||
insta::assert_snapshot!(stdout, @r###"
|
insta::assert_snapshot!(stdout, @r###"
|
||||||
@ [1;34m230dd059e1b059aefc0da06a2e5a7dbf22362f22[0m
|
@ [1;34m230dd059e1b059aefc0da06a2e5a7dbf22362f22[0m
|
||||||
o [34m0000000000000000000000000000000000000000[0m
|
o [34m0000000000000000000000000000000000000000[0m
|
||||||
"###);
|
"###);
|
||||||
|
|
||||||
|
// 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 that NO_COLOR does NOT override the request for color in the config file
|
||||||
test_env.add_env_var("NO_COLOR", "");
|
test_env.add_env_var("NO_COLOR", "");
|
||||||
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
|
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
|
||||||
|
@ -148,6 +162,7 @@ fn test_help() {
|
||||||
|
|
||||||
GLOBAL OPTIONS:
|
GLOBAL OPTIONS:
|
||||||
--at-operation <AT_OPERATION> Operation to load the repo at [default: @] [aliases: at-op]
|
--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
|
-h, --help Print help information, more help with --help than with -h
|
||||||
--no-commit-working-copy Don't commit the working copy
|
--no-commit-working-copy Don't commit the working copy
|
||||||
-R, --repository <REPOSITORY> Path to repository to operate on
|
-R, --repository <REPOSITORY> Path to repository to operate on
|
||||||
|
|
Loading…
Reference in a new issue