Skip to content

Commit

Permalink
feat(config): add fmt cmd & float rule comments
Browse files Browse the repository at this point in the history
This commit adds a fmt command which allows users to prepare PRs to the
configuration repository in a unified way.

The 'custom' formatter basically just ensures that the yaml array is
sorted by application name to make for easier diffs.

Serializing of Option::None has been disabled to keep the yaml file more
concise.

Finally, an option for adding comments to float rules has been included
as some of these rules can be quite esoteric and there is value in
having them annotated with comments in the configuration to preserve and
pass down the knowledge.

The config generation command has been renamed to
'ahk-app-specific-configuration' (with a short alias of 'ahk-asc') to
emphasise that an ahk file is being generated (similar to
'ahk-library').

re #62
  • Loading branch information
LGUG2Z committed Apr 3, 2022
1 parent c2cc21d commit c426c06
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions komorebi-core/src/config_generation.rs
Expand Up @@ -57,12 +57,22 @@ pub struct IdWithIdentifier {
id: String,
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct IdWithIdentifierAndComment {
kind: ApplicationIdentifier,
id: String,
#[serde(skip_serializing_if = "Option::is_none")]
comment: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct ApplicationConfiguration {
name: String,
identifier: IdWithIdentifier,
#[serde(skip_serializing_if = "Option::is_none")]
options: Option<Vec<ApplicationOptions>>,
float_identifiers: Option<Vec<IdWithIdentifier>>,
#[serde(skip_serializing_if = "Option::is_none")]
float_identifiers: Option<Vec<IdWithIdentifierAndComment>>,
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
Expand All @@ -73,7 +83,13 @@ impl ApplicationConfigurationGenerator {
Ok(serde_yaml::from_str(content)?)
}

pub fn generate(content: &str) -> Result<Vec<String>> {
pub fn format(content: &str) -> Result<String> {
let mut cfgen = Self::load(content)?;
cfgen.sort_by(|a, b| a.name.cmp(&b.name));
Ok(serde_yaml::to_string(&cfgen)?)
}

pub fn generate_ahk(content: &str) -> Result<Vec<String>> {
let mut cfgen = Self::load(content)?;
cfgen.sort_by(|a, b| a.name.cmp(&b.name));

Expand All @@ -93,6 +109,7 @@ impl ApplicationConfigurationGenerator {
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));
}
}
Expand All @@ -107,10 +124,16 @@ impl ApplicationConfigurationGenerator {
// Don't want to send duped signals especially as configs get larger
if !float_rules.contains(&float_rule) {
float_rules.push(float_rule.clone());

if let Some(comment) = float.comment {
lines.push(format!("; {}", comment));
};

lines.push(float_rule);
}
}
}

lines.push(String::from(""));
}

Expand Down
8 changes: 6 additions & 2 deletions komorebic.lib.sample.ahk
Expand Up @@ -304,8 +304,12 @@ AhkLibrary() {
Run, komorebic.exe ahk-library, , Hide
}

ApplicationSpecificConfiguration(path) {
Run, komorebic.exe application-specific-configuration %path%, , Hide
AhkAppSpecificConfiguration(path) {
Run, komorebic.exe ahk-app-specific-configuration %path%, , Hide
}

FormatAppSpecificConfiguration(path) {
Run, komorebic.exe format-app-specific-configuration %path%, , Hide
}

NotificationSchema() {
Expand Down
49 changes: 40 additions & 9 deletions komorebic/src/main.rs
Expand Up @@ -422,10 +422,22 @@ struct Unsubscribe {
named_pipe: String,
}

#[derive(Parser, AhkFunction)]
pub struct ApplicationSpecificConfiguration {
/// YAML file from which the application-specific configurations should be loaded
path: 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,
}
)+
};
}

gen_application_specific_configuration_subcommand_args! {
AhkAppSpecificConfiguration,
FormatAppSpecificConfiguration,
}

#[derive(Parser)]
Expand Down Expand Up @@ -649,10 +661,14 @@ enum SubCommand {
ToggleMouseFollowsFocus,
/// Generate a library of AutoHotKey helper functions
AhkLibrary,
/// Generate a collection of common application configurations to use in komorebi.ahk
/// Generate common app-specific configurations and fixes to use in komorebi.ahk
#[clap(arg_required_else_help = true)]
#[clap(alias = "ahk-asc")]
AhkAppSpecificConfiguration(AhkAppSpecificConfiguration),
/// Format a YAML file for use with the 'ahk-app-specific-configuration' command
#[clap(arg_required_else_help = true)]
#[clap(alias = "app-specific-configuration")]
ApplicationSpecificConfiguration(ApplicationSpecificConfiguration),
#[clap(alias = "fmt-asc")]
FormatAppSpecificConfiguration(FormatAppSpecificConfiguration),
/// Generate a JSON Schema of subscription notifications
NotificationSchema,
}
Expand Down Expand Up @@ -1147,9 +1163,9 @@ fn main() -> Result<()> {
SubCommand::WindowHidingBehaviour(arg) => {
send_message(&*SocketMessage::WindowHidingBehaviour(arg.hiding_behaviour).as_bytes()?)?;
}
SubCommand::ApplicationSpecificConfiguration(arg) => {
SubCommand::AhkAppSpecificConfiguration(arg) => {
let content = fs::read_to_string(resolve_windows_path(&arg.path)?)?;
let lines = ApplicationConfigurationGenerator::generate(&content)?;
let lines = ApplicationConfigurationGenerator::generate_ahk(&content)?;

let mut generated_config = HOME_DIR.clone();
generated_config.push("komorebi.generated.ahk");
Expand All @@ -1174,6 +1190,21 @@ fn main() -> Result<()> {

println!("\n#Include %A_ScriptDir%\\komorebi.generated.ahk");
}
SubCommand::FormatAppSpecificConfiguration(arg) => {
let file_path = resolve_windows_path(&arg.path)?;
let content = fs::read_to_string(&file_path)?;
let formatted_content = ApplicationConfigurationGenerator::format(&content)?;

let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(file_path)?;

file.write_all(formatted_content.as_bytes())?;

println!("File successfully formatted for PRs to https://github.com/LGUG2Z/komorebi-application-specific-configuration");
}
SubCommand::NotificationSchema => {
let home = HOME_DIR.clone();
let mut socket = home;
Expand Down

0 comments on commit c426c06

Please sign in to comment.