nix/scripts/hypr
2026-02-07 00:45:47 -05:00

283 lines
8 KiB
Bash
Executable file

#!/bin/sh
usage() {
cat <<EOF
Usage: hypr <subcommand> [app] [args...]
Commands:
volume {up,down,toggle} Adjust volume accordingly and notify
brightness {up,down} Adjust brightness accordingly and notify
spawnfocus <app> [args...] Focus existing window or spawn app with args
pull [app] Pull window to current workspace (picker if no app)
borders Initialize dynamic borders
exit Safely exit hyprland
Options:
-h, --help Show this help message
EOF
}
[ -z "$1" ] && {
usage
exit 1
}
cmd="$1"
shift
case "$cmd" in
-h | --help)
usage
exit 0
;;
exit)
pkill hypridle
pkill hyprpaper
hyprctl dispatch exit
exit 0
;;
brightness)
BRIGHT_STEP=5
max_brightness="$(brightnessctl max)"
case "$1" in
up)
brightnessctl set "$BRIGHT_STEP"%+
notify-send -u low -t 2500 -r 5555 "brightness $(brightnessctl get | awk -v max="$max_brightness" '{printf "%d%%", $1*100/max}')"
;;
down)
brightnessctl set "$BRIGHT_STEP"%-
notify-send -u low -t 2500 -r 5555 "brightness $(brightnessctl get | awk -v max="$max_brightness" '{printf "%d%%", $1*100/max}')"
;;
*)
echo "Invalid subcommand: $1" >&2
exit 1
;;
esac
;;
volume)
SINK="@DEFAULT_SINK@"
VOL_STEP=5
get_vol() { pactl get-sink-volume "$SINK" | awk 'NR==1{print $5+0}'; }
case "$1" in
up)
vol=$(get_vol)
[ "$vol" -lt 100 ] && vol=$((vol + VOL_STEP))
[ "$vol" -gt 100 ] && vol=100
pactl set-sink-volume "$SINK" "${vol}%"
;;
down)
vol=$(get_vol)
vol=$((vol - VOL_STEP))
[ "$vol" -lt 0 ] && vol=0
pactl set-sink-volume "$SINK" "${vol}%"
;;
toggle)
pactl set-sink-mute "$SINK" toggle
;;
*)
echo "Invalid subcommand: $1" >&2
exit 1
;;
esac
muted=$(pactl get-sink-mute "$SINK" | awk '{print $2}')
vol=$(get_vol)
[ "$vol" -gt 100 ] && vol=100
if [ "$muted" = "yes" ]; then
notify-send -u low -t 2500 -r 5556 "volume: ${vol}% (muted)"
else
notify-send -u low -t 2500 -r 5556 "volume: ${vol}%"
fi
;;
pull)
APP="$1"
if [ -n "$APP" ]; then
case "$APP" in
google-chrome | google-chrome-stable) CLASS="google-chrome" ;;
chromium | ungoogled-chromium) CLASS="Chromium" ;;
firefox) CLASS="firefox" ;;
alacritty) CLASS="Alacritty" ;;
code | vscodium) CLASS="Code" ;;
signal-desktop | signal) CLASS="signal" ;;
telegram-desktop | telegram) CLASS="TelegramDesktop" ;;
ghostty) CLASS="com.mitchellh.ghostty" ;;
bitwarden-desktop | bitwarden) CLASS="Bitwarden" ;;
slack) CLASS="Slack" ;;
discord) CLASS="discord" ;;
vesktop) CLASS="vesktop" ;;
*) CLASS="$APP" ;;
esac
CUR_ADDR=$(hyprctl -j activewindow | jq -r '.address')
WIN_ADDRS=$(
hyprctl -j clients 2>/dev/null | jq -r --arg class "$CLASS" '
.[]? | select(
((.xdgTag // "") | ascii_downcase | contains($class | ascii_downcase)) or
((.initialClass // "") | ascii_downcase | contains($class | ascii_downcase)) or
((.class // "") | ascii_downcase | contains($class | ascii_downcase))
) | "\(.class)\t\(.title)\t\(.address)"'
)
WIN_COUNT=$(echo "$WIN_ADDRS" | grep -c .)
if [ "$WIN_COUNT" -eq 1 ]; then
WIN_ADDR=$(echo "$WIN_ADDRS" | awk -F'\t' '{print $3}')
elif [ "$WIN_COUNT" -gt 1 ]; then
SELECTED=$(echo "$WIN_ADDRS" |
awk -F'\t' -v cur="$CUR_ADDR" '{if($3!=cur) print $1 ": " $2 "\t" $3}' |
rofi -dmenu -i -p "pull window")
WIN_ADDR=$(echo "$SELECTED" | awk -F'\t' '{print $2}')
fi
fi
if [ -z "$SELECTED" ]; then
exit 0
fi
if [ -z "$WIN_ADDR" ]; then
CUR_ADDR=$(hyprctl -j activewindow | jq -r '.address')
WIN_ADDRS=$(hyprctl -j clients 2>/dev/null | jq -r '.[]? | "\(.class)\t\(.title)\t\(.address)"')
SELECTED=$(echo "$WIN_ADDRS" |
awk -F'\t' -v cur="$CUR_ADDR" '{if($3!=cur) print $1 ": " $2 "\t" $3}' |
rofi -dmenu -i -p "pull window")
WIN_ADDR=$(echo "$SELECTED" | awk -F'\t' '{print $2}')
fi
if [ -n "$WIN_ADDR" ]; then
CURRENT_WS="$(hyprctl activeworkspace | head -n1 | awk -F'[()]' '{print $2}')"
hyprctl dispatch movetoworkspace "$CURRENT_WS,address:$WIN_ADDR"
hyprctl dispatch focuswindow "address:$WIN_ADDR"
fi
;;
spawnfocus)
WS=""
while [ $# -gt 0 ]; do
case "$1" in
--ws)
if [ $# -lt 2 ]; then
echo "Missing workspace number after --ws"
exit 1
fi
WS="$2"
shift 2
;;
-*)
echo "Unknown option $1"
exit 1
;;
*)
break
;;
esac
done
APP="$1"
[ -z "$APP" ] && {
echo 'Specify an app'
exit 1
}
shift
case "$APP" in
google-chrome | google-chrome-stable) CLASS="google-chrome" ;;
chromium | ungoogled-chromium) CLASS="Chromium" ;;
firefox) CLASS="firefox" ;;
alacritty) CLASS="Alacritty" ;;
code | vscodium) CLASS="Code" ;;
signal-desktop | signal) CLASS="signal" ;;
telegram-desktop | telegram) CLASS="TelegramDesktop" ;;
ghostty) CLASS="com.mitchellh.ghostty" ;;
bitwarden-desktop | bitwarden) CLASS="Bitwarden" ;;
slack) CLASS="Slack" ;;
discord) CLASS="discord" ;;
vesktop) CLASS="vesktop" ;;
*) CLASS="$APP" ;;
esac
WIN_ADDRS=$(hyprctl -j clients 2>/dev/null | jq -r --arg class "$CLASS" '
.[]? | select(
((.xdgTag // "") | ascii_downcase | contains($class | ascii_downcase)) or
((.initialClass // "") | ascii_downcase | contains($class | ascii_downcase)) or
((.class // "") | ascii_downcase | contains($class | ascii_downcase))
) | "\(.class)\t\(.title)\t\(.address)"
')
WIN_COUNT=$(echo "$WIN_ADDRS" | grep -c .)
if [ "$WIN_COUNT" -eq 0 ]; then
WIN_ADDR=""
elif [ "$WIN_COUNT" -eq 1 ]; then
WIN_ADDR=$(echo "$WIN_ADDRS" | awk -F'\t' '{print $3}')
elif [ "$WIN_COUNT" -gt 1 ]; then
SELECTED=$(echo "$WIN_ADDRS" | awk -F'\t' '{print $1 ": " $2 "\t" $3}' | rofi -dmenu -i -p "select window")
if [ -z "$SELECTED" ]; then
exit 0
fi
WIN_ADDR=$(echo "$SELECTED" | awk -F'\t' '{print $2}')
fi
if [ -n "$WIN_ADDR" ]; then
if [ -n "$WS" ]; then
WIN_WS=$(hyprctl clients -j | jq -r --arg addr "$WIN_ADDR" '.[] | select(.address == $addr) | .workspace.id')
if [ "$WIN_WS" != "$WS" ]; then
hyprctl dispatch movetoworkspacesilent "$WS,address:$WIN_ADDR"
fi
hyprctl dispatch workspace "$WS"
else
WIN_WS=$(hyprctl clients -j | jq -r --arg addr "$WIN_ADDR" '.[] | select(.address == $addr) | .workspace.id')
hyprctl dispatch workspace "$WIN_WS"
fi
hyprctl dispatch focuswindow "address:$WIN_ADDR"
else
EXISTING_HEX=$(hyprctl -j clients 2>/dev/null | jq -r --arg class "$CLASS" '
.[]? | select(
((.xdgTag // "") | ascii_downcase | contains($class | ascii_downcase)) or
((.initialClass // "") | ascii_downcase | contains($class | ascii_downcase)) or
((.class // "") | ascii_downcase | contains($class | ascii_downcase))
) | .address | ltrimstr("0x")
' | tr '\n' ' ')
if [ -n "$WS" ]; then
hyprctl dispatch workspace "$WS"
fi
if [ $# -eq 0 ]; then
"$APP" &
else
"$APP" "$@" &
fi
socat -u UNIX-CONNECT:"$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock" - | while read -r line; do
case "$line" in
openwindow*)
window_id=$(echo "$line" | cut -d'>' -f3 | cut -d',' -f1)
event_class=$(echo "$line" | cut -d'>' -f3 | cut -d',' -f3)
class_lower=$(echo "$event_class" | tr '[:upper:]' '[:lower:]')
target_lower=$(echo "$CLASS" | tr '[:upper:]' '[:lower:]')
case "$class_lower" in
*"$target_lower"*)
case " $EXISTING_HEX " in
*" $window_id "*) ;;
*)
addr="0x$window_id"
if [ -n "$WS" ]; then
hyprctl dispatch movetoworkspacesilent "$WS,address:$addr"
fi
hyprctl dispatch focuswindow "address:$addr"
break
;;
esac
;;
esac
;;
esac
done
fi
;;
*)
echo "Unknown subcommand: $cmd"
usage
exit 1
;;
esac