From 169ee6e6e58c58c19030632203c64fbe0cefce36 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Tue, 16 Feb 2021 03:20:47 -0700 Subject: [PATCH] Fix colors with comprehensive tests --- src/app.rs | 30 ++-- src/config.rs | 394 ++++++++++++++++++++++++++++---------------------- 2 files changed, 243 insertions(+), 181 deletions(-) diff --git a/src/app.rs b/src/app.rs index 20cd0c6..d30c011 100644 --- a/src/app.rs +++ b/src/app.rs @@ -235,7 +235,7 @@ impl TTApp { .year(self.calendar_year) .date_style(dates_with_styles) .months_per_row(self.config.uda_calendar_months_per_row); - c.title_background_color = self.config.uda_style_calendar_title.bg; + c.title_background_color = self.config.uda_style_calendar_title.bg.unwrap_or(Color::Black); f.render_widget(c, rects[0]); } @@ -457,9 +457,7 @@ impl TTApp { for (i, context) in contexts.iter().enumerate() { let mut style = Style::default(); if &self.contexts[i].active == "yes" { - style = style - .fg(self.config.uda_style_context_active.fg) - .bg(self.config.uda_style_context_active.bg) + style = self.config.uda_style_context_active; } rows.push(Row::StyledData(context.iter(), style)); if i == self.context_table_state.selected().unwrap_or_default() { @@ -569,23 +567,31 @@ impl TTApp { for tag_name in virtual_tag_names_in_precedence.iter().rev() { if tag_name == "uda." || tag_name == "priority" { if let Some(p) = task.priority() { - let c = self + let s = self .config .color .get(&format!("color.uda.priority.{}", p)) .cloned() .unwrap_or_default(); - style = config::blend(style, c); + style = style.patch(s); + } + } else if tag_name == "tag." { + if let Some(tags) = task.tags() { + for t in tags { + let color_tag_name = format!("color.tag.{}", t); + let s = self.config.color.get(&color_tag_name).cloned().unwrap_or_default(); + style = style.patch(s); + } } } else if tag_name == "project." { if let Some(p) = task.project() { - let c = self + let s = self .config .color .get(&format!("color.project.{}", p)) .cloned() .unwrap_or_default(); - style = config::blend(style, c); + style = style.patch(s); } } else if task .tags() @@ -593,8 +599,8 @@ impl TTApp { .contains(&tag_name.to_string().replace(".", "").to_uppercase()) { let color_tag_name = format!("color.{}", tag_name); - let c = self.config.color.get(&color_tag_name).cloned().unwrap_or_default(); - style = config::blend(style, c); + let s = self.config.color.get(&color_tag_name).cloned().unwrap_or_default(); + style = style.patch(s); } } @@ -1781,9 +1787,9 @@ mod tests { let style = app.style_for_task(&task); dbg!(style); - assert!(style == Style::default().fg(Color::Green)); + assert_eq!(style, Style::default().fg(Color::Indexed(2))); - let task = app.task_by_id(2).unwrap(); + let task = app.task_by_id(11).unwrap(); dbg!(task.tags().unwrap()); let style = app.style_for_task(&task); dbg!(style); diff --git a/src/config.rs b/src/config.rs index 69fa104..b0828b1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,78 +4,6 @@ use std::process::Command; use std::str; use tui::style::{Color, Modifier, Style}; -pub fn blend(style: Style, c: TColor) -> Style { - let mut style = style; - - if c.fg != Color::Reset { - style = style.fg(c.fg); - } - - if c.bg != Color::Reset { - style = style.bg(c.bg); - } - - for m in c.modifiers { - style = style.add_modifier(m); - } - - style -} - -#[derive(Debug, Clone)] -pub struct TColor { - pub fg: Color, - pub bg: Color, - pub modifiers: Vec, -} - -impl Default for TColor { - fn default() -> Self { - TColor::default() - } -} - -impl TColor { - pub fn default() -> Self { - Self { - fg: Color::Reset, - bg: Color::Reset, - modifiers: vec![], - } - } - - pub fn new(fg: Color, bg: Color, modifiers: Vec) -> Self { - Self { fg, bg, modifiers } - } - - pub fn upgrade(&mut self) { - if self.modifiers.contains(&Modifier::BOLD) { - self.fg = match self.fg { - Color::Red => Color::LightRed, - Color::Green => Color::LightGreen, - Color::Yellow => Color::LightYellow, - Color::Blue => Color::LightBlue, - Color::Magenta => Color::LightMagenta, - Color::Cyan => Color::LightCyan, - Color::White => Color::Indexed(7), - Color::Black => Color::Indexed(0), - x => x, - }; - self.bg = match self.bg { - Color::Red => Color::LightRed, - Color::Green => Color::LightGreen, - Color::Yellow => Color::LightYellow, - Color::Blue => Color::LightBlue, - Color::Magenta => Color::LightMagenta, - Color::Cyan => Color::LightCyan, - Color::White => Color::Indexed(7), - Color::Black => Color::Indexed(0), - x => x, - }; - } - } -} - trait TaskWarriorBool { fn get_bool(&self) -> Option; } @@ -107,7 +35,7 @@ impl TaskWarriorBool for str { #[derive(Debug)] pub struct Config { pub enabled: bool, - pub color: HashMap, + pub color: HashMap, pub filter: String, pub obfuscate: bool, pub print_empty_columns: bool, @@ -121,8 +49,8 @@ pub struct Config { pub uda_selection_dim: bool, pub uda_selection_blink: bool, pub uda_calendar_months_per_row: usize, - pub uda_style_context_active: TColor, - pub uda_style_calendar_title: TColor, + pub uda_style_context_active: Style, + pub uda_style_calendar_title: Style, } impl Config { @@ -144,10 +72,8 @@ impl Config { uda_selection_dim: Self::get_uda_selection_dim(), uda_selection_blink: Self::get_uda_selection_blink(), uda_calendar_months_per_row: Self::get_uda_months_per_row(), - uda_style_calendar_title: Self::get_uda_style("calendar.title") - .unwrap_or_else(|| TColor::new(Color::Reset, Color::Reset, vec![])), - uda_style_context_active: Self::get_uda_style("context.active") - .unwrap_or_else(|| TColor::new(Color::Reset, Color::Reset, vec![])), + uda_style_calendar_title: Self::get_uda_style("calendar.title").unwrap_or_default(), + uda_style_context_active: Self::get_uda_style("context.active").unwrap_or_default(), }) } @@ -155,7 +81,7 @@ impl Config { HashMap::new() } - fn get_uda_style(config: &str) -> Option { + fn get_uda_style(config: &str) -> Option