From 97eb6e4353144160e953cd6e2dac2ce0da538ace Mon Sep 17 00:00:00 2001 From: Schweber <64630479+Schweber@users.noreply.github.com> Date: Sat, 13 Jan 2024 06:03:40 +0100 Subject: [PATCH] Add --class --- README.md | 9 ++++++--- hdrop | 31 ++++++++++++++++++++++++++----- hdrop.1.scd | 3 +++ 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7b42920..405f7bf 100755 --- a/README.md +++ b/README.md @@ -18,7 +18,10 @@ This Bash script emulates the main features of [tdrop](https://github.com/noctui #### Options: > -b, --background -> changes the default behaviour: if the specified program is not running, launch it in the background instead of in the foreground. Thereafter 'hdrop -b' will work the same as without this flag. +> Changes the default behaviour: if the specified program is not running, launch it in the background instead of in the foreground. Thereafter 'hdrop -b' will work the same as without this flag. +> +> -c, --class +> Set classname of the program to be run. Use this if the classname is different from the name of the [COMMAND]. > > -i, --insensitive > Case insensitive partial matching of class names. Can work as a stopgap if a running program is not recognized and a new instance is launched instead. Note: incorrect matches may occur, adding a special handling of the program to hdrop is preferable. @@ -36,8 +39,8 @@ This Bash script emulates the main features of [tdrop](https://github.com/noctui Multiple instances of the same program can be run concurrently, if different class names are assigned to each instance. Presently there is support for the following flags in the [COMMAND] string: -> -a ([foot](https://codeberg.org/dnkl/foot/) terminal emulator) -> --class (all other programs) +> `-a` ([foot](https://codeberg.org/dnkl/foot/) terminal emulator) +> `--class` (all other programs) #### Example bindings in Hyprland config: diff --git a/hdrop b/hdrop index 23cf2f6..4d3e719 100755 --- a/hdrop +++ b/hdrop @@ -13,10 +13,14 @@ print_help() { echo " not running, launch it in the background instead of foreground." echo " Thereafter 'hdrop -b' will work the same as without this flag." echo "" + echo " -c, --class" + echo " Set classname of the program to be run. Use this if the" + echo " classname is different from the name of the [COMMAND]." + echo "" echo " -i, --insensitive" echo " Case insensitive partial matching of class names." - echo " Try this if a running program is not recognized and a new" - echo " instance is launched instead." + echo " You can try this if a running program is not recognized and" + echo " a new instance is launched instead." echo "" echo " -v, --verbose" echo " Show notifications regarding the matching process." @@ -41,7 +45,7 @@ print_help() { } print_version() { - echo "hdrop version: 0.3.0" + echo "hdrop version: 0.3.1" } notify() { @@ -69,6 +73,10 @@ hdrop_flags() { BACKGROUND="hyprctl dispatch -- exec [workspace special:hdrop silent] " shift ;; + -c | --class) + CLASS_OVERRIDE="$2" + shift 2 + ;; -i | --insensitive) INSENSITIVE=true shift @@ -95,7 +103,10 @@ while true; do HDROP_FLAGS+=("$1") shift ;; - *) break ;; + *) + HDROP_FLAGS+=("$2") + break + ;; esac done @@ -103,8 +114,10 @@ BACKGROUND="" INSENSITIVE=false VERBOSE=false +# shellcheck disable=SC2128 if [[ -n $HDROP_FLAGS ]]; then - hdrop_flags $(getopt --options bivVh --longoptions background,insensitive,verbose,version,help -n hdrop -- "${HDROP_FLAGS[@]}") + # shellcheck disable=SC2046 + hdrop_flags $(getopt --options bc:ivVh --longoptions background,class:,insensitive,verbose,version,help -n hdrop -- "${HDROP_FLAGS[@]}") fi CLASS="$1" @@ -117,9 +130,11 @@ foot) ;; logseq) CLASS="Logseq" + if $VERBOSE; then notify_low "hdrop: Using '$CLASS' as hardcoded classname of '$1' for matching"; fi ;; telegram-desktop) CLASS="org.telegram.desktop" + if $VERBOSE; then notify_low "hdrop: Using '$CLASS' as hardcoded classname of '$1' for matching"; fi ;; *) OPT=$(getopt --longoptions class: -n hdrop -- "$@") @@ -131,6 +146,7 @@ if [[ -n $OPT ]]; then case "$1" in -a | --class) CLASS="$2" + if $VERBOSE; then notify_low "hdrop: Extracted '$CLASS' from [COMMAND] for matching"; fi ;; esac fi @@ -141,6 +157,11 @@ if $INSENSITIVE && [[ $(hyprctl clients -j | jq -r ".[] | select((.class |test(\ if $VERBOSE; then notify_low "hdrop: --insensitive -> Using class '$CLASS' after insensitive (partial) matching"; fi fi +if [[ -n $CLASS_OVERRIDE ]]; then + if $VERBOSE; then notify_low "hdrop: --class -> Using given class '$CLASS_OVERRIDE' instead of '$CLASS' for matching"; fi + CLASS="$CLASS_OVERRIDE" +fi + if [[ $(hyprctl clients -j | jq -r ".[] | select(.class==\"$CLASS\" and .workspace.id!=$ACTIVE_WORKSPACE)") ]]; then hyprctl dispatch -- movetoworkspacesilent "$ACTIVE_WORKSPACE","$CLASS" || notify "hdrop: Error moving '$COMMANDLINE' to current workspace" hyprctl dispatch -- focuswindow "$CLASS" || notify "hdrop: Error focusing '$COMMANDLINE' on current workspace" diff --git a/hdrop.1.scd b/hdrop.1.scd index f203b2e..9d225a5 100755 --- a/hdrop.1.scd +++ b/hdrop.1.scd @@ -16,6 +16,9 @@ hdrop - run, show and hide programs on Hyprland *-b, --background* Changes the default behaviour: if the specified program is not running, launch it in the background instead of foreground. Thereafter 'hdrop -b' will work the same as without this flag. +*-c, --class* + Set classname of the program to be run. Use this if the classname is different from the name of the [COMMAND]. + *-i, --insensitive* Case insensitive partial matching of class names. Can work as a stopgap if a running program is not recognized and a new instance is launched instead. Note: incorrect matches may occur, adding a special handling of the program to `hdrop` is preferable.