feat(multi): keep hotkeys visible and add README cheat sheet
This commit is contained in:
1 parent
c4466067a5
commit
c58b26ecf8
3 files changed
+159
-21
No files matched your search
@@ -18,6 +18,7 @@ type fake struct {
|
||||
syncState string
|
||||
keys map[string]string // existing prefix bindings by key
|
||||
solo string
|
||||
prefix string
|
||||
n int
|
||||
}
|
||||
|
||||
@@ -33,6 +34,8 @@ func (f *fake) Output(argv ...string) (string, error) {
|
||||
f.calls = append(f.calls, strings.Join(argv, " "))
|
||||
f.n++
|
||||
switch argv[1] {
|
||||
case "show-options":
|
||||
return f.prefix, nil
|
||||
case "new-window", "new-session":
|
||||
return fmt.Sprintf("@%d %%%d", f.n, f.n), nil
|
||||
case "split-window":
|
||||
@@ -121,6 +124,8 @@ func TestTmuxInside(t *testing.T) {
|
||||
"tmux list-keys -T prefix S",
|
||||
"tmux list-keys -T prefix e",
|
||||
"tmux source-file /tmp/l/netssh-multi.tmux.conf",
|
||||
"tmux set-window-option -t @1 @netssh_keys keys: Ctrl+b then S: sync on/off | Ctrl+b then e: solo on/off",
|
||||
"tmux set-window-option -t @1 pane-border-format #{@netssh_host}#{?pane_synchronized, [SYNC],#{?@netssh_solo, [SOLO],}} #{?pane_active, | #{@netssh_keys},} ",
|
||||
"tmux select-window -t @1",
|
||||
}
|
||||
if strings.Join(f.calls, "\n") != strings.Join(want, "\n") {
|
||||
@@ -234,7 +239,7 @@ func TestTmuxKeyBindings(t *testing.T) {
|
||||
t.Errorf("conf lacks %q:\n%s", want, conf)
|
||||
}
|
||||
}
|
||||
if hint != "keys: C-b S broadcast to all panes, C-b e exclude/include this pane" {
|
||||
if hint != "keys: Ctrl+b then S: sync on/off | Ctrl+b then e: solo on/off" {
|
||||
t.Errorf("hint %q", hint)
|
||||
}
|
||||
|
||||
@@ -279,3 +284,34 @@ func TestToggleSyncPane(t *testing.T) {
|
||||
t.Fatalf("zellij: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTmuxPersistentHints(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
keys map[string]string
|
||||
want string
|
||||
}{
|
||||
{"custom prefix", nil, "keys: Ctrl+a then Ctrl+s: sync on/off | Ctrl+a then e: solo on/off"},
|
||||
{"occupied key", map[string]string{"C-s": "bind-key -T prefix C-s display-time"}, "keys: Ctrl+a then e: solo on/off; C-a C-s kept your own binding (use netssh sync)"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f := newFake(nil, "tmux")
|
||||
f.prefix, f.keys = "C-a", tc.keys
|
||||
if err := Open(Tmux, panes("a", "b"), Options{PerWindow: 1, SyncKey: "C-s", SoloKey: "e"}, f); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
n := 0
|
||||
for _, call := range f.calls {
|
||||
if strings.Contains(call, " @netssh_keys ") {
|
||||
n++
|
||||
if !strings.HasSuffix(call, " @netssh_keys "+tc.want) {
|
||||
t.Fatalf("wrong persistent hint: %s", call)
|
||||
}
|
||||
}
|
||||
}
|
||||
if n != 2 {
|
||||
t.Fatalf("hints installed in %d windows, want 2", n)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,7 @@ func openTmux(panes []Pane, opts Options, r Runner) error {
|
||||
}
|
||||
groups := chunks(panes, opts.PerWindow)
|
||||
first := ""
|
||||
var windows []string
|
||||
for i, group := range groups {
|
||||
name := windowLabel(opts.Label, i, len(groups))
|
||||
format := []string{"-P", "-F", "#{window_id} #{pane_id}", "-n", name}
|
||||
@@ -52,6 +53,7 @@ func openTmux(panes []Pane, opts Options, r Runner) error {
|
||||
}
|
||||
}
|
||||
window, pane, _ := strings.Cut(out, " ")
|
||||
windows = append(windows, window)
|
||||
if first == "" {
|
||||
first = window
|
||||
}
|
||||
@@ -91,6 +93,17 @@ func openTmux(panes []Pane, opts Options, r Runner) error {
|
||||
}
|
||||
if keys != "" {
|
||||
opts.Hint(keys)
|
||||
// Window-local hints remain visible after attaching, including in an
|
||||
// existing session. Keep other windows and the user's status bar intact.
|
||||
for _, window := range windows {
|
||||
if _, err := r.Output("tmux", "set-window-option", "-t", window, "@netssh_keys", keys); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := r.Output("tmux", "set-window-option", "-t", window, "pane-border-format",
|
||||
" #{@netssh_host}#{?pane_synchronized, [SYNC],#{?@netssh_solo, [SOLO],}} #{?pane_active, | #{@netssh_keys},} "); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if inside {
|
||||
_, err := r.Output("tmux", "select-window", "-t", first)
|
||||
@@ -149,8 +162,8 @@ func bindKeys(r Runner, opts Options) (string, error) {
|
||||
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"},
|
||||
{opts.SyncKey, syncBinding, "sync on/off"},
|
||||
{opts.SoloKey, soloBinding, "solo on/off"},
|
||||
} {
|
||||
if key.key == "" {
|
||||
continue
|
||||
@@ -161,7 +174,7 @@ func bindKeys(r Runner, opts Options) (string, error) {
|
||||
continue
|
||||
}
|
||||
fmt.Fprintf(&conf, key.binding, key.key)
|
||||
hints = append(hints, prefix+" "+key.key+" "+key.what)
|
||||
hints = append(hints, displayKey(prefix)+" then "+displayKey(key.key)+": "+key.what)
|
||||
}
|
||||
if conf.Len() > 0 {
|
||||
path := filepath.Join(opts.LayoutDir, "netssh-multi.tmux.conf")
|
||||
@@ -172,7 +185,7 @@ func bindKeys(r Runner, opts Options) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
hint := "keys: " + strings.Join(hints, ", ")
|
||||
hint := "keys: " + strings.Join(hints, " | ")
|
||||
if len(hints) == 0 {
|
||||
hint = "toggle broadcast with: netssh sync, netssh sync --pane"
|
||||
}
|
||||
@@ -192,3 +205,9 @@ func freeSession(r Runner, name string) string {
|
||||
candidate = name + "-" + strconv.Itoa(n)
|
||||
}
|
||||
}
|
||||
|
||||
// displayKey expands tmux's modifier notation for the on-screen legend.
|
||||
func displayKey(key string) string {
|
||||
key = strings.ReplaceAll(key, "C-", "Ctrl+")
|
||||
return strings.ReplaceAll(key, "M-", "Alt+")
|
||||
}
|
||||
Reference in new issue
Block a user