Skip to content

Commit

Permalink
Feature: File-extension config item option (#32)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
1ynnshell committed Jul 1, 2024
1 parent 72db194 commit ebdb1a5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
5 changes: 5 additions & 0 deletions fixtures/schemes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ base16-mellow-purple
base16-mexico-light
base16-mocha
base16-monokai
base16-moonlight
base16-mountain
base16-nebula
base16-nord-light
Expand All @@ -177,6 +178,10 @@ base16-pico
base16-pinky
base16-pop
base16-porple
base16-precious-dark-eleven
base16-precious-dark-fifteen
base16-precious-light-warm
base16-precious-light-white
base16-primer-dark-dimmed
base16-primer-dark
base16-primer-light
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ pub struct ConfigItem {
pub themes_dir: String,
#[serde(rename = "supported-systems")]
pub supported_systems: Option<Vec<SupportedSchemeSystems>>,
#[serde(rename = "theme-file-extension")]
pub theme_file_extension: Option<String>,
}

impl fmt::Display for ConfigItem {
Expand Down Expand Up @@ -171,6 +173,7 @@ impl Config {
themes_dir: BASE16_SHELL_THEMES_DIR.to_string(),
hook: Some(BASE16_SHELL_HOOK.to_string()),
supported_systems: Some(vec![SupportedSchemeSystems::Base16]), // DEFAULT_SCHEME_SYSTEM
theme_file_extension: None,
};

// Add default `item` if no items exist
Expand Down
17 changes: 13 additions & 4 deletions src/operations/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,16 @@ pub fn apply(config_path: &Path, data_path: &Path, full_scheme_name: &str) -> Re
.with_context(|| format!("Themes are missing from {}, try running `{} install` or `{} update` and try again.", item.name, REPO_NAME, REPO_NAME))?;
let theme_option = &theme_dir.filter_map(Result::ok).find(|entry| {
let path = entry.path();
let filename = path.file_stem().and_then(|name| name.to_str());

full_scheme_name == filename.unwrap_or_default()
match &item.theme_file_extension {
Some(extension) => {
let filename = path.file_name().and_then(|name| name.to_str());
format!("{}{}", full_scheme_name, extension) == filename.unwrap_or_default()
}
None => {
let filename = path.file_stem().and_then(|name| name.to_str());
full_scheme_name == filename.unwrap_or_default()
}
}
});

// Copy that theme to the data_path or log a message that it isn't found
Expand All @@ -113,7 +120,9 @@ pub fn apply(config_path: &Path, data_path: &Path, full_scheme_name: &str) -> Re
// Run hook for item if provided
if let Some(hook_text) = &item.hook {
let hook_script =
hook_text.replace("%f", format!("\"{}\"", data_theme_path.display()).as_str());
hook_text
.replace("%f", format!("\"{}\"", data_theme_path.display()).as_str())
.replace("%n", full_scheme_name);
let command_vec =
get_shell_command_from_string(config_path, hook_script.as_str())?;
Command::new(&command_vec[0])
Expand Down
47 changes: 47 additions & 0 deletions tests/cli_apply_subcommand_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,50 @@ hook = "echo \"path: %f\""
cleanup()?;
Ok(())
}

#[test]
fn test_cli_apply_subcommand_with_config_theme_file_extension() -> Result<()> {
// -------
// Arrange
// -------
let scheme_name = "base16-uwunicorn";
let (config_path, data_path, command_vec, cleanup) = setup(
"test_cli_apply_subcommand_with_custom_schemes",
format!("apply {}", &scheme_name).as_str(),
)?;
let config_content = r#"
[[items]]
path = "https://github.com/tinted-theming/tinted-alacritty"
name = "tinted-alacritty"
themes-dir = "colors"
hook = "echo \"expected alacritty output: %n\""
[[items]]
name = "base16-emacs"
path = "https://github.com/tinted-theming/base16-emacs"
theme-file-extension="-theme.el"
themes-dir="build"
hook = "echo \"expected emacs output: %n\""
"#;
let expected_output =
"expected alacritty output: base16-uwunicorn\nexpected emacs output: base16-uwunicorn\n";
write_to_file(&config_path, config_content)?;

// ---
// Act
// ---
utils::run_install_command(&config_path, &data_path)?;
let (stdout, stderr) = utils::run_command(command_vec).unwrap();

// ------
// Assert
// ------
assert_eq!(stdout, expected_output,);
assert!(
stderr.is_empty(),
"stderr does not contain the expected output"
);

cleanup()?;
Ok(())
}

0 comments on commit ebdb1a5

Please sign in to comment.