Skip to content

Commit

Permalink
Allow ANSI colors in themes
Browse files Browse the repository at this point in the history
  • Loading branch information
tomleb committed Dec 11, 2022
1 parent cdc54f5 commit 6a267d5
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions helix-view/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,23 @@ impl ThemePalette {
Self { palette: default }
}

pub fn hex_string_to_rgb(s: &str) -> Result<Color, String> {
if s.starts_with('#') && s.len() >= 7 {
pub fn string_to_rgb(s: &str) -> Result<Color, String> {
if s.starts_with('#') {
Self::hex_string_to_rgb(s)
} else {
Self::ansi_string_to_rgb(s)
}
}

fn ansi_string_to_rgb(s: &str) -> Result<Color, String> {
if let Ok(index) = u8::from_str_radix(s, 10) {
return Ok(Color::Indexed(index))
}
Err(format!("Theme: malformed ANSI: {}", s))
}

fn hex_string_to_rgb(s: &str) -> Result<Color, String> {
if s.len() >= 7 {
if let (Ok(red), Ok(green), Ok(blue)) = (
u8::from_str_radix(&s[1..3], 16),
u8::from_str_radix(&s[3..5], 16),
Expand All @@ -377,7 +392,7 @@ impl ThemePalette {
.get(value)
.copied()
.ok_or("")
.or_else(|_| Self::hex_string_to_rgb(value))
.or_else(|_| Self::string_to_rgb(value))
}

pub fn parse_modifier(value: &Value) -> Result<Modifier, String> {
Expand Down Expand Up @@ -453,7 +468,7 @@ impl TryFrom<Value> for ThemePalette {
let mut palette = HashMap::with_capacity(map.len());
for (name, value) in map {
let value = Self::parse_value_as_str(&value)?;
let color = Self::hex_string_to_rgb(value)?;
let color = Self::string_to_rgb(value)?;
palette.insert(name, color);
}

Expand Down

0 comments on commit 6a267d5

Please sign in to comment.