#!/usr/bin/env bash # msg-loop.sh — reliably send a message to a loop's tmux session AND submit it. # ------------------------------------------------------------------------- # `tmux send-keys -l ` frequently leaves the text sitting UNSENT in # the input box (the immediate Enter is swallowed while the TUI is still ingesting # the paste). This types the message, then retries Enter/C-m until the text is no # longer in the input box (= it was submitted), or gives up after a bounded loop. # # msg-loop.sh # exit 0 = delivered; 2 = could not confirm submit; 1 = bad args / no session. set -uo pipefail S="${1:?usage: msg-loop.sh }"; shift MSG="$*" [ -n "$MSG" ] || { echo "msg-loop: empty message"; exit 1; } tmux has-session -t "$S" 2>/dev/null || { echo "msg-loop: no tmux session '$S'"; exit 1; } PREFIX="$(printf '%s' "$MSG" | head -c 28)" # Submitted ⇔ the message's leading text is no longer in the input box (bottom of the pane). in_input_box() { tmux capture-pane -pt "$S" 2>/dev/null | tail -4 | grep -qF -- "$PREFIX"; } tmux send-keys -t "$S" -l -- "$MSG" 2>/dev/null || { echo "msg-loop: send-keys failed"; exit 1; } sleep 0.8 for _ in 1 2 3 4 5 6 7 8 9 10; do tmux send-keys -t "$S" Enter 2>/dev/null || true sleep 1.2 in_input_box || { echo "$S: delivered ✔"; exit 0; } tmux send-keys -t "$S" C-m 2>/dev/null || true # some TUI states take C-m not Enter sleep 0.8 in_input_box || { echo "$S: delivered ✔"; exit 0; } done echo "$S: WARNING — could not confirm submit; message may remain in the input box" exit 2