#!/usr/bin/env bash set -euo pipefail COLUMN_WIDTH="${COLUMN_WIDTH:-0.5}" ICON="${ICON:-󰓒}" fail_json() { printf '{"text":"%s !","tooltip":"hyprscroll-overflow: %s","class":"error"}\n' \ "$ICON" "${1//\"/\\\"}" exit 0 } command -v hyprctl >/dev/null 2>&1 || fail_json "hyprctl not in PATH" command -v jq >/dev/null 2>&1 || fail_json "jq not in PATH" read -r focused_mon_id focused_ws_id < <( hyprctl -j monitors 2>/dev/null | jq -r ' .[] | select(.focused==true) | "\(.id) \(.activeWorkspace.id)" ' ) || fail_json "failed to read monitors" [[ -n "${focused_mon_id:-}" && -n "${focused_ws_id:-}" ]] || fail_json "no focused monitor?" win_count="$( hyprctl -j clients 2>/dev/null | jq --argjson ws "$focused_ws_id" --argjson mid "$focused_mon_id" ' [ .[] | select(.mapped == true) | select(.workspace.id == $ws) | select(.monitor == $mid) ] | length ' )" || fail_json "failed to count clients" max_visible="$(awk -v w="$COLUMN_WIDTH" 'BEGIN{ if (w<=0) {print 1} else {print int(1.0/w)} }')" \ || fail_json "awk failed" (( max_visible < 1 )) && max_visible=1 overflow=$(( win_count - max_visible )) layout="$(hyprctl getoption general:layout 2>/dev/null | awk '/str:/ {print $2; exit}')" [[ -n "${layout:-}" ]] || layout="" if (( overflow > 0 )) && [[ "$layout" == "scrolling" ]]; then printf '{"text":"%s +%d","tooltip":"%d windows; approx %d fit (column_width=%s)","class":"overflow"}\n' \ "$ICON" "$overflow" "$win_count" "$max_visible" "$COLUMN_WIDTH" else # truly empty (and tooltip empty) + class for CSS printf '{"text":"","tooltip":"","class":"hidden"}\n' fi