-
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): add cfgen for apps based on yaml def
This commit introduces a configuration generator for application-specific config options passed to the cli via a file path. The hope is to have a public repository that any user can contribute application-specific configs and fixes to, and for the generated AHK to be available to any new user as part of the initial setup to make the onboarding as frictionless as possible. re #62
- Loading branch information
Showing
6 changed files
with
174 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use clap::ArgEnum; | ||
use color_eyre::Result; | ||
use schemars::JsonSchema; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
use strum::Display; | ||
use strum::EnumString; | ||
|
||
use crate::ApplicationIdentifier; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize, Display, EnumString, ArgEnum, JsonSchema)] | ||
#[strum(serialize_all = "snake_case")] | ||
#[serde(rename_all = "snake_case")] | ||
pub enum ApplicationOptions { | ||
ObjectNameChange, | ||
Layered, | ||
BorderOverflow, | ||
TrayAndMultiWindow, | ||
Force, | ||
} | ||
|
||
impl ApplicationOptions { | ||
#[must_use] | ||
pub fn cfgen(&self, kind: &ApplicationIdentifier, id: &str) -> String { | ||
format!( | ||
"Run, {}, , Hide", | ||
match self { | ||
ApplicationOptions::ObjectNameChange => { | ||
format!( | ||
"komorebic.exe identify-object-name-change-application {} {}", | ||
kind, id | ||
) | ||
} | ||
ApplicationOptions::Layered => { | ||
format!("komorebic.exe identify-layered-application {} {}", kind, id) | ||
} | ||
ApplicationOptions::BorderOverflow => { | ||
format!( | ||
"komorebic.exe identify-border-overflow-application {} {}", | ||
kind, id | ||
) | ||
} | ||
ApplicationOptions::TrayAndMultiWindow => { | ||
format!("komorebic.exe identify-tray-application {} {}", kind, id) | ||
} | ||
ApplicationOptions::Force => { | ||
format!("komorebic.exe manage-rule {} {}", kind, id) | ||
} | ||
} | ||
) | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] | ||
pub struct IdWithIdentifier { | ||
kind: ApplicationIdentifier, | ||
id: String, | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] | ||
pub struct ApplicationConfiguration { | ||
name: String, | ||
identifier: IdWithIdentifier, | ||
options: Option<Vec<ApplicationOptions>>, | ||
float_identifiers: Option<Vec<IdWithIdentifier>>, | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] | ||
pub struct ApplicationConfigurationGenerator; | ||
|
||
impl ApplicationConfigurationGenerator { | ||
fn load(content: &str) -> Result<Vec<ApplicationConfiguration>> { | ||
Ok(serde_yaml::from_str(content)?) | ||
} | ||
|
||
pub fn generate(content: &str) -> Result<Vec<String>> { | ||
let mut cfgen = Self::load(content)?; | ||
cfgen.sort_by(|a, b| a.name.cmp(&b.name)); | ||
|
||
let mut lines = vec![ | ||
String::from("; Generated by komorebic.exe"), | ||
String::from("; To use this file, add the line below to the top of your komorebi.ahk configuration file"), | ||
String::from("; #Include %A_ScriptDir%\\komorebi.generated.ahk"), | ||
String::from("") | ||
]; | ||
|
||
let mut float_rules = vec![]; | ||
|
||
for app in cfgen { | ||
lines.push(format!("; {}", app.name)); | ||
if let Some(options) = app.options { | ||
for opt in options { | ||
if let ApplicationOptions::TrayAndMultiWindow = opt { | ||
lines.push(String::from("; If you have disabled minimize/close to tray for this application, you can delete/comment out the next line")); | ||
} | ||
lines.push(opt.cfgen(&app.identifier.kind, &app.identifier.id)); | ||
} | ||
} | ||
|
||
if let Some(float_identifiers) = app.float_identifiers { | ||
for float in float_identifiers { | ||
let float_rule = format!( | ||
"Run, komorebic.exe float-rule {}, {}, , Hide", | ||
float.kind, float.id | ||
); | ||
|
||
// Don't want to send duped signals especially as configs get larger | ||
if !float_rules.contains(&float_rule) { | ||
float_rules.push(float_rule.clone()); | ||
lines.push(float_rule); | ||
} | ||
} | ||
} | ||
lines.push(String::from("")); | ||
} | ||
|
||
Ok(lines) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters