From c735d92e8c639a71cd5c01fd1379d1a0bbe284ea Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Fri, 7 Apr 2023 23:25:30 -0700 Subject: [PATCH] formatter: allow using "default" terminal color The "default" color resets the terminal color to default. --- CHANGELOG.md | 3 +++ docs/config.md | 5 ++++- src/config-schema.json | 1 + src/formatter.rs | 31 +++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c3c7f9e1..f65c54f4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * The progress display on `jj git clone/fetch` now includes the downloaded size. +* The formatter now supports a "default" color that can override another color + defined by a parent style. + ### Fixed bugs * Modify/delete conflicts now include context lines diff --git a/docs/config.md b/docs/config.md index 43afa82d9..8e8e9f95e 100644 --- a/docs/config.md +++ b/docs/config.md @@ -92,8 +92,11 @@ The following colors are available: * magenta * cyan * white +* default -They each come in a bright version too, e.g. "bright red". +All of them but "default" come in a bright version too, e.g. "bright red". The +"default" color can be used to override a color defined by a parent style +(explained below). If you use a string value for a color, as in the example above, it will be used for the foreground color. You can also set the background color, or make the diff --git a/src/config-schema.json b/src/config-schema.json index 4884de3c9..ca147a885 100644 --- a/src/config-schema.json +++ b/src/config-schema.json @@ -128,6 +128,7 @@ "definitions": { "colors": { "enum": [ + "default", "black", "red", "green", diff --git a/src/formatter.rs b/src/formatter.rs index da2934684..e44ee6151 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -372,6 +372,7 @@ fn rules_from_config(config: &config::Config) -> Result Result { match color_name { + "default" => Ok(Color::Reset), "black" => Ok(Color::Black), "red" => Ok(Color::DarkRed), "green" => Ok(Color::DarkGreen), @@ -861,6 +862,36 @@ mod tests { @"invalid color: bloo"); } + #[test] + fn test_color_formatter_normal_color() { + // The "default" color resets the color. It is possible to reset only the + // background or only the foreground. + let config = config_from_string( + r#" + colors."outer" = {bg="yellow", fg="blue"} + colors."outer default_fg" = "default" + colors."outer default_bg" = {bg = "default"} + "#, + ); + let mut output: Vec = vec![]; + let mut formatter = ColorFormatter::for_config(&mut output, &config).unwrap(); + formatter.push_label("outer").unwrap(); + formatter.write_str("Blue on yellow, ").unwrap(); + formatter.push_label("default_fg").unwrap(); + formatter.write_str(" default fg, ").unwrap(); + formatter.pop_label().unwrap(); + formatter.write_str(" and back.\nBlue on yellow, ").unwrap(); + formatter.push_label("default_bg").unwrap(); + formatter.write_str(" default bg, ").unwrap(); + formatter.pop_label().unwrap(); + formatter.write_str(" and back.").unwrap(); + insta::assert_snapshot!(String::from_utf8(output).unwrap(), + @r###" + Blue on yellow,  default fg,  and back. + Blue on yellow,  default bg,  and back. + "###); + } + #[test] fn test_color_formatter_sibling() { // A partial match on one rule does not eliminate other rules.