summaryrefslogtreecommitdiffstats
path: root/zellij-utils/src/input/theme.rs
diff options
context:
space:
mode:
authorJae-Heon Ji <32578710+jaeheonji@users.noreply.github.com>2022-07-24 21:30:58 +0900
committerGitHub <noreply@github.com>2022-07-24 21:30:58 +0900
commit09481aac1986b7486e92d9dcd2baeb96a5d71e6e (patch)
treec64e376d003e9fb01d5f5567854e4bb5d816243d /zellij-utils/src/input/theme.rs
parenteacac9fe67d6cebb27b7f5bcff4f35ca399b13ce (diff)
feat: support `themes` directory (#1577)
* feat: add serde struct for theme file * feat: update client to load the theme palette * feat: merge themes in the setup phase * chore: delete debug message in test * feat: add theme_dir options * fix: boxing large enum variant
Diffstat (limited to 'zellij-utils/src/input/theme.rs')
-rw-r--r--zellij-utils/src/input/theme.rs51
1 files changed, 48 insertions, 3 deletions
diff --git a/zellij-utils/src/input/theme.rs b/zellij-utils/src/input/theme.rs
index fc8329a7e..b148e0d43 100644
--- a/zellij-utils/src/input/theme.rs
+++ b/zellij-utils/src/input/theme.rs
@@ -2,15 +2,26 @@ use serde::{
de::{Error, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
+
+use std::fs::File;
+use std::io::Read;
+use std::path::Path;
use std::{collections::HashMap, fmt};
-use super::options::Options;
+use super::{config::ConfigError, options::Options};
use crate::data::{Palette, PaletteColor};
use crate::shared::detect_theme_hue;
/// Intermediate deserialization of themes
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
-pub struct ThemesFromYaml(HashMap<String, Theme>);
+pub struct ThemesFromYamlIntermediate(HashMap<String, Theme>);
+
+#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
+pub struct ThemesFromYaml {
+ pub themes: ThemesFromYamlIntermediate,
+}
+
+type ThemesFromYamlResult = Result<ThemesFromYaml, ConfigError>;
#[derive(Debug, Default, Clone, Copy, PartialEq, Deserialize, Serialize)]
pub struct UiConfigFromYaml {
@@ -23,7 +34,12 @@ pub struct FrameConfigFromYaml {
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
-struct Theme {
+struct ThemeFromYaml {
+ palette: PaletteFromYaml,
+}
+
+#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
+pub struct Theme {
#[serde(flatten)]
palette: PaletteFromYaml,
}
@@ -125,6 +141,30 @@ impl Default for PaletteColorFromYaml {
}
impl ThemesFromYaml {
+ pub fn from_path(theme_path: &Path) -> ThemesFromYamlResult {
+ let mut theme_file = File::open(&theme_path)
+ .or_else(|_| File::open(&theme_path.with_extension("yaml")))
+ .map_err(|e| ConfigError::IoPath(e, theme_path.into()))?;
+
+ let mut theme = String::new();
+ theme_file.read_to_string(&mut theme)?;
+
+ let theme: ThemesFromYaml = match serde_yaml::from_str(&theme) {
+ Err(e) => return Err(ConfigError::Serde(e)),
+ Ok(theme) => theme,
+ };
+
+ Ok(theme)
+ }
+}
+
+impl From<ThemesFromYaml> for ThemesFromYamlIntermediate {
+ fn from(yaml: ThemesFromYaml) -> Self {
+ yaml.themes
+ }
+}
+
+impl ThemesFromYamlIntermediate {
pub fn theme_config(self, opts: &Options) -> Option<Palette> {
let mut from_yaml = self;
match &opts.theme {
@@ -182,3 +222,8 @@ impl From<PaletteColorFromYaml> for PaletteColor {
}
}
}
+
+// The unit test location.
+#[cfg(test)]
+#[path = "./unit/theme_test.rs"]
+mod theme_test;