Skip to content

Commit

Permalink
add native-border option to --tmux flag
Browse files Browse the repository at this point in the history
This commit adds the `native-border` resulting in
the following:
```
--tmux[=[center|top|bottom|left|right][,SIZE[%]][,SIZE[%]][,native-border]]
```

By default, when not specified, the `-B` flag is passed to the
`tmux popup-window` command such that no border is drawn around
the tmux popup window.

When the `native-border` option is present the `-B` flag is omitted
and the popup window is drawn using the border style configured in
the tmux config file.

Fixes #4156

Signed-off-by: Andreas Auernhammer <[email protected]>
  • Loading branch information
aead committed Jan 3, 2025
1 parent fb3bf6c commit dc73b93
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ADVANCED.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fzf --height 70% --tmux 70%
You can also specify the position, width, and height of the popup window in
the following format:

* `[center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]`
* `[center|top|bottom|left|right][,SIZE[%]][,SIZE[%][,border-native]]`

```sh
# 100% width and 60% height
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ fzf --height -3
With `--tmux` option, fzf will start in a tmux popup.
```sh
# --tmux [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]
# --tmux [center|top|bottom|left|right][,SIZE[%]][,SIZE[%][,border-native]]
fzf --tmux center # Center, 50% width and height
fzf --tmux 80% # Center, 80% width and height
Expand Down
8 changes: 6 additions & 2 deletions man/man1/fzf.1
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ Adaptive height has the following limitations:
Minimum height when \fB\-\-height\fR is given in percent (default: 10).
Ignored when \fB\-\-height\fR is not specified.
.TP
.BI "\-\-tmux" "[=[center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]]"
.BI "\-\-tmux" "[=[center|top|bottom|left|right][,SIZE[%]][,SIZE[%]][,border-native]]"
Start fzf in a tmux popup (default \fBcenter,50%\fR). Requires tmux 3.3 or
later. This option is ignored if you are not running fzf inside tmux.

Expand All @@ -286,7 +286,11 @@ e.g.
fzf \-\-tmux bottom,30%

# Popup on the top with 80% width and 40% height
fzf \-\-tmux top,80%,40%\fR
fzf \-\-tmux top,80%,40%

# Popup with a native window border in the center
# with 80% width and 50% height.
fzf \-\-tmux center,80%,50%,border-native\fR

.TP
.BI "\-\-layout=" "LAYOUT"
Expand Down
26 changes: 20 additions & 6 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"regexp"
"slices"
"strconv"
"strings"
"unicode"
Expand Down Expand Up @@ -254,6 +255,7 @@ type tmuxOptions struct {
height sizeSpec
position windowPosition
index int
border bool
}

type layoutType int
Expand Down Expand Up @@ -309,15 +311,16 @@ func defaultTmuxOptions(index int) *tmuxOptions {
position: posCenter,
width: sizeSpec{50, true},
height: sizeSpec{50, true},
index: index}
index: index,
}
}

func parseTmuxOptions(arg string, index int) (*tmuxOptions, error) {
var err error
opts := defaultTmuxOptions(index)
tokens := splitRegexp.Split(arg, -1)
errorToReturn := errors.New("invalid tmux option: " + arg + " (expected: [center|top|bottom|left|right][,SIZE[%]][,SIZE[%]])")
if len(tokens) == 0 || len(tokens) > 3 {
errorToReturn := errors.New("invalid tmux option: " + arg + " (expected: [center|top|bottom|left|right][,SIZE[%]][,SIZE[%][,border-native]])")
if len(tokens) == 0 || len(tokens) > 4 {
return nil, errorToReturn
}

Expand All @@ -340,6 +343,15 @@ func parseTmuxOptions(arg string, index int) (*tmuxOptions, error) {
tokens = append([]string{"center"}, tokens...)
}

if i := slices.Index(tokens, "border-native"); i >= 0 {
// The border option, if present, is the last one
if i != len(tokens)-1 {
return nil, errorToReturn
}
tokens = slices.Delete(tokens, i, i+1)
opts.border = true
}

// One size given
var size1 sizeSpec
if len(tokens) > 1 {
Expand Down Expand Up @@ -650,7 +662,8 @@ func defaultOptions() *Options {
WalkerRoot: []string{"."},
WalkerSkip: []string{".git", "node_modules"},
Help: false,
Version: false}
Version: false,
}
}

func optString(arg string, prefixes ...string) (bool, string) {
Expand Down Expand Up @@ -1725,7 +1738,7 @@ func strLines(str string) []string {
}

func parseSize(str string, maxPercent float64, label string) (sizeSpec, error) {
var spec = sizeSpec{}
spec := sizeSpec{}
var val float64
var err error
percent := strings.HasSuffix(str, "%")
Expand Down Expand Up @@ -1811,7 +1824,8 @@ func parseInfoStyle(str string) (infoStyle, string, error) {
}
for _, spec := range []infoSpec{
{"inline", infoInline},
{"inline-right", infoInlineRight}} {
{"inline-right", infoInlineRight},
} {
if strings.HasPrefix(str, spec.name+":") {
return spec.style, strings.ReplaceAll(str[len(spec.name)+1:], "\n", " "), nil
}
Expand Down
5 changes: 4 additions & 1 deletion src/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func runTmux(args []string, opts *Options) (int, error) {
// M Both The mouse position
// W Both The window position on the status line
// S -y The line above or below the status line
tmuxArgs := []string{"display-popup", "-E", "-B", "-d", dir}
tmuxArgs := []string{"display-popup", "-E", "-d", dir}
if !opts.Tmux.border {
tmuxArgs = append(tmuxArgs, "-B")
}
switch opts.Tmux.position {
case posUp:
tmuxArgs = append(tmuxArgs, "-xC", "-y0")
Expand Down

0 comments on commit dc73b93

Please sign in to comment.