From 710a8d76ea590ed205864d1687adb3694838435c Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 11 Sep 2023 11:26:32 -0700 Subject: [PATCH] feat(cli): add ahk flag to static config start cmd This commit introduces a new --ahk flag to the start command, which works similarly to the --whkd, and attempts to load an ahk key binding configuration script on startup. The attempt is made from within the komorebic process, rather than the komorebi process. resolve #529 --- komorebic/src/main.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 888704c34..887a9379a 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -607,6 +607,7 @@ struct ActiveWindowBorderOffset { } #[derive(Parser, AhkFunction)] +#[allow(clippy::struct_excessive_bools)] struct Start { /// Allow the use of komorebi's custom focus-follows-mouse implementation #[clap(action, short, long = "ffm")] @@ -623,6 +624,9 @@ struct Start { /// Start whkd in a background process #[clap(action, long)] whkd: bool, + /// Start autohotkey configuration file + #[clap(action, long)] + ahk: bool, } #[derive(Parser, AhkFunction)] @@ -1398,10 +1402,22 @@ fn main() -> Result<()> { )?; } SubCommand::Start(arg) => { + let mut ahk: String = String::from("autohotkey.exe"); + + if let Ok(komorebi_ahk_exe) = std::env::var("KOMOREBI_AHK_EXE") { + if which(&komorebi_ahk_exe).is_ok() { + ahk = komorebi_ahk_exe; + } + } + if arg.whkd && which("whkd").is_err() { return Err(anyhow!("could not find whkd, please make sure it is installed before using the --whkd flag")); } + if arg.ahk && which(&ahk).is_err() { + return Err(anyhow!("could not find autohotkey, please make sure it is installed before using the --ahk flag")); + } + let mut buf: PathBuf; // The komorebi.ps1 shim will only exist in the Path if installed by Scoop @@ -1502,6 +1518,28 @@ if (!(Get-Process whkd -ErrorAction SilentlyContinue)) } } } + + if arg.ahk { + let home = HOME_DIR.clone(); + let mut config_ahk = home; + config_ahk.push("komorebi.ahk"); + + let script = format!( + r#" + Start-Process {ahk} {config} -WindowStyle hidden + "#, + config = config_ahk.as_os_str().to_string_lossy() + ); + + match powershell_script::run(&script) { + Ok(_) => { + println!("{script}"); + } + Err(error) => { + println!("Error: {error}"); + } + } + } } SubCommand::Stop(arg) => { if arg.whkd {