From bddacf341cb7406ad9ac6f1055c4ca4d82630815 Mon Sep 17 00:00:00 2001 From: Myned Date: Wed, 11 Sep 2024 18:26:51 -0500 Subject: [PATCH] toggle: refactor with proper flags Signed-off-by: Myned --- options/custom/scripts/toggle.sh | 63 ++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/options/custom/scripts/toggle.sh b/options/custom/scripts/toggle.sh index c68ebab..1d66233 100644 --- a/options/custom/scripts/toggle.sh +++ b/options/custom/scripts/toggle.sh @@ -1,27 +1,58 @@ #! /usr/bin/env bash -# Toggle tagged window, launch if needed -#?? toggle TAG WORKSPACE [COMMAND] -# TODO: Use proper flags -# TODO: Support floating groups +set -x -if (("$#" >= 3)); then - # Launch if tag does not exist yet - if ! hyprctl -j clients | jq -r '.[].tags[]' | grep "$1"; then - hyprctl dispatch exec -- "${@:3}" +# Toggle pinned window, launch if needed +#?? toggle --type TYPE --expression EXPRESSION --workspace WORKSPACE [COMMAND] +#!! Regex may need to be double-escaped +# https://jqlang.github.io/jq/manual/#regular-expressions + +focus=false + +while (("$#" > 0)); do + case "$1" in + -e | --expression) + shift + expression="$1" + ;; + -f | --focus) + focus=true + ;; + -t | --type) + shift + type="$1" + ;; + -w | --workspace) + shift + workspace="$1" + ;; + --) + shift + break + ;; + esac + shift +done + +command="${*}" + +if [[ "$command" ]]; then + if ! hyprctl -j clients | jq -re "any(.[].$type | test(\"$expression\"); . == true)"; then + hyprctl dispatch exec -- "$command" # Launch window exit fi fi -# Dispatchers do not currently support matching by tag, so select address -window="address:$(hyprctl -j clients | jq -r "first(.[] | select(.tags[] | startswith(\"$1\")).address)")" +current_workspace="$(hyprctl -j clients | jq -r "first(.[] | select(.$type | test(\"$expression\")).workspace.name)")" -workspace="$(hyprctl -j clients | jq -r "first(.[] | select(.tags[] | startswith(\"$1\")).workspace.name)")" +if [[ "$current_workspace" == "$workspace" ]]; then + hyprctl dispatch movetoworkspacesilent "0,$type:$expression" # Move to current workspace first, otherwise some windows freeze + hyprctl dispatch pin "$type:$expression" # Pin -if [[ "$workspace" == "$2" ]]; then - hyprctl dispatch pin "$window" # Pin - (("$#" >= 3)) && hyprctl dispatch focuswindow "$window" # Focus if third argument + if "$focus"; then + hyprctl dispatch focuswindow "$type:$expression" # Focus + fi else - hyprctl dispatch pin "$window" # Unpin - hyprctl dispatch movetoworkspacesilent "$2,$window" + hyprctl dispatch pin "$type:$expression" # Unpin + hyprctl dispatch movetoworkspacesilent "$workspace,$type:$expression" # Move to workspace fi