diff --git a/src/info/mod.rs b/src/info/mod.rs index 7f68d5af4..4e4d2b6d9 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -1,6 +1,6 @@ use crate::cli::{self, Config}; use crate::ui::get_ascii_colors; -use crate::ui::text_color::TextColor; +use crate::ui::{text_color::TextColor, ColorizeOption}; use anyhow::Result; use author::Author; use colored::{Color, ColoredString, Colorize}; @@ -56,7 +56,7 @@ impl std::fmt::Display for Info { let (git_info_field_str, git_info_field_len) = self.get_git_info_field(); writeln!(f, "{}", git_info_field_str)?; let separator = "-".repeat(git_info_field_len); - writeln!(f, "{}", separator.color(self.text_colors.underline))?; + writeln!(f, "{}", separator.try_color(self.text_colors.underline))?; } if !self.config.disabled_fields.project && !self.repo_name.is_empty() { @@ -225,7 +225,7 @@ impl Info { info: &str, f: &mut std::fmt::Formatter, ) -> std::fmt::Result { - let info_colored = info.color(self.text_colors.info); + let info_colored = info.try_color(self.text_colors.info); writeln!( f, "{} {}", @@ -247,7 +247,7 @@ impl Info { let formatted_label = format!( "{}{}", label.color(self.text_colors.subtitle), - ":".color(self.text_colors.colon) + ":".try_color(self.text_colors.colon) ); self.bold(&formatted_label) } @@ -268,7 +268,7 @@ impl Info { format!( "{} {} {}", &self.bold(&self.git_username).color(self.text_colors.title), - &self.bold("~").color(self.text_colors.tilde), + &self.bold("~").try_color(self.text_colors.tilde), &self.bold(&self.git_version).color(self.text_colors.title) ), git_info_length + 3, @@ -291,7 +291,7 @@ impl Info { let pad = title.len() + 2; for (i, author) in self.authors.iter().enumerate() { - let author_str = format!("{}", author).color(self.text_colors.info); + let author_str = format!("{}", author).try_color(self.text_colors.info); if i == 0 { author_field.push_str(&format!("{}", author_str)); @@ -351,7 +351,7 @@ impl Info { for (i, language) in languages.iter().enumerate() { let formatted_number = format!("{:.*}", 1, language.1); let language_with_perc = - format!("{} ({} %)", language.0, formatted_number).color(self.text_colors.info); + format!("{} ({} %)", language.0, formatted_number).try_color(self.text_colors.info); let language_chip = "\u{25CF}".color(language.2); let language_str = format!("{} {} ", language_chip, language_with_perc); if i % 2 == 0 { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 0b3446eb0..be968c741 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,11 +1,28 @@ use crate::info::langs::language::Language; -use colored::Color; +use colored::{Color, ColoredString, Colorize}; pub mod ascii_art; pub mod image_backends; pub mod printer; pub mod text_color; +pub trait ColorizeOption { + fn try_color>(self, color: Option) -> ColoredString; +} + +impl ColorizeOption for T +where + T: Colorize, + T: Into, +{ + fn try_color>(self, color: Option) -> ColoredString { + match color { + Some(color) => Colorize::color(self, color), + None => self.into(), + } + } +} + pub fn get_ascii_colors( ascii_language: &Option, dominant_language: &Language, diff --git a/src/ui/text_color.rs b/src/ui/text_color.rs index 15ea3d819..46461f3a6 100644 --- a/src/ui/text_color.rs +++ b/src/ui/text_color.rs @@ -3,22 +3,22 @@ use colored::Color; pub struct TextColor { pub title: Color, - pub tilde: Color, - pub underline: Color, + pub tilde: Option, + pub underline: Option, pub subtitle: Color, - pub colon: Color, - pub info: Color, + pub colon: Option, + pub info: Option, } impl TextColor { fn new(color: Color) -> TextColor { TextColor { title: color, - tilde: Color::White, - underline: Color::White, + tilde: None, + underline: None, subtitle: color, - colon: Color::White, - info: Color::White, + colon: None, + info: None, } } @@ -37,11 +37,11 @@ impl TextColor { .collect::>(); text_color_set.title = *custom_color.get(0).unwrap_or(&default_colors[0]); - text_color_set.tilde = *custom_color.get(1).unwrap_or(&Color::White); - text_color_set.underline = *custom_color.get(2).unwrap_or(&Color::White); + text_color_set.tilde = custom_color.get(1).cloned(); + text_color_set.underline = custom_color.get(2).cloned(); text_color_set.subtitle = *custom_color.get(3).unwrap_or(&default_colors[0]); - text_color_set.colon = *custom_color.get(4).unwrap_or(&Color::White); - text_color_set.info = *custom_color.get(5).unwrap_or(&Color::White); + text_color_set.colon = custom_color.get(4).cloned(); + text_color_set.info = custom_color.get(5).cloned(); } text_color_set