Files
dotfiles/roles/hyprland/files/waybar/scripts/page.sh
2025-09-07 20:17:11 +02:00

151 lines
4.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# page.sh 110 Waybar “page” indicator
# deps: socat, awk, sed, grep
page=$1
icons=( "" "" "" "" "" "" "" "" "" "" "󰝚" )
to_page() {
(( $1 <= 3 )) && echo 1 && return
(( $1 <= 6 )) && echo 2 && return
(( $1 <= 9 )) && echo 3 && return
(( $1 <= 12 )) && echo 4 && return
(( $1 <= 15 )) && echo 5 && return
(( $1 <= 18 )) && echo 6 && return
(( $1 <= 21 )) && echo 7 && return
(( $1 <= 24 )) && echo 8 && return
(( $1 <= 27 )) && echo 9 && return
echo 10
}
# Return 0 if THIS page has at least one window, else 1
is_occupied() {
# Get workspace id + windows count pairs from JSON
# Example snippets: {"id":1,"windows":3, ...}
while read -r id windows; do
(( $(to_page "$id") == page )) && (( windows > 0 )) && return 0
done < <(
hyprctl -j workspaces \
| grep -o '"id":[0-9]\+|"windows":[0-9]\+' \
| sed -n 'N;s/"id":\([0-9]\+\)\n"windows":\([0-9]\+\)/\1 \2/p'
)
return 1
}
emit() {
local classes=()
if (( $1 == page )); then
classes+=(active)
else
is_occupied && classes+=(occupied)
fi
if ((${#classes[@]})); then
printf '{"text":"%s","class":"%s"}\n' \
"${icons[page]}" "$(IFS=' '; echo "${classes[*]}")"
else
printf '{"text":"%s"}\n' "${icons[page]}"
fi
}
# Initial state
active_ws=$(hyprctl activeworkspace | awk '/^ID /{print $2}')
current_page=$(to_page "${active_ws:-1}")
emit "$current_page"
current_occ=$(is_occupied && echo 1 || echo 0)
# Live updates
socket="$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock"
socat -U - UNIX-CONNECT:"$socket" | while read -r line; do
[[ $line =~ ^(workspace|openwindow|closewindow|movewindow) ]] || continue
if [[ $line == workspace* ]]; then
ws=${line#*>>}; ws=${ws%% *}
new_page=$(to_page "$ws")
else
new_page=$current_page
fi
new_occ=$(is_occupied && echo 1 || echo 0)
if (( new_page != current_page || new_occ != current_occ )); then
emit "$new_page"
current_page=$new_page
current_occ=$new_occ
fi
done
# # page.sh 110 Waybar “page” indicator
# # deps: socat, awk • (still no jq)
#
# page=$1 # this script instances page (110)
#
# # ── icon lookup table ────────────────────────────────────────────
# icons=( "" "" "" "" "" "" "" "" "" "" "󰝚" )
#
# # map numeric workspace → page (110)
# to_page() {
# (( $1 <= 3 )) && echo 1 && return
# (( $1 <= 6 )) && echo 2 && return
# (( $1 <= 9 )) && echo 3 && return
# (( $1 <= 12 )) && echo 4 && return
# (( $1 <= 15 )) && echo 5 && return
# (( $1 <= 18 )) && echo 6 && return
# (( $1 <= 21 )) && echo 7 && return
# (( $1 <= 24 )) && echo 8 && return
# (( $1 <= 27 )) && echo 9 && return
# echo 10
# }
#
# # does THIS page contain at least one window?
# is_occupied() {
# while read -r ws_id; do
# (( $(to_page "$ws_id") == page )) && return 0
# done < <(hyprctl clients | awk '/workspace:/ {print $2}')
# return 1
# }
#
# emit() {
# local classes=()
#
# if (( $1 == page )); then # ← is THIS instance the active page?
# classes+=(active) # yes → mark it only “active”
# else
# is_occupied && classes+=(occupied) # no → maybe “occupied”
# fi
#
# if ((${#classes[@]})); then
# printf '{"text":"%s","class":"%s"}\n' \
# "${icons[page]}" "$(IFS=' '; echo "${classes[*]}")"
# else
# printf '{"text":"%s"}\n' "${icons[page]}"
# fi
# }
#
# # ── initial state ────────────────────────────────────────────────
# active_ws=$(hyprctl activeworkspace | awk '/^ID /{print $2}')
# current_page=$(to_page "${active_ws:-1}")
# emit "$current_page"
# current_occ=$(is_occupied && echo 1 || echo 0)
#
# # ── live updates via Hyprland socket2 ───────────────────────────
# socket="$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock"
# socat -U - UNIX-CONNECT:"$socket" | while read -r line; do
# [[ $line =~ ^(workspace|openwindow|closewindow) ]] || continue
#
# # recompute state if anything relevant changed
# if [[ $line == workspace* ]]; then
# ws=${line#*>>}; ws=${ws%% *}
# new_page=$(to_page "$ws")
# else
# new_page=$current_page
# fi
# new_occ=$(is_occupied && echo 1 || echo 0)
#
# # update Waybar only when something actually changed
# if (( new_page != current_page || new_occ != current_occ )); then
# emit "$new_page"
# current_page=$new_page
# current_occ=$new_occ
# fi
# done
#