#!/usr/bin/env bash set -euo pipefail # Estimate how many "columns" fit on the focused monitor: # max_visible_cols ≈ floor(1 / column_width) # Then compare with number of windows in the active workspace on that monitor. # # Notes: # - This is an approximation (hyprscrolling can have edge cases), but works well in practice. # - If you use different column_width per monitor, you can extend this by detecting monitor + mapping widths. COLUMN_WIDTH="${COLUMN_WIDTH:-0.5}" # match your hyprscrolling config ICON="${ICON:-󰓒}" # pick any nerd-font icon you like active_ws="$(hyprctl -j activeworkspace | jq -r '.id')" focused_mon="$(hyprctl -j monitors | jq -r '.[] | select(.focused==true) | .name')" # Count windows on the active workspace (and on the focused monitor). # (Many people keep workspaces per-monitor; this keeps the widget “per your current screen”.) win_count="$(hyprctl -j clients \ | jq --argjson ws "$active_ws" --arg mon "$focused_mon" ' [ .[] | select(.workspace.id == $ws) | select(.monitor == $mon) | select(.mapped == true) ] | length ')" # floor(1 / COLUMN_WIDTH) using awk (portable) max_visible="$(awk -v w="$COLUMN_WIDTH" 'BEGIN{ if (w<=0) {print 1} else {print int(1.0/w)} }')" if [ "$max_visible" -lt 1 ]; then max_visible=1; fi overflow=$(( win_count - max_visible )) if [ "$overflow" -gt 0 ]; then # Waybar expects JSON for custom modules printf '{"text":"%s +%d","tooltip":"%d windows on this workspace, approx %d fit on-screen (column_width=%s)","class":"overflow"}\n' \ "$ICON" "$overflow" "$win_count" "$max_visible" "$COLUMN_WIDTH" else # empty output hides it if you use "return-type: json" + "format": "{}" printf '{"text":"","tooltip":"%d windows, approx %d fit on-screen","class":"ok"}\n' \ "$win_count" "$max_visible" fi