Skip to content

Commit a43b723

Browse files
fix: make TUI grid refresh transactional
1 parent 95f5570 commit a43b723

1 file changed

Lines changed: 37 additions & 17 deletions

File tree

‎.agents/skills/prototype-openclaw-tui/scripts/launch-tmux-grid.sh‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
set -euo pipefail
2+
set -Eeuo pipefail
33

44
usage() {
55
echo "usage: $0 [--open | --refresh] <session> <cwd> <title> <command> <title> <command> [<title> <command> ...] (2-6 variants)" >&2
@@ -13,8 +13,8 @@ configure_tmux_session() {
1313
tmux set-option -t "$session_id" mouse on
1414
tmux set-option -t "$session_id" status-left " TUI PROTOTYPES "
1515
tmux set-option -t "$session_id" status-right "Ctrl-b z: zoom"
16-
tmux set-option -t "$session_id" pane-border-status top
17-
tmux set-option -t "$session_id" pane-border-format ' #[bold]#{pane_title} #[default]'
16+
tmux set-window-option -t "$window_id" pane-border-status top
17+
tmux set-window-option -t "$window_id" pane-border-format ' #[bold]#{pane_title} #[default]'
1818
tmux set-window-option -t "$window_id" allow-rename off
1919
tmux set-window-option -t "$window_id" automatic-rename off
2020
tmux set-window-option -t "$window_id" remain-on-exit on
@@ -275,36 +275,54 @@ pane_command_prefix=(/bin/sh -c)
275275
if [[ ${TERM:-} == "dumb" && -n ${NO_COLOR:-} ]]; then
276276
pane_command_prefix=(env -u NO_COLOR /bin/sh -c)
277277
fi
278+
placeholder_command=(/bin/sh -c 'while :; do sleep 3600; done')
278279

279280
if [[ $refresh_session == true ]]; then
280281
session_id=$(tmux display-message -p -t "=$session_name" '#{session_id}')
281-
window_id=""
282+
previous_window_id=""
282283
while read -r candidate_id candidate_name; do
283284
if [[ $candidate_name == prototypes ]]; then
284-
window_id=$candidate_id
285+
previous_window_id=$candidate_id
285286
break
286287
fi
287288
done < <(tmux list-windows -t "$session_id" -F '#{window_id} #{window_name}')
288-
if [[ -z $window_id ]]; then
289+
if [[ -z $previous_window_id ]]; then
289290
echo "tmux session has no prototypes window: $session_name" >&2
290291
exit 1
291292
fi
292-
configure_tmux_session "$session_id" "$window_id"
293293

294-
pane_ids=()
295-
while IFS= read -r pane_id; do
296-
pane_ids[${#pane_ids[@]}]=$pane_id
297-
done < <(tmux list-panes -t "$window_id" -F '#{pane_id}')
298-
first_pane=${pane_ids[0]}
299-
tmux select-pane -t "$first_pane"
300-
for ((index = ${#pane_ids[@]} - 1; index >= 1; index--)); do
301-
tmux kill-pane -t "${pane_ids[index]}"
302-
done
294+
refresh_window_created=false
295+
previous_window_renamed=false
296+
cleanup_failed_refresh() {
297+
if [[ $previous_window_renamed == true ]]; then
298+
tmux rename-window -t "$previous_window_id" prototypes 2>/dev/null || true
299+
fi
300+
if [[ $refresh_window_created == true ]]; then
301+
tmux kill-window -t "$window_id" 2>/dev/null || true
302+
fi
303+
}
304+
trap cleanup_failed_refresh ERR
305+
306+
first_pane=$(tmux new-window -d -P -F '#{pane_id}' \
307+
-t "$session_id:" -n "prototypes-refresh-$$" -c "$prototype_cwd" \
308+
"${placeholder_command[@]}")
309+
refresh_window_created=true
310+
window_id=$(tmux display-message -p -t "$first_pane" '#{window_id}')
311+
configure_tmux_session "$session_id" "$window_id"
303312

304313
tmux respawn-pane -k -t "$first_pane" -c "$prototype_cwd" \
305314
"${pane_command_prefix[@]}" "${pane_commands[0]}"
306315
tmux select-pane -t "$first_pane" -T "${pane_titles[0]}"
307316
build_comparison_grid "$window_id" "$first_pane"
317+
318+
tmux rename-window -t "$previous_window_id" "prototypes-previous-$$"
319+
previous_window_renamed=true
320+
tmux rename-window -t "$window_id" prototypes
321+
tmux select-window -t "$window_id"
322+
tmux kill-window -t "$previous_window_id"
323+
previous_window_renamed=false
324+
refresh_window_created=false
325+
trap - ERR
308326
else
309327
created_session=false
310328
cleanup_partial_session() {
@@ -316,11 +334,13 @@ else
316334

317335
first_pane=$(tmux new-session -d -P -F '#{pane_id}' \
318336
-s "$session_name" -n prototypes -c "$prototype_cwd" \
319-
"${pane_command_prefix[@]}" "${pane_commands[0]}")
337+
"${placeholder_command[@]}")
320338
created_session=true
321339
session_id=$(tmux display-message -p -t "$first_pane" '#{session_id}')
322340
window_id=$(tmux display-message -p -t "$first_pane" '#{window_id}')
323341
configure_tmux_session "$session_id" "$window_id"
342+
tmux respawn-pane -k -t "$first_pane" -c "$prototype_cwd" \
343+
"${pane_command_prefix[@]}" "${pane_commands[0]}"
324344
tmux select-pane -t "$first_pane" -T "${pane_titles[0]}"
325345
if [[ $open_external == true ]]; then
326346
if open_external_terminal "$session_name"; then

0 commit comments

Comments
 (0)