From 7ae1a97d2b46b21ac14b87d26e53d7372184093f Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 10 Dec 2024 15:17:34 +0900 Subject: [PATCH] formatter: rename Style fields to match configuration keys We could rename fields at serde layer, but it's probably better to use the same names if possible. --- cli/src/formatter.rs | 38 ++++++++++++++++++------------------- cli/src/template_builder.rs | 4 ++-- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/cli/src/formatter.rs b/cli/src/formatter.rs index 8213879401..11c648b74c 100644 --- a/cli/src/formatter.rs +++ b/cli/src/formatter.rs @@ -255,18 +255,18 @@ impl Formatter for SanitizingFormatter { #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct Style { - pub fg_color: Option, - pub bg_color: Option, + pub fg: Option, + pub bg: Option, pub bold: Option, - pub underlined: Option, + pub underline: Option, } impl Style { fn merge(&mut self, other: &Style) { - self.fg_color = other.fg_color.or(self.fg_color); - self.bg_color = other.bg_color.or(self.bg_color); + self.fg = other.fg.or(self.fg); + self.bg = other.bg.or(self.bg); self.bold = other.bold.or(self.bold); - self.underlined = other.underlined.or(self.underlined); + self.underline = other.underline.or(self.underline); } } @@ -374,23 +374,23 @@ impl ColorFormatter { self.current_style = Style::default(); } } - if new_style.underlined != self.current_style.underlined { - if new_style.underlined.unwrap_or_default() { + if new_style.underline != self.current_style.underline { + if new_style.underline.unwrap_or_default() { queue!(self.output, SetAttribute(Attribute::Underlined))?; } else { queue!(self.output, SetAttribute(Attribute::NoUnderline))?; } } - if new_style.fg_color != self.current_style.fg_color { + if new_style.fg != self.current_style.fg { queue!( self.output, - SetForegroundColor(new_style.fg_color.unwrap_or(Color::Reset)) + SetForegroundColor(new_style.fg.unwrap_or(Color::Reset)) )?; } - if new_style.bg_color != self.current_style.bg_color { + if new_style.bg != self.current_style.bg { queue!( self.output, - SetBackgroundColor(new_style.bg_color.unwrap_or(Color::Reset)) + SetBackgroundColor(new_style.bg.unwrap_or(Color::Reset)) )?; } self.current_style = new_style; @@ -421,24 +421,22 @@ fn rules_from_config(config: &StackedConfig) -> Result { let value = config.get_value(["colors", key])?; if let Some(color_name) = value.as_str() { let style = Style { - fg_color: Some(color_for_name_or_hex(color_name).map_err(to_config_err)?), - bg_color: None, + fg: Some(color_for_name_or_hex(color_name).map_err(to_config_err)?), + bg: None, bold: None, - underlined: None, + underline: None, }; result.push((labels, style)); } else if let Some(style_table) = value.as_inline_table() { let mut style = Style::default(); if let Some(item) = style_table.get("fg") { if let Some(color_name) = item.as_str() { - style.fg_color = - Some(color_for_name_or_hex(color_name).map_err(to_config_err)?); + style.fg = Some(color_for_name_or_hex(color_name).map_err(to_config_err)?); } } if let Some(item) = style_table.get("bg") { if let Some(color_name) = item.as_str() { - style.bg_color = - Some(color_for_name_or_hex(color_name).map_err(to_config_err)?); + style.bg = Some(color_for_name_or_hex(color_name).map_err(to_config_err)?); } } if let Some(item) = style_table.get("bold") { @@ -448,7 +446,7 @@ fn rules_from_config(config: &StackedConfig) -> Result { } if let Some(item) = style_table.get("underline") { if let Some(value) = item.as_bool() { - style.underlined = Some(value); + style.underline = Some(value); } } result.push((labels, style)); diff --git a/cli/src/template_builder.rs b/cli/src/template_builder.rs index b61757dd76..0b0246de8f 100644 --- a/cli/src/template_builder.rs +++ b/cli/src/template_builder.rs @@ -1643,10 +1643,10 @@ mod tests { self.aliases_map.insert(decl, defn).unwrap(); } - fn add_color(&mut self, label: &str, fg_color: crossterm::style::Color) { + fn add_color(&mut self, label: &str, fg: crossterm::style::Color) { let labels = label.split_whitespace().map(|s| s.to_owned()).collect(); let style = formatter::Style { - fg_color: Some(fg_color), + fg: Some(fg), ..Default::default() }; self.color_rules.push((labels, style));