From 798a661d251791dce07f79602cd69d34bb0a8471 Mon Sep 17 00:00:00 2001 From: John Toohey Date: Tue, 2 Jan 2024 11:46:00 +0000 Subject: [PATCH] Added parsing for Ansi 3 bit colours --- src/color.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/theme.rs | 4 ++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/color.rs b/src/color.rs index dae45ae..2f9db25 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,3 +1,5 @@ +use std::str::FromStr; + use crate::error::{Result, VividError}; use ansi_colours::ansi256_from_rgb; @@ -100,10 +102,41 @@ impl Color { Color::Ansi3Bit(color) => format!("{}", *color as u8 + colortype.bg_addition()), } } + + fn from_ansi_name(s: &str) -> Result { + match s { + "black" => Ok(Self::Ansi3Bit(Ansi3Bit::Black)), + "red" => Ok(Self::Ansi3Bit(Ansi3Bit::Red)), + "green" => Ok(Self::Ansi3Bit(Ansi3Bit::Green)), + "yellow" => Ok(Self::Ansi3Bit(Ansi3Bit::Yellow)), + "blue" => Ok(Self::Ansi3Bit(Ansi3Bit::Blue)), + "magenta" => Ok(Self::Ansi3Bit(Ansi3Bit::Magenta)), + "cyan" => Ok(Self::Ansi3Bit(Ansi3Bit::Cyan)), + "white" => Ok(Self::Ansi3Bit(Ansi3Bit::White)), + "bright_black" => Ok(Self::Ansi3Bit(Ansi3Bit::BrightBlack)), + "bright_red" => Ok(Self::Ansi3Bit(Ansi3Bit::BrightRed)), + "bright_green" => Ok(Self::Ansi3Bit(Ansi3Bit::BrightGreen)), + "bright_yellow" => Ok(Self::Ansi3Bit(Ansi3Bit::BrightYellow)), + "bright_blue" => Ok(Self::Ansi3Bit(Ansi3Bit::BrightBlue)), + "bright_magenta" => Ok(Self::Ansi3Bit(Ansi3Bit::BrightMagenta)), + "bright_cyan" => Ok(Self::Ansi3Bit(Ansi3Bit::BrightCyan)), + "bright_white" => Ok(Self::Ansi3Bit(Ansi3Bit::BrightWhite)), + _ => Err(VividError::ColorParseError(s.to_string())), + } + } +} + +impl FromStr for Color { + type Err = VividError; + fn from_str(s: &str) -> Result { + Color::from_hex_str(s).or(Color::from_ansi_name(s)) + } } #[cfg(test)] mod tests { + use crate::color::Ansi3Bit; + use super::{Color, ColorMode, ColorType}; #[test] @@ -156,4 +189,18 @@ mod tests { let style_24bit = red.get_style(ColorType::Foreground, ColorMode::BitDepth24); assert_eq!("38;2;255;0;0", style_24bit); } + + #[test] + fn ansi_3bit() { + assert_eq!(Color::Ansi3Bit(Ansi3Bit::Black), "black".parse().unwrap()); + assert_eq!(Color::Ansi3Bit(Ansi3Bit::Green), "green".parse().unwrap()); + assert_eq!( + Color::Ansi3Bit(Ansi3Bit::BrightYellow), + "bright_yellow".parse().unwrap() + ); + assert_eq!( + Color::Ansi3Bit(Ansi3Bit::BrightCyan), + "bright_cyan".parse().unwrap() + ); + } } diff --git a/src/theme.rs b/src/theme.rs index 42a6988..2095d8c 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -51,7 +51,7 @@ impl Theme { for (key, value) in map { match (key, value) { (Yaml::String(key), Yaml::String(value)) => { - colors.insert(key.clone(), Color::from_hex_str(value)?); + colors.insert(key.clone(), value.parse()?); } _ => return Err(VividError::UnexpectedYamlType), } @@ -71,7 +71,7 @@ impl Theme { self.colors .get(color_str) .cloned() - .or_else(|| Color::from_hex_str(color_str).ok()) + .or_else(|| color_str.parse().ok()) .ok_or_else(|| VividError::UnknownColor(color_str.to_string())) }