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

formatter: allow using "default" terminal color

The "default" color resets the terminal color to default.
This commit is contained in:
Ilya Grigoriev 2023-04-07 23:25:30 -07:00
parent e468f9c97a
commit c735d92e8c
4 changed files with 39 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -128,6 +128,7 @@
"definitions": {
"colors": {
"enum": [
"default",
"black",
"red",
"green",

View file

@ -372,6 +372,7 @@ fn rules_from_config(config: &config::Config) -> Result<Rules, config::ConfigErr
fn color_for_name(color_name: &str) -> Result<Color, config::ConfigError> {
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<u8> = 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.