diff options
| author | Dan Davison <dandavison7@gmail.com> | 2020-06-27 14:37:25 -0400 |
|---|---|---|
| committer | Dan Davison <dandavison7@gmail.com> | 2020-06-27 15:56:39 -0400 |
| commit | ba3fbd694cb00ff116aef8fc586787793a14cd2a (patch) | |
| tree | b0ad630248d6a4ff501605e9b5765082cabfd4bc /src | |
| parent | aa4b558819ae5882c5a0c37afa736aa1045acef3 (diff) | |
Refactor: ANSI 16 color (name, number) hash map
Diffstat (limited to 'src')
| -rw-r--r-- | src/color.rs | 71 | ||||
| -rw-r--r-- | src/parse_style.rs | 20 | ||||
| -rw-r--r-- | src/syntect_color.rs | 2 |
3 files changed, 51 insertions, 42 deletions
diff --git a/src/color.rs b/src/color.rs index da05c26..711b6ae 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,7 +1,9 @@ +use std::collections::HashMap; use std::process; use std::str::FromStr; use ansi_term::Color; +use lazy_static::lazy_static; use syntect::highlighting::Color as SyntectColor; use crate::bat::terminal::to_ansi_color; @@ -48,37 +50,44 @@ pub fn color_to_string(color: Color) -> String { // See // https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit -pub fn ansi_color_name_to_number(name: &str) -> Option<u8> { - match name.to_lowercase().as_ref() { - "black" => Some(0), - "red" => Some(1), - "green" => Some(2), - "yellow" => Some(3), - "blue" => Some(4), - "magenta" => Some(5), - "purple" => Some(5), - "cyan" => Some(6), - "white" => Some(7), - "bright-black" => Some(8), - "brightblack" => Some(8), - "bright-red" => Some(9), - "brightred" => Some(9), - "bright-green" => Some(10), - "brightgreen" => Some(10), - "bright-yellow" => Some(11), - "brightyellow" => Some(11), - "bright-blue" => Some(12), - "brightblue" => Some(12), - "bright-magenta" => Some(13), - "brightmagenta" => Some(13), - "bright-purple" => Some(13), - "brightpurple" => Some(13), - "bright-cyan" => Some(14), - "brightcyan" => Some(14), - "bright-white" => Some(15), - "brightwhite" => Some(15), - _ => None, - } +lazy_static! { + static ref ANSI_16_COLORS: HashMap<&'static str, u8> = { + vec![ + ("black", 0), + ("red", 1), + ("green", 2), + ("yellow", 3), + ("blue", 4), + ("magenta", 5), + ("purple", 5), + ("cyan", 6), + ("white", 7), + ("bright-black", 8), + ("brightblack", 8), + ("bright-red", 9), + ("brightred", 9), + ("bright-green", 10), + ("brightgreen", 10), + ("bright-yellow", 11), + ("brightyellow", 11), + ("bright-blue", 12), + ("brightblue", 12), + ("bright-magenta", 13), + ("brightmagenta", 13), + ("bright-purple", 13), + ("brightpurple", 13), + ("bright-cyan", 14), + ("brightcyan", 14), + ("bright-white", 15), + ("brightwhite", 15), + ] + .into_iter() + .collect() + }; +} + +pub fn ansi_16_color_name_to_number(name: &str) -> Option<u8> { + ANSI_16_COLORS.get(name).map(|n| *n) } pub fn get_minus_background_color_default(is_light_mode: bool, is_true_color: bool) -> Color { diff --git a/src/parse_style.rs b/src/parse_style.rs index e2a57d4..c06919f 100644 --- a/src/parse_style.rs +++ b/src/parse_style.rs @@ -330,7 +330,7 @@ mod tests { use ansi_term; - use crate::color::ansi_color_name_to_number; + use crate::color::ansi_16_color_name_to_number; #[test] fn test_parse_ansi_term_style() { @@ -343,7 +343,7 @@ mod tests { ( ansi_term::Style { foreground: Some(ansi_term::Color::Fixed( - ansi_color_name_to_number("red").unwrap() + ansi_16_color_name_to_number("red").unwrap() )), ..ansi_term::Style::new() }, @@ -357,10 +357,10 @@ mod tests { ( ansi_term::Style { foreground: Some(ansi_term::Color::Fixed( - ansi_color_name_to_number("red").unwrap() + ansi_16_color_name_to_number("red").unwrap() )), background: Some(ansi_term::Color::Fixed( - ansi_color_name_to_number("green").unwrap() + ansi_16_color_name_to_number("green").unwrap() )), ..ansi_term::Style::new() }, @@ -374,10 +374,10 @@ mod tests { ( ansi_term::Style { foreground: Some(ansi_term::Color::Fixed( - ansi_color_name_to_number("red").unwrap() + ansi_16_color_name_to_number("red").unwrap() )), background: Some(ansi_term::Color::Fixed( - ansi_color_name_to_number("green").unwrap() + ansi_16_color_name_to_number("green").unwrap() )), is_blink: true, is_bold: true, @@ -402,7 +402,7 @@ mod tests { ( ansi_term::Style { background: Some(ansi_term::Color::Fixed( - ansi_color_name_to_number("white").unwrap() + ansi_16_color_name_to_number("white").unwrap() )), is_italic: true, is_hidden: true, @@ -418,7 +418,7 @@ mod tests { ( ansi_term::Style { background: Some(ansi_term::Color::Fixed( - ansi_color_name_to_number("white").unwrap() + ansi_16_color_name_to_number("white").unwrap() )), is_bold: true, is_italic: true, @@ -444,7 +444,7 @@ mod tests { ( ansi_term::Style { background: Some(ansi_term::Color::Fixed( - ansi_color_name_to_number("white").unwrap() + ansi_16_color_name_to_number("white").unwrap() )), is_italic: true, is_hidden: true, @@ -469,7 +469,7 @@ mod tests { ( ansi_term::Style { background: Some(ansi_term::Color::Fixed( - ansi_color_name_to_number("white").unwrap() + ansi_16_color_name_to_number("white").unwrap() )), is_italic: true, is_hidden: true, diff --git a/src/syntect_color.rs b/src/syntect_color.rs index dccbc63..3569b31 100644 --- a/src/syntect_color.rs +++ b/src/syntect_color.rs @@ -5,7 +5,7 @@ use syntect::highlighting::Color; use crate::color; pub fn syntect_color_from_ansi_name(name: &str) -> Option<Color> { - color::ansi_color_name_to_number(name).and_then(syntect_color_from_ansi_number) + color::ansi_16_color_name_to_number(name).and_then(syntect_color_from_ansi_number) } /// Convert 8-bit ANSI code to #RGBA string with ANSI code in red channel and 0 in alpha channel. |
