Skip to content

Commit 0ef5f58

Browse files
authored
Default to terminal foreground color for tilde, underline, colon and info (#604)
* default to terminal foreground color for tilde, underline, colon and info * use ColorizeOption trait
1 parent 29e1056 commit 0ef5f58

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

src/info/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cli::{self, Config};
22
use crate::ui::get_ascii_colors;
3-
use crate::ui::text_color::TextColor;
3+
use crate::ui::{text_color::TextColor, ColorizeOption};
44
use anyhow::Result;
55
use author::Author;
66
use colored::{Color, ColoredString, Colorize};
@@ -56,7 +56,7 @@ impl std::fmt::Display for Info {
5656
let (git_info_field_str, git_info_field_len) = self.get_git_info_field();
5757
writeln!(f, "{}", git_info_field_str)?;
5858
let separator = "-".repeat(git_info_field_len);
59-
writeln!(f, "{}", separator.color(self.text_colors.underline))?;
59+
writeln!(f, "{}", separator.try_color(self.text_colors.underline))?;
6060
}
6161

6262
if !self.config.disabled_fields.project && !self.repo_name.is_empty() {
@@ -225,7 +225,7 @@ impl Info {
225225
info: &str,
226226
f: &mut std::fmt::Formatter,
227227
) -> std::fmt::Result {
228-
let info_colored = info.color(self.text_colors.info);
228+
let info_colored = info.try_color(self.text_colors.info);
229229
writeln!(
230230
f,
231231
"{} {}",
@@ -247,7 +247,7 @@ impl Info {
247247
let formatted_label = format!(
248248
"{}{}",
249249
label.color(self.text_colors.subtitle),
250-
":".color(self.text_colors.colon)
250+
":".try_color(self.text_colors.colon)
251251
);
252252
self.bold(&formatted_label)
253253
}
@@ -268,7 +268,7 @@ impl Info {
268268
format!(
269269
"{} {} {}",
270270
&self.bold(&self.git_username).color(self.text_colors.title),
271-
&self.bold("~").color(self.text_colors.tilde),
271+
&self.bold("~").try_color(self.text_colors.tilde),
272272
&self.bold(&self.git_version).color(self.text_colors.title)
273273
),
274274
git_info_length + 3,
@@ -291,7 +291,7 @@ impl Info {
291291
let pad = title.len() + 2;
292292

293293
for (i, author) in self.authors.iter().enumerate() {
294-
let author_str = format!("{}", author).color(self.text_colors.info);
294+
let author_str = format!("{}", author).try_color(self.text_colors.info);
295295

296296
if i == 0 {
297297
author_field.push_str(&format!("{}", author_str));
@@ -351,7 +351,7 @@ impl Info {
351351
for (i, language) in languages.iter().enumerate() {
352352
let formatted_number = format!("{:.*}", 1, language.1);
353353
let language_with_perc =
354-
format!("{} ({} %)", language.0, formatted_number).color(self.text_colors.info);
354+
format!("{} ({} %)", language.0, formatted_number).try_color(self.text_colors.info);
355355
let language_chip = "\u{25CF}".color(language.2);
356356
let language_str = format!("{} {} ", language_chip, language_with_perc);
357357
if i % 2 == 0 {

src/ui/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
use crate::info::langs::language::Language;
2-
use colored::Color;
2+
use colored::{Color, ColoredString, Colorize};
33

44
pub mod ascii_art;
55
pub mod image_backends;
66
pub mod printer;
77
pub mod text_color;
88

9+
pub trait ColorizeOption {
10+
fn try_color<S: Into<Color>>(self, color: Option<S>) -> ColoredString;
11+
}
12+
13+
impl<T> ColorizeOption for T
14+
where
15+
T: Colorize,
16+
T: Into<ColoredString>,
17+
{
18+
fn try_color<S: Into<Color>>(self, color: Option<S>) -> ColoredString {
19+
match color {
20+
Some(color) => Colorize::color(self, color),
21+
None => self.into(),
22+
}
23+
}
24+
}
25+
926
pub fn get_ascii_colors(
1027
ascii_language: &Option<Language>,
1128
dominant_language: &Language,

src/ui/text_color.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ use colored::Color;
33

44
pub struct TextColor {
55
pub title: Color,
6-
pub tilde: Color,
7-
pub underline: Color,
6+
pub tilde: Option<Color>,
7+
pub underline: Option<Color>,
88
pub subtitle: Color,
9-
pub colon: Color,
10-
pub info: Color,
9+
pub colon: Option<Color>,
10+
pub info: Option<Color>,
1111
}
1212

1313
impl TextColor {
1414
fn new(color: Color) -> TextColor {
1515
TextColor {
1616
title: color,
17-
tilde: Color::White,
18-
underline: Color::White,
17+
tilde: None,
18+
underline: None,
1919
subtitle: color,
20-
colon: Color::White,
21-
info: Color::White,
20+
colon: None,
21+
info: None,
2222
}
2323
}
2424

@@ -37,11 +37,11 @@ impl TextColor {
3737
.collect::<Vec<Color>>();
3838

3939
text_color_set.title = *custom_color.get(0).unwrap_or(&default_colors[0]);
40-
text_color_set.tilde = *custom_color.get(1).unwrap_or(&Color::White);
41-
text_color_set.underline = *custom_color.get(2).unwrap_or(&Color::White);
40+
text_color_set.tilde = custom_color.get(1).cloned();
41+
text_color_set.underline = custom_color.get(2).cloned();
4242
text_color_set.subtitle = *custom_color.get(3).unwrap_or(&default_colors[0]);
43-
text_color_set.colon = *custom_color.get(4).unwrap_or(&Color::White);
44-
text_color_set.info = *custom_color.get(5).unwrap_or(&Color::White);
43+
text_color_set.colon = custom_color.get(4).cloned();
44+
text_color_set.info = custom_color.get(5).cloned();
4545
}
4646

4747
text_color_set

0 commit comments

Comments
 (0)