Skip to content

Commit

Permalink
ui: define default ui.color in config/misc.toml, use serde to parse v…
Browse files Browse the repository at this point in the history
…alue

It looks a bit odd that FromStr is forwarded to Deserialize, not the other way
around, but that's convenient because Deserialize can be derived.
  • Loading branch information
yuja committed Dec 11, 2024
1 parent bcfe8ef commit 564ce04
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
not converted to a string `"true"`, and vice versa.

* The following configuration variables are now parsed strictly:
`ui.progress-indicator`, `ui.quiet`
`ui.color`, `ui.progress-indicator`, `ui.quiet`

* The deprecated `[alias]` config section is no longer respected. Move command
aliases to the `[aliases]` section.
Expand Down
1 change: 1 addition & 0 deletions cli/src/config/misc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ context = 3
# TODO: delete ui.allow-filesets in jj 0.26+
allow-filesets = true
always-allow-large-revsets = false
color = "auto"
diff-instructions = true
graph.style = "curved"
paginate = "auto"
Expand Down
28 changes: 10 additions & 18 deletions cli/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ use jj_lib::config::ConfigGetError;
use jj_lib::config::StackedConfig;
use minus::MinusError;
use minus::Pager as MinusPager;
use serde::de::Deserialize as _;
use serde::de::IntoDeserializer as _;
use tracing::instrument;

use crate::command_error::CommandError;
Expand Down Expand Up @@ -262,7 +264,8 @@ pub struct Ui {
output: UiOutput,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ColorChoice {
Always,
Never,
Expand All @@ -272,16 +275,13 @@ pub enum ColorChoice {
}

impl FromStr for ColorChoice {
type Err = &'static str;
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"always" => Ok(ColorChoice::Always),
"never" => Ok(ColorChoice::Never),
"debug" => Ok(ColorChoice::Debug),
"auto" => Ok(ColorChoice::Auto),
_ => Err("must be one of always, never, or auto"),
}
// serde::de::value::Error is Box<str> wrapper. Map it to String to hide
// the implementation detail.
Self::deserialize(s.into_deserializer())
.map_err(|err: serde::de::value::Error| err.to_string())
}
}

Expand All @@ -297,20 +297,12 @@ impl fmt::Display for ColorChoice {
}
}

fn color_setting(config: &StackedConfig) -> ColorChoice {
config
.get::<String>("ui.color")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or_default()
}

fn prepare_formatter_factory(
config: &StackedConfig,
stdout: &Stdout,
) -> Result<FormatterFactory, ConfigGetError> {
let terminal = stdout.is_terminal();
let (color, debug) = match color_setting(config) {
let (color, debug) = match config.get("ui.color")? {
ColorChoice::Always => (true, false),
ColorChoice::Never => (false, false),
ColorChoice::Debug => (true, true),
Expand Down
16 changes: 16 additions & 0 deletions cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,22 @@ fn test_color_config() {
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
◆ 0000000000000000000000000000000000000000
"###);

// Invalid --color
let stderr = test_env.jj_cmd_cli_error(&repo_path, &["log", "--color=foo"]);
insta::assert_snapshot!(stderr, @r"
error: invalid value 'foo' for '--color <WHEN>': unknown variant `foo`, expected one of `always`, `never`, `debug`, `auto`
For more information, try '--help'.
");
// Invalid ui.color
let stderr = test_env.jj_cmd_failure(&repo_path, &["log", "--config-toml=ui.color=true"]);
insta::assert_snapshot!(stderr, @r"
Config error: Invalid type or value for ui.color
Caused by: wanted string or table
For help, see https://martinvonz.github.io/jj/latest/config/.
");
}

#[test]
Expand Down

0 comments on commit 564ce04

Please sign in to comment.