repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

Improve tmux skill helpers

5f9866cc713d7c556f8fa5afb61c0e7ff992599a

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (3)

+179 / −0 text lines

Diff layout

pi/agent/skills/tmux/SKILL.md

Modified

+20 / −0 text lines

8686 tmux -L "$SOCKET" paste-buffer -t "$pane"
8787 tmux -L "$SOCKET" send-keys -t "$pane" Enter
90wait_for_text() {
91 local pane="$1" pattern="$2" timeout="${3:-15}"
92 "$TMUX_SKILL_DIR/scripts/wait-for-text.sh" -L "$SOCKET" -t "$pane" -p "$pattern" -T "$timeout"
93}
8994```
96## Skill Scripts
98Set `TMUX_SKILL_DIR` to this skill directory when using bundled helpers.
100```bash
101TMUX_SKILL_DIR="/home/me/.pi/agent/skills/tmux"
102"$TMUX_SKILL_DIR/scripts/find-sessions.sh" -L "$SOCKET"
103"$TMUX_SKILL_DIR/scripts/wait-for-text.sh" -L "$SOCKET" -t "$pane" -p '^>>>' -T 15
104```
106- `find-sessions.sh` lists sessions and pane IDs; prefer `%pane_id` from its output.
107- `wait-for-text.sh` polls captured pane text with regex/fixed-string matching; use it instead of blind sleeps.
91109## Shell-Aware Launch
93111Probe the user's shell/terminal before pane work:
101119For pane launches, keep commands simple. Nushell/Fish/PowerShell use `;`, not Bash `&&`.
121For Python REPLs, launch with `PYTHON_BASIC_REPL=1 python3 -q`. Python 3.13+ defaults to PyREPL, which emits terminal-control behavior that confuses `send-keys`/`capture-pane`; `PYTHON_BASIC_REPL=1` forces the old plain prompt.
103123```bash
104124launch_pi() {
105125 local pane="$1" root="$2" cmd="$3" shell

pi/agent/skills/tmux/scripts/find-sessions.sh

Added
new file mode 100755

+85 / −0 text lines

1#!/usr/bin/env bash
2set -euo pipefail
3
4usage() {
5 cat <<'USAGE'
6Usage: find-sessions.sh [-L socket-name|-S socket-path|--all] [-q pattern]
7
8List tmux sessions with panes.
9
10Options:
11 -L, --socket tmux socket name
12 -S, --socket-path tmux socket path
13 -A, --all scan /tmp/tmux-$UID and PI_TMUX_SOCKET_DIR
14 -q, --query case-insensitive session-name filter
15 -h, --help show help
16USAGE
17}
19socket_name=""
20socket_path=""
21query=""
22scan_all=false
23socket_dir="${PI_TMUX_SOCKET_DIR:-${TMPDIR:-/tmp}/pi-tmux-sockets}"
25while [[ $# -gt 0 ]]; do
26 case "$1" in
27 -L|--socket) socket_name="${2-}"; shift 2 ;;
28 -S|--socket-path) socket_path="${2-}"; shift 2 ;;
29 -A|--all) scan_all=true; shift ;;
30 -q|--query) query="${2-}"; shift 2 ;;
31 -h|--help) usage; exit 0 ;;
32 *) echo "Unknown option: $1" >&2; usage; exit 1 ;;
33 esac
34done
36[[ -z "$socket_name" || -z "$socket_path" ]] || { echo "use -L or -S, not both" >&2; exit 1; }
37[[ "$scan_all" == false || ( -z "$socket_name" && -z "$socket_path" ) ]] || { echo "--all cannot combine with -L/-S" >&2; exit 1; }
38command -v tmux >/dev/null || { echo "tmux not found" >&2; exit 1; }
40list_one() {
41 local label="$1"; shift
42 local tmux_cmd=(tmux "$@")
43 local sessions panes
45 sessions="$("${tmux_cmd[@]}" list-sessions -F '#{session_name}|#{session_attached}|#{session_created_string}' 2>/dev/null || true)"
46 [[ -n "$sessions" ]] || return 1
47 [[ -z "$query" ]] || sessions="$(printf '%s\n' "$sessions" | grep -i -- "$query" || true)"
48 [[ -n "$sessions" ]] || return 0
50 echo "Sessions on $label:"
51 while IFS='|' read -r name attached created; do
52 [[ -n "$name" ]] || continue
53 [[ "$attached" == 1 ]] && attached_label=attached || attached_label=detached
54 printf ' %s (%s, %s)\n' "$name" "$attached_label" "$created"
55 panes="$("${tmux_cmd[@]}" list-panes -t "$name" -F ' #{pane_id} idx=#{pane_index} title=#{pane_title} cmd=#{pane_current_command}' 2>/dev/null || true)"
56 printf '%s\n' "$panes"
57 done <<< "$sessions"
58}
60if [[ "$scan_all" == true ]]; then
61 paths=()
62 [[ -d "$socket_dir" ]] && while IFS= read -r -d '' path; do paths+=("$path"); done < <(find "$socket_dir" -maxdepth 1 -type s -print0 2>/dev/null)
63 default_dir="/tmp/tmux-$(id -u)"
64 [[ -d "$default_dir" ]] && while IFS= read -r -d '' path; do paths+=("$path"); done < <(find "$default_dir" -maxdepth 1 -type s -print0 2>/dev/null)
65 [[ ${#paths[@]} -gt 0 ]] || { echo "No tmux sockets found" >&2; exit 1; }
67 found=false
68 for path in "${paths[@]}"; do
69 if list_one "socket path '$path'" -S "$path"; then found=true; fi
70 done
71 [[ "$found" == true ]] || { echo "No sessions found" >&2; exit 1; }
72 exit 0
73fi
75tmux_args=()
76label="default socket"
77if [[ -n "$socket_name" ]]; then
78 tmux_args=(-L "$socket_name")
79 label="socket name '$socket_name'"
80elif [[ -n "$socket_path" ]]; then
81 tmux_args=(-S "$socket_path")
82 label="socket path '$socket_path'"
83fi
85list_one "$label" "${tmux_args[@]}" || { echo "No sessions found on $label" >&2; exit 1; }

pi/agent/skills/tmux/scripts/wait-for-text.sh

Added
new file mode 100755

+74 / −0 text lines

1#!/usr/bin/env bash
2set -euo pipefail
3
4usage() {
5 cat <<'USAGE'
6Usage: wait-for-text.sh [-L socket-name|-S socket-path] -t target -p pattern [options]
7
8Poll a tmux pane until text appears.
9
10Options:
11 -L, --socket tmux socket name
12 -S, --socket-path tmux socket path
13 -t, --target pane target; prefer %pane_id
14 -p, --pattern regex pattern; required
15 -F, --fixed fixed string match
16 -T, --timeout seconds; default 15
17 -i, --interval poll interval seconds; default 0.5
18 -l, --lines history lines; default 1000
19 -h, --help show help
20USAGE
21}
23socket_name=""
24socket_path=""
25target=""
26pattern=""
27grep_flag="-E"
28timeout=15
29interval=0.5
30lines=1000
32while [[ $# -gt 0 ]]; do
33 case "$1" in
34 -L|--socket) socket_name="${2-}"; shift 2 ;;
35 -S|--socket-path) socket_path="${2-}"; shift 2 ;;
36 -t|--target) target="${2-}"; shift 2 ;;
37 -p|--pattern) pattern="${2-}"; shift 2 ;;
38 -F|--fixed) grep_flag="-F"; shift ;;
39 -T|--timeout) timeout="${2-}"; shift 2 ;;
40 -i|--interval) interval="${2-}"; shift 2 ;;
41 -l|--lines) lines="${2-}"; shift 2 ;;
42 -h|--help) usage; exit 0 ;;
43 *) echo "Unknown option: $1" >&2; usage; exit 1 ;;
44 esac
45done
47[[ -n "$target" && -n "$pattern" ]] || { echo "target and pattern required" >&2; usage; exit 1; }
48[[ "$timeout" =~ ^[0-9]+$ ]] || { echo "timeout must be integer seconds" >&2; exit 1; }
49[[ "$lines" =~ ^[0-9]+$ ]] || { echo "lines must be integer" >&2; exit 1; }
50[[ -z "$socket_name" || -z "$socket_path" ]] || { echo "use -L or -S, not both" >&2; exit 1; }
51command -v tmux >/dev/null || { echo "tmux not found" >&2; exit 1; }
53tmux_cmd=(tmux)
54[[ -n "$socket_name" ]] && tmux_cmd+=(-L "$socket_name")
55[[ -n "$socket_path" ]] && tmux_cmd+=(-S "$socket_path")
57deadline=$(($(date +%s) + timeout))
58pane_text=""
60while :; do
61 pane_text="$("${tmux_cmd[@]}" capture-pane -p -J -t "$target" -S "-${lines}" 2>/dev/null || true)"
62 if printf '%s\n' "$pane_text" | grep "$grep_flag" -- "$pattern" >/dev/null 2>&1; then
63 exit 0
64 fi
66 if (( $(date +%s) >= deadline )); then
67 echo "Timed out after ${timeout}s waiting for pattern: $pattern" >&2
68 echo "Last ${lines} lines from $target:" >&2
69 printf '%s\n' "$pane_text" >&2
70 exit 1
71 fi
73 sleep "$interval"
74done