From a02cd699a0e9292a63b0ce5f931db6a7f165e00f Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 23 Aug 2021 07:49:34 -0700 Subject: [PATCH] refactor(derive-ahk): push up generation logic This commit pushes as much of the generation logic as possible to the derive-ahk crate, so that when it is used in komorebic, we only need to do an as_bytes() call to prepare it for being written to a file. Besides that, this commit changes the generation command name to 'ahk-library' for clarity, and adds both additional samples and instructions in the readme file and Scoop post-install hook. --- .goreleaser.yml | 5 +- README.md | 12 +- derive-ahk/src/lib.rs | 7 +- komorebi.sample.with.lib.ahk | 218 +++++++++++++++++++++++++++++++++++ komorebic.lib.sample.ahk | 6 +- komorebic/src/main.rs | 7 +- 6 files changed, 238 insertions(+), 17 deletions(-) create mode 100644 komorebi.sample.with.lib.ahk diff --git a/.goreleaser.yml b/.goreleaser.yml index cb231096..a9671172 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -55,5 +55,6 @@ scoop: pre_install: - if (Get-Process -Name komorebi -ErrorAction SilentlyContinue) { komorebic stop } post_install: - - Write-Host "Run 'cp $original_dir\komorebi.sample.ahk $Env:UserProfile\komorebi.ahk' to get started with the sample configuration" - - Write-Host "Once you have a configuration file in place, you can run 'komorebic start' to start the window manager" + - Write-Host "`nRun 'cp $original_dir\komorebi.sample.ahk $Env:UserProfile\komorebi.ahk' to get started with the sample configuration" + - Write-Host "`nRun 'komorebic ahk-library' if you would like to generate an AHK helper library to use in your configuration" + - Write-Host "`nOnce you have a configuration file in place, you can run 'komorebic start' to start the window manager" diff --git a/README.md b/README.md index 30acc6ba..2584119a 100644 --- a/README.md +++ b/README.md @@ -213,15 +213,19 @@ manage-rule Add a rule to always manage the specified applicati workspace-rule Add a rule to associate an application with a workspace identify-tray-application Identify an application that closes to the system tray focus-follows-mouse Enable or disable focus follows mouse for the operating system -ahk-lib Generate a library of AutoHotKey helper functions +ahk-library Generate a library of AutoHotKey helper functions help Print this message or the help of the given subcommand(s) ``` -Additionally, you may run `komorebic.exe ahk-lib` to generate [a helper library for AutoHotKey](komorebic.lib.sample.ahk) -which wraps every `komorebic` command in a native AHK function. +### AutoHotKey Helper Library for `komorebic` + +Additionally, you may run `komorebic.exe ahk-library` to +generate [a helper library for AutoHotKey](komorebic.lib.sample.ahk) which wraps every `komorebic` command in a native +AHK function. If you include the generated library at the top of your `~/komorebi.ahk` configuration file, you will be able to call -any of the functions that it contains. +any of the functions that it contains. A sample AHK script that shows how this library can be +used [is available here](komorebi.sample.with.lib.ahk). ## Features diff --git a/derive-ahk/src/lib.rs b/derive-ahk/src/lib.rs index e7836991..51f1dfcf 100644 --- a/derive-ahk/src/lib.rs +++ b/derive-ahk/src/lib.rs @@ -83,13 +83,12 @@ pub fn ahk(input: proc_macro::TokenStream) -> proc_macro::TokenStream { quote! { impl #name { - fn ahk_functions() -> Vec { - let mut v: Vec = vec![]; - v.push(String::from("; Generated by komorebic.exe ahk-lib")); + fn generate_ahk_library() -> String { + let mut v: Vec = vec![String::from("; Generated by komorebic.exe")]; #stream - v + v.join("\n") } } } diff --git a/komorebi.sample.with.lib.ahk b/komorebi.sample.with.lib.ahk new file mode 100644 index 00000000..3217fd9b --- /dev/null +++ b/komorebi.sample.with.lib.ahk @@ -0,0 +1,218 @@ +#SingleInstance Force +#Include %A_ScriptDir%\komorebic.lib.ahk + +; Enable hot reloading of changes to this file +WatchConfiguration("enable") + +; Enable focus follows mouse +FocusFollowsMouse("enable") + +; Ensure there are 5 workspaces created on monitor 0 +EnsureWorkspaces(0, 5) + +; Configure the 1st workspace +WorkspaceName(0, 0, "bsp") + +; Configure the 2nd workspace +WorkspaceName(0, 1, "columns") ; Optionally set the name of the workspace +WorkspacePadding(0, 1, 30) ; Set the padding around the edge of the screen +ContainerPadding(0, 1, 30) ; Set the padding between the containers on the screen +WorkspaceRule("exe", "slack.exe", 0, 1) ; Always show chat apps on this workspace + +; Configure the 3rd workspace +WorkspaceName(0, 2, "thicc") +WorkspacePadding(0, 2, 200) ; Set some super thicc padding + +; Configure the 4th workspace +WorkspaceName(0, 3, "matrix") +WorkspacePadding(0, 3, 0) ; No padding at all +ContainerPadding(0, 3, 0) ; Matrix-y hacker vibes + +; Configure the 5th workspace +WorkspaceName(0, 4, "floaty") +WorkspaceTiling(0, 4, "disable") ; Everything floats here + +; Configure floating rules +FloatRule("class", "SunAwtDialog") ; All the IntelliJ popups +FloatRule("title", "Control Panek") +FloatRule("class", "TaskManagerWindow") +FloatRule("exe", "Wally.exe") +FloatRule("exe", "wincompose.exe") +FloatRule("exe", "1Password.exe") +FloatRule("exe", "Wox.exe") + +; Change the focused window, Alt + Vim direction keys +!h:: +Focus("left") +return + +!j:: +Focus("down") +return + +!k:: +Focus("up") +return + +!l:: +Focus("right") +return + +; Move the focused window in a given direction, Alt + Shift + Vim direction keys +!+h:: +Move("left") +return + +!+j:: +Move("down") +return + +!+k:: +Move("up") +return + +!+l:: +Move("right") +return + +; Stack the focused window in a given direction, Alt + Shift + direction keys +!+Left:: +Stack("left") +return + +!+Down:: +Stack("down") +return + +!+Up:: +Stack("up") +return + +!+Right:: +Stack("right") +return + +!]:: +CycleStack("next") +return + +![:: +CycleStack("previous") +return + +; Unstack the focused window, Alt + Shift + D +!+d:: +Unstack() +return + +; Promote the focused window to the top of the tree, Alt + Shift + Enter +!+Enter:: +Promote() +return + +; Manage the focused window +!=:: +Manage() +return + +; Unmanage the focused window +!-:: +Unmanage() +return + +; Switch to an equal-width, max-height column layout on the main workspace, Alt + Shift + C +!+c:: +ChangeLayout("columns") +return + +; Switch to the default bsp tiling layout on the main workspace, Alt + Shift + T +!+t:: +ChangeLayout("bsp") +return + +; Toggle the Monocle layout for the focused window, Alt + Shift + F +!+f:: +ToggleMonocle() +return + +; Toggle native maximize for the focused window, Alt + Shift + = +!+=:: +ToggleMaximize() +return + +; Flip horizontally, Alt + X +!x:: +FlipLayout("horizontal") +return + +; Flip vertically, Alt + Y +!y:: +FlipLayout("vertical") +return + +; Force a retile if things get janky, Alt + Shift + R +!+r:: +Retile() +return + +; Float the focused window, Alt + T +!t:: +ToggleFloat() +return + +; Reload ~/komorebi.ahk, Alt + O +!o:: +ReloadConfiguration() +return + +; Pause responding to any window events or komorebic commands, Alt + P +!p:: +TogglePause() +return + +; Switch to workspace +!1:: +Send ! +FocusWorkspace(0) +return + +!2:: +Send ! +FocusWorkspace(1) +return + +!3:: +Send ! +FocusWorkspace(2) +return + +!4:: +Send ! +FocusWorkspace(3) +return + +!5:: +Send ! +FocusWorkspace(4) +return + +; Move window to workspace +!+1:: +MoveToWorkspace(0) +return + +!+2:: +MoveToWorkspace(1) +return + +!+3:: +MoveToWorkspace(2) +return + +!+4:: +MoveToWorkspace(3) +return + +!+5:: +MoveToWorkspace(4) +return diff --git a/komorebic.lib.sample.ahk b/komorebic.lib.sample.ahk index c484aed0..ef92a352 100644 --- a/komorebic.lib.sample.ahk +++ b/komorebic.lib.sample.ahk @@ -1,4 +1,4 @@ -; Generated by komorebic.exe generate-ahk-library +; Generated by komorebic.exe Start() { Run, komorebic.exe start, , Hide @@ -168,6 +168,6 @@ FocusFollowsMouse(boolean_state) { Run, komorebic.exe focus-follows-mouse %boolean_state%, , Hide } -AhkLib() { - Run, komorebic.exe ahk-lib, , Hide +AhkLibrary() { + Run, komorebic.exe ahk-library, , Hide } \ No newline at end of file diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 80054fe8..1a9dad53 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -337,7 +337,7 @@ enum SubCommand { /// Enable or disable focus follows mouse for the operating system FocusFollowsMouse(FocusFollowsMouse), /// Generate a library of AutoHotKey helper functions - AhkLib, + AhkLibrary, } pub fn send_message(bytes: &[u8]) -> Result<()> { @@ -354,7 +354,7 @@ fn main() -> Result<()> { let opts: Opts = Opts::parse(); match opts.subcmd { - SubCommand::AhkLib => { + SubCommand::AhkLibrary => { let mut library = dirs::home_dir().context("there is no home directory")?; library.push("komorebic.lib.ahk"); let mut file = OpenOptions::new() @@ -363,8 +363,7 @@ fn main() -> Result<()> { .truncate(true) .open(library.clone())?; - let library_text: String = SubCommand::ahk_functions().join("\n"); - file.write_all(library_text.as_bytes())?; + file.write_all(SubCommand::generate_ahk_library().as_bytes())?; println!( "\nAHK helper library for komorebic written to {}",