feat(multi): tmux keys to toggle broadcast and exclude single panes
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>
This commit is contained in:
1 parent
699b9469b6
commit
396d489187
14 files changed
+342
-31
No files matched your search
@@ -1,6 +1,8 @@
|
||||
package multiplex
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -70,8 +72,9 @@ func openTmux(panes []Pane, opts Options, r Runner) error {
|
||||
_, _ = 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],} "},
|
||||
{"pane-border-format", " #{@netssh_host}#{?pane_synchronized, [SYNC],#{?@netssh_solo, [SOLO],}} "},
|
||||
}
|
||||
if opts.Sync {
|
||||
windowOptions = append(windowOptions, []string{"synchronize-panes", "on"})
|
||||
@@ -82,14 +85,103 @@ func openTmux(panes []Pane, opts Options, r Runner) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
opts.Hint("toggle broadcast with: netssh sync (or Ctrl+b :setw synchronize-panes)")
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user