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

cli: respect $NO_COLOR environment variable

This commit is contained in:
Martin von Zweigbergk 2022-03-19 10:00:13 -07:00 committed by Martin von Zweigbergk
parent 7ba1c6bdb6
commit 2f59e8b68a
3 changed files with 32 additions and 9 deletions

View file

@ -21,6 +21,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
config file. That will then be read instead of your regular config file. This
is mostly intended for testing and scripts.
* The [standard `$NO_COLOR` environment variable](https://no-color.org/) is now
respected.
## [0.3.3] - 2022-03-16
No changes, only trying to get the automated build to work.

View file

@ -46,6 +46,21 @@ fn new_formatter<'output>(
}
}
fn use_color(settings: &UserSettings) -> bool {
if std::env::var("NO_COLOR").is_ok() {
return false;
}
let color_setting = settings
.config()
.get_string("ui.color")
.unwrap_or_else(|_| "auto".to_string());
match color_setting.as_str() {
"always" => true,
"never" => false,
_ => atty::is(Stream::Stdout),
}
}
impl<'stdout> Ui<'stdout> {
pub fn new(
cwd: PathBuf,
@ -65,15 +80,7 @@ impl<'stdout> Ui<'stdout> {
pub fn for_terminal(settings: UserSettings) -> Ui<'static> {
let cwd = std::env::current_dir().unwrap();
let stdout: Box<dyn Write + 'static> = Box::new(io::stdout());
let color_setting = settings
.config()
.get_string("ui.color")
.unwrap_or_else(|_| "auto".to_string());
let color = match color_setting.as_str() {
"always" => true,
"never" => false,
_ => atty::is(Stream::Stdout),
};
let color = use_color(&settings);
Ui::new(cwd, stdout, color, settings)
}

View file

@ -85,6 +85,8 @@ fn test_repo_arg_with_git_clone() {
#[test]
fn test_color_config() {
let test_env = TestEnvironment::default();
// Test that color is used if it's requested in the config file
let mut config_file = std::fs::File::options()
.append(true)
.open(test_env.config_path())
@ -110,4 +112,15 @@ color="always""#,
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
o 0000000000000000000000000000000000000000
"###);
// Test that NO_COLOR overrides the request for color in the config file
let assert = test_env
.jj_cmd(&repo_path, &["log", "-T", "commit_id"])
.env("NO_COLOR", "")
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
o 0000000000000000000000000000000000000000
"###);
}