#!/bin/bash
# Show what a Claude session is doing: last output lines, state, real vs. suggested input, context fill.
# Usage: cc-peek %<pane> [lines]
set -euo pipefail
pane=${1:?pane id}; n=${2:-8}
name=$(tmux display -p -t "$pane" '#{session_name} #{window_name}')
screen=$(tmux capture-pane -p -t "$pane")
echo "== $pane $name"
grep -v '^\s*$' <<<"$screen" | grep -vE '^\s*(⬆|⏵⏵|─|❯)' | tail -n "$n" | cut -c1-220
done=$(grep -oE 'done [0-9]{1,2}:[0-9]{2}' <<<"$screen" | tail -1 || true)
busy=$(grep -E '^\s*[·✻✽✶✢*] .*…' <<<"$screen" | tail -1 || true)
ask=$(grep -qE 'Enter to select' <<<"$screen" && echo "SELECTION DIALOG open" || true)
err=$(grep -qE 'API Error' <<<"$screen" && echo "API Error on screen" || true)
ctx=$(grep -oE '[0-9]+%' <<<"$screen" | tail -1 || true)
# Input line: dim text (ESC[2m) is Claude's suggestion, not something a human typed
input=$(tmux capture-pane -e -p -t "$pane" | grep '❯' | tail -1 || true)
if grep -q $'\e\[2m' <<<"$input"; then inp="suggestion only (grey), nobody typed this"
elif [[ -n $(sed 's/\x1b\[[0-9;]*m//g; s/❯//; s/[[:space:]]//g' <<<"$input") ]]; then inp="REAL TEXT typed, not sent yet"
else inp="empty"; fi
echo "-- state: ${busy:+busy | }${done:-no done marker}${ask:+ | $ask}${err:+ | $err} | input: $inp | context ${ctx:-?}"
