Files
tmux/agent-cmd.sh

71 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Detect available agents, prompt with fzf, run the selected one.
# Detaches the client if all panes in the window are dead afterwards.
agents=()
command -v claude &>/dev/null && agents+=(claude)
command -v opencode &>/dev/null && agents+=(opencode)
command -v agy &>/dev/null && agents+=(agy)
command -v codex &>/dev/null && agents+=(codex)
if [ ${#agents[@]} -eq 0 ]; then
echo "No agent commands found (claude, opencode, gemini)"
exit 1
fi
# This runs as the window's default-command, which tmux starts *before* the
# popup client attaches. Until then the pane keeps a stale default size (often
# 80x24), so wait for the attach before measuring — otherwise fzf is sized
# against the wrong dimensions and renders far too small.
for _ in $(seq 1 100); do
[ "$(tmux display-message -p '#{session_attached}')" -gt 0 ] && break
sleep 0.02
done
# Minimize fzf prompt size while centering it, using the real pane dimensions.
width=24
height=$((${#agents[@]} + 6))
cols=$(tmux display-message -p '#{pane_width}')
lines=$(tmux display-message -p '#{pane_height}')
hmargin=$(((cols - width) / 2))
vpad=$(((lines - height) / 2))
[ $hmargin -lt 0 ] && hmargin=0
[ $vpad -lt 0 ] && vpad=0
tput cup "$vpad" 0
agent=$(printf '%s\n' "${agents[@]}" | fzf \
--prompt='agent> ' \
--reverse \
--border=rounded \
--height="$height" \
--margin="0,$hmargin" \
--padding=1)
# Reset cursor so the agent doesn't render starting where fzf left it.
tput cup 0 0
tput ed
if [ -n "$agent" ]; then
"$agent" || true
fi
# Brief delay to let tmux update pane status
sleep 0.1
window_id=$(tmux display-message -p '#{window_id}')
my_pane=$(tmux display-message -p '#{pane_id}')
# Count live sibling panes (not us, not dead)
other_live=$(tmux list-panes -t "$window_id" -F '#{pane_id} #{pane_dead}' \
| awk -v me="$my_pane" '$1 != me && $2 == "0"' | wc -l | tr -d ' ')
if [ "$other_live" -eq 0 ]; then
# No live siblings — close the popup and kill the window
# (also cleans up any dead siblings).
tmux detach-client
tmux kill-window -t "$window_id" 2>/dev/null || true
else
# User has split off other live panes — only kill ours, keep popup open.
tmux kill-pane 2>/dev/null || true
fi