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 | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs index e2e10932..173a40f3 100644 --- a/helix-view/src/theme.rs +++ b/helix-view/src/theme.rs @@ -35,6 +35,75 @@ 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 |