Skip to content

Commit

Permalink
feat(cli): add ahk flag to static config start cmd
Browse files Browse the repository at this point in the history
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
  • Loading branch information
LGUG2Z committed Sep 11, 2023
1 parent 09792a7 commit 710a8d7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions komorebic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 710a8d7

Please sign in to comment.