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
This commit is contained in:
Jae-Heon Ji 2023-04-27 00:26:07 +09:00 committed by GitHub
parent 0a8bbd7f23
commit ff36798c9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 82 additions and 36 deletions

View File

@ -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).

View File

@ -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"
}
}

View File

@ -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] = &[

View File

@ -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)
}
}

View File

@ -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 {