#!/bin/bash
# Stamp every window with its circled-number icon as a WINDOW-level user option.
#
# Why not do this in the format string: the obvious approach nests ten
# #{?#{==:#{window_index},N},...} conditionals, which tmux re-evaluates for
# every window on every redraw -- and redraws are driven by PANE ACTIVITY, so a
# busy pane re-runs all ten per window, several times a second.
#
# Here the work happens ONCE per window change (driven by hooks) and the render
# path becomes #{@icon}: a plain variable lookup, no conditionals, no forks.
#
# Do NOT call this from the status format itself -- that would make it a #()
# job, which renders EMPTY while restarting and is the original bug.
symbols=(❶ ❷ ❸ ❹ ❺ ❻ ❼ ❽ ❾ ❿)
while IFS= read -r w; do
    idx=${w##*:}
    [[ $idx =~ ^[0-9]+$ ]] || continue
    if (( idx >= 1 && idx <= ${#symbols[@]} )); then
        icon=${symbols[idx-1]}
    else
        icon=$idx
    fi
    tmux set -w -t "$w" @icon "$icon" 2>/dev/null
done < <(tmux list-windows -a -F '#{session_name}:#{window_index}' 2>/dev/null)
