Improve tmux skill helpers
5f9866cc713d7c556f8fa5afb61c0e7ff992599a
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
- Parent 1: 5e4e566c2cfb
bugabingas dorkfiles
5f9866cc713d7c556f8fa5afb61c0e7ff992599a
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
+179 / −0 text lines
+20 / −0 text lines
## Skill ScriptsSet `TMUX_SKILL_DIR` to this skill directory when using bundled helpers.```bashTMUX_SKILL_DIR="/home/me/.pi/agent/skills/tmux""$TMUX_SKILL_DIR/scripts/find-sessions.sh" -L "$SOCKET""$TMUX_SKILL_DIR/scripts/wait-for-text.sh" -L "$SOCKET" -t "$pane" -p '^>>>' -T 15```- `find-sessions.sh` lists sessions and pane IDs; prefer `%pane_id` from its output.- `wait-for-text.sh` polls captured pane text with regex/fixed-string matching; use it instead of blind sleeps.new file mode 100755+85 / −0 text lines
#!/usr/bin/env bashset -euo pipefailusage() { cat <<'USAGE'Usage: find-sessions.sh [-L socket-name|-S socket-path|--all] [-q pattern]List tmux sessions with panes.Options: -L, --socket tmux socket name -S, --socket-path tmux socket path -A, --all scan /tmp/tmux-$UID and PI_TMUX_SOCKET_DIR -q, --query case-insensitive session-name filter -h, --help show helpUSAGE}socket_name=""socket_path=""query=""scan_all=falsesocket_dir="${PI_TMUX_SOCKET_DIR:-${TMPDIR:-/tmp}/pi-tmux-sockets}"while [[ $# -gt 0 ]]; do case "$1" in -L|--socket) socket_name="${2-}"; shift 2 ;; -S|--socket-path) socket_path="${2-}"; shift 2 ;; -A|--all) scan_all=true; shift ;; -q|--query) query="${2-}"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "Unknown option: $1" >&2; usage; exit 1 ;; esacdone[[ -z "$socket_name" || -z "$socket_path" ]] || { echo "use -L or -S, not both" >&2; exit 1; }[[ "$scan_all" == false || ( -z "$socket_name" && -z "$socket_path" ) ]] || { echo "--all cannot combine with -L/-S" >&2; exit 1; }command -v tmux >/dev/null || { echo "tmux not found" >&2; exit 1; }list_one() { local label="$1"; shift local tmux_cmd=(tmux "$@") local sessions panes sessions="$("${tmux_cmd[@]}" list-sessions -F '#{session_name}|#{session_attached}|#{session_created_string}' 2>/dev/null || true)" [[ -n "$sessions" ]] || return 1 [[ -z "$query" ]] || sessions="$(printf '%s\n' "$sessions" | grep -i -- "$query" || true)" [[ -n "$sessions" ]] || return 0 echo "Sessions on $label:" while IFS='|' read -r name attached created; do [[ -n "$name" ]] || continue [[ "$attached" == 1 ]] && attached_label=attached || attached_label=detached printf ' %s (%s, %s)\n' "$name" "$attached_label" "$created" panes="$("${tmux_cmd[@]}" list-panes -t "$name" -F ' #{pane_id} idx=#{pane_index} title=#{pane_title} cmd=#{pane_current_command}' 2>/dev/null || true)" printf '%s\n' "$panes" done <<< "$sessions"}if [[ "$scan_all" == true ]]; then paths=() [[ -d "$socket_dir" ]] && while IFS= read -r -d '' path; do paths+=("$path"); done < <(find "$socket_dir" -maxdepth 1 -type s -print0 2>/dev/null) default_dir="/tmp/tmux-$(id -u)" [[ -d "$default_dir" ]] && while IFS= read -r -d '' path; do paths+=("$path"); done < <(find "$default_dir" -maxdepth 1 -type s -print0 2>/dev/null) [[ ${#paths[@]} -gt 0 ]] || { echo "No tmux sockets found" >&2; exit 1; } found=false for path in "${paths[@]}"; do if list_one "socket path '$path'" -S "$path"; then found=true; fi done [[ "$found" == true ]] || { echo "No sessions found" >&2; exit 1; } exit 0fitmux_args=()label="default socket"if [[ -n "$socket_name" ]]; then tmux_args=(-L "$socket_name") label="socket name '$socket_name'"elif [[ -n "$socket_path" ]]; then tmux_args=(-S "$socket_path") label="socket path '$socket_path'"filist_one "$label" "${tmux_args[@]}" || { echo "No sessions found on $label" >&2; exit 1; }new file mode 100755+74 / −0 text lines
#!/usr/bin/env bashset -euo pipefailusage() { cat <<'USAGE'Usage: wait-for-text.sh [-L socket-name|-S socket-path] -t target -p pattern [options]Poll a tmux pane until text appears.Options: -L, --socket tmux socket name -S, --socket-path tmux socket path -t, --target pane target; prefer %pane_id -p, --pattern regex pattern; required -F, --fixed fixed string match -T, --timeout seconds; default 15 -i, --interval poll interval seconds; default 0.5 -l, --lines history lines; default 1000 -h, --help show helpUSAGE}socket_name=""socket_path=""target=""pattern=""grep_flag="-E"timeout=15interval=0.5lines=1000while [[ $# -gt 0 ]]; do case "$1" in -L|--socket) socket_name="${2-}"; shift 2 ;; -S|--socket-path) socket_path="${2-}"; shift 2 ;; -t|--target) target="${2-}"; shift 2 ;; -p|--pattern) pattern="${2-}"; shift 2 ;; -F|--fixed) grep_flag="-F"; shift ;; -T|--timeout) timeout="${2-}"; shift 2 ;; -i|--interval) interval="${2-}"; shift 2 ;; -l|--lines) lines="${2-}"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "Unknown option: $1" >&2; usage; exit 1 ;; esacdone[[ -n "$target" && -n "$pattern" ]] || { echo "target and pattern required" >&2; usage; exit 1; }[[ "$timeout" =~ ^[0-9]+$ ]] || { echo "timeout must be integer seconds" >&2; exit 1; }[[ "$lines" =~ ^[0-9]+$ ]] || { echo "lines must be integer" >&2; exit 1; }[[ -z "$socket_name" || -z "$socket_path" ]] || { echo "use -L or -S, not both" >&2; exit 1; }command -v tmux >/dev/null || { echo "tmux not found" >&2; exit 1; }tmux_cmd=(tmux)[[ -n "$socket_name" ]] && tmux_cmd+=(-L "$socket_name")[[ -n "$socket_path" ]] && tmux_cmd+=(-S "$socket_path")deadline=$(($(date +%s) + timeout))pane_text=""while :; do pane_text="$("${tmux_cmd[@]}" capture-pane -p -J -t "$target" -S "-${lines}" 2>/dev/null || true)" if printf '%s\n' "$pane_text" | grep "$grep_flag" -- "$pattern" >/dev/null 2>&1; then exit 0 fi if (( $(date +%s) >= deadline )); then echo "Timed out after ${timeout}s waiting for pattern: $pattern" >&2 echo "Last ${lines} lines from $target:" >&2 printf '%s\n' "$pane_text" >&2 exit 1 fi sleep "$interval"done