mirror of
https://github.com/martinvonz/jj.git
synced 2025-02-03 18:32:09 +00:00
cli: respect $NO_COLOR
environment variable
This commit is contained in:
parent
7ba1c6bdb6
commit
2f59e8b68a
3 changed files with 32 additions and 9 deletions
|
@ -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
|
config file. That will then be read instead of your regular config file. This
|
||||||
is mostly intended for testing and scripts.
|
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
|
## [0.3.3] - 2022-03-16
|
||||||
|
|
||||||
No changes, only trying to get the automated build to work.
|
No changes, only trying to get the automated build to work.
|
||||||
|
|
25
src/ui.rs
25
src/ui.rs
|
@ -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> {
|
impl<'stdout> Ui<'stdout> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
cwd: PathBuf,
|
cwd: PathBuf,
|
||||||
|
@ -65,15 +80,7 @@ impl<'stdout> Ui<'stdout> {
|
||||||
pub fn for_terminal(settings: UserSettings) -> Ui<'static> {
|
pub fn for_terminal(settings: UserSettings) -> Ui<'static> {
|
||||||
let cwd = std::env::current_dir().unwrap();
|
let cwd = std::env::current_dir().unwrap();
|
||||||
let stdout: Box<dyn Write + 'static> = Box::new(io::stdout());
|
let stdout: Box<dyn Write + 'static> = Box::new(io::stdout());
|
||||||
let color_setting = settings
|
let color = use_color(&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),
|
|
||||||
};
|
|
||||||
Ui::new(cwd, stdout, color, settings)
|
Ui::new(cwd, stdout, color, settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -85,6 +85,8 @@ fn test_repo_arg_with_git_clone() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_color_config() {
|
fn test_color_config() {
|
||||||
let test_env = TestEnvironment::default();
|
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()
|
let mut config_file = std::fs::File::options()
|
||||||
.append(true)
|
.append(true)
|
||||||
.open(test_env.config_path())
|
.open(test_env.config_path())
|
||||||
|
@ -110,4 +112,15 @@ color="always""#,
|
||||||
@ [1;34m230dd059e1b059aefc0da06a2e5a7dbf22362f22[0m
|
@ [1;34m230dd059e1b059aefc0da06a2e5a7dbf22362f22[0m
|
||||||
o [34m0000000000000000000000000000000000000000[0m
|
o [34m0000000000000000000000000000000000000000[0m
|
||||||
"###);
|
"###);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
"###);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue