From 7b89eff51d4fa282b4d7966b3a65bfb58c1b9327 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Tue, 22 Mar 2022 09:46:19 -0400 Subject: [PATCH] Remap White ANSI color to Default (#625) Allows the "white" color to have the greatest chance of being a dark, visible color for light terminal themes. A macro is used to remap `White` to `Default` to minimize size of the diff and prevent contributors from needing to be familiar with the `Default` color on top of the 8 standard colors. In other places where `White` was assumed to be the default, `Default` is now used instead, also. For #33 Closes #611 --- src/info/langs/language.rs | 15 +++++++++++++-- src/info/mod.rs | 2 +- src/ui/ascii_art.rs | 24 ++++++++++++------------ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/info/langs/language.rs b/src/info/langs/language.rs index 8b16037f1..340e00230 100644 --- a/src/info/langs/language.rs +++ b/src/info/langs/language.rs @@ -8,9 +8,20 @@ pub struct Colors { true_colors: Option>, } +/// Maps colors to preferred versions. Used to allow contributors to include +/// colors with minimal confusion. +macro_rules! clean_color { + ( White ) => { + clean_color!(Default) + }; + ( $color:ident ) => { + DynColors::Ansi(AnsiColors::$color) + }; +} + macro_rules! define_colors { - ( [ $($color:ident),+ ] ) => { Colors { basic_colors: vec![$(DynColors::Ansi(AnsiColors::$color)),+], true_colors: None } }; - ( [ $($bc:ident),+ ] : [ $($c:ident($r:expr, $g:expr, $b:expr)),+ ] ) => { Colors { basic_colors: vec![$(DynColors::Ansi(AnsiColors::$bc)),+], true_colors: Some(vec![$(DynColors::$c($r, $g, $b)),+]) } }; + ( [ $($color:ident),+ ] ) => { Colors { basic_colors: vec![$( clean_color!($color) ),+], true_colors: None } }; + ( [ $($bc:ident),+ ] : [ $($c:ident($r:expr, $g:expr, $b:expr)),+ ] ) => { Colors { basic_colors: vec![$(clean_color!($bc)),+], true_colors: Some(vec![$(DynColors::$c($r, $g, $b)),+]) } }; } #[derive(PartialEq, EnumString, EnumIter, IntoStaticStr)] diff --git a/src/info/mod.rs b/src/info/mod.rs index 363a90ab6..19703310d 100644 --- a/src/info/mod.rs +++ b/src/info/mod.rs @@ -335,7 +335,7 @@ impl Info { languages.push(( "Other".to_string(), other_perc, - DynColors::Ansi(AnsiColors::White), + DynColors::Ansi(AnsiColors::Default), )); languages } else { diff --git a/src/ui/ascii_art.rs b/src/ui/ascii_art.rs index f1566664d..7c507d075 100644 --- a/src/ui/ascii_art.rs +++ b/src/ui/ascii_art.rs @@ -155,7 +155,7 @@ impl<'a> Tokens<'a> { let mut width = end - start; let mut colored_segment = String::new(); let mut whole_string = String::new(); - let mut color = &DynColors::Ansi(AnsiColors::White); + let mut color = &DynColors::Ansi(AnsiColors::Default); self.truncate(start, end).for_each(|token| { match token { @@ -168,7 +168,7 @@ impl<'a> Tokens<'a> { colored_segment = String::new(); color = colors .get(col as usize) - .unwrap_or(&DynColors::Ansi(AnsiColors::White)); + .unwrap_or(&DynColors::Ansi(AnsiColors::Default)); } Token::Space => { width = width.saturating_sub(1); @@ -266,52 +266,52 @@ mod test { assert_eq!( Tokens("").render(&colors_shim, 0, 0, true), - "\u{1b}[37;1m\u{1b}[0m" + "\u{1b}[39;1m\u{1b}[0m" ); assert_eq!( Tokens(" ").render(&colors_shim, 0, 0, true), - "\u{1b}[37;1m\u{1b}[0m" + "\u{1b}[39;1m\u{1b}[0m" ); assert_eq!( Tokens(" ").render(&colors_shim, 0, 5, true), - "\u{1b}[37;1m \u{1b}[0m" + "\u{1b}[39;1m \u{1b}[0m" ); assert_eq!( Tokens(" ").render(&colors_shim, 1, 5, true), - "\u{1b}[37;1m \u{1b}[0m" + "\u{1b}[39;1m \u{1b}[0m" ); assert_eq!( Tokens(" ").render(&colors_shim, 3, 5, true), - "\u{1b}[37;1m \u{1b}[0m" + "\u{1b}[39;1m \u{1b}[0m" ); assert_eq!( Tokens(" ").render(&colors_shim, 0, 4, true), - "\u{1b}[37;1m \u{1b}[0m" + "\u{1b}[39;1m \u{1b}[0m" ); assert_eq!( Tokens(" ").render(&colors_shim, 0, 3, true), - "\u{1b}[37;1m \u{1b}[0m" + "\u{1b}[39;1m \u{1b}[0m" ); assert_eq!( Tokens(" {1} {5} {9} a").render(&colors_shim, 4, 10, true), - "\u{1b}[37;1m\u{1b}[0m\u{1b}[37;1m\u{1b}[0m\u{1b}[37;1m \u{1b}[0m\u{1b}[37;1m a\u{1b}[0m " + "\u{1b}[39;1m\u{1b}[0m\u{1b}[39;1m\u{1b}[0m\u{1b}[39;1m \u{1b}[0m\u{1b}[39;1m a\u{1b}[0m " ); // Tests for bold disabled assert_eq!( Tokens(" ").render(&colors_shim, 0, 0, false), - "\u{1b}[37m\u{1b}[0m" + "\u{1b}[39m\u{1b}[0m" ); assert_eq!( Tokens(" ").render(&colors_shim, 0, 5, false), - "\u{1b}[37m \u{1b}[0m" + "\u{1b}[39m \u{1b}[0m" ); }