diff --git a/scratchpad/scratchpad b/scratchpad/scratchpad index bcb0681..b3b74e1 100755 --- a/scratchpad/scratchpad +++ b/scratchpad/scratchpad @@ -9,55 +9,56 @@ green="\033[0;32m" red="\033[0;31m" blue="\033[0;34m" nocolor="\033[0m" +default_scratchpad_name="${HYPRLAND_SCRATCHPAD_NAME:-scratchpad}" check() { - command -v "$1" 1>/dev/null + command -v "$1" 1>/dev/null } ok() { - echo -e "[$green  $nocolor] $*" + echo -e "[$green  $nocolor] $*" } err() { - echo -e "[$red  $nocolor] $*" + echo -e "[$red  $nocolor] $*" } optional() { - echo -e "[$blue  $nocolor] $*" + echo -e "[$blue  $nocolor] $*" } notify() { - # shellcheck disable=SC2015 - check notify-send && { - notify-send "$@" - } || { - echo "$@" - } + # shellcheck disable=SC2015 + check notify-send && { + notify-send "$@" + } || { + echo "$@" + } } checkUtils() { - # shellcheck disable=SC2015 - check sed && ok "sed" || err "sed" - # shellcheck disable=SC2015 - check jq && ok "jq" || err "jq" - # shellcheck disable=SC2015 - check notify-send && ok "notify-send (Optional)" || optional "notify-send (Optional)" - exit + # shellcheck disable=SC2015 + check sed && ok "sed" || err "sed" + # shellcheck disable=SC2015 + check jq && ok "jq" || err "jq" + # shellcheck disable=SC2015 + check notify-send && ok "notify-send (Optional)" || optional "notify-send (Optional)" + exit } basicChecks() { - check hyprctl || { - notify "Seriously mate!!" "Start Hyprland before this script" - exit 1 - } - pgrep Hyprland &>/dev/null || { - notify "Make Sure Hyprland Session is running." - exit 1 - } + check hyprctl || { + notify "Seriously mate!!" "Start Hyprland before this script" + exit 1 + } + pgrep Hyprland &>/dev/null || { + notify "Make Sure Hyprland Session is running." + exit 1 + } } help() { - cat </dev/null; then exit; fi # Menu running already? → exit - - _current_workspace="$(hyprctl monitors -j | jq '.[] | select(.focused==true)' | jq -j '.activeWorkspace.name')" - - [ -z "$_current_workspace" ] && { - notify "Scratchpad" "Some Error Occured while getting current workspace." - exit 1 - } - - readarray -t _clients_on_spad < <(hyprctl clients -j | - jq '.[] | select(.workspace.name=="special:scratchpad")' | - jq -j '[.class, .title, .address] | @text' | - sed -z -e 's/]/]\n/g' \ - -e 's/["]//g' \ - -e 's/,/ /g' \ - -e 's/\[//g' \ - -e 's/]//g') - - [ "${#_clients_on_spad[@]}" -eq 0 ] && { - notify "No Clients" "There was no client on scratchpad" - exit 0 - } - [ "${#_clients_on_spad[@]}" -eq 1 ] && { - $_listing || { - _selected_client="${_clients_on_spad[0]}" - } - } - [ -z "$_selected_client" ] && _selected_client="$(echo "${_clients_on_spad[*]}" | eval "${_menu_cmd[*]}")" - _address="$(echo "$_selected_client" | awk '{print $NF}')" - hyprctl dispatch movetoworkspace "$_current_workspace,address:$_address" - hyprctl dispatch focuswindow "address:$_address" - [ "$(hyprctl activewindow -j | jq '.floating')" = true ] && { - hyprctl dispatch bringactivetotop none - } - IFS="$_ifs" + _ifs="$IFS" + IFS=$'\n'$'\n' + + if pgrep "${_menu_cmd%% *}" >/dev/null; then exit; fi # Menu running already? → exit + + _current_workspace="$(hyprctl monitors -j | jq '.[] | select(.focused==true)' | jq -j '.activeWorkspace.name')" + + [ -z "$_current_workspace" ] && { + notify "Scratchpad" "Some Error Occured while getting current workspace." + exit 1 + } + + readarray -t _clients_on_spad < <(hyprctl clients -j | + jq '.[] | select(.workspace.name=="special:'"$default_scratchpad_name"'")' | + jq -j '[.class, .title, .address] | @text' | + sed -z -e 's/]/]\n/g' \ + -e 's/["]//g' \ + -e 's/,/ /g' \ + -e 's/\[//g' \ + -e 's/]//g') + + [ "${#_clients_on_spad[@]}" -eq 0 ] && { + notify "No Clients" "There was no client on $default_scratchpad_name" + exit 0 + } + [ "${#_clients_on_spad[@]}" -eq 1 ] && { + $_listing || { + _selected_client="${_clients_on_spad[0]}" + } + } + [ -z "$_selected_client" ] && _selected_client="$(echo "${_clients_on_spad[*]}" | eval "${_menu_cmd[*]}")" + _address="$(echo "$_selected_client" | awk '{print $NF}')" + hyprctl dispatch movetoworkspace "$_current_workspace,address:$_address" + hyprctl dispatch focuswindow "address:$_address" + [ "$(hyprctl activewindow -j | jq '.floating')" = true ] && { + hyprctl dispatch bringactivetotop none + } + IFS="$_ifs" } send() { - hyprctl dispatch movetoworkspacesilent "special:scratchpad" + hyprctl dispatch movetoworkspacesilent "special:$default_scratchpad_name" } getArgs() { - while [ "$#" -gt 0 ]; do - case "$1" in - -h | --help) - help - exit 0 - ;; - -g) - getBack - exit 0 - ;; - -l) - _listing=true - ;; - -t) - takes - ;; - -m) - _menu_cmd=("$2") - shift - ;; - -c) - checkUtils - ;; - *) - help - echo "" - echo "Wrong argument given" - exit 1 - ;; - esac - shift - done - send + while [ "$#" -gt 0 ]; do + case "$1" in + -h | --help) + help + exit 0 + ;; + -g) + getBack + exit 0 + ;; + -l) + _listing=true + ;; + -t) + takes + ;; + -m) + _menu_cmd=("$2") + shift + ;; + -c) + checkUtils + ;; + -n) + default_scratchpad_name="$2" + shift + ;; + *) + help + echo "" + echo "Wrong argument given" + exit 1 + ;; + esac + shift + done + send } basicChecks