summaryrefslogtreecommitdiffstats
path: root/zellij-utils/src/input/theme.rs
diff options
context:
space:
mode:
authora-kenji <aks.kenji@protonmail.com>2021-05-29 14:53:26 +0200
committera-kenji <aks.kenji@protonmail.com>2021-05-30 15:07:21 +0200
commitce73b6cca0d6132a57a5a7a2321e1cec8b071720 (patch)
tree2aad7fc6a2b67d2c3957f6a4cc1b9599578651a7 /zellij-utils/src/input/theme.rs
parent00bbe2b0f83cb1c4f5c121bc7c10fb5d3e523bab (diff)
Add color theme config
* add option `theme` that allows for setting of a theme, the default is `default` * under `themes` the themes can be described as follows: either: ``` themes: default: fg: [0,0,0] bg: [0,0,0] black: [0,0,0] red: [0,0,0] green: [0,0,0] yellow: [0,0,0] blue: [0,0,0] magenta: [0,0,0] cyan: [0,0,0] white: [0,0,0] orange: [0,0,0] ``` or ``` themes: default: fg: 0 bg: 0 black: 0 red: 0 green: 0 yellow: 0 blue: 0 magenta: 0 cyan: 0 white: 0 orange: 0 ``` If the key is different from default, it needs to either be specified on start with `options --theme [THEME]`, or in the configuration file under theme: [THEME]. closes #390
Diffstat (limited to 'zellij-utils/src/input/theme.rs')
-rw-r--r--zellij-utils/src/input/theme.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/zellij-utils/src/input/theme.rs b/zellij-utils/src/input/theme.rs
new file mode 100644
index 000000000..35bd3d87b
--- /dev/null
+++ b/zellij-utils/src/input/theme.rs
@@ -0,0 +1,94 @@
+use serde::{Deserialize, Serialize};
+use std::collections::HashMap;
+
+use super::options::Options;
+use zellij_tile::data::{Palette, PaletteColor};
+
+/// Intermediate deserialization of themes
+#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
+pub struct ThemesFromYaml(HashMap<String, Theme>);
+
+#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
+struct Theme {
+ #[serde(flatten)]
+ palette: PaletteFromYaml,
+}
+
+/// Intermediate deserialization struct
+#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
+pub struct PaletteFromYaml {
+ pub fg: PaletteColorFromYaml,
+ pub bg: PaletteColorFromYaml,
+ pub black: PaletteColorFromYaml,
+ pub red: PaletteColorFromYaml,
+ pub green: PaletteColorFromYaml,
+ pub yellow: PaletteColorFromYaml,
+ pub blue: PaletteColorFromYaml,
+ pub magenta: PaletteColorFromYaml,
+ pub cyan: PaletteColorFromYaml,
+ pub white: PaletteColorFromYaml,
+ pub orange: PaletteColorFromYaml,
+}
+
+/// Intermediate deserialization enum
+// This is here in order to make the untagged enum work
+#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
+#[serde(untagged)]
+pub enum PaletteColorFromYaml {
+ Rgb((u8, u8, u8)),
+ EightBit(u8),
+}
+
+impl Default for PaletteColorFromYaml {
+ fn default() -> Self {
+ PaletteColorFromYaml::EightBit(0)
+ }
+}
+
+impl ThemesFromYaml {
+ pub fn theme_config(self, opts: &Options) -> Option<Palette> {
+ let mut from_yaml = self;
+ match &opts.theme {
+ Some(theme) => from_yaml.from_default_theme(theme.to_owned()),
+ None => from_yaml.from_default_theme("default".into()),
+ }
+ }
+
+ fn get_theme(&mut self, theme: String) -> Option<Theme> {
+ self.0.remove(&theme)
+ }
+
+ fn from_default_theme(&mut self, theme: String) -> Option<Palette> {
+ self.clone()
+ .get_theme(theme)
+ .map(|t| Palette::from(t.palette))
+ }
+}
+
+impl From<PaletteFromYaml> for Palette {
+ fn from(yaml: PaletteFromYaml) -> Self {
+ Palette {
+ fg: yaml.fg.into(),
+ bg: yaml.fg.into(),
+ black: yaml.black.into(),
+ red: yaml.red.into(),
+ green: yaml.green.into(),
+ yellow: yaml.yellow.into(),
+ blue: yaml.blue.into(),
+ magenta: yaml.magenta.into(),
+ cyan: yaml.cyan.into(),
+ white: yaml.white.into(),
+ orange: yaml.orange.into(),
+ ..Palette::default()
+ }
+ }
+}
+
+impl From<PaletteColorFromYaml> for PaletteColor {
+ fn from(yaml: PaletteColorFromYaml) -> Self {
+ match yaml {
+ PaletteColorFromYaml::Rgb(color) => PaletteColor::Rgb(color),
+ PaletteColorFromYaml::EightBit(color) => PaletteColor::EightBit(color),
+ }
+ }
+}