Assist: reuse existing terminal for tmux attach

- Check for existing tmux clients before launching new terminal
- Use 'tmux switch-client' to reuse existing terminal window
- Fall back to new terminal only when no client is attached
This commit is contained in:
Levi Neely 2026-02-18 16:31:21 +01:00
parent b8013b1b87
commit 4b87a7795f
1 changed files with 20 additions and 6 deletions

View File

@ -663,12 +663,26 @@ func attachSession(id string) error {
return fmt.Errorf("session does not support attach")
}
go func() {
cmd := exec.Command(terminalCommand, "-e", "tmux", "attach", "-t", target)
if err := cmd.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to launch terminal: %v\n", err)
}
}()
// Check if there's already a tmux client running
clientsCmd := exec.Command("tmux", "list-clients")
clientsOutput, err := clientsCmd.Output()
if err == nil && len(clientsOutput) > 0 {
// There's an existing tmux client, switch to the target session/window
go func() {
cmd := exec.Command("tmux", "switch-client", "-t", target)
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to switch tmux client: %v\n", err)
}
}()
} else {
// No existing client, launch new terminal
go func() {
cmd := exec.Command(terminalCommand, "-e", "tmux", "attach", "-t", target)
if err := cmd.Start(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to launch terminal: %v\n", err)
}
}()
}
return nil
}