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 multiple themes in one file #1855

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 0 additions & 16 deletions example/themes/gruvbox-light.kdl

This file was deleted.

13 changes: 13 additions & 0 deletions example/themes/gruvbox-dark.kdl → example/themes/gruvbox.kdl
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
themes {
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
}
gruvbox-dark {
fg 213 196 161
bg 40 40 40
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
---
source: zellij-utils/src/input/./unit/theme_test.rs
assertion_line: 15
expression: "format!(\"{:#?}\", theme)"
---
(
"dracula",
Theme {
{
"dracula": Theme {
palette: Palette {
source: Default,
theme_hue: Dark,
Expand Down Expand Up @@ -106,4 +104,4 @@ expression: "format!(\"{:#?}\", theme)"
),
},
},
)
}
4 changes: 2 additions & 2 deletions zellij-utils/src/input/unit/theme_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ fn theme_test_dir(theme: String) -> PathBuf {
#[test]
fn dracula_theme_from_file() {
let path = theme_test_dir("dracula.kdl".into());
let theme = Theme::from_path(path).unwrap();
let theme = Themes::from_path(path).unwrap();
assert_snapshot!(format!("{:#?}", theme));
}

#[test]
fn no_theme_is_err() {
let path = theme_test_dir("nonexistent.kdl".into());
let theme = Theme::from_path(path);
let theme = Themes::from_path(path);
assert!(theme.is_err());
}
34 changes: 2 additions & 32 deletions zellij-utils/src/kdl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1587,10 +1587,8 @@ impl Themes {
let themes = Themes::from_data(themes);
Ok(themes)
}
}

impl Theme {
pub fn from_path(path_to_theme_file: PathBuf) -> Result<(String, Self), ConfigError> {
pub fn from_path(path_to_theme_file: PathBuf) -> Result<Self, ConfigError> {
// String is the theme name
let mut file = File::open(path_to_theme_file.clone())?;
let mut kdl_config = String::new();
Expand All @@ -1602,34 +1600,6 @@ impl Theme {
kdl_config.span().len(),
))?;
let all_themes_in_file = Themes::from_kdl(kdl_themes)?;
let theme_file_name = path_to_theme_file
.file_name()
.ok_or(ConfigError::new_kdl_error(
"Failed to find file name".into(),
kdl_config.span().offset(),
kdl_config.span().len(),
))?
.to_string_lossy()
.to_string();
if let Some(theme_name) = theme_file_name.strip_suffix(".kdl") {
let theme =
all_themes_in_file
.get_theme(theme_name)
.ok_or(ConfigError::new_kdl_error(
format!(
"Not theme with name {} found in file {:?}",
theme_name, path_to_theme_file
),
kdl_config.span().offset(),
kdl_config.span().len(),
))?;
Ok((theme_name.to_string(), theme.clone()))
} else {
Err(ConfigError::new_kdl_error(
"no theme file found".into(),
kdl_config.span().offset(),
kdl_config.span().len(),
))
}
Ok(all_themes_in_file)
}
}
8 changes: 3 additions & 5 deletions zellij-utils/src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::input::theme::Theme;
use crate::input::theme::Themes;
use crate::{
cli::{CliArgs, Command},
consts::{
Expand Down Expand Up @@ -241,10 +241,8 @@ impl Setup {
for entry in (theme_dir.read_dir()?).flatten() {
if let Some(extension) = entry.path().extension() {
if extension == "kdl" {
match Theme::from_path(entry.path()) {
Ok((theme_name, theme)) => {
config.themes.insert(theme_name, theme);
},
match Themes::from_path(entry.path()) {
Ok(themes) => config.themes = config.themes.merge(themes),
Err(e) => {
log::error!("error loading theme file: {:?}", e);
},
Expand Down