Skip to content

Commit

Permalink
Add support for negative --height
Browse files Browse the repository at this point in the history
  fzf --height=-1

Close #3487
  • Loading branch information
junegunn committed Dec 21, 2023
1 parent 87fc1c8 commit d7b61ed
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
CHANGELOG
=========

0.44.2
0.45.0
------
- Added support for negative height
```sh
# Terminal height minus 1, so you can still see the command line
fzf --height=-1
```
- This handles a terminal resize better than `--height=$(($(tput lines) - 1))`
- Added `accept-or-print-query` action that acts like `accept` but prints the
current query when there's no match for the query
```sh
Expand Down
20 changes: 16 additions & 4 deletions man/man1/fzf.1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
..
.TH fzf 1 "Nov 2023" "fzf 0.44.1" "fzf - a command-line fuzzy finder"
.TH fzf 1 "Dec 2023" "fzf 0.45.0" "fzf - a command-line fuzzy finder"

.SH NAME
fzf - a command-line fuzzy finder
Expand Down Expand Up @@ -192,9 +192,21 @@ Label characters for \fBjump\fR and \fBjump-accept\fR
.TP
.BI "--height=" "[~]HEIGHT[%]"
Display fzf window below the cursor with the given height instead of using
the full screen. When prefixed with \fB~\fR, fzf will automatically determine
the height in the range according to the input size. Note that adaptive height
is not compatible with top/bottom margin and padding given in percent size.
the full screen.

If a negative value is specified, the height is calculated as the terminal
height minus the given value.

fzf --height=-1

When prefixed with \fB~\fR, fzf will automatically determine the height in the
range according to the input size. Note that adaptive height is not compatible
with top/bottom margin and padding given in percent size. It is also not
compatible with a negative height value.

# Will not take up 100% of the screen
seq 5 | fzf --height=~100%

.TP
.BI "--min-height=" "HEIGHT"
Minimum height when \fB--height\fR is given in percent (default: 10).
Expand Down
2 changes: 1 addition & 1 deletion src/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func Run(opts *Options, version string, revision string) {
padHeight := 0
heightUnknown := opts.Height.auto
if heightUnknown {
maxFit, padHeight = terminal.MaxFitAndPad(opts)
maxFit, padHeight = terminal.MaxFitAndPad()
}
deferred := opts.Select1 || opts.Exit0
go terminal.Loop()
Expand Down
10 changes: 10 additions & 0 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const usage = `usage: fzf [options]
Layout
--height=[~]HEIGHT[%] Display fzf window below the cursor with the given
height instead of using fullscreen.
A negative value is calcalated as the terminal height
minus the given value.
If prefixed with '~', fzf will determine the height
according to the input size.
--min-height=HEIGHT Minimum height when --height is given in percent
Expand Down Expand Up @@ -157,6 +159,7 @@ type heightSpec struct {
size float64
percent bool
auto bool
inverse bool
}

type sizeSpec struct {
Expand Down Expand Up @@ -1386,6 +1389,13 @@ func parseHeight(str string) heightSpec {
heightSpec.auto = true
str = str[1:]
}
if strings.HasPrefix(str, "-") {
if heightSpec.auto {
errorExit("negative(-) height is not compatible with adaptive(~) height")
}
heightSpec.inverse = true
str = str[1:]
}

size := parseSize(str, 100, "height")
heightSpec.size = size.size
Expand Down
13 changes: 10 additions & 3 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,17 @@ func makeSpinner(unicode bool) []string {
}

func evaluateHeight(opts *Options, termHeight int) int {
size := opts.Height.size
if opts.Height.percent {
return util.Max(int(opts.Height.size*float64(termHeight)/100.0), opts.MinHeight)
if opts.Height.inverse {
size = 100 - size
}
return util.Max(int(size*float64(termHeight)/100.0), opts.MinHeight)
}
if opts.Height.inverse {
size = float64(termHeight) - size
}
return int(opts.Height.size)
return int(size)
}

// NewTerminal returns new Terminal object
Expand Down Expand Up @@ -819,7 +826,7 @@ func (t *Terminal) extraLines() int {
return extra
}

func (t *Terminal) MaxFitAndPad(opts *Options) (int, int) {
func (t *Terminal) MaxFitAndPad() (int, int) {
_, screenHeight, marginInt, paddingInt := t.adjustMarginAndPadding()
padHeight := marginInt[0] + marginInt[2] + paddingInt[0] + paddingInt[2]
fit := screenHeight - padHeight - t.extraLines()
Expand Down

0 comments on commit d7b61ed

Please sign in to comment.