(sessionName: string, env?: Record<string, string>)
| 58 | const repoRoot = getWorktreeRoot(); |
| 59 | // When another tmux server is already running (e.g. from a sibling worktree), |
| 60 | // `tmux new-session` attaches to that existing server and inherits its |
| 61 | // environment — NOT our current process.env. Pass critical vars with -e and |
| 62 | // set them on the session so later windows see values like KILO_PORT_OFFSET |
| 63 | // and CI-provided PATH updates. |
| 64 | const sessionEnv = worktreeEnvironment(repoRoot, env); |
| 65 | const newSessionEnv = { ...sessionEnv }; |
| 66 | delete newSessionEnv.OLDPWD; |
| 67 | const envArgs = Object.entries(newSessionEnv) |
| 68 | .map(([k, v]) => `-e ${escapeForShell(`${k}=${v}`)}`) |
| 69 | .join(' '); |
| 70 | const envPrefix = `${envArgs} `; |
| 71 | execSync( |
| 72 | `tmux new-session -d ${envPrefix}-s ${sessionName} -n dashboard -c ${escapeForShell(repoRoot)} ${buildInteractiveShellCommand( |
| 73 | `cd ${escapeForShell(repoRoot)}`, |
| 74 | undefined, |
| 75 | repoRoot |
| 76 | )}`, |
| 77 | { stdio: 'ignore' } |
| 78 | ); |
| 79 | for (const [key, value] of Object.entries({ ...sessionEnv, OLDPWD: repoRoot })) { |
| 80 | execSync( |
| 81 | `tmux set-environment -t ${sessionName} ${escapeForShell(key)} ${escapeForShell(value)}`, |
| 82 | { stdio: 'ignore' } |
| 83 | ); |
| 84 | } |
| 85 | // Destroy the session when no clients are attached (e.g. terminal window closed). |
| 86 | // The session starts detached (-d) so we must defer the option via a hook; |
| 87 | // setting it immediately would destroy the session before any client attaches. |
| 88 | execSync( |
| 89 | `tmux set-hook -t ${sessionName} client-attached "set-option -t ${sessionName} destroy-unattached on"`, |
| 90 | { stdio: 'ignore' } |
| 91 | ); |
| 92 | execSync(`tmux set-option -t ${sessionName} mouse on`, { stdio: 'ignore' }); |
| 93 | // Enable focus events so tmux detects terminal tab switches (needed for client-focus-in hook) |
| 94 | execSync(`tmux set-option -t ${sessionName} focus-events on`, { stdio: 'ignore' }); |
| 95 | // Copy tmux selection to system clipboard (macOS pbcopy) |
| 96 | execSync(`tmux set-option -t ${sessionName} set-clipboard on`, { stdio: 'ignore' }); |
| 97 | execSync( |
| 98 | `tmux bind-key -T copy-mode MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"`, |
| 99 | { stdio: 'ignore' } |
| 100 | ); |
| 101 | execSync( |
| 102 | `tmux bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"`, |
| 103 | { stdio: 'ignore' } |
| 104 | ); |
| 105 | execSync(`tmux set-option -t ${sessionName} status-position bottom`, { stdio: 'ignore' }); |
| 106 | execSync(`tmux set-option -t ${sessionName} window-status-format " #I:#W "`, { stdio: 'ignore' }); |
| 107 | execSync(`tmux set-option -t ${sessionName} window-status-current-format " #I:#W "`, { |
| 108 | stdio: 'ignore', |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | function setSessionEnvironment(sessionName: string, env: Record<string, string>): void { |
no test coverage detected