Skip to content

Commit

Permalink
Added feature to choose scratchpad name
Browse files Browse the repository at this point in the history
May resolve #76
  • Loading branch information
niksingh710 committed Dec 10, 2023
1 parent 285e276 commit 488f110
Showing 1 changed file with 107 additions and 100 deletions.
207 changes: 107 additions & 100 deletions scratchpad/scratchpad
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,65 @@ 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 <<EOF
cat <<EOF
This is a bash script for quick movement of windows to Scratchpad for Hyprland using hyprctl.
Invoking the command without flag will move the focused window to scratchpad.
flags:
-h: Displays This help menu
-c: Checks for all dependencies
-n: Sets the name of scratchpad workspace. default is scratchpad (can be set using env var HYPRLAND_SCRATCHPAD_NAME)
Usefull if you want to have multiple scratchpads.
-g: To bring back client from scratchpad to current workspace.
if only one client is present then no menu is popped and client will me moved.
if want menu before moving even if count of client is one then use -l.
Expand All @@ -87,89 +90,93 @@ EOF
}

takes() {
hyprctl dispatch workspace "special:scratchpad"
exit 0
hyprctl dispatch workspace "special:$default_scratchpad_name"
exit 0
}

getBack() {
_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: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
Expand Down

0 comments on commit 488f110

Please sign in to comment.