Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support themes directory #1577

Merged
merged 7 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions zellij-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ use zellij_utils::{
data::{ClientId, InputMode, Style},
envs,
errors::{ClientContext, ContextType, ErrorInstruction},
input::{actions::Action, config::Config, options::Options},
input::{actions::Action, config::Config, options::Options, theme::Theme},
ipc::{ClientAttributes, ClientToServerMsg, ExitReason, ServerToClientMsg},
setup::{find_default_config_dir, get_theme_dir},
termwiz::input::InputEvent,
};
use zellij_utils::{cli::CliArgs, input::layout::LayoutFromYaml};
Expand Down Expand Up @@ -145,7 +146,22 @@ pub fn start_client(
config.env.set_vars();

let palette = config.themes.clone().map_or_else(
|| os_input.load_palette(),
jaeheonji marked this conversation as resolved.
Show resolved Hide resolved
|| match config_options.clone().theme {
Some(theme) => {
let theme_dir =
match get_theme_dir(opts.config_dir.clone().or_else(find_default_config_dir)) {
Some(dir) => dir,
None => return os_input.load_palette(),
};

let theme_path = theme_dir.as_path().join(theme.as_str());
match Theme::from_path(&theme_path) {
Ok(t) => t.get_palette(),
Err(_) => os_input.load_palette(),
}
},
None => os_input.load_palette(),
},
|t| {
t.theme_config(&config_options)
.unwrap_or_else(|| os_input.load_palette())
Expand Down
64 changes: 63 additions & 1 deletion zellij-utils/src/input/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ use serde::{
de::{Error, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use thiserror::Error;

use std::fs::File;
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use std::{collections::HashMap, fmt};

use super::options::Options;
use crate::data::{Palette, PaletteColor};
use crate::shared::detect_theme_hue;

type ThemeResult = Result<Theme, ThemeError>;

/// Intermediate deserialization of themes
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct ThemesFromYaml(HashMap<String, Theme>);
Expand All @@ -23,11 +30,29 @@ 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,
}

#[derive(Debug, Error)]
pub enum ThemeError {
a-kenji marked this conversation as resolved.
Show resolved Hide resolved
// Deserialization error
#[error("Deserialization error: {0}")]
Serde(#[from] serde_yaml::Error),
// Io error
#[error("IoError: {0}")]
Io(#[from] io::Error),
// Io error with path context
#[error("IoError: {0}, File: {1}")]
IoPath(io::Error, PathBuf),
}

/// Intermediate deserialization struct
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Default)]
pub struct PaletteFromYaml {
Expand Down Expand Up @@ -124,6 +149,38 @@ impl Default for PaletteColorFromYaml {
}
}

impl Theme {
pub fn from_path(theme_path: &Path) -> ThemeResult {
let mut theme_file = File::open(&theme_path)
.or_else(|_| File::open(&theme_path.with_extension("yaml")))
.map_err(|e| ThemeError::IoPath(e, theme_path.into()))?;

let mut theme = String::new();
theme_file.read_to_string(&mut theme)?;

let theme_from_yaml: ThemeFromYaml = match serde_yaml::from_str(&theme) {
Err(e) => return Err(ThemeError::Serde(e)),
Ok(config) => config,
};

theme_from_yaml.try_into()
}

pub fn get_palette(self) -> Palette {
Palette::from(self.palette)
}
}

impl TryFrom<ThemeFromYaml> for Theme {
type Error = ThemeError;

fn try_from(theme_from_yaml: ThemeFromYaml) -> ThemeResult {
Ok(Self {
palette: theme_from_yaml.palette,
})
}
}

impl ThemesFromYaml {
pub fn theme_config(self, opts: &Options) -> Option<Palette> {
let mut from_yaml = self;
Expand Down Expand Up @@ -182,3 +239,8 @@ impl From<PaletteColorFromYaml> for PaletteColor {
}
}
}

// The unit test location.
#[cfg(test)]
#[path = "./unit/theme_test.rs"]
mod theme_test;
12 changes: 12 additions & 0 deletions zellij-utils/src/input/unit/fixtures/themes/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
palette:
bg: [40, 42, 54]
red: [255, 85, 85]
green: [80, 250, 123]
yellow: [241, 250, 140]
blue: [98, 114, 164]
magenta: [255, 121, 198]
orange: [255, 184, 108]
fg: [248, 248, 242]
cyan: [139, 233, 253]
black: [0, 0, 0]
white: [255, 255, 255]
21 changes: 21 additions & 0 deletions zellij-utils/src/input/unit/theme_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use super::super::theme::*;

fn theme_test_dir(theme: String) -> PathBuf {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let theme_dir = root.join("src/input/unit/fixtures/themes");
theme_dir.join(theme)
}

#[test]
fn default_theme_is_ok() {
let path = theme_test_dir("default.yaml".into());
let theme = Theme::from_path(&path);
assert!(theme.is_ok());
}

#[test]
fn no_theme_is_err() {
let path = theme_test_dir("nonexistent.yaml".into());
let theme = Theme::from_path(&path);
assert!(theme.is_err());
}
4 changes: 4 additions & 0 deletions zellij-utils/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ pub fn get_layout_dir(config_dir: Option<PathBuf>) -> Option<PathBuf> {
config_dir.map(|dir| dir.join("layouts"))
}

pub fn get_theme_dir(config_dir: Option<PathBuf>) -> Option<PathBuf> {
config_dir.map(|dir| dir.join("themes"))
}

pub fn dump_asset(asset: &[u8]) -> std::io::Result<()> {
std::io::stdout().write_all(asset)?;
Ok(())
Expand Down