diff options
author | Canop <cano.petrole@gmail.com> | 2020-07-09 17:01:03 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2020-07-09 17:01:03 +0200 |
commit | d094c44ca6032fdc7f8c2da4bd31bc0aaeaa8adc (patch) | |
tree | 8612848721ec9e877163ca0c422f73c285829638 /src/skin/skin_entry.rs | |
parent | efd3ea97514545be187982bbe62d8ad012e17ebd (diff) |
add [ext-colors] to color files depending on their extension
Diffstat (limited to 'src/skin/skin_entry.rs')
-rw-r--r-- | src/skin/skin_entry.rs | 72 |
1 files changed, 3 insertions, 69 deletions
diff --git a/src/skin/skin_entry.rs b/src/skin/skin_entry.rs index 8fab1fd..a986013 100644 --- a/src/skin/skin_entry.rs +++ b/src/skin/skin_entry.rs @@ -3,14 +3,12 @@ /// a string with TTY colors use { + super::*, crate::errors::InvalidSkinError, crossterm::style::{ Attribute::{self, *}, Attributes, - Color::{self, *}, }, - std::result::Result, - super::*, termimad::CompoundStyle, }; @@ -50,70 +48,6 @@ impl SkinEntry { } -/// read a color from a string. -/// It may be either -/// - "none" -/// - one of the few known color name. Example: "darkred" -/// - grayscale with level in [0,24[. Example: "grey(5)" -/// - an Ansi code. Example "ansi(106)" -/// - RGB. Example: "rgb(25, 100, 0)" -/// This function needs a lowercase string (assuming lowercasing -/// has be done before, to ensure case-insensitive parsing) -fn parse_color(s: &str) -> Result<Option<Color>, InvalidSkinError> { - if let Some(c) = regex!(r"^ansi\((?P<value>\d+)\)$").captures(&s) { - let value: &str = c.name("value").unwrap().as_str(); - let value = value.parse(); - if let Ok(value) = value { - return Ok(ansi(value)); // all ANSI values are ok - } else { - return Err(InvalidSkinError::InvalidColor { raw: s.to_owned() }); - } - } - - if let Some(c) = regex!(r"^gr[ae]y(?:scale)?\((?P<level>\d+)\)$").captures(&s) { - let level: &str = c.name("level").unwrap().as_str(); - let level = level.parse(); - if let Ok(level) = level { - if level > 23 { - return Err(InvalidSkinError::InvalidGreyLevel { level }); - } - return Ok(gray(level)); - } else { - return Err(InvalidSkinError::InvalidColor { raw: s.to_owned() }); - } - } - - if let Some(c) = regex!(r"^rgb\((?P<r>\d+),\s*(?P<g>\d+),\s*(?P<b>\d+)\)$").captures(&s) { - let r = c.name("r").unwrap().as_str().parse(); - let g = c.name("g").unwrap().as_str().parse(); - let b = c.name("b").unwrap().as_str().parse(); - if let (Ok(r), Ok(g), Ok(b)) = (r, g, b) { - return Ok(rgb(r, g, b)); - } else { - return Err(InvalidSkinError::InvalidColor { raw: s.to_owned() }); - } - } - - match s { - "black" => Ok(rgb(0, 0, 0)), // crossterm black isn't black - "blue" => Ok(Some(Blue)), - "cyan" => Ok(Some(Cyan)), - "darkblue" => Ok(Some(DarkBlue)), - "darkcyan" => Ok(Some(DarkCyan)), - "darkgreen" => Ok(Some(DarkGreen)), - "darkmagenta" => Ok(Some(DarkMagenta)), - "darkred" => Ok(Some(DarkRed)), - "green" => Ok(Some(Green)), - "grey" => Ok(Some(Grey)), - "magenta" => Ok(Some(Magenta)), - "red" => Ok(Some(Red)), - "yellow" => Ok(Some(Yellow)), - "darkyellow" => Ok(Some(DarkYellow)), - "white" => Ok(Some(White)), - "none" => Ok(None), - _ => Err(InvalidSkinError::InvalidColor { raw: s.to_owned() }), - } -} fn parse_attribute(s: &str) -> Result<Attribute, InvalidSkinError> { match s { @@ -153,8 +87,8 @@ fn parse_compound_style(s: &str) -> Result<CompoundStyle, InvalidSkinError> { " ); if let Some(c) = parts_rex.captures(&s) { - let fg_color = parse_color(c.name("fg").unwrap().as_str())?; - let bg_color = parse_color(c.name("bg").unwrap().as_str())?; + let fg_color = colors::parse(c.name("fg").unwrap().as_str())?; + let bg_color = colors::parse(c.name("bg").unwrap().as_str())?; let attrs = parse_attributes(c.name("attributes").unwrap().as_str())?; Ok(CompoundStyle::new( fg_color, |