Skip to content

Commit

Permalink
feat: provide default themes (zellij-org#2307)
Browse files Browse the repository at this point in the history
* refactor: move themes to zellij-assets

* feat: add theme to the binary

* chore: move new theme from example to assets
  • Loading branch information
jaeheonji authored Apr 26, 2023
1 parent 0a8bbd7 commit ff36798
Show file tree
Hide file tree
Showing 27 changed files with 82 additions and 36 deletions.
18 changes: 2 additions & 16 deletions example/themes/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
# Themes

Themes can contain different flavors in one file, or can be created as individual files.
It contains examples showing how to write a theme.

Example:

```
gruvbox.kdl
├─ gruvbox-light
└─ gruvbox-dark
or
gruvbox-light.kdl
└─ gruvbox-light
gruvbox-dark.kdl
└─ gruvbox-dark
```
If you would like to add a theme to zellij, please refer [zellij-utils/assets/themes](../../zellij-utils/assets/themes).
34 changes: 34 additions & 0 deletions example/themes/example.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file shows how to write a theme file
// using `gruvbox` theme.

themes {
// example of how to set a theme in RGB format
gruvbox-light {
fg 60 56 54
bg 251 82 75
black 40 40 40
red 205 75 69
green 152 151 26
yellow 215 153 33
blue 69 133 136
magenta 177 98 134
cyan 104 157 106
white 213 196 161
orange 214 93 14
}

// example of how to set a theme in HEX format
gruvbox-dark {
fg "#D5C4A1"
bg "#282828"
black "#3C3836"
red "#CC241D"
green "#98971A"
yellow "#D79921"
blue "#3C8588"
magenta "#B16286"
cyan "#689D6A"
white "#FBF1C7"
orange "#D65D0E"
}
}
Binary file modified zellij-utils/assets/plugins/compact-bar.wasm
Binary file not shown.
Binary file modified zellij-utils/assets/plugins/status-bar.wasm
Binary file not shown.
Binary file modified zellij-utils/assets/plugins/strider.wasm
Binary file not shown.
Binary file modified zellij-utils/assets/plugins/tab-bar.wasm
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions zellij-utils/src/consts.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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] = &[
Expand Down
14 changes: 14 additions & 0 deletions zellij-utils/src/kdl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
38 changes: 18 additions & 20 deletions zellij-utils/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ff36798

Please sign in to comment.