on master help sign in
Allow ANSI colors in themes (#5119)
commit29638babea410d6d8397b02e6a826de0076a69c0 authortomleb <tomleb@users.noreply.github.com> dateSun 18 Jun 2023, 22:02 (3 years ago) parentd5af603
1 file changed+19−4
helix-view/src/theme.rs+19−4
helix-view/src/theme.rs+19−4
@@ -390,8 +390,23 @@ impl ThemePalette {
390390 Self { palette: default }
391391 }
392392
393− pub fn hex_string_to_rgb(s: &str) -> Result<Color, String> {
394− if s.starts_with('#') && s.len() >= 7 {
393+ pub fn string_to_rgb(s: &str) -> Result<Color, String> {
394+ if s.starts_with('#') {
395+ Self::hex_string_to_rgb(s)
396+ } else {
397+ Self::ansi_string_to_rgb(s)
398+ }
399+ }
400+
401+ fn ansi_string_to_rgb(s: &str) -> Result<Color, String> {
402+ if let Ok(index) = s.parse::<u8>() {
403+ return Ok(Color::Indexed(index));
404+ }
405+ Err(format!("Theme: malformed ANSI: {}", s))
406+ }
407+
408+ fn hex_string_to_rgb(s: &str) -> Result<Color, String> {
409+ if s.len() >= 7 {
395410 if let (Ok(red), Ok(green), Ok(blue)) = (
396411 u8::from_str_radix(&s[1..3], 16),
397412 u8::from_str_radix(&s[3..5], 16),
@@ -417,7 +432,7 @@ impl ThemePalette {
417432 .get(value)
418433 .copied()
419434 .ok_or("")
420− .or_else(|_| Self::hex_string_to_rgb(value))
435+ .or_else(|_| Self::string_to_rgb(value))
421436 }
422437
423438 pub fn parse_modifier(value: &Value) -> Result<Modifier, String> {
@@ -493,7 +508,7 @@ impl TryFrom<Value> for ThemePalette {
493508 let mut palette = HashMap::with_capacity(map.len());
494509 for (name, value) in map {
495510 let value = Self::parse_value_as_str(&value)?;
496− let color = Self::hex_string_to_rgb(value)?;
511+ let color = Self::string_to_rgb(value)?;
497512 palette.insert(name, color);
498513 }
499514