|
|
@ -1,5 +1,6 @@
|
|
|
|
use std::{
|
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
collections::HashMap,
|
|
|
|
|
|
|
|
convert::TryFrom,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -11,8 +12,6 @@ use toml::Value;
|
|
|
|
|
|
|
|
|
|
|
|
pub use crate::graphics::{Color, Modifier, Style};
|
|
|
|
pub use crate::graphics::{Color, Modifier, Style};
|
|
|
|
|
|
|
|
|
|
|
|
/// Color theme for syntax highlighting.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub static DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| {
|
|
|
|
pub static DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| {
|
|
|
|
toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme")
|
|
|
|
toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme")
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -54,22 +53,10 @@ impl Loader {
|
|
|
|
.map(|entries| {
|
|
|
|
.map(|entries| {
|
|
|
|
entries
|
|
|
|
entries
|
|
|
|
.filter_map(|entry| {
|
|
|
|
.filter_map(|entry| {
|
|
|
|
if let Ok(entry) = entry {
|
|
|
|
let entry = entry.ok()?;
|
|
|
|
let path = entry.path();
|
|
|
|
let path = entry.path();
|
|
|
|
if let Some(ext) = path.extension() {
|
|
|
|
(path.extension()? == "toml")
|
|
|
|
if ext != "toml" {
|
|
|
|
.then(|| path.file_stem().unwrap().to_string_lossy().into_owned())
|
|
|
|
return None;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some(
|
|
|
|
|
|
|
|
entry
|
|
|
|
|
|
|
|
.file_name()
|
|
|
|
|
|
|
|
.to_string_lossy()
|
|
|
|
|
|
|
|
.trim_end_matches(".toml")
|
|
|
|
|
|
|
|
.to_owned(),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
})
|
|
|
@ -103,13 +90,23 @@ impl<'de> Deserialize<'de> for Theme {
|
|
|
|
let mut styles = HashMap::new();
|
|
|
|
let mut styles = HashMap::new();
|
|
|
|
|
|
|
|
|
|
|
|
if let Ok(mut colors) = HashMap::<String, Value>::deserialize(deserializer) {
|
|
|
|
if let Ok(mut colors) = HashMap::<String, Value>::deserialize(deserializer) {
|
|
|
|
let palette = parse_palette(colors.remove("palette"));
|
|
|
|
// TODO: alert user of parsing failures in editor
|
|
|
|
// scopes.reserve(colors.len());
|
|
|
|
let palette = colors
|
|
|
|
|
|
|
|
.remove("palette")
|
|
|
|
|
|
|
|
.map(|value| {
|
|
|
|
|
|
|
|
ThemePalette::try_from(value).unwrap_or_else(|err| {
|
|
|
|
|
|
|
|
warn!("{}", err);
|
|
|
|
|
|
|
|
ThemePalette::default()
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
|
|
styles.reserve(colors.len());
|
|
|
|
styles.reserve(colors.len());
|
|
|
|
for (name, style_value) in colors {
|
|
|
|
for (name, style_value) in colors {
|
|
|
|
let mut style = Style::default();
|
|
|
|
let mut style = Style::default();
|
|
|
|
parse_style(&mut style, style_value, &palette);
|
|
|
|
if let Err(err) = palette.parse_style(&mut style, style_value) {
|
|
|
|
// scopes.push(name);
|
|
|
|
warn!("{}", err);
|
|
|
|
|
|
|
|
}
|
|
|
|
styles.insert(name, style);
|
|
|
|
styles.insert(name, style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -119,121 +116,120 @@ impl<'de> Deserialize<'de> for Theme {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn parse_palette(value: Option<Value>) -> HashMap<String, Color> {
|
|
|
|
impl Theme {
|
|
|
|
match value {
|
|
|
|
pub fn get(&self, scope: &str) -> Style {
|
|
|
|
Some(Value::Table(entries)) => entries,
|
|
|
|
self.try_get(scope)
|
|
|
|
_ => return HashMap::default(),
|
|
|
|
.unwrap_or_else(|| Style::default().fg(Color::Rgb(0, 0, 255)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.into_iter()
|
|
|
|
|
|
|
|
.filter_map(|(name, value)| {
|
|
|
|
|
|
|
|
let color = parse_color(value, &HashMap::default())?;
|
|
|
|
|
|
|
|
Some((name, color))
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.collect()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn parse_style(style: &mut Style, value: Value, palette: &HashMap<String, Color>) {
|
|
|
|
pub fn try_get(&self, scope: &str) -> Option<Style> {
|
|
|
|
//TODO: alert user of parsing failures
|
|
|
|
self.styles.get(scope).copied()
|
|
|
|
if let Value::Table(entries) = value {
|
|
|
|
|
|
|
|
for (name, value) in entries {
|
|
|
|
|
|
|
|
match name.as_str() {
|
|
|
|
|
|
|
|
"fg" => {
|
|
|
|
|
|
|
|
if let Some(color) = parse_color(value, palette) {
|
|
|
|
|
|
|
|
*style = style.fg(color);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
"bg" => {
|
|
|
|
|
|
|
|
if let Some(color) = parse_color(value, palette) {
|
|
|
|
|
|
|
|
*style = style.bg(color);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
"modifiers" => {
|
|
|
|
|
|
|
|
if let Value::Array(arr) = value {
|
|
|
|
|
|
|
|
for modifier in arr.iter().filter_map(parse_modifier) {
|
|
|
|
|
|
|
|
*style = style.add_modifier(modifier);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
|
|
pub fn scopes(&self) -> &[String] {
|
|
|
|
|
|
|
|
&self.scopes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn find_scope_index(&self, scope: &str) -> Option<usize> {
|
|
|
|
|
|
|
|
self.scopes().iter().position(|s| s == scope)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Some(color) = parse_color(value, palette) {
|
|
|
|
}
|
|
|
|
*style = style.fg(color);
|
|
|
|
|
|
|
|
|
|
|
|
struct ThemePalette {
|
|
|
|
|
|
|
|
palette: HashMap<String, Color>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for ThemePalette {
|
|
|
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
|
|
|
Self::new(HashMap::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn hex_string_to_rgb(s: &str) -> Option<(u8, u8, u8)> {
|
|
|
|
impl ThemePalette {
|
|
|
|
|
|
|
|
pub fn new(palette: HashMap<String, Color>) -> Self {
|
|
|
|
|
|
|
|
Self { palette }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn hex_string_to_rgb(s: &str) -> Result<Color, String> {
|
|
|
|
if s.starts_with('#') && s.len() >= 7 {
|
|
|
|
if s.starts_with('#') && s.len() >= 7 {
|
|
|
|
if let (Ok(red), Ok(green), Ok(blue)) = (
|
|
|
|
if let (Ok(red), Ok(green), Ok(blue)) = (
|
|
|
|
u8::from_str_radix(&s[1..3], 16),
|
|
|
|
u8::from_str_radix(&s[1..3], 16),
|
|
|
|
u8::from_str_radix(&s[3..5], 16),
|
|
|
|
u8::from_str_radix(&s[3..5], 16),
|
|
|
|
u8::from_str_radix(&s[5..7], 16),
|
|
|
|
u8::from_str_radix(&s[5..7], 16),
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
Some((red, green, blue))
|
|
|
|
return Ok(Color::Rgb(red, green, blue));
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn parse_color(value: Value, palette: &HashMap<String, Color>) -> Option<Color> {
|
|
|
|
Err(format!("Theme: malformed hexcode: {}", s))
|
|
|
|
if let Value::String(s) = value {
|
|
|
|
|
|
|
|
if let Some(color) = palette.get(&s) {
|
|
|
|
|
|
|
|
Some(*color)
|
|
|
|
|
|
|
|
} else if let Some((red, green, blue)) = hex_string_to_rgb(&s) {
|
|
|
|
|
|
|
|
Some(Color::Rgb(red, green, blue))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
warn!("malformed hexcode in theme: {}", s);
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
warn!("unrecognized value in theme: {}", value);
|
|
|
|
fn parse_value_as_str(value: &Value) -> Result<&str, String> {
|
|
|
|
None
|
|
|
|
value
|
|
|
|
|
|
|
|
.as_str()
|
|
|
|
|
|
|
|
.ok_or(format!("Theme: unrecognized value: {}", value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn parse_modifier(value: &Value) -> Option<Modifier> {
|
|
|
|
pub fn parse_color(&self, value: Value) -> Result<Color, String> {
|
|
|
|
if let Value::String(s) = value {
|
|
|
|
let value = Self::parse_value_as_str(&value)?;
|
|
|
|
match s.as_str() {
|
|
|
|
|
|
|
|
"bold" => Some(Modifier::BOLD),
|
|
|
|
self.palette
|
|
|
|
"dim" => Some(Modifier::DIM),
|
|
|
|
.get(value)
|
|
|
|
"italic" => Some(Modifier::ITALIC),
|
|
|
|
.copied()
|
|
|
|
"underlined" => Some(Modifier::UNDERLINED),
|
|
|
|
.ok_or("")
|
|
|
|
"slow_blink" => Some(Modifier::SLOW_BLINK),
|
|
|
|
.or_else(|_| Self::hex_string_to_rgb(value))
|
|
|
|
"rapid_blink" => Some(Modifier::RAPID_BLINK),
|
|
|
|
}
|
|
|
|
"reversed" => Some(Modifier::REVERSED),
|
|
|
|
|
|
|
|
"hidden" => Some(Modifier::HIDDEN),
|
|
|
|
pub fn parse_modifier(value: &Value) -> Result<Modifier, String> {
|
|
|
|
"crossed_out" => Some(Modifier::CROSSED_OUT),
|
|
|
|
value
|
|
|
|
_ => {
|
|
|
|
.as_str()
|
|
|
|
warn!("unrecognized modifier in theme: {}", s);
|
|
|
|
.and_then(|s| s.parse().ok())
|
|
|
|
None
|
|
|
|
.ok_or(format!("Theme: invalid modifier: {}", value))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn parse_style(&self, style: &mut Style, value: Value) -> Result<(), String> {
|
|
|
|
|
|
|
|
if let Value::Table(entries) = value {
|
|
|
|
|
|
|
|
for (name, value) in entries {
|
|
|
|
|
|
|
|
match name.as_str() {
|
|
|
|
|
|
|
|
"fg" => *style = style.fg(self.parse_color(value)?),
|
|
|
|
|
|
|
|
"bg" => *style = style.bg(self.parse_color(value)?),
|
|
|
|
|
|
|
|
"modifiers" => {
|
|
|
|
|
|
|
|
let modifiers = value
|
|
|
|
|
|
|
|
.as_array()
|
|
|
|
|
|
|
|
.ok_or("Theme: modifiers should be an array")?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for modifier in modifiers {
|
|
|
|
|
|
|
|
*style = style.add_modifier(Self::parse_modifier(modifier)?);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => return Err(format!("Theme: invalid style attribute: {}", name)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
warn!("unrecognized modifier in theme: {}", value);
|
|
|
|
*style = style.fg(self.parse_color(value)?);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Theme {
|
|
|
|
impl TryFrom<Value> for ThemePalette {
|
|
|
|
pub fn get(&self, scope: &str) -> Style {
|
|
|
|
type Error = String;
|
|
|
|
self.try_get(scope)
|
|
|
|
|
|
|
|
.unwrap_or_else(|| Style::default().fg(Color::Rgb(0, 0, 255)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn try_get(&self, scope: &str) -> Option<Style> {
|
|
|
|
fn try_from(value: Value) -> Result<Self, Self::Error> {
|
|
|
|
self.styles.get(scope).copied()
|
|
|
|
let map = match value {
|
|
|
|
}
|
|
|
|
Value::Table(entries) => entries,
|
|
|
|
|
|
|
|
_ => return Ok(Self::default()),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
let mut palette = HashMap::with_capacity(map.len());
|
|
|
|
pub fn scopes(&self) -> &[String] {
|
|
|
|
for (name, value) in map {
|
|
|
|
&self.scopes
|
|
|
|
let value = Self::parse_value_as_str(&value)?;
|
|
|
|
|
|
|
|
let color = Self::hex_string_to_rgb(value)?;
|
|
|
|
|
|
|
|
palette.insert(name, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn find_scope_index(&self, scope: &str) -> Option<usize> {
|
|
|
|
Ok(Self::new(palette))
|
|
|
|
self.scopes().iter().position(|s| s == scope)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -242,23 +238,21 @@ fn test_parse_style_string() {
|
|
|
|
let fg = Value::String("#ffffff".to_string());
|
|
|
|
let fg = Value::String("#ffffff".to_string());
|
|
|
|
|
|
|
|
|
|
|
|
let mut style = Style::default();
|
|
|
|
let mut style = Style::default();
|
|
|
|
parse_style(&mut style, fg, &HashMap::default());
|
|
|
|
let palette = ThemePalette::default();
|
|
|
|
|
|
|
|
palette.parse_style(&mut style, fg).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(style, Style::default().fg(Color::Rgb(255, 255, 255)));
|
|
|
|
assert_eq!(style, Style::default().fg(Color::Rgb(255, 255, 255)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_palette() {
|
|
|
|
fn test_palette() {
|
|
|
|
|
|
|
|
use helix_core::hashmap;
|
|
|
|
let fg = Value::String("my_color".to_string());
|
|
|
|
let fg = Value::String("my_color".to_string());
|
|
|
|
|
|
|
|
|
|
|
|
let mut style = Style::default();
|
|
|
|
let mut style = Style::default();
|
|
|
|
parse_style(
|
|
|
|
let palette =
|
|
|
|
&mut style,
|
|
|
|
ThemePalette::new(hashmap! { "my_color".to_string() => Color::Rgb(255, 255, 255) });
|
|
|
|
fg,
|
|
|
|
palette.parse_style(&mut style, fg).unwrap();
|
|
|
|
&vec![("my_color".to_string(), Color::Rgb(255, 255, 255))]
|
|
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
|
|
.collect(),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(style, Style::default().fg(Color::Rgb(255, 255, 255)));
|
|
|
|
assert_eq!(style, Style::default().fg(Color::Rgb(255, 255, 255)));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -274,9 +268,10 @@ fn test_parse_style_table() {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let mut style = Style::default();
|
|
|
|
let mut style = Style::default();
|
|
|
|
|
|
|
|
let palette = ThemePalette::default();
|
|
|
|
if let Value::Table(entries) = table {
|
|
|
|
if let Value::Table(entries) = table {
|
|
|
|
for (_name, value) in entries {
|
|
|
|
for (_name, value) in entries {
|
|
|
|
parse_style(&mut style, value, &HashMap::default());
|
|
|
|
palette.parse_style(&mut style, value).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|