feat: support multiple themes in one file (#1855)

* feat: support multiple themes in a one file

* chore: update example of multiple themes to gruvbox
This commit is contained in:
Jae-Heon Ji 2022-10-28 22:48:51 +09:00 committed by GitHub
parent 9fa73d5758
commit 38d23d7218
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 60 deletions

View file

@ -1,16 +0,0 @@
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
}
}

View file

@ -1,4 +1,17 @@
themes { 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 { gruvbox-dark {
fg 213 196 161 fg 213 196 161
bg 40 40 40 bg 40 40 40

View file

@ -1,11 +1,9 @@
--- ---
source: zellij-utils/src/input/./unit/theme_test.rs source: zellij-utils/src/input/./unit/theme_test.rs
assertion_line: 15
expression: "format!(\"{:#?}\", theme)" expression: "format!(\"{:#?}\", theme)"
--- ---
( {
"dracula", "dracula": Theme {
Theme {
palette: Palette { palette: Palette {
source: Default, source: Default,
theme_hue: Dark, theme_hue: Dark,
@ -106,4 +104,4 @@ expression: "format!(\"{:#?}\", theme)"
), ),
}, },
}, },
) }

View file

@ -11,13 +11,13 @@ fn theme_test_dir(theme: String) -> PathBuf {
#[test] #[test]
fn dracula_theme_from_file() { fn dracula_theme_from_file() {
let path = theme_test_dir("dracula.kdl".into()); 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)); assert_snapshot!(format!("{:#?}", theme));
} }
#[test] #[test]
fn no_theme_is_err() { fn no_theme_is_err() {
let path = theme_test_dir("nonexistent.kdl".into()); let path = theme_test_dir("nonexistent.kdl".into());
let theme = Theme::from_path(path); let theme = Themes::from_path(path);
assert!(theme.is_err()); assert!(theme.is_err());
} }

View file

@ -1587,10 +1587,8 @@ impl Themes {
let themes = Themes::from_data(themes); let themes = Themes::from_data(themes);
Ok(themes) Ok(themes)
} }
}
impl Theme { pub fn from_path(path_to_theme_file: PathBuf) -> Result<Self, ConfigError> {
pub fn from_path(path_to_theme_file: PathBuf) -> Result<(String, Self), ConfigError> {
// String is the theme name // String is the theme name
let mut file = File::open(path_to_theme_file.clone())?; let mut file = File::open(path_to_theme_file.clone())?;
let mut kdl_config = String::new(); let mut kdl_config = String::new();
@ -1602,34 +1600,6 @@ impl Theme {
kdl_config.span().len(), kdl_config.span().len(),
))?; ))?;
let all_themes_in_file = Themes::from_kdl(kdl_themes)?; let all_themes_in_file = Themes::from_kdl(kdl_themes)?;
let theme_file_name = path_to_theme_file Ok(all_themes_in_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(),
))
}
} }
} }

View file

@ -1,4 +1,4 @@
use crate::input::theme::Theme; use crate::input::theme::Themes;
use crate::{ use crate::{
cli::{CliArgs, Command}, cli::{CliArgs, Command},
consts::{ consts::{
@ -241,10 +241,8 @@ impl Setup {
for entry in (theme_dir.read_dir()?).flatten() { for entry in (theme_dir.read_dir()?).flatten() {
if let Some(extension) = entry.path().extension() { if let Some(extension) = entry.path().extension() {
if extension == "kdl" { if extension == "kdl" {
match Theme::from_path(entry.path()) { match Themes::from_path(entry.path()) {
Ok((theme_name, theme)) => { Ok(themes) => config.themes = config.themes.merge(themes),
config.themes.insert(theme_name, theme);
},
Err(e) => { Err(e) => {
log::error!("error loading theme file: {:?}", e); log::error!("error loading theme file: {:?}", e);
}, },