From 05c77a853fd2976f62ccb3e81db62d034ac586c0 Mon Sep 17 00:00:00 2001 From: Daniel Luz Date: Sun, 23 Mar 2025 23:11:49 -0300 Subject: [PATCH] formatter: add support for reversing colors --- CHANGELOG.md | 4 ++++ cli/src/config-schema.json | 3 +++ cli/src/formatter.rs | 20 ++++++++++++++++++-- docs/config.md | 10 +++++++--- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5dfc3498..4bc091ddb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). now show up in `git diff` (as if you had run `git add --intent-to-add` on them). +* Reversing colors is now supported. For example, to highlight words by + reversing colors rather than underlining, you can set + `colors."diff token"={ underline = false, reverse = true }` in your config. + ### Fixed bugs * `jj log -p --stat` now shows diff stats as well as the default color-words/git diff --git a/cli/src/config-schema.json b/cli/src/config-schema.json index f9b1b5e08..4f597dfbc 100644 --- a/cli/src/config-schema.json +++ b/cli/src/config-schema.json @@ -342,6 +342,9 @@ }, "underline": { "type": "boolean" + }, + "reverse": { + "type": "boolean" } } } diff --git a/cli/src/formatter.rs b/cli/src/formatter.rs index 8fb18f640..2a7cfc938 100644 --- a/cli/src/formatter.rs +++ b/cli/src/formatter.rs @@ -266,6 +266,7 @@ pub struct Style { pub bold: Option, pub italic: Option, pub underline: Option, + pub reverse: Option, } impl Style { @@ -275,6 +276,7 @@ impl Style { self.bold = other.bold.or(self.bold); self.italic = other.italic.or(self.italic); self.underline = other.underline.or(self.underline); + self.reverse = other.reverse.or(self.reverse); } } @@ -396,6 +398,13 @@ impl ColorFormatter { queue!(self.output, SetAttribute(Attribute::NoUnderline))?; } } + if new_style.reverse != self.current_style.reverse { + if new_style.reverse.unwrap_or_default() { + queue!(self.output, SetAttribute(Attribute::Reverse))?; + } else { + queue!(self.output, SetAttribute(Attribute::NoReverse))?; + } + } if new_style.fg != self.current_style.fg { queue!( self.output, @@ -436,6 +445,7 @@ fn rules_from_config(config: &StackedConfig) -> Result { bold: None, italic: None, underline: None, + reverse: None, }) } else if value.is_inline_table() { Style::deserialize(value.into_deserializer()) @@ -884,7 +894,8 @@ mod tests { colors.bold_font = { bold = true } colors.italic_text = { italic = true } colors.underlined_text = { underline = true } - colors.multiple = { fg = "green", bg = "yellow", bold = true, italic = true, underline = true } + colors.reversed_colors = { reverse = true } + colors.multiple = { fg = "green", bg = "yellow", bold = true, italic = true, underline = true, reverse = true } "#, ); let mut output: Vec = vec![]; @@ -909,6 +920,10 @@ mod tests { write!(formatter, " underlined only ").unwrap(); formatter.pop_label().unwrap(); writeln!(formatter).unwrap(); + formatter.push_label("reversed_colors").unwrap(); + write!(formatter, " reverse only ").unwrap(); + formatter.pop_label().unwrap(); + writeln!(formatter).unwrap(); formatter.push_label("multiple").unwrap(); write!(formatter, " single rule ").unwrap(); formatter.pop_label().unwrap(); @@ -926,7 +941,8 @@ mod tests {  bold only   italic only   underlined only  -  single rule  +  reverse only  +  single rule   two rules  [EOF] "); diff --git a/docs/config.md b/docs/config.md index 2313ca189..6290d50fa 100644 --- a/docs/config.md +++ b/docs/config.md @@ -129,12 +129,14 @@ change_id = "#ff1525" ``` If you use a string value for a color, as in the examples above, it will be used -for the foreground color. You can also set the background color, or make the -text bold, italic, or underlined. For that, you need to use a table: +for the foreground color. You can also set the background color, reverse colors +(swap foreground and background), or make the text bold, italic, or underlined. +For that, you need to use a table: ```toml [colors] -commit_id = { fg = "green", bg = "#ff1525", bold = true, italic = true, underline = true } +commit_id = { fg = "green", bg = "#ff1525", bold = true, underline = true } +change_id = { reverse = true, italic = true } ``` The key names are called "labels". The above used `commit_id` as label. You can @@ -205,6 +207,8 @@ can override the default style with the following keys: # Highlight hunks with background "diff removed token" = { bg = "#221111", underline = false } "diff added token" = { bg = "#002200", underline = false } +# Alternatively, swap colors +"diff token" = { reverse = true, underline = false } ``` ### Diff format