#!/usr/bin/env bash
# tmux/.config/tmux/plugins/pr-status.sh
# Shows open-PR count for the repo in the current pane's cwd.
# Cached for 60s per repo in $TMPDIR to keep the status bar snappy.
set -euo pipefail
pane_path="${TMUX_PANE_PATH:-${PWD}}"
cd "$pane_path" 2>/dev/null || exit 0
if ! git rev-parse --git-dir >/dev/null 2>&1; then
exit 0
fi
if ! command -v gh >/dev/null 2>&1; then
exit 0
fi
remote_url=$(git config --get remote.origin.url 2>/dev/null || true)
[[ -z "$remote_url" ]] && exit 0
cache_dir="${TMPDIR:-/tmp}/tmux-pr-status"
mkdir -p "$cache_dir"
# Hash the remote URL for the cache key.
key=$(printf '%s' "$remote_url" | shasum | cut -c1-12)
cache="$cache_dir/$key"
now=$(date +%s)
if [[ -f "$cache" ]]; then
mtime=$(stat -f %m "$cache" 2>/dev/null || stat -c %Y "$cache")
if (( now - mtime < 60 )); then
cat "$cache"
exit 0
fi
fi
count=$(gh pr list --state open --limit 50 --json number -q 'length' 2>/dev/null || printf '0')
out=""
if [[ "$count" -gt 0 ]]; then
out="PR:${count} "
fi
printf '%s' "$out" | tee "$cache"