diff options
author | Jae-Heon Ji <32578710+jaeheonji@users.noreply.github.com> | 2023-04-27 00:26:07 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-27 00:26:07 +0900 |
commit | ff36798c9e52c9c73111afd03c9a69ecb1aee073 (patch) | |
tree | c03640f71a6dc08ef544e8dd34aa4ce5c49ff28f /zellij-utils/src | |
parent | 0a8bbd7f2310b849dec324aae88c546f82ec7fe8 (diff) |
feat: provide default themes (#2307)
* refactor: move themes to zellij-assets
* feat: add theme to the binary
* chore: move new theme from example to assets
Diffstat (limited to 'zellij-utils/src')
-rw-r--r-- | zellij-utils/src/consts.rs | 14 | ||||
-rw-r--r-- | zellij-utils/src/kdl/mod.rs | 14 | ||||
-rw-r--r-- | zellij-utils/src/setup.rs | 38 |
3 files changed, 46 insertions, 20 deletions
diff --git a/zellij-utils/src/consts.rs b/zellij-utils/src/consts.rs index 5709db891..b851b1524 100644 --- a/zellij-utils/src/consts.rs +++ b/zellij-utils/src/consts.rs @@ -1,5 +1,6 @@ //! Zellij program-wide constants. +use crate::input::theme::Themes; use directories_next::ProjectDirs; use lazy_static::lazy_static; use once_cell::sync::OnceCell; @@ -28,6 +29,19 @@ lazy_static! { pub static ref ZELLIJ_PROJ_DIR: ProjectDirs = ProjectDirs::from("org", "Zellij Contributors", "Zellij").unwrap(); pub static ref ZELLIJ_CACHE_DIR: PathBuf = ZELLIJ_PROJ_DIR.cache_dir().to_path_buf(); + pub static ref ZELLIJ_DEFAULT_THEMES: Themes = { + let mut default_themes = Themes::default(); + + let path = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/themes")); + match Themes::from_dir(path) { + Ok(themes) => { + default_themes = default_themes.merge(themes); + }, + Err(_) => {}, + } + + default_themes + }; } pub const FEATURES: &[&str] = &[ diff --git a/zellij-utils/src/kdl/mod.rs b/zellij-utils/src/kdl/mod.rs index a892cf8c3..1d488f39d 100644 --- a/zellij-utils/src/kdl/mod.rs +++ b/zellij-utils/src/kdl/mod.rs @@ -1758,4 +1758,18 @@ impl Themes { let all_themes_in_file = Themes::from_kdl(kdl_themes)?; Ok(all_themes_in_file) } + + pub fn from_dir(path_to_theme_dir: PathBuf) -> Result<Self, ConfigError> { + let mut themes = Themes::default(); + for entry in std::fs::read_dir(path_to_theme_dir)? { + let entry = entry?; + let path = entry.path(); + if let Some(extension) = path.extension() { + if extension == "kdl" { + themes = themes.merge(Themes::from_path(path)?); + } + } + } + Ok(themes) + } } diff --git a/zellij-utils/src/setup.rs b/zellij-utils/src/setup.rs index 0086c6a99..a40df7091 100644 --- a/zellij-utils/src/setup.rs +++ b/zellij-utils/src/setup.rs @@ -5,7 +5,7 @@ use crate::{ cli::{CliArgs, Command}, consts::{ FEATURES, SYSTEM_DEFAULT_CONFIG_DIR, SYSTEM_DEFAULT_DATA_DIR_PREFIX, VERSION, - ZELLIJ_PROJ_DIR, + ZELLIJ_DEFAULT_THEMES, ZELLIJ_PROJ_DIR, }, errors::prelude::*, input::{ @@ -63,6 +63,16 @@ pub fn get_default_data_dir() -> PathBuf { .unwrap_or_else(xdg_data_dir) } +#[cfg(not(test))] +fn get_default_themes() -> Themes { + ZELLIJ_DEFAULT_THEMES.to_owned() +} + +#[cfg(test)] +fn get_default_themes() -> Themes { + Themes::default() +} + pub fn xdg_config_dir() -> PathBuf { ZELLIJ_PROJ_DIR.config_dir().to_owned() } @@ -310,25 +320,13 @@ impl Setup { None => config.options.clone(), }; - if let Some(theme_dir) = config_options - .theme_dir - .clone() - .or_else(|| get_theme_dir(cli_args.config_dir.clone().or_else(find_default_config_dir))) - { - if theme_dir.is_dir() { - for entry in (theme_dir.read_dir()?).flatten() { - if let Some(extension) = entry.path().extension() { - if extension == "kdl" { - match Themes::from_path(entry.path()) { - Ok(themes) => config.themes = config.themes.merge(themes), - Err(e) => { - log::error!("error loading theme file: {:?}", e); - }, - } - } - } - } - } + config.themes = config.themes.merge(get_default_themes()); + + let user_theme_dir = config_options.theme_dir.clone().or_else(|| { + get_theme_dir(cli_args.config_dir.clone().or_else(find_default_config_dir)) + }); + if let Some(user_theme_dir) = user_theme_dir { + config.themes = config.themes.merge(Themes::from_dir(user_theme_dir)?); } if let Some(Command::Setup(ref setup)) = &cli_args.command { |