forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmux-send.sh
More file actions
executable file
·335 lines (304 loc) · 11 KB
/
Copy pathtmux-send.sh
File metadata and controls
executable file
·335 lines (304 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#!/usr/bin/env bash
#######################################################################
# tmux-send.sh - Send input to a TUI app in a tmux session
#######################################################################
#
# DESCRIPTION:
# Sends text input to a tmux session running a TUI application.
# Uses BRACKETED PASTE MODE which is REQUIRED for TUI apps to receive
# input correctly. Standard tmux send-keys drops characters!
#
# IMPORTANT:
# This script handles the bracketed paste escape sequences automatically.
# You do NOT need to add escape sequences to your input.
#
# USAGE:
# ./scripts/tmux/tmux-send.sh SESSION_NAME "your text here"
# ./scripts/tmux/tmux-send.sh SESSION_NAME --key KEY
# ./scripts/tmux/tmux-send.sh SESSION_NAME --paste
#
# ARGUMENTS:
# SESSION_NAME Name of the tmux session
# TEXT Text to send (will be wrapped in bracketed paste)
#
# OPTIONS:
# --key KEY Send a special key instead of text
# Supported: Enter, Escape, Up, Down, Left, Right,
# C-c, C-u, C-d, C-v, Tab
# --paste Paste current clipboard content using bracketed paste
# mode. This triggers the app's paste handler correctly,
# unlike --key C-v which just sends the raw keystroke.
# By default, Enter is pressed after pasting. Use with
# --no-enter to paste without submitting (useful for
# testing attachment UI before sending).
# --no-enter Don't automatically press Enter after text
# --retry N Retry session detection N times (default: 3)
# --delay MS Wait time in ms after Enter (default: 500, use 200 for faster tests)
# --wait-idle SECS Wait until terminal output is stable for SECS seconds (for streaming)
# This polls every 250ms until output hasn't changed for SECS seconds.
# Useful for rapid message testing where you need to wait for streaming.
# Max wait time is 120 seconds to prevent infinite loops.
# --force Bypass duplicate detection (send even if same text was just sent)
# --help Show this help message
#
# EXAMPLES:
# # Send a command to the app
# ./scripts/tmux/tmux-send.sh tui-test-123 "/help"
#
# # Send text without pressing Enter
# ./scripts/tmux/tmux-send.sh tui-test-123 "partial text" --no-enter
#
# # Send a special key
# ./scripts/tmux/tmux-send.sh tui-test-123 --key Escape
#
# # Send Ctrl+C to interrupt
# ./scripts/tmux/tmux-send.sh tui-test-123 --key C-c
#
# # Send a message and wait for CLI to finish streaming before returning
# ./scripts/tmux/tmux-send.sh tui-test-123 "hello" --wait-idle 2
#
# # Paste clipboard content and submit immediately
# ./scripts/tmux/tmux-send.sh tui-test-123 --paste
#
# # Paste clipboard content but don't submit (view attachment card first)
# ./scripts/tmux/tmux-send.sh tui-test-123 --paste --no-enter
#
# WHY BRACKETED PASTE?
# Many TUI apps (like those using OpenTUI, Ink, or similar frameworks)
# process keyboard input character-by-character. When tmux sends
# characters rapidly, they get dropped or garbled. Bracketed paste mode
# (\e[200~...\e[201~) tells the terminal to treat the input as a paste
# operation, which is processed atomically.
#
# EXIT CODES:
# 0 - Success
# 1 - Error (missing arguments, session not found)
#
#######################################################################
set -e
# Get project root for logging
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
# Defaults
AUTO_ENTER=true
SPECIAL_KEY=""
PASTE_CLIPBOARD=false
RETRY_COUNT=3
RETRY_DELAY=0.3
POST_ENTER_DELAY=0.5
FORCE_SEND=false
WAIT_IDLE_SECONDS=0
WAIT_IDLE_MAX=120
WAIT_IDLE_POLL_INTERVAL=0.25
# Check minimum arguments
if [[ $# -lt 1 ]]; then
echo "Usage: $0 SESSION_NAME \"text\" [OPTIONS]" >&2
echo " $0 SESSION_NAME --key KEY" >&2
echo "Run with --help for more information" >&2
exit 1
fi
# First argument is always session name
SESSION_NAME="$1"
shift
# Handle --help first
if [[ "$SESSION_NAME" == "--help" ]]; then
head -n 55 "$0" | tail -n +2 | sed 's/^# //' | sed 's/^#//'
exit 0
fi
# Parse remaining arguments
TEXT=""
while [[ $# -gt 0 ]]; do
case $1 in
--key)
SPECIAL_KEY="$2"
shift 2
;;
--paste)
PASTE_CLIPBOARD=true
shift
;;
--no-enter)
AUTO_ENTER=false
shift
;;
--retry)
RETRY_COUNT="$2"
shift 2
;;
--delay)
# Convert ms to seconds for sleep command
POST_ENTER_DELAY=$(echo "scale=3; $2 / 1000" | bc)
shift 2
;;
--wait-idle)
WAIT_IDLE_SECONDS="$2"
shift 2
;;
--force)
FORCE_SEND=true
shift
;;
--help)
head -n 55 "$0" | tail -n +2 | sed 's/^# //' | sed 's/^#//'
exit 0
;;
*)
TEXT="$1"
shift
;;
esac
done
# Verify session exists with retry logic
# Sometimes sessions take a moment to be fully registered
SESSION_FOUND=false
for ((i=1; i<=RETRY_COUNT; i++)); do
if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
SESSION_FOUND=true
break
fi
if [[ $i -lt $RETRY_COUNT ]]; then
sleep "$RETRY_DELAY"
fi
done
if [[ "$SESSION_FOUND" != true ]]; then
echo "❌ Session '$SESSION_NAME' not found after $RETRY_COUNT attempts" >&2
echo " Run: tmux list-sessions" >&2
exit 1
fi
# Send special key if specified
if [[ -n "$SPECIAL_KEY" ]]; then
tmux send-keys -t "$SESSION_NAME" "$SPECIAL_KEY"
# Log the special key send as YAML
SESSION_DIR="$PROJECT_ROOT/debug/tmux-sessions/$SESSION_NAME"
if [[ -d "$SESSION_DIR" ]]; then
TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
# Append YAML entry to commands.yaml
cat >> "$SESSION_DIR/commands.yaml" << EOF
- timestamp: $TIMESTAMP
type: key
input: "$SPECIAL_KEY"
EOF
fi
exit 0
fi
# Paste clipboard content if --paste flag is set
if [[ "$PASTE_CLIPBOARD" == true ]]; then
# Get clipboard content using pbpaste (macOS) or xclip (Linux)
if command -v pbpaste &>/dev/null; then
CLIPBOARD_CONTENT=$(pbpaste)
elif command -v xclip &>/dev/null; then
CLIPBOARD_CONTENT=$(xclip -selection clipboard -o)
elif command -v xsel &>/dev/null; then
CLIPBOARD_CONTENT=$(xsel --clipboard --output)
else
echo "❌ No clipboard utility found (pbpaste, xclip, or xsel)" >&2
exit 1
fi
if [[ -z "$CLIPBOARD_CONTENT" ]]; then
echo "❌ Clipboard is empty" >&2
exit 1
fi
# Send clipboard content using bracketed paste mode
tmux send-keys -t "$SESSION_NAME" $'\e[200~'"$CLIPBOARD_CONTENT"$'\e[201~'
# Optionally press Enter
if [[ "$AUTO_ENTER" == true ]]; then
tmux send-keys -t "$SESSION_NAME" Enter
fi
# Log the paste as YAML
SESSION_DIR="$PROJECT_ROOT/debug/tmux-sessions/$SESSION_NAME"
if [[ -d "$SESSION_DIR" ]]; then
TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
CONTENT_LENGTH=${#CLIPBOARD_CONTENT}
cat >> "$SESSION_DIR/commands.yaml" << EOF
- timestamp: $TIMESTAMP
type: paste
content_length: $CONTENT_LENGTH
auto_enter: $AUTO_ENTER
EOF
fi
exit 0
fi
# Check if text was provided
if [[ -z "$TEXT" ]]; then
echo "❌ No text or --key specified" >&2
exit 1
fi
# Deduplication check (skip if same text as last send)
SESSION_DIR="$PROJECT_ROOT/debug/tmux-sessions/$SESSION_NAME"
LAST_SENT_FILE="$SESSION_DIR/.last-sent-text"
if [[ "$FORCE_SEND" != true ]] && [[ -f "$LAST_SENT_FILE" ]]; then
LAST_SENT=$(cat "$LAST_SENT_FILE")
if [[ "$LAST_SENT" == "$TEXT" ]]; then
echo "⚠️ Skipping duplicate send (same as last). Use --force to send anyway." >&2
exit 0
fi
fi
# Clear any existing input in the buffer before sending new text
# This prevents concatenation when commands are sent in rapid succession
tmux send-keys -t "$SESSION_NAME" C-u
sleep 0.05
# Send text using bracketed paste mode
# \e[200~ = start bracketed paste
# \e[201~ = end bracketed paste
tmux send-keys -t "$SESSION_NAME" $'\e[200~'"$TEXT"$'\e[201~'
# Optionally press Enter (with small delay to let TUI apps process the paste first)
if [[ "$AUTO_ENTER" == true ]]; then
sleep 0.05
tmux send-keys -t "$SESSION_NAME" Enter
# Wait for CLI to process Enter and clear input buffer before returning
# This prevents the next send from concatenating with the previous input
# Default 500ms is needed for TUI CLIs to fully process the command and reset input state
# Use --delay to customize (e.g., --delay 200 for faster tests if not testing rapid input)
sleep $POST_ENTER_DELAY
fi
# If --wait-idle is specified, poll until terminal output stabilizes
# This is essential for rapid message testing where we need to wait for streaming to complete
# Works with both --auto-enter and --no-enter modes
if [[ "$WAIT_IDLE_SECONDS" != "0" && -n "$WAIT_IDLE_SECONDS" ]]; then
LAST_OUTPUT=""
STABLE_START=0
POLL_COUNT=0
# Calculate max polls: WAIT_IDLE_MAX / WAIT_IDLE_POLL_INTERVAL (120 / 0.25 = 480)
MAX_POLLS=480
while true; do
# Capture current terminal output
CURRENT_OUTPUT=$(tmux capture-pane -t "$SESSION_NAME" -p 2>/dev/null || echo "")
CURRENT_TIME=$(date +%s)
if [[ "$CURRENT_OUTPUT" == "$LAST_OUTPUT" ]]; then
# Output unchanged - check if stable long enough
if [[ "$STABLE_START" == "0" ]]; then
STABLE_START=$CURRENT_TIME
fi
STABLE_DURATION=$((CURRENT_TIME - STABLE_START))
if [[ "$STABLE_DURATION" -ge "$WAIT_IDLE_SECONDS" ]]; then
# Output has been stable for the required duration
break
fi
else
# Output changed - reset stability timer
LAST_OUTPUT="$CURRENT_OUTPUT"
STABLE_START=0
fi
# Check max wait timeout using simple integer counter
POLL_COUNT=$((POLL_COUNT + 1))
if [[ "$POLL_COUNT" -ge "$MAX_POLLS" ]]; then
echo "⚠️ --wait-idle timed out after ${WAIT_IDLE_MAX}s" >&2
break
fi
sleep $WAIT_IDLE_POLL_INTERVAL
done
fi
# Log the text send as YAML and update last-sent tracker
if [[ -d "$SESSION_DIR" ]]; then
TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)
# Escape special characters in text for YAML (double quotes, backslashes)
ESCAPED_TEXT=$(echo "$TEXT" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g')
# Append YAML entry to commands.yaml
cat >> "$SESSION_DIR/commands.yaml" << EOF
- timestamp: $TIMESTAMP
type: text
input: "$ESCAPED_TEXT"
auto_enter: $AUTO_ENTER
EOF
# Update last-sent tracker for deduplication
echo "$TEXT" > "$LAST_SENT_FILE"
fi