Skip to content

Commit 4d03c52

Browse files
MrFlounderclaude
andcommitted
fix(restart): create new window before killing old one
The previous approach killed the current window first, which destroyed the terminal context. Now we: 1. Create new window with temp name 2. Kill old window 3. Rename temp to ws{N} This ensures the user is never left without a window. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b3e4314 commit 4d03c52

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

src/crabcode

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,27 +1686,38 @@ restart_workspace() {
16861686
local port_msg="Using port $env_api_port"
16871687
[ "$need_override" = "true" ] && port_msg="Port $env_api_port in use → using $api_port"
16881688

1689-
# Kill existing window if it exists (to recreate full layout)
16901689
local current_window=$(tmux display-message -p '#{window_name}' 2>/dev/null)
1690+
local old_window=""
1691+
1692+
# Determine which window to replace
16911693
if tmux list-windows -t "$SESSION_NAME" -F "#{window_name}" 2>/dev/null | grep -q "^$window_name$"; then
1692-
echo " Killing existing window to recreate layout..."
1693-
tmux kill-window -t "$SESSION_NAME:$window_name" 2>/dev/null || true
1694+
old_window="$window_name"
16941695
elif [[ "$(pwd)" == "$dir"* ]] && [ -n "$current_window" ]; then
1695-
# We're in the workspace dir but window has different name - kill current
1696-
echo " Killing current window to recreate layout..."
1697-
tmux kill-window -t "$SESSION_NAME:$current_window" 2>/dev/null || true
1696+
old_window="$current_window"
16981697
fi
16991698

1700-
sleep 0.3
1699+
if [ -n "$old_window" ]; then
1700+
echo " Recreating workspace layout..."
1701+
# Create new window with temp name first
1702+
local temp_name="_restart_temp_$$"
1703+
create_workspace_layout "$temp_name" "$dir" "$dev_cmd" "$claude_cmd" "$port_msg" "add"
17011704

1702-
echo " Recreating workspace layout..."
1703-
create_workspace_layout "$window_name" "$dir" "$dev_cmd" "$claude_cmd" "$port_msg" "add"
1705+
# Kill old window
1706+
tmux kill-window -t "$SESSION_NAME:$old_window" 2>/dev/null || true
17041707

1705-
# Record mood event
1706-
mood_record_event "restart" "workspace $num"
1708+
# Rename temp to proper name
1709+
tmux rename-window -t "$SESSION_NAME:$temp_name" "$window_name" 2>/dev/null || true
17071710

1708-
success "Workspace $num restarted with fresh layout!"
1709-
tmux select-window -t "$SESSION_NAME:$window_name"
1711+
# Record mood event
1712+
mood_record_event "restart" "workspace $num"
1713+
1714+
success "Workspace $num restarted with fresh layout!"
1715+
tmux select-window -t "$SESSION_NAME:$window_name"
1716+
else
1717+
echo -e "${YELLOW} No existing window found - creating new...${NC}"
1718+
create_workspace_layout "$window_name" "$dir" "$dev_cmd" "$claude_cmd" "$port_msg" "add"
1719+
tmux select-window -t "$SESSION_NAME:$window_name"
1720+
fi
17101721
else
17111722
echo -e "${YELLOW} Not in tmux session - run 'crabcode $num' to start${NC}"
17121723
fi

0 commit comments

Comments
 (0)