Skip to content

Instantly share code, notes, and snippets.

@happytomatoe
Last active August 19, 2026 07:27
Show Gist options
  • Select an option

  • Save happytomatoe/9b7bb9c0522b9181abb6d1dcbbe93a80 to your computer and use it in GitHub Desktop.

Select an option

Save happytomatoe/9b7bb9c0522b9181abb6d1dcbbe93a80 to your computer and use it in GitHub Desktop.
Wait for terminal output to settle (herdr pane idle detection)
#!/usr/bin/env bash
# Wait for herdr pane output to settle (no changes for N ms)
# Usage: wait-idle <pane-id> [idle-ms] [poll-ms]
# pane-id: herdr pane ID (e.g., w6B:p2)
# idle-ms: exit after this many ms of no changes (default: 200)
# poll-ms: how often to check (default: same as idle-ms)
set -euo pipefail
PANE_ID="${1:?Usage: wait-idle <pane-id> [idle-ms] [poll-ms]}"
IDLE_MS="${2:-200}"
POLL_MS="${3:-$IDLE_MS}"
# Convert ms to seconds for sleep
IDLE_SEC=$(echo "scale=3; $IDLE_MS / 1000" | bc)
POLL_SEC=$(echo "scale=3; $POLL_MS / 1000" | bc)
LAST_OUTPUT=""
UNCHANGED_SINCE=""
while true; do
# Read current pane output
CURRENT=$(herdr pane read "$PANE_ID" --source recent-unwrapped --lines 50 2>/dev/null || echo "")
NOW=$(date +%s%3N 2>/dev/null || date +%s)
if [[ "$CURRENT" == "$LAST_OUTPUT" ]]; then
# No change - check if we've been idle long enough
if [[ -n "$UNCHANGED_SINCE" ]]; then
ELAPSED=$((NOW - UNCHANGED_SINCE))
if [[ $ELAPSED -ge $IDLE_MS ]]; then
echo "idle"
exit 0
fi
fi
else
# Output changed - reset timer
LAST_OUTPUT="$CURRENT"
UNCHANGED_SINCE="$NOW"
fi
sleep "$POLL_SEC"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment