mirror of
https://gitea.zaclys.com/yannic/dotfiles.git
synced 2026-07-28 17:22:49 +02:00
35 lines
1.5 KiB
Bash
35 lines
1.5 KiB
Bash
#!/usr/bin/env bash
|
|
# Count tmux-agents-mon agents in a given state, for the powerkit segments.
|
|
#
|
|
# Usage: agents-mon-count <blocked|working|idle>
|
|
# Prints the number of agents currently in that state (0 if none).
|
|
#
|
|
# Uses `agents-mon scan` (always live TSV, one line per agent, state in field 4)
|
|
# rather than `agents-mon status` — the latter serves a cache up to 6s stale.
|
|
# The three powerkit plugins (agents_blocked/working/idle) each call this on every
|
|
# status refresh, so we cache the parsed counts for ~2s in a shared tmp file to
|
|
# avoid running three full `tmux list-panes -a` + `capture-pane` scans per cycle.
|
|
set -u
|
|
|
|
state="${1:-}"
|
|
case "$state" in
|
|
blocked | working | idle) ;;
|
|
*) printf '0'; exit 0 ;;
|
|
esac
|
|
|
|
BIN="${AGENTS_MON_BIN:-$HOME/.tmux/plugins/tmux-agents-mon/target/release/agents-mon}"
|
|
[ -x "$BIN" ] && command -v tmux >/dev/null 2>&1 || { printf '0'; exit 0; }
|
|
|
|
cache="${TMPDIR:-/tmp}/agents-mon-counts-$(id -u)"
|
|
|
|
# Refresh the cache when it is missing or older than ~2 seconds.
|
|
if [ -z "$(find "$cache" -newermt '-2 seconds' 2>/dev/null)" ]; then
|
|
"$BIN" scan 2>/dev/null | awk -F'\t' '
|
|
{ c[$4]++ }
|
|
END { printf "blocked=%d\nworking=%d\nidle=%d\n", c["blocked"], c["working"], c["idle"] }
|
|
' >"$cache.tmp.$$" 2>/dev/null && mv -f "$cache.tmp.$$" "$cache" 2>/dev/null
|
|
fi
|
|
|
|
# Emit the requested count (0 if the cache is unreadable for any reason).
|
|
awk -F= -v s="$state" '$1==s { print $2; found=1 } END { if (!found) print 0 }' "$cache" 2>/dev/null || printf '0'
|