Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting preview window's size to achieve a specific items list size #4314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions man/man1/fzf.1
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,9 @@ default until \fBtoggle\-preview\fR action is triggered.
* If size is given as 0, preview window will not be visible, but fzf will still
execute the command in the background.

* If size is -N (negative and % is not used), the preview window will occupy the
amount of space for which the entry list's size is N.

* Long lines are truncated by default. Line wrap can be enabled with
\fBwrap\fR flag.

Expand Down
13 changes: 5 additions & 8 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ type previewOpts struct {
}

func (o *previewOpts) Visible() bool {
return o.size.size > 0 || o.alternative != nil && o.alternative.size.size > 0
return o.size.size != 0 || o.alternative != nil && o.alternative.size.size > 0
}

func (o *previewOpts) Toggle() {
Expand Down Expand Up @@ -468,7 +468,7 @@ func parseLabelPosition(opts *labelOpts, arg string) error {
}

func (a previewOpts) aboveOrBelow() bool {
return a.size.size > 0 && (a.position == posUp || a.position == posDown)
return a.size.size != 0 && (a.position == posUp || a.position == posDown)
}

type previewOptsCompare int
Expand Down Expand Up @@ -1878,24 +1878,21 @@ func parseSize(str string, maxPercent float64, label string) (sizeSpec, error) {
}

if val < 0 {
return spec, errors.New(label + " must be non-negative")
return spec, errors.New(label + " (with %) must be non-negative")
}
if val > maxPercent {
return spec, fmt.Errorf("%s too large (max: %d%%)", label, int(maxPercent))
}
} else {
if strings.Contains(str, ".") {
return spec, errors.New(label + " (without %) must be a non-negative integer")
return spec, errors.New(label + " (without %) must be an integer")
}

i, err := atoi(str)
if err != nil {
return spec, err
}
val = float64(i)
if val < 0 {
return spec, errors.New(label + " must be non-negative")
}
}
return sizeSpec{val, percent}, nil
}
Expand Down Expand Up @@ -1969,7 +1966,7 @@ func parsePreviewWindow(opts *previewOpts, input string) error {
func parsePreviewWindowImpl(opts *previewOpts, input string) error {
var err error
tokenRegex := regexp.MustCompile(`[:,]*(<([1-9][0-9]*)\(([^)<]+)\)|[^,:]+)`)
sizeRegex := regexp.MustCompile("^[0-9]+%?$")
sizeRegex := regexp.MustCompile("^-?[0-9]+%?$")
offsetRegex := regexp.MustCompile(`^(\+{(-?[0-9]+|n)})?([+-][0-9]+)*(-?/[1-9][0-9]*)?$`)
headerRegex := regexp.MustCompile("^~(0|[1-9][0-9]*)$")
tokens := tokenRegex.FindAllStringSubmatch(input, -1)
Expand Down
9 changes: 9 additions & 0 deletions src/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,15 @@ func TestPreviewOpts(t *testing.T) {
opts.Preview.size.size == 15) {
t.Error(opts.Preview)
}
opts = optsFor("--preview-window=up:-15:wrap:hidden")
if !(opts.Preview.command == "" &&
opts.Preview.hidden == true &&
opts.Preview.wrap == true &&
opts.Preview.position == posUp &&
opts.Preview.size.percent == false &&
opts.Preview.size.size == -15) {
t.Error(opts.Preview)
}
opts = optsFor("--preview=foo", "--preview-window=up", "--preview-window=default:70%")
if !(opts.Preview.command == "foo" &&
opts.Preview.position == posRight &&
Expand Down
8 changes: 7 additions & 1 deletion src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,13 @@ func calculateSize(base int, size sizeSpec, occupied int, minSize int) int {
if size.percent {
return util.Constrain(int(float64(base)*0.01*size.size), minSize, max)
}
return util.Constrain(int(size.size)+minSize-1, minSize, max)
var unconstrained int
if size.size < 0 {
unconstrained = int(float64(base) + size.size) - occupied + 1
} else {
unconstrained = int(size.size) + minSize - 1
}
return util.Constrain(unconstrained, minSize, max)
}

func (t *Terminal) minPreviewSize(opts *previewOpts) (int, int) {
Expand Down
Loading