Skip to content

Commit

Permalink
refactor(ahk): merge ahk v1/v2 exe env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
LGUG2Z committed Feb 19, 2023
1 parent 0c8eceb commit f87d4d5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 52 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,8 @@ scoop install autohotkey1.1
If you install AutoHotKey using a different method, the name of the executable file may differ from the name given by
`scoop`, and thus what is expected by default in `komorebi`.

You may override the executables that `komorebi` looks for to launch and reload `komorebi.ahk` configuration files using
by setting one of the following two environment variables depending on which version of AutoHotKey you wish to use:

- `$Env:KOMOREBI_AHK_V1_EXE`
- `$Env:KOMOREBI_AHK_V2_EXE`
You may override the executable that `komorebi` looks for to launch and reload `komorebi.ahk` configuration files by
setting the `$Env:KOMOREBI_AHK_EXE` environment variable.

Please keep in mind that even when setting a custom executable name using these environment variables, the executables
are still required to be in your `Path`.
Expand Down
49 changes: 11 additions & 38 deletions komorebi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,17 @@ lazy_static! {
})
};
static ref DATA_DIR: PathBuf = dirs::data_local_dir().expect("there is no local data directory").join("komorebi");
static ref AHK_V1_EXE: String = {
let mut ahk_v1: String = String::from("autohotkey.exe");
static ref AHK_EXE: String = {
let mut ahk: String = String::from("autohotkey.exe");

if let Ok(komorebi_ahk_v1_exe) = std::env::var("KOMOREBI_AHK_V1_EXE") {
if which(&komorebi_ahk_v1_exe).is_ok() {
ahk_v1 = komorebi_ahk_v1_exe;
if let Ok(komorebi_ahk_exe) = std::env::var("KOMOREBI_AHK_EXE") {
if which(&komorebi_ahk_exe).is_ok() {
ahk = komorebi_ahk_exe;
}
}

ahk_v1
ahk
};
static ref AHK_V2_EXE: String = {
let mut ahk_v2: String = String::from("AutoHotkey64.exe");

if let Ok(komorebi_ahk_v2_exe) = std::env::var("KOMOREBI_AHK_V2_EXE") {
if which(&komorebi_ahk_v2_exe).is_ok() {
ahk_v2 = komorebi_ahk_v2_exe;
}
}

ahk_v2
};

static ref WINDOWS_11: bool = {
matches!(
os_info::get().version(),
Expand Down Expand Up @@ -251,11 +239,8 @@ pub fn load_configuration() -> Result<()> {
let mut config_pwsh = home.clone();
config_pwsh.push("komorebi.ps1");

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

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

if config_pwsh.exists() {
let powershell_exe = if which("pwsh.exe").is_ok() {
Expand All @@ -275,29 +260,17 @@ pub fn load_configuration() -> Result<()> {
Command::new(powershell_exe)
.arg(config_pwsh.as_os_str())
.output()?;
} else if config_v1.exists() && which(&*AHK_V1_EXE).is_ok() {
} else if config_ahk.exists() && which(&*AHK_EXE).is_ok() {
tracing::info!(
"loading configuration file: {}",
config_v1
config_ahk
.as_os_str()
.to_str()
.ok_or_else(|| anyhow!("cannot convert path to string"))?
);

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

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

Expand Down
13 changes: 4 additions & 9 deletions komorebi/src/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,13 @@ impl WindowManager {
let mut config_pwsh = home.clone();
config_pwsh.push("komorebi.ps1");

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

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

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

Ok(())
Expand Down

0 comments on commit f87d4d5

Please sign in to comment.