Skip to content

Commit ebdb1a5

Browse files
authored
Feature: File-extension config item option (#32)
Add `theme-file-extension` option in item config to allow users to define a custom extension if the last `.*` isn't the whole extension, in examples like `*.module.css` and `*-theme.el` in emacs' case
1 parent 72db194 commit ebdb1a5

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

fixtures/schemes.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ base16-mellow-purple
155155
base16-mexico-light
156156
base16-mocha
157157
base16-monokai
158+
base16-moonlight
158159
base16-mountain
159160
base16-nebula
160161
base16-nord-light
@@ -177,6 +178,10 @@ base16-pico
177178
base16-pinky
178179
base16-pop
179180
base16-porple
181+
base16-precious-dark-eleven
182+
base16-precious-dark-fifteen
183+
base16-precious-light-warm
184+
base16-precious-light-white
180185
base16-primer-dark-dimmed
181186
base16-primer-dark
182187
base16-primer-light

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ pub struct ConfigItem {
9393
pub themes_dir: String,
9494
#[serde(rename = "supported-systems")]
9595
pub supported_systems: Option<Vec<SupportedSchemeSystems>>,
96+
#[serde(rename = "theme-file-extension")]
97+
pub theme_file_extension: Option<String>,
9698
}
9799

98100
impl fmt::Display for ConfigItem {
@@ -171,6 +173,7 @@ impl Config {
171173
themes_dir: BASE16_SHELL_THEMES_DIR.to_string(),
172174
hook: Some(BASE16_SHELL_HOOK.to_string()),
173175
supported_systems: Some(vec![SupportedSchemeSystems::Base16]), // DEFAULT_SCHEME_SYSTEM
176+
theme_file_extension: None,
174177
};
175178

176179
// Add default `item` if no items exist

src/operations/apply.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,16 @@ pub fn apply(config_path: &Path, data_path: &Path, full_scheme_name: &str) -> Re
8686
.with_context(|| format!("Themes are missing from {}, try running `{} install` or `{} update` and try again.", item.name, REPO_NAME, REPO_NAME))?;
8787
let theme_option = &theme_dir.filter_map(Result::ok).find(|entry| {
8888
let path = entry.path();
89-
let filename = path.file_stem().and_then(|name| name.to_str());
90-
91-
full_scheme_name == filename.unwrap_or_default()
89+
match &item.theme_file_extension {
90+
Some(extension) => {
91+
let filename = path.file_name().and_then(|name| name.to_str());
92+
format!("{}{}", full_scheme_name, extension) == filename.unwrap_or_default()
93+
}
94+
None => {
95+
let filename = path.file_stem().and_then(|name| name.to_str());
96+
full_scheme_name == filename.unwrap_or_default()
97+
}
98+
}
9299
});
93100

94101
// Copy that theme to the data_path or log a message that it isn't found
@@ -113,7 +120,9 @@ pub fn apply(config_path: &Path, data_path: &Path, full_scheme_name: &str) -> Re
113120
// Run hook for item if provided
114121
if let Some(hook_text) = &item.hook {
115122
let hook_script =
116-
hook_text.replace("%f", format!("\"{}\"", data_theme_path.display()).as_str());
123+
hook_text
124+
.replace("%f", format!("\"{}\"", data_theme_path.display()).as_str())
125+
.replace("%n", full_scheme_name);
117126
let command_vec =
118127
get_shell_command_from_string(config_path, hook_script.as_str())?;
119128
Command::new(&command_vec[0])

tests/cli_apply_subcommand_tests.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,50 @@ hook = "echo \"path: %f\""
240240
cleanup()?;
241241
Ok(())
242242
}
243+
244+
#[test]
245+
fn test_cli_apply_subcommand_with_config_theme_file_extension() -> Result<()> {
246+
// -------
247+
// Arrange
248+
// -------
249+
let scheme_name = "base16-uwunicorn";
250+
let (config_path, data_path, command_vec, cleanup) = setup(
251+
"test_cli_apply_subcommand_with_custom_schemes",
252+
format!("apply {}", &scheme_name).as_str(),
253+
)?;
254+
let config_content = r#"
255+
[[items]]
256+
path = "https://github.com/tinted-theming/tinted-alacritty"
257+
name = "tinted-alacritty"
258+
themes-dir = "colors"
259+
hook = "echo \"expected alacritty output: %n\""
260+
261+
[[items]]
262+
name = "base16-emacs"
263+
path = "https://github.com/tinted-theming/base16-emacs"
264+
theme-file-extension="-theme.el"
265+
themes-dir="build"
266+
hook = "echo \"expected emacs output: %n\""
267+
"#;
268+
let expected_output =
269+
"expected alacritty output: base16-uwunicorn\nexpected emacs output: base16-uwunicorn\n";
270+
write_to_file(&config_path, config_content)?;
271+
272+
// ---
273+
// Act
274+
// ---
275+
utils::run_install_command(&config_path, &data_path)?;
276+
let (stdout, stderr) = utils::run_command(command_vec).unwrap();
277+
278+
// ------
279+
// Assert
280+
// ------
281+
assert_eq!(stdout, expected_output,);
282+
assert!(
283+
stderr.is_empty(),
284+
"stderr does not contain the expected output"
285+
);
286+
287+
cleanup()?;
288+
Ok(())
289+
}

0 commit comments

Comments
 (0)