package multiplex import ( "strconv" "strings" ) // paneScript runs the pane's command and keeps a failed one on screen, so an // unreachable host shows its error instead of the pane vanishing. const paneScript = `"$@"; status=$?; if [ "$status" -ne 0 ]; then echo; echo "netssh exited with status $status, press Enter to close"; read -r _; fi` // tmuxArgv runs the pane through sh with argv as arguments: no quoting, and // independent of the user's default-shell (which may be fish). func tmuxArgv(p Pane) []string { return append([]string{"sh", "-c", paneScript, "netssh-pane"}, p.Argv...) } // openTmux adds a window per chunk of panes: to the current session inside // tmux, else to a new detached session that is attached at the end. func openTmux(panes []Pane, opts Options, r Runner) error { inside := r.Getenv("TMUX") != "" session := "" if !inside { session = freeSession(r, "netssh-"+opts.Label) } groups := chunks(panes, opts.PerWindow) first := "" for i, group := range groups { name := windowLabel(opts.Label, i, len(groups)) format := []string{"-P", "-F", "#{window_id} #{pane_id}", "-n", name} var create []string switch { case inside: create = append([]string{"tmux", "new-window"}, format...) case i == 0: // A large virtual size leaves room for the splits; attaching resizes it. create = append([]string{"tmux", "new-session", "-d", "-x", "240", "-y", "70", "-s", session}, format...) default: create = append([]string{"tmux", "new-window", "-t", session + ":"}, format...) } out, err := r.Output(append(create, tmuxArgv(group[0])...)...) if err != nil { return err } window, pane, _ := strings.Cut(out, " ") if first == "" { first = window } paneIDs := []string{pane} for _, p := range group[1:] { id, err := r.Output(append([]string{"tmux", "split-window", "-t", window, "-P", "-F", "#{pane_id}"}, tmuxArgv(p)...)...) if err != nil { return err } paneIDs = append(paneIDs, id) // Re-tile after every split so the next one still has room. if _, err := r.Output("tmux", "select-layout", "-t", window, "tiled"); err != nil { return err } } for j, id := range paneIDs { // A pane may already be gone (host unreachable and closed); not fatal. _, _ = r.Output("tmux", "set-option", "-p", "-t", id, "@netssh_host", group[j].Title) } windowOptions := [][]string{ {"pane-border-status", "top"}, {"pane-border-format", " #{@netssh_host}#{?pane_synchronized, [SYNC],} "}, } if opts.Sync { windowOptions = append(windowOptions, []string{"synchronize-panes", "on"}) } for _, option := range windowOptions { if _, err := r.Output("tmux", "set-window-option", "-t", window, option[0], option[1]); err != nil { return err } } } if inside { _, err := r.Output("tmux", "select-window", "-t", first) return err } opts.Hint("toggle broadcast with: netssh sync (or Ctrl+b :setw synchronize-panes)") return r.Exec("tmux", "attach-session", "-t", session) } // freeSession returns name, or name-2, name-3, … if a session already uses it. func freeSession(r Runner, name string) string { candidate := name for n := 2; ; n++ { if _, err := r.Output("tmux", "has-session", "-t", "="+candidate); err != nil { return candidate } candidate = name + "-" + strconv.Itoa(n) } }