Skip to content

Commit

Permalink
Added parsing for Ansi 3 bit colours
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Toohey authored and sharkdp committed Jan 16, 2024
1 parent 8d2f918 commit 798a661
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
47 changes: 47 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use crate::error::{Result, VividError};
use ansi_colours::ansi256_from_rgb;

Expand Down Expand Up @@ -100,10 +102,41 @@ impl Color {
Color::Ansi3Bit(color) => format!("{}", *color as u8 + colortype.bg_addition()),
}
}

fn from_ansi_name(s: &str) -> Result<Color> {
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<Self> {
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]
Expand Down Expand Up @@ -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()
);
}
}
4 changes: 2 additions & 2 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -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()))
}

Expand Down

0 comments on commit 798a661

Please sign in to comment.