Skip to content

Commit

Permalink
feat(ahk): add support for ahk2
Browse files Browse the repository at this point in the history
Some users prefer to use AutoHotKey v2, so this commit adds a check for
both komorebi.ahk and komorebi.akh2 files, and the corresponding AHK
executables, whenever commands around configuration loading are run (on
startup, when manually reloading, and when watching for changes).

If both files exist in the home directory, komorebi.ahk will be
preferred (at least until AHKv2 is out of beta).

An example of a configuration file compatible with AHKv2 by @crosstyan
has been added to the documentation.

resolve #10
  • Loading branch information
LGUG2Z committed Aug 17, 2021
1 parent 2e95597 commit 4dadffa
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ Once `komorebi` is running, you can execute the `komorebi.sample.ahk` script to
If you have AutoHotKey installed and a `komorebi.ahk` file in your home directory (run `$Env:UserProfile` at a
PowerShell prompt to find your home directory), `komorebi` will automatically try to load it when starting.

There is also tentative support for loading a AutoHotKey v2, if the file is named `komorebi.ahk2` and
the `AutoHotKey64.exe` executable for AutoHotKey v2 is in your `Path`. If both `komorebi.ahk` and `komorebi.ahk2` files
exist in your home directory, only `komorebi.ahk` will be loaded. An example of an AutoHotKey v2 configuration file
for _komorebi_ can be found [here](https://gist.github.com/crosstyan/dafacc0778dabf693ce9236c57b201cd).

If you are experiencing behaviour where
[closing a window leaves a blank tile, but minimizing the same window does not](https://github.com/LGUG2Z/komorebi/issues/6)
, you have probably enabled a 'close/minimize to tray' option for that application. You can tell _komorebi_ to handle
Expand Down
34 changes: 29 additions & 5 deletions komorebi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,38 @@ fn setup() -> Result<WorkerGuard> {

pub fn load_configuration() -> Result<()> {
let home = dirs::home_dir().context("there is no home directory")?;
let mut config = home;
config.push("komorebi.ahk");

if config.exists() && which("autohotkey.exe").is_ok() {
let mut config_v1 = home.clone();
config_v1.push("komorebi.ahk");

let mut config_v2 = home;
config_v2.push("komorebi.ahk2");

if config_v1.exists() && which("autohotkey.exe").is_ok() {
tracing::info!(
"loading configuration file: {}",
config_v1
.as_os_str()
.to_str()
.context("cannot convert path to string")?
);

Command::new("autohotkey.exe")
.arg(config.as_os_str())
.arg(config_v1.as_os_str())
.output()?;
}
} else if config_v2.exists() && which("AutoHotkey64.exe").is_ok() {
tracing::info!(
"loading configuration file: {}",
config_v2
.as_os_str()
.to_str()
.context("cannot convert path to string")?
);

Command::new("AutoHotkey64.exe")
.arg(config_v2.as_os_str())
.output()?;
};

Ok(())
}
Expand Down
37 changes: 32 additions & 5 deletions komorebi/src/window_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::VecDeque;
use std::io::ErrorKind;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
Expand Down Expand Up @@ -121,12 +122,32 @@ impl WindowManager {
#[tracing::instrument(skip(self))]
pub fn watch_configuration(&mut self, enable: bool) -> Result<()> {
let home = dirs::home_dir().context("there is no home directory")?;
let mut config = home;
config.push("komorebi.ahk");

let mut config_v1 = home.clone();
config_v1.push("komorebi.ahk");

let mut config_v2 = home;
config_v2.push("komorebi.ahk2");

if config_v1.exists() {
self.configure_watcher(enable, config_v1)?;
} else if config_v2.exists() {
self.configure_watcher(enable, config_v2)?;
}

Ok(())
}

fn configure_watcher(&mut self, enable: bool, config: PathBuf) -> Result<()> {
if config.exists() {
if enable {
tracing::info!("watching configuration for changes");
tracing::info!(
"watching configuration for changes: {}",
config
.as_os_str()
.to_str()
.context("cannot convert path to string")?
);
// Always make absolutely sure that there isn't an already existing watch, because
// hotwatch allows multiple watches to be registered for the same path
match self.hotwatch.unwatch(config.clone()) {
Expand All @@ -144,15 +165,21 @@ impl WindowManager {
// Editing in Notepad sends a NoticeWrite while editing in (Neo)Vim sends
// a NoticeRemove, presumably because of the use of swap files?
DebouncedEvent::NoticeWrite(_) | DebouncedEvent::NoticeRemove(_) => {
tracing::info!("reloading changed configuration file");
thread::spawn(|| {
load_configuration().expect("could not load configuration");
});
}
_ => {}
})?;
} else {
tracing::info!("no longer watching configuration for changes");
tracing::info!(
"no longer watching configuration for changes: {}",
config
.as_os_str()
.to_str()
.context("cannot convert path to string")?
);

self.hotwatch.unwatch(config)?;
};
}
Expand Down

0 comments on commit 4dadffa

Please sign in to comment.