diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-07-12 16:37:59 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-07-13 10:43:41 -0400 |
commit | 3c1610ffd605bc775f160840ecb195449128e736 (patch) | |
tree | 06f670ac299a92330053f2ffac8af811baa1d5db | |
parent | d99a64c8c383e196693e438652eb2ad5006e48bc (diff) |
Refactor: simplify color parsing
-rw-r--r-- | src/color.rs | 14 | ||||
-rw-r--r-- | src/parse_style.rs | 6 |
2 files changed, 7 insertions, 13 deletions
diff --git a/src/color.rs b/src/color.rs index 53d6a929..ed0f4f75 100644 --- a/src/color.rs +++ b/src/color.rs @@ -9,7 +9,10 @@ use syntect::highlighting::Color as SyntectColor; use crate::bat::terminal::to_ansi_color; use crate::syntect_color; -pub fn color_from_rgb_or_ansi_code(s: &str, true_color: bool) -> Color { +pub fn parse_color(s: &str, true_color: bool) -> Option<Color> { + if s == "normal" { + return None; + } let die = || { eprintln!("Invalid color or style attribute: {}", s); process::exit(1); @@ -23,14 +26,7 @@ pub fn color_from_rgb_or_ansi_code(s: &str, true_color: bool) -> Color { .or_else(|| syntect_color::syntect_color_from_ansi_name(s)) .unwrap_or_else(die) }; - to_ansi_color(syntect_color, true_color) -} - -pub fn color_from_rgb_or_ansi_code_with_default(arg: &str, true_color: bool) -> Option<Color> { - match arg { - "normal" => None, - s => Some(color_from_rgb_or_ansi_code(s, true_color)), - } + Some(to_ansi_color(syntect_color, true_color)) } pub fn color_to_string(color: Color) -> String { diff --git a/src/parse_style.rs b/src/parse_style.rs index e24e7921..73a42c65 100644 --- a/src/parse_style.rs +++ b/src/parse_style.rs @@ -245,8 +245,7 @@ fn parse_ansi_term_style( style.foreground = default.and_then(|s| s.ansi_term_style.foreground); is_syntax_highlighted = default.map(|s| s.is_syntax_highlighted).unwrap_or(false); } else { - style.foreground = - color::color_from_rgb_or_ansi_code_with_default(word, true_color); + style.foreground = color::parse_color(word, true_color); } seen_foreground = true; } else if !seen_background { @@ -261,8 +260,7 @@ fn parse_ansi_term_style( background_is_auto = true; style.background = default.and_then(|s| s.ansi_term_style.background); } else { - style.background = - color::color_from_rgb_or_ansi_code_with_default(word, true_color); + style.background = color::parse_color(word, true_color); } seen_background = true; } else { |