Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/theme.rs')
| -rw-r--r-- | helix-view/src/theme.rs | 140 |
1 files changed, 5 insertions, 135 deletions
diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs index 173a40f3..61d490ff 100644 --- a/helix-view/src/theme.rs +++ b/helix-view/src/theme.rs @@ -35,75 +35,6 @@ pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| Theme { ..Theme::from(BASE16_DEFAULT_THEME_DATA.clone()) }); -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Mode { - Dark, - Light, -} - -#[cfg(feature = "term")] -impl From<termina::escape::csi::ThemeMode> for Mode { - fn from(mode: termina::escape::csi::ThemeMode) -> Self { - match mode { - termina::escape::csi::ThemeMode::Dark => Self::Dark, - termina::escape::csi::ThemeMode::Light => Self::Light, - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Config { - light: String, - dark: String, - /// A theme to choose when the terminal did not declare either light or dark mode. - /// When not specified the dark theme is preferred. - fallback: Option<String>, -} - -impl Config { - pub fn choose(&self, preference: Option<Mode>) -> &str { - match preference { - Some(Mode::Light) => &self.light, - Some(Mode::Dark) => &self.dark, - None => self.fallback.as_ref().unwrap_or(&self.dark), - } - } -} - -impl<'de> Deserialize<'de> for Config { - fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> - where - D: serde::Deserializer<'de>, - { - #[derive(Deserialize)] - #[serde(untagged, deny_unknown_fields, rename_all = "kebab-case")] - enum InnerConfig { - Constant(String), - Adaptive { - dark: String, - light: String, - fallback: Option<String>, - }, - } - - let inner = InnerConfig::deserialize(deserializer)?; - - let (light, dark, fallback) = match inner { - InnerConfig::Constant(theme) => (theme.clone(), theme.clone(), None), - InnerConfig::Adaptive { - light, - dark, - fallback, - } => (light, dark, fallback), - }; - - Ok(Self { - light, - dark, - fallback, - }) - } -} #[derive(Clone, Debug)] pub struct Loader { /// Theme directories to search from highest to lowest priority @@ -296,7 +227,6 @@ pub struct Theme { // tree-sitter highlight styles are stored in a Vec to optimize lookups scopes: Vec<String>, highlights: Vec<Style>, - rainbow_length: usize, } impl From<Value> for Theme { @@ -323,20 +253,12 @@ impl<'de> Deserialize<'de> for Theme { } } -#[allow(clippy::type_complexity)] fn build_theme_values( mut values: Map<String, Value>, -) -> ( - HashMap<String, Style>, - Vec<String>, - Vec<Style>, - usize, - Vec<String>, -) { +) -> (HashMap<String, Style>, Vec<String>, Vec<Style>, Vec<String>) { let mut styles = HashMap::new(); let mut scopes = Vec::new(); let mut highlights = Vec::new(); - let mut rainbow_length = 0; let mut warnings = Vec::new(); @@ -355,27 +277,6 @@ fn build_theme_values( styles.reserve(values.len()); scopes.reserve(values.len()); highlights.reserve(values.len()); - - for (i, style) in values - .remove("rainbow") - .and_then(|value| match palette.parse_style_array(value) { - Ok(styles) => Some(styles), - Err(err) => { - warnings.push(err); - None - } - }) - .unwrap_or_else(default_rainbow) - .into_iter() - .enumerate() - { - let name = format!("rainbow.{i}"); - styles.insert(name.clone(), style); - scopes.push(name); - highlights.push(style); - rainbow_length += 1; - } - for (name, style_value) in values { let mut style = Style::default(); if let Err(err) = palette.parse_style(&mut style, style_value) { @@ -388,19 +289,9 @@ fn build_theme_values( highlights.push(style); } - (styles, scopes, highlights, rainbow_length, warnings) + (styles, scopes, highlights, warnings) } -fn default_rainbow() -> Vec<Style> { - vec![ - Style::default().fg(Color::Red), - Style::default().fg(Color::Yellow), - Style::default().fg(Color::Green), - Style::default().fg(Color::Blue), - Style::default().fg(Color::Cyan), - Style::default().fg(Color::Magenta), - ] -} impl Theme { /// To allow `Highlight` to represent arbitrary RGB colors without turning it into an enum, /// we interpret the last 256^3 numbers as RGB. @@ -409,7 +300,7 @@ impl Theme { /// Interpret a Highlight with the RGB foreground fn decode_rgb_highlight(highlight: Highlight) -> Option<(u8, u8, u8)> { (highlight.get() > Self::RGB_START).then(|| { - let [b, g, r, ..] = (highlight.get() + 1).to_le_bytes(); + let [b, g, r, ..] = (highlight.get() + 1).to_ne_bytes(); (r, g, b) }) } @@ -418,7 +309,7 @@ impl Theme { pub fn rgb_highlight(r: u8, g: u8, b: u8) -> Highlight { // -1 because highlight is "non-max": u32::MAX is reserved for the null pointer // optimization. - Highlight::new(u32::from_le_bytes([b, g, r, u8::MAX]) - 1) + Highlight::new(u32::from_ne_bytes([b, g, r, u8::MAX]) - 1) } #[inline] @@ -491,10 +382,6 @@ impl Theme { }) } - pub fn rainbow_length(&self) -> usize { - self.rainbow_length - } - fn from_toml(value: Value) -> (Self, Vec<String>) { if let Value::Table(table) = value { Theme::from_keys(table) @@ -505,14 +392,12 @@ impl Theme { } fn from_keys(toml_keys: Map<String, Value>) -> (Self, Vec<String>) { - let (styles, scopes, highlights, rainbow_length, load_errors) = - build_theme_values(toml_keys); + let (styles, scopes, highlights, load_errors) = build_theme_values(toml_keys); let theme = Self { styles, scopes, highlights, - rainbow_length, ..Default::default() }; (theme, load_errors) @@ -656,21 +541,6 @@ impl ThemePalette { } Ok(()) } - - fn parse_style_array(&self, value: Value) -> Result<Vec<Style>, String> { - let mut styles = Vec::new(); - - for v in value - .as_array() - .ok_or_else(|| format!("Could not parse value as an array: '{value}'"))? - { - let mut style = Style::default(); - self.parse_style(&mut style, v.clone())?; - styles.push(style); - } - - Ok(styles) - } } impl TryFrom<Value> for ThemePalette { |