diff --git a/komorebi-core/src/config_generation.rs b/komorebi-core/src/config_generation.rs index f2c6fe8e..2fd97dc3 100644 --- a/komorebi-core/src/config_generation.rs +++ b/komorebi-core/src/config_generation.rs @@ -89,8 +89,36 @@ impl ApplicationConfigurationGenerator { Ok(serde_yaml::to_string(&cfgen)?) } - pub fn generate_ahk(content: &str) -> Result> { - let mut cfgen = Self::load(content)?; + fn merge(base_content: &str, override_content: &str) -> Result> { + let base_cfgen = Self::load(base_content)?; + let override_cfgen = Self::load(override_content)?; + + let mut final_cfgen = base_cfgen.clone(); + + for entry in override_cfgen { + let mut replace_idx = None; + for (idx, base_entry) in base_cfgen.iter().enumerate() { + if base_entry.name == entry.name { + replace_idx = Option::from(idx); + } + } + + match replace_idx { + None => final_cfgen.push(entry), + Some(idx) => final_cfgen[idx] = entry, + } + } + + Ok(final_cfgen) + } + + pub fn generate_ahk(base_content: &str, override_content: Option<&str>) -> Result> { + let mut cfgen = if let Some(override_content) = override_content { + Self::merge(base_content, override_content)? + } else { + Self::load(base_content)? + }; + cfgen.sort_by(|a, b| a.name.cmp(&b.name)); let mut lines = vec![ diff --git a/komorebic.lib.sample.ahk b/komorebic.lib.sample.ahk index 86b58948..8b498bbd 100644 --- a/komorebic.lib.sample.ahk +++ b/komorebic.lib.sample.ahk @@ -304,8 +304,8 @@ AhkLibrary() { Run, komorebic.exe ahk-library, , Hide } -AhkAppSpecificConfiguration(path) { - Run, komorebic.exe ahk-app-specific-configuration %path%, , Hide +AhkAppSpecificConfiguration(path, override_path) { + Run, komorebic.exe ahk-app-specific-configuration %path% %override_path%, , Hide } FormatAppSpecificConfiguration(path) { diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 893e0aa3..5361626c 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -422,22 +422,18 @@ struct Unsubscribe { named_pipe: String, } -macro_rules! gen_application_specific_configuration_subcommand_args { - // SubCommand Pattern - ( $( $name:ident ),+ $(,)? ) => { - $( - #[derive(clap::Parser, derive_ahk::AhkFunction)] - pub struct $name { - /// YAML file from which the application-specific configurations should be loaded - path: String, - } - )+ - }; +#[derive(Parser, AhkFunction)] +struct AhkAppSpecificConfiguration { + /// YAML file from which the application-specific configurations should be loaded + path: String, + /// Optional YAML file of overrides to apply over the first file + override_path: Option, } -gen_application_specific_configuration_subcommand_args! { - AhkAppSpecificConfiguration, - FormatAppSpecificConfiguration, +#[derive(Parser, AhkFunction)] +struct FormatAppSpecificConfiguration { + /// YAML file from which the application-specific configurations should be loaded + path: String, } #[derive(Parser)] @@ -1165,7 +1161,16 @@ fn main() -> Result<()> { } SubCommand::AhkAppSpecificConfiguration(arg) => { let content = fs::read_to_string(resolve_windows_path(&arg.path)?)?; - let lines = ApplicationConfigurationGenerator::generate_ahk(&content)?; + let lines = if let Some(override_path) = arg.override_path { + let override_content = fs::read_to_string(resolve_windows_path(&override_path)?)?; + + ApplicationConfigurationGenerator::generate_ahk( + &content, + Option::from(override_content.as_str()), + )? + } else { + ApplicationConfigurationGenerator::generate_ahk(&content, None)? + }; let mut generated_config = HOME_DIR.clone(); generated_config.push("komorebi.generated.ahk");