formatter: make Style::fg_color an Option

We want to be able combine styles by replacing only some of the
attributes (foreground color, underlining, etc.) in the config. We
could implement that having keeping the current style and then update
it based on what we find in the config for a label we just
added. However, it's simpler if we can parse a configured style
without knowing the current style and just return a `Style` with some
fields blank. This commit prepares for that by making the foreground
color field optional.
This commit is contained in:
Martin von Zweigbergk 2023-01-07 08:02:47 -08:00 committed by Martin von Zweigbergk
parent 2080913d15
commit db5dbf3540

View file

@ -150,17 +150,9 @@ impl<W: Write> Formatter for PlainTextFormatter<W> {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Style { pub struct Style {
pub fg_color: Color, pub fg_color: Option<Color>,
}
impl Default for Style {
fn default() -> Self {
Self {
fg_color: Color::Reset,
}
}
} }
pub struct ColorFormatter<W> { pub struct ColorFormatter<W> {
@ -239,16 +231,19 @@ impl<W: Write> ColorFormatter<W> {
} else if !is_bright(&new_style.fg_color) && is_bright(&self.current_style.fg_color) { } else if !is_bright(&new_style.fg_color) && is_bright(&self.current_style.fg_color) {
queue!(self.output, SetAttribute(Attribute::Reset))?; queue!(self.output, SetAttribute(Attribute::Reset))?;
} }
queue!(self.output, SetForegroundColor(new_style.fg_color))?; queue!(
self.output,
SetForegroundColor(new_style.fg_color.unwrap_or(Color::Reset))
)?;
self.current_style = new_style; self.current_style = new_style;
} }
Ok(()) Ok(())
} }
} }
fn is_bright(color: &Color) -> bool { fn is_bright(color: &Option<Color>) -> bool {
matches!( matches!(
color, color.unwrap_or(Color::Reset),
Color::DarkGrey Color::DarkGrey
| Color::Red | Color::Red
| Color::Green | Color::Green
@ -291,25 +286,25 @@ fn rules_from_config(config: &config::Config) -> HashMap<Vec<String>, Style> {
result result
} }
fn color_for_name(color_name: &str) -> Color { fn color_for_name(color_name: &str) -> Option<Color> {
match color_name { match color_name {
"black" => Color::Black, "black" => Some(Color::Black),
"red" => Color::DarkRed, "red" => Some(Color::DarkRed),
"green" => Color::DarkGreen, "green" => Some(Color::DarkGreen),
"yellow" => Color::DarkYellow, "yellow" => Some(Color::DarkYellow),
"blue" => Color::DarkBlue, "blue" => Some(Color::DarkBlue),
"magenta" => Color::DarkMagenta, "magenta" => Some(Color::DarkMagenta),
"cyan" => Color::DarkCyan, "cyan" => Some(Color::DarkCyan),
"white" => Color::Grey, "white" => Some(Color::Grey),
"bright black" => Color::DarkGrey, "bright black" => Some(Color::DarkGrey),
"bright red" => Color::Red, "bright red" => Some(Color::Red),
"bright green" => Color::Green, "bright green" => Some(Color::Green),
"bright yellow" => Color::Yellow, "bright yellow" => Some(Color::Yellow),
"bright blue" => Color::Blue, "bright blue" => Some(Color::Blue),
"bright magenta" => Color::Magenta, "bright magenta" => Some(Color::Magenta),
"bright cyan" => Color::Cyan, "bright cyan" => Some(Color::Cyan),
"bright white" => Color::White, "bright white" => Some(Color::White),
_ => Color::Reset, _ => None,
} }
} }