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:
parent
b8013b1b87
commit
4b87a7795f
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Reference in New Issue