Files
kanban/scripts/tmux-web-attach.sh
Gahow Wang 76b2ac69f8 fix: let active tmux clients control window size
Keep the global window-size=latest policy for linked web and iOS sessions so switching between phone and Mac sends SIGWINCH and reflows the shared pane.
2026-07-28 23:53:01 +08:00

58 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Attach a browser (via ttyd) to a single tmux window by linking it into a
# private temporary session. Unlike grouped sessions, link-window leaves no
# permanent group label on the source session, and each browser tab gets an
# independent view (own active window, own status setting).
# Usage: tmux-web-attach.sh <session> [window-index]
set -euo pipefail
sess="${1:-}"
win="${2:-}"
if [ -z "$sess" ]; then
echo "missing session. open this page from the kanban Live Agents panel."
sleep 2
exit 1
fi
# Only accept existing session names; never interpret arbitrary input.
if ! tmux list-sessions -F '#{session_name}' 2>/dev/null | grep -Fxq -- "$sess"; then
echo "unknown session: $sess"
sleep 2
exit 1
fi
# Default to the session's active window; validate the index either way.
if [ -z "$win" ]; then
win="$(tmux display-message -p -t "$sess" '#{window_index}')"
fi
if ! tmux list-windows -t "$sess" -F '#{window_index}' | grep -Fxq -- "$win"; then
echo "unknown window: $sess:$win"
sleep 2
exit 1
fi
src="$sess:$win"
tmp="web-$$-$RANDOM"
tmux new-session -d -s "$tmp" -x 220 -y 60
tmux set-option -t "$tmp" status off
# mouse on (session-scoped, local clients unaffected): wheel / touch scrolling
# from the browser enters copy-mode and scrolls history naturally, no prefix keys.
tmux set-option -t "$tmp" mouse on
# Replace the placeholder window with a link to the target window (-k kills the placeholder).
first="$(tmux display-message -p -t "$tmp" '#{window_index}')"
tmux link-window -k -s "$src" -t "$tmp:$first"
# A tmux window has one size shared by every viewer. Keep the global
# "window-size latest" policy so the most recently active Mac, web, or iOS
# client owns that size and full-screen programs receive SIGWINCH to reflow.
cleanup() {
tmux kill-session -t "$tmp" 2>/dev/null || true
}
trap cleanup EXIT HUP TERM INT
# destroy-unattached must be set only after attaching (it kills detached sessions instantly).
tmux attach -t "$tmp" \; set-option destroy-unattached on