Dotfiles/wayland/.local/bin/hypr-workspaces.sh

57 lines
1.7 KiB
Bash
Executable File

# --- SAFETY BLOCK: Auto-detect Hyprland Signature ---
# If the variable is missing (common in scripts), find the most recent Hyprland instance.
if [ -z "$HYPRLAND_INSTANCE_SIGNATURE" ]; then
# Look into /run/user/1000/hypr/ and grab the newest directory name
HYPRLAND_INSTANCE_SIGNATURE="$(ls -1t "$XDG_RUNTIME_DIR/hypr" | head -n 1)"
fi
# Define the socket path manually to be sure
SOCKET="$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock"
# 1. Determine Monitor Name
MONITOR="$1"
if [ -z "$MONITOR" ]; then
MONITOR=$(hyprctl monitors -j | jq -r '.[] | select(.focused == true) | .name')
fi
# Function to draw the bar
generate_workspaces() {
# Get Active ID for the SPECIFIC monitor
ACTIVE_ID=$(hyprctl monitors -j | jq -r ".[] | select(.name == \"${MONITOR}\") | .activeWorkspace.id")
# Fallback if jq returns null
if [ -z "$ACTIVE_ID" ] || [ "$ACTIVE_ID" = "null" ]; then
ACTIVE_ID=$(hyprctl activeworkspace -j | jq -r '.id')
fi
for i in $(seq 1 9); do
if [ "$i" -eq "$ACTIVE_ID" ] 2>/dev/null; then
echo "ws${i}_state|string|focused"
else
echo "ws${i}_state|string|normal"
fi
done
echo ""
}
# --- MAIN EXECUTION ---
# 1. Run once immediately to populate the bar
generate_workspaces
# 2. Check if socket exists before running socat
if [ ! -S "$SOCKET" ]; then
# Print error to bar so you see it
echo "ws1_state|string|NoSock"
exit 1
fi
# 3. Listen loop (Quote the event strings to fix the syntax error)
socat -U - "UNIX-CONNECT:$SOCKET" | while read -r line; do
case "$line" in
"workspace>>"*|"focusedmon>>"*|"createworkspace>>"*|"destroyworkspace>>"*|"movewindow>>"*)
generate_workspaces
;;
esac
done