From e6b5b78857cff017135db2008bb62648632feee6 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 7 Dec 2024 11:14:23 -0800 Subject: [PATCH] feat(cli): add kill cmd This commit adds a new komorebic command, "kill", to kill background processes that may be started by "komorebic start", without terminating the main komorebi process. This is useful when iterating on changes to external components like the bar which may require restarts. --- komorebic/src/main.rs | 90 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 7521b3b0..24c1db04 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -800,6 +800,22 @@ struct Stop { masir: bool, } +#[derive(Parser)] +struct Kill { + /// Kill whkd if it is running as a background process + #[clap(long)] + whkd: bool, + /// Kill ahk if it is running as a background process + #[clap(long)] + ahk: bool, + /// Kill komorebi-bar if it is running as a background process + #[clap(long)] + bar: bool, + /// Kill masir if it is running as a background process + #[clap(long)] + masir: bool, +} + #[derive(Parser)] struct SaveResize { /// File to which the resize layout dimensions should be saved @@ -922,6 +938,8 @@ enum SubCommand { Start(Start), /// Stop the komorebi.exe process and restore all hidden windows Stop(Stop), + /// Kill background processes started by komorebic + Kill(Kill), /// Check komorebi configuration and related files for common errors Check, /// Show the path to komorebi.json @@ -2305,6 +2323,78 @@ Stop-Process -Name:komorebi -ErrorAction SilentlyContinue } } } + SubCommand::Kill(arg) => { + if arg.whkd { + let script = r" +Stop-Process -Name:whkd -ErrorAction SilentlyContinue + "; + match powershell_script::run(script) { + Ok(_) => { + println!("{script}"); + } + Err(error) => { + println!("Error: {error}"); + } + } + } + + if arg.bar { + let script = r" +Stop-Process -Name:komorebi-bar -ErrorAction SilentlyContinue + "; + match powershell_script::run(script) { + Ok(_) => { + println!("{script}"); + } + Err(error) => { + println!("Error: {error}"); + } + } + } + + if arg.masir { + let script = r" +Stop-Process -Name:masir -ErrorAction SilentlyContinue + "; + match powershell_script::run(script) { + Ok(_) => { + println!("{script}"); + } + Err(error) => { + println!("Error: {error}"); + } + } + } + + if arg.ahk { + let script = r#" +if (Get-Command Get-CimInstance -ErrorAction SilentlyContinue) { + (Get-CimInstance Win32_Process | Where-Object { + ($_.CommandLine -like '*komorebi.ahk"') -and + ($_.Name -in @('AutoHotkey.exe', 'AutoHotkey64.exe', 'AutoHotkey32.exe', 'AutoHotkeyUX.exe')) + } | Select-Object -First 1) | ForEach-Object { + Stop-Process -Id $_.ProcessId -ErrorAction SilentlyContinue + } +} else { + (Get-WmiObject Win32_Process | Where-Object { + ($_.CommandLine -like '*komorebi.ahk"') -and + ($_.Name -in @('AutoHotkey.exe', 'AutoHotkey64.exe', 'AutoHotkey32.exe', 'AutoHotkeyUX.exe')) + } | Select-Object -First 1) | ForEach-Object { + Stop-Process -Id $_.ProcessId -ErrorAction SilentlyContinue + } +} +"#; + + match powershell_script::run(script) { + Ok(_) => { + println!("{script}"); + } + Err(error) => { + println!("Error: {error}"); + } + } + } + } SubCommand::IgnoreRule(arg) => { send_message(&SocketMessage::IgnoreRule(arg.identifier, arg.id))?; }