Skip to content

Commit 019c2c1

Browse files
committed
refactor(ui): simplify tmux session creation with proper quoting
Use a quote helper function to properly escape tmux session names and commands Remove redundant commands and streamline pane title setting
1 parent c3c8d59 commit 019c2c1

File tree

1 file changed

+11
-41
lines changed

1 file changed

+11
-41
lines changed

internal/adapters/ui/handlers.go

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -708,64 +708,34 @@ func (t *tui) handleConnectGroupTmux(groupName string) {
708708
// If inside tmux, we shouldn't nest sessions easily without care.
709709
// For simplicity, let's assume we want to launch a new tmux session.
710710

711-
var cmdParts []string
712-
// Start first pane with name
713-
cmdParts = append(cmdParts, fmt.Sprintf("tmux new-session -d -s %s -n '%s' 'ssh %s'",
714-
sessionName, groupServers[0].Alias, groupServers[0].Alias))
715-
// Enable pane synchronization
716-
cmdParts = append(cmdParts, fmt.Sprintf("tmux set-window-option -t %s synchronize-panes on", sessionName))
717-
718-
for i := 1; i < len(groupServers); i++ {
719-
// Create split with command, but setting pane title requires extra step or different flag
720-
// -P allows setting options on create, -F format.
721-
// Standard way to set pane title is printf escape sequence inside the shell or -T title
722-
// But ssh usually overwrites it.
723-
// We can use tmux select-pane -T after creation?
724-
// Or rename window? But we have multiple panes in one window.
725-
// tmux allow-rename off might be needed.
726-
727-
// Simple approach: execute ssh
728-
cmdParts = append(cmdParts, fmt.Sprintf("tmux split-window -t %s 'ssh %s'", sessionName, groupServers[i].Alias))
729-
// We can try to set pane title if tmux version supports it, but ssh often overrides.
730-
// Let's rely on ssh displaying the hostname.
731-
732-
// If user wants "pane title" visible, we need `set -g pane-border-status top`
733-
// Let's enable pane border status for this session
711+
quote := func(s string) string {
712+
return "'" + strings.ReplaceAll(s, "'", "'\\''") + "'"
734713
}
735714

736-
// Enable pane titles
737-
cmdParts = append(cmdParts, fmt.Sprintf("tmux set-option -t %s pane-border-status top", sessionName))
738-
cmdParts = append(cmdParts, fmt.Sprintf("tmux set-option -t %s pane-border-format \"#{pane_index} #T\"", sessionName))
739-
740-
// Set titles for all panes (trickier because ssh runs immediately)
741-
// We can wrap ssh command: "printf '\033]2;%s\033\\'; ssh %s"
742-
743-
// Let's rebuild the command loop to include title setting via tmux select-pane -T
744-
cmdParts = []string{}
715+
var cmdParts []string
745716

746717
// Use BuildSSHCommand to get the full SSH command string
747718
sshCmd0 := BuildSSHCommand(groupServers[0])
748719

749720
// Start session with first server
750-
cmdParts = append(cmdParts, fmt.Sprintf("tmux new-session -d -s %s \"%s\"", sessionName, sshCmd0))
721+
cmdParts = append(cmdParts, fmt.Sprintf("tmux new-session -d -s %s %s", quote(sessionName), quote(sshCmd0)))
751722
// Set title for the first pane (which is active immediately after creation)
752-
cmdParts = append(cmdParts, fmt.Sprintf("tmux select-pane -t %s -T \"%s\"", sessionName, groupServers[0].Alias))
723+
cmdParts = append(cmdParts, fmt.Sprintf("tmux select-pane -t %s -T %s", quote(sessionName), quote(groupServers[0].Alias)))
753724

754725
// Enable pane options
755-
cmdParts = append(cmdParts, fmt.Sprintf("tmux set-window-option -t %s synchronize-panes on", sessionName))
756-
cmdParts = append(cmdParts, fmt.Sprintf("tmux set-option -t %s pane-border-status top", sessionName))
757-
cmdParts = append(cmdParts, fmt.Sprintf("tmux set-option -t %s pane-border-format \" #{pane_title} \"", sessionName))
726+
cmdParts = append(cmdParts, fmt.Sprintf("tmux set-option -t %s pane-border-status top", quote(sessionName)))
727+
cmdParts = append(cmdParts, fmt.Sprintf("tmux set-option -t %s pane-border-format %s", quote(sessionName), quote(" #{pane_title} ")))
758728

759729
for i := 1; i < len(groupServers); i++ {
760730
sshCmdI := BuildSSHCommand(groupServers[i])
761731
// Split window
762-
cmdParts = append(cmdParts, fmt.Sprintf("tmux split-window -t %s \"%s\"", sessionName, sshCmdI))
732+
cmdParts = append(cmdParts, fmt.Sprintf("tmux split-window -t %s %s", quote(sessionName), quote(sshCmdI)))
763733
// Set title for the new pane (it becomes active after split)
764-
cmdParts = append(cmdParts, fmt.Sprintf("tmux select-pane -t %s -T \"%s\"", sessionName, groupServers[i].Alias))
734+
cmdParts = append(cmdParts, fmt.Sprintf("tmux select-pane -t %s -T %s", quote(sessionName), quote(groupServers[i].Alias)))
765735
}
766736

767-
cmdParts = append(cmdParts, fmt.Sprintf("tmux select-layout -t %s tiled", sessionName))
768-
cmdParts = append(cmdParts, fmt.Sprintf("tmux attach-session -t %s", sessionName))
737+
cmdParts = append(cmdParts, fmt.Sprintf("tmux select-layout -t %s tiled", quote(sessionName)))
738+
cmdParts = append(cmdParts, fmt.Sprintf("tmux attach-session -t %s", quote(sessionName)))
769739

770740
fullCmd := strings.Join(cmdParts, " ; ")
771741

0 commit comments

Comments
 (0)