Set --border-label as the title of the tmux floating pane (#4853)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
The pane sets the options itself before running fzf, so that they are in place no matter how quickly the command exits, targeted at $TMUX_PANE; the default target would resolve to the active pane of the session's current window. - pane-border-format is set to '#{pane_title}' so that the label is displayed on the border when pane-border-status is enabled; pane-border-status itself is a window option in released tmux versions and is left alone - When a border style is explicitly specified with --border, a popup is used instead of a floating pane so that the fzf-drawn border is the only border shown; give 'border-native' to force a floating pane - 'none' and 'line' are treated as no border; fzf draws no box for either, so the label is displayed on the native border - Remove 'border-fzf' which is now redundant; it was never released - The title is escaped for select-pane -T which expands format expressions; a lone ';' is escaped as tmux would treat it as a command separator - The label is skipped when ANSI stripping leaves an empty string - --border-label-pos is ignored - Fix remain-on-exit set on the original pane instead of the floating pane
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+146 added−40 removed
- tests:+15 added−2 removed
- other:+131 added−38 removed
Provenance
- Commit
77e6394- Tree
7434cca- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
1e31e5d
8 files changed+146 added−40 removed
modified:man/man1/fzf.1+10 added-4 removed
415 unmodified lines416 layout options so that the specified number of items are visible in the list417 section (default: \fB10+\fR).418 Ignored when \fB\-\-height\fR is not specified or set as an absolute value.419 .TP420-.BI "\-\-popup" "[=[center|top|bottom|left|right][,SIZE[%]][,SIZE[%]][,border-native|border-fzf]]"420+.BI "\-\-popup" "[=[center|top|bottom|left|right][,SIZE[%]][,SIZE[%]][,border-native]]"421 Start fzf in a tmux or Zellij floating pane (default \fBcenter,50%\fR).422 Requires tmux 3.3+ or Zellij 0.44+. This option is ignored if you423 are not running fzf inside tmux or Zellij. \fB\-\-tmux\fR is an alias for this option.424 425 On tmux 3.7 or above, the floating pane is not modal; you can switch to426 other panes and windows while fzf is running, move and resize the pane with427 the mouse, zoom it to fullscreen, and use copy\-mode in it. The pane always428 has a native border, which is what makes it movable and resizable, so429-\fBborder\-native\fR is implied. On tmux versions below 3.7, or when430-\fBborder\-fzf\fR is given, a modal tmux popup is used instead and fzf draws431-its own border.429+\fBborder\-native\fR is implied. \fB\-\-border\-label\fR is set as the title430+of the pane, and is displayed on the border if \fBpane\-border\-status\fR431+is enabled in tmux (\fB\-\-border\-label\-pos\fR is ignored).432 433+A modal tmux popup is used instead on tmux versions below 3.7, or when a434+border style is explicitly specified with \fB\-\-border\fR, so that the435+fzf\-drawn border is the only border shown. \fBnone\fR and \fBline\fR are436+treated as no border. Give \fBborder\-native\fR to437+force a floating pane nonetheless.438+439 e.g.440 \fB# Popup in the center with 70% width and height441 fzf \-\-popup 70%442 1968 unmodified linesmodified:src/options.go+6 added-18 removed
75 unmodified lines76 Add '+' to automatically increase the value77 according to the other layout options (default: 10+).78 --popup[=OPTS] Start fzf in a floating pane (requires tmux 3.3+ or Zellij 0.44+)79 [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]80- [,border-native|border-fzf] (default: center,50%)80+ [,border-native] (default: center,50%)81 --tmux[=OPTS] Alias for --popup82 83 LAYOUT84 --layout=LAYOUT Choose layout: [default|reverse|reverse-list]249 unmodified lines334 posCenter335 posNext // adjacent to the input section, on the list side336 )337 338-type tmuxBorder int339-340-const (341- tmuxBorderAuto tmuxBorder = iota342- tmuxBorderNative343- tmuxBorderFzf344-)345-338 type tmuxOptions struct {339 width sizeSpec340 height sizeSpec341 position windowPosition342 index int351- border tmuxBorder343+ border bool344 }345 346 type layoutType int347 78 unmodified lines426 func parseTmuxOptions(arg string, index int) (*tmuxOptions, error) {427 var err error428 opts := defaultTmuxOptions(index)429 tokens := splitRegexp.Split(arg, -1)438- errorToReturn := errors.New("invalid popup option: " + arg + " (expected: [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]][,border-native|border-fzf])")430+ errorToReturn := errors.New("invalid popup option: " + arg + " (expected: [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]][,border-native])")431 if len(tokens) == 0 || len(tokens) > 4 {432 return nil, errorToReturn433 }434 435 for i, token := range tokens {444- if token == "border-native" || token == "border-fzf" {445- tokens = append(tokens[:i], tokens[i+1:]...) // cut the border option446- if token == "border-native" {447- opts.border = tmuxBorderNative448- } else {449- opts.border = tmuxBorderFzf450- }436+ if token == "border-native" {437+ tokens = append(tokens[:i], tokens[i+1:]...) // cut the 'border-native' option438+ opts.border = true439 break440 }441 }442 3552 unmodified linesmodified:src/proxy.go+2 added-2 removed
24 unmodified lines25 26 func popupArgStr(args []string, opts *Options) (string, string) {27 fzf, rest := args[0], args[1:]28 args = []string{"--bind=ctrl-z:ignore"}29- if opts.Tmux.border != tmuxBorderNative && (opts.BorderShape == tui.BorderUndefined || opts.BorderShape == tui.BorderLine) {29+ if !opts.Tmux.border && (opts.BorderShape == tui.BorderUndefined || opts.BorderShape == tui.BorderLine) {30 if tui.DefaultBorderShape == tui.BorderRounded {31 rest = append(rest, "--border=rounded")32 } else {33 rest = append(rest, "--border=sharp")34 }35 }36- if opts.Tmux.border == tmuxBorderNative && opts.Margin == defaultMargin() {36+ if opts.Tmux.border && opts.Margin == defaultMargin() {37 args = append(args, "--margin=0,1")38 }39 argStr := escapeSingleQuote(fzf)40 for _, arg := range append(args, rest...) {148 unmodified linesadded:src/tmux_test.go+39 added
1+package fzf2+3+import "testing"4+5+func TestEscapeTmuxTitle(t *testing.T) {6+ for _, tc := range []struct {7+ given string8+ expected string9+ }{10+ {"", ""},11+ {" fzf ", " fzf "},12+ {"#", "##"},13+ {"##", "####"},14+ {" C# notes #S ", " C## notes ##S "},15+ {"100%", "100%"},16+ {";", `\;`},17+ {"; rm", "; rm"},18+ {" ; ", " ; "},19+ } {20+ if actual := escapeTmuxTitle(tc.given); actual != tc.expected {21+ t.Errorf("expected %q, got %q", tc.expected, actual)22+ }23+ }24+}25+26+func TestEscapeTmuxTitleSeparator(t *testing.T) {27+ for _, tc := range []struct {28+ given string29+ expected string30+ }{31+ {"#;", "##;"},32+ {";#", ";##"},33+ {";;", ";;"},34+ } {35+ if actual := escapeTmuxTitle(tc.given); actual != tc.expected {36+ t.Errorf("expected %q, got %q", tc.expected, actual)37+ }38+ }39+}modified:src/tmux.go+67 added-11 removed
4 unmodified lines5 "os"6 "os/exec"7 "path/filepath"8 "strings"9+10+ "github.com/junegunn/fzf/src/tui"11 )12 13 // Returns the size of the current window if the tmux server supports14 // floating panes (tmux 3.7 or above)20 unmodified lines35 }36 return width, height, true37 }38 39+// A lone ';' argument is a command separator to tmux, aborting the whole40+// command at parse time41+func escapeTmuxSeparator(str string) string {42+ if str == ";" {43+ return `\;`44+ }45+ return str46+}47+48+// Escape a string for use as the pane title; select-pane -T expands format49+// expressions denoted by '#', but not time conversion specifiers50+func escapeTmuxTitle(str string) string {51+ return escapeTmuxSeparator(strings.ReplaceAll(str, "#", "##"))52+}53+54 // Convert sizeSpec to the number of cells, clamped between the minimum55 // footprint of 3, including the border, and the window size56 func tmuxDim(spec sizeSpec, window int) int {57 dim := int(spec.size)48 unmodified lines106 return nil, err107 }108 f.Close()109 code := escapeSingleQuote(codeFile)110+111+ // Pane options are set by the pane itself before running fzf, so112+ // that they are in place no matter how quickly the command exits.113+ // The target must be explicit; without it, the commands would114+ // resolve to the active pane of the session's current window,115+ // which is not necessarily the pane running them.116+ //117+ // The pane should always close on exit like a popup, even when118+ // remain-on-exit is on.119+ setup := `tmux set-option -p -t "$TMUX_PANE" remain-on-exit off 2> /dev/null; `120+121+ // Set --border-label as the title of the floating pane, and as its122+ // pane-border-format so that it is displayed on the border when123+ // pane-border-status is enabled. pane-border-format is pane-scoped,124+ // but pane-border-status is a window option that only becomes125+ // pane-scoped in the next release of tmux, so it is left alone.126+ // https://github.com/tmux/tmux/commit/7a18fa281db3127+ // --border-label-pos is ignored.128+ // Skipped when fzf draws its own border with the label on it.129+ // '--border=none' is included; fzf would not display the label, but130+ // the native border of a floating pane cannot be removed, so131+ // display the label on it nonetheless.132+ if opts.BorderLabel.label != "" &&133+ (opts.BorderShape == tui.BorderUndefined || opts.BorderShape == tui.BorderLine ||134+ opts.BorderShape == tui.BorderNone) {135+ // Strip ANSI sequences fzf would otherwise render itself136+ label, _, _ := extractColor(opts.BorderLabel.label, nil, nil)137+ if label != "" {138+ setup += fmt.Sprintf(`tmux select-pane -t "$TMUX_PANE" -T %s 2> /dev/null; `,139+ escapeSingleQuote(escapeTmuxTitle(label)))140+ // The title is displayed verbatim; substituted values are141+ // not expanded again142+ setup += `tmux set-option -p -t "$TMUX_PANE" pane-border-format '#{pane_title}' 2> /dev/null; `143+ }144+ }93- paneCmd := fmt.Sprintf("%s %s; echo $? > %s; tmux wait-for -S %s",94- escapeSingleQuote(sh), escapeSingleQuote(temp), code, signal)145+ paneCmd := fmt.Sprintf("%s%s %s; echo $? > %s; tmux wait-for -S %s",146+ setup, escapeSingleQuote(sh), escapeSingleQuote(temp), code, signal)147 // Unzoom the window first; creating a floating pane over a zoomed148 // window crashes the tmux server on 3.7b, and newer versions of97- // tmux unzoom the window anyway. The pane should always close on98- // exit like a popup, even when remain-on-exit is on. set-option99- // targets the new pane as new-pane makes it the current pane.149+ // tmux unzoom the window anyway.150 target := os.Getenv("TMUX_PANE")151 newPane := fmt.Sprintf(102- "tmux if -F -t %s '#{window_zoomed_flag}' %s ';' new-pane -P -F '#{pane_id}' -t %s -c %s -x %d -y %d -X %d -Y %d %s -c %s ';' set-option -p remain-on-exit off",152+ "tmux if -F -t %s '#{window_zoomed_flag}' %s ';' new-pane -P -F '#{pane_id}' -t %s -c %s -x %d -y %d -X %d -Y %d %s -c %s",153 escapeSingleQuote(target), escapeSingleQuote("resize-pane -Z -t "+target),154 escapeSingleQuote(target), escapeSingleQuote(dir), width-2, height-2, x, y,155 escapeSingleQuote(sh), escapeSingleQuote(paneCmd))156+157 // The pane is killed when the proxy process is interrupted or hung up,158 // like a popup dying with its client. wait-for runs in the background159 // and is awaited with the interruptible wait builtin so that the trap160 // can fire while blocked. The trap is installed before creating the19 unmodified lines180 181 func runTmux(args []string, opts *Options) (int, error) {182 // On tmux 3.7 or above, fzf runs in a floating pane instead of a popup.183 // A floating pane always has a native border, so 'border-native' is133- // implied. Give 'border-fzf' to fall back to a popup where fzf draws134- // its own border.135- if opts.Tmux.border != tmuxBorderFzf {184+ // implied. When a border style is explicitly specified with --border, a185+ // popup is used instead so that the fzf-drawn border is the only border186+ // shown; the native border of a floating pane cannot be removed. 'none'187+ // and 'line' are treated as no border; fzf draws no box for either, and188+ // 'line' only makes sense with --height. Give 'border-native' to force189+ // a floating pane nonetheless.190+ if opts.Tmux.border || opts.BorderShape == tui.BorderUndefined ||191+ opts.BorderShape == tui.BorderLine || opts.BorderShape == tui.BorderNone {192 if windowWidth, windowHeight, ok := tmuxFloatingPaneInfo(); ok {137- opts.Tmux.border = tmuxBorderNative193+ opts.Tmux.border = true194 argStr, dir := popupArgStr(args, opts)195 return runTmuxFloatingPane(argStr, dir, windowWidth, windowHeight, opts)196 }197 }7 unmodified lines205 // M Both The mouse position206 // W Both The window position on the status line207 // S -y The line above or below the status line208 tmuxArgs := []string{"display-popup", "-E", "-d", dir}153- if opts.Tmux.border != tmuxBorderNative {209+ if !opts.Tmux.border {210 tmuxArgs = append(tmuxArgs, "-B")211 }212 switch opts.Tmux.position {213 case posUp:22 unmodified linesmodified:src/zellij.go+1 added-1 removed
9 unmodified lines10 zellijArgs := []string{11 "run", "--floating", "--close-on-exit", "--block-until-exit",12 "--cwd", dir,13 }14- if opts.Tmux.border != tmuxBorderNative {14+ if !opts.Tmux.border {15 zellijArgs = append(zellijArgs, "--borderless", "true")16 }17 switch opts.Tmux.position {18 case posUp:23 unmodified linesmodified:test/test_tmux.rb+15 added-2 removed
30 unmodified lines31 assert system('tmux', 'kill-pane', '-t', pane)32 tmux.until { |lines| assert lines.any_include?('code:130') }33 end34 35- def test_border_fzf_falls_back_to_popup35+ def test_floating_pane_border_label36+ tmux.send_keys "seq 100 | #{fzf(%(--popup center,80% --margin 0 --border-label ' #fzf-label 100% '))}", :Enter37+ tmux.until { |lines| assert_equal 100, lines.item_count }38+ pane = floating_pane39+ refute_nil pane40+ title = IO.popen(['tmux', 'display-message', '-p', '-t', pane, "\#{pane_title}"], &:read)41+ assert_equal ' #fzf-label 100% ', title.chomp42+ format = IO.popen(['tmux', 'show-options', '-p', '-t', pane, 'pane-border-format'], &:read)43+ assert_includes format, "\#{pane_title}"44+ tmux.send_keys :Enter45+ assert_equal '1', fzf_output46+ end47+48+ def test_explicit_border_falls_back_to_popup49 # display-popup requires an attached client, which the test environment50 # may not have; intercept it with a tmux shim on PATH51 dir = Dir.mktmpdir52 real = `command -v tmux`.chomp6 unmodified lines59 fi60 exec #{real.shellescape} "$@"61 SH62 FileUtils.chmod(0o755, shim)50- tmux.send_keys "seq 100 | PATH=#{dir.shellescape}:$PATH #{FZF} --popup center,border-fzf", :Enter63+ tmux.send_keys "seq 100 | PATH=#{dir.shellescape}:$PATH #{FZF} --popup center --border rounded", :Enter64 tmux.until { |lines| assert lines.any_include?('popup-used') }65 refute floating_pane66 ensure67 FileUtils.remove_entry(dir) if dir10 unmodified linesmodified:CHANGELOG.md+6 added-2 removed
4 unmodified lines5 ------------6 - On tmux 3.7 or above, `--popup` starts fzf in a floating pane instead of a popup (#4850)7 - Unlike a popup, a floating pane is not modal; you can switch to other panes and windows while fzf is running, move and resize the pane with the mouse, zoom it to fullscreen, and use copy-mode in it8 - A floating pane always has a native border, which is what makes the pane movable and resizable, so `border-native` is implied9- - Give `border-fzf` to fall back to a popup where fzf draws its own border9+ - A popup is used instead when a border style is explicitly specified with `--border`, so that the fzf-drawn border is the only border shown (`none` and `line` are treated as no border)10 ```sh11- fzf --popup center,80%,border-fzf11+ fzf --popup --border rounded12 ```13+ - `--border-label` is set as the title of the floating pane, and is displayed on the border if `pane-border-status` is enabled in tmux14+ ```sh15+ fzf --popup --border-label ' fzf '16+ ```17 - Added `result-final` event, a variant of `result` that is not triggered while the input stream is still open (#4835)18 - Use it for one-shot, per-query actions that would otherwise re-fire on every intermediate snapshot during loading19 ```sh20 # 'result' fires per intermediate snapshot (header keeps updating during load);3336 unmodified lines