In windows opened by netssh multi, prefix S toggles broadcast for all panes and prefix e excludes the current pane (a pane-level synchronize-panes off, shown as [SOLO] in the border) or includes it again. netssh sync --pane does the same from a shell. S and e are unbound in a default tmux (E would be "spread evenly"). netssh writes the bindings to a small tmux config and sources it, binds a key only if the user's tmux.conf does not use it (netssh's own earlier binding is refreshed), and the bindings act only in windows marked @netssh. multi.sync_key and multi.solo_key change the keys or turn them off (none); also in the configurator. zellij syncs whole tabs only, so excluding a pane is tmux-only. testenv: the render test presses the keys through the xterm client (send-keys -K) and checks [SOLO], that broadcast skips the excluded pane, that S toggles, and that other windows are unaffected. The client gets GNU grep: BusyBox grep -oF counted only one match per line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
195 lines
6.4 KiB
Go
195 lines
6.4 KiB
Go
package multiplex
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"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
|
|
}
|
|
if !inside && i == 0 {
|
|
// tmux cuts the status bar's session name at 10 characters
|
|
// ("[netssh-pv"); widen it for this session only.
|
|
if _, err := r.Output("tmux", "set-option", "-t", session, "status-left-length", "40"); 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{
|
|
{"@netssh", "1"}, // the key bindings act only in these windows
|
|
{"pane-border-status", "top"},
|
|
{"pane-border-format", " #{@netssh_host}#{?pane_synchronized, [SYNC],#{?@netssh_solo, [SOLO],}} "},
|
|
}
|
|
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
|
|
}
|
|
}
|
|
}
|
|
keys, err := bindKeys(r, opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if keys != "" {
|
|
opts.Hint(keys)
|
|
}
|
|
if inside {
|
|
_, err := r.Output("tmux", "select-window", "-t", first)
|
|
return err
|
|
}
|
|
return r.Exec("tmux", "attach-session", "-t", session)
|
|
}
|
|
|
|
// soloToggle excludes the current pane from broadcast (a pane-level
|
|
// synchronize-panes off) or includes it again by removing that override.
|
|
func soloToggle(solo bool) [][]string {
|
|
if solo {
|
|
return [][]string{
|
|
{"tmux", "set-option", "-pu", "synchronize-panes"},
|
|
{"tmux", "set-option", "-pu", "@netssh_solo"},
|
|
}
|
|
}
|
|
return [][]string{
|
|
{"tmux", "set-option", "-p", "synchronize-panes", "off"},
|
|
{"tmux", "set-option", "-p", "@netssh_solo", "1"},
|
|
}
|
|
}
|
|
|
|
// keyBindings toggle broadcast in netssh multi windows; elsewhere they only
|
|
// show a message, so they do not change what the keys mean to other windows.
|
|
const syncBinding = `bind-key -T prefix %s if-shell -F '#{@netssh}' {
|
|
set-window-option synchronize-panes
|
|
display-message 'netssh: broadcast #{?synchronize-panes,on,off}'
|
|
} {
|
|
display-message 'netssh: not a netssh multi window'
|
|
}
|
|
`
|
|
|
|
const soloBinding = `bind-key -T prefix %s if-shell -F '#{@netssh}' {
|
|
if-shell -F '#{@netssh_solo}' {
|
|
set-option -pu synchronize-panes
|
|
set-option -pu @netssh_solo
|
|
display-message 'netssh: pane included in broadcast'
|
|
} {
|
|
set-option -p synchronize-panes off
|
|
set-option -p @netssh_solo 1
|
|
display-message 'netssh: pane excluded from broadcast'
|
|
}
|
|
} {
|
|
display-message 'netssh: not a netssh multi window'
|
|
}
|
|
`
|
|
|
|
// bindKeys installs the broadcast keys in the tmux server unless the user's
|
|
// own configuration already uses them, and returns a hint naming the keys.
|
|
func bindKeys(r Runner, opts Options) (string, error) {
|
|
prefix, err := r.Output("tmux", "show-options", "-gv", "prefix")
|
|
if err != nil || prefix == "" {
|
|
prefix = "C-b"
|
|
}
|
|
var conf strings.Builder
|
|
var hints, taken []string
|
|
for _, key := range []struct{ key, binding, what string }{
|
|
{opts.SyncKey, syncBinding, "broadcast to all panes"},
|
|
{opts.SoloKey, soloBinding, "exclude/include this pane"},
|
|
} {
|
|
if key.key == "" {
|
|
continue
|
|
}
|
|
// An existing binding is replaced only if netssh made it.
|
|
if existing, _ := r.Output("tmux", "list-keys", "-T", "prefix", key.key); existing != "" && !strings.Contains(existing, "@netssh") {
|
|
taken = append(taken, prefix+" "+key.key)
|
|
continue
|
|
}
|
|
fmt.Fprintf(&conf, key.binding, key.key)
|
|
hints = append(hints, prefix+" "+key.key+" "+key.what)
|
|
}
|
|
if conf.Len() > 0 {
|
|
path := filepath.Join(opts.LayoutDir, "netssh-multi.tmux.conf")
|
|
if err := r.WriteFile(path, []byte(conf.String())); err != nil {
|
|
return "", err
|
|
}
|
|
if _, err := r.Output("tmux", "source-file", path); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
hint := "keys: " + strings.Join(hints, ", ")
|
|
if len(hints) == 0 {
|
|
hint = "toggle broadcast with: netssh sync, netssh sync --pane"
|
|
}
|
|
if len(taken) > 0 {
|
|
hint += "; " + strings.Join(taken, " and ") + " kept your own binding (use netssh sync)"
|
|
}
|
|
return hint, nil
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|