Enhance --min-height option to take number followed by +

This commit is contained in:
Junegunn Choi 2025-01-28 18:34:12 +09:00
parent b83dd6c6b4
commit eb0257d48f
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
9 changed files with 71 additions and 14 deletions

View File

@ -53,6 +53,15 @@ CHANGELOG
# Load 'ps -ef' output on start and reload it on CTRL-R
fzf --bind 'start,ctrl-r:reload:ps -ef'
```
- `--min-height` option now takes a number followed by `+`, which tells fzf to show at least that many items in the list section. The default value is now changed to `10+`.
```sh
# You will only see the input section which takes 3 lines
fzf --style=full --height 1% --min-height 3
# You will see 3 items in the list section
fzf --style full --height 1% --min-height 3+
```
- Shell integration scripts were updated to use `--min-height 20+` by default
- Added `bell` action to ring the terminal bell
```sh
# Press CTRL-Y to copy the current line to the clipboard and ring the bell

View File

@ -336,9 +336,12 @@ Adaptive height has the following limitations:
* It will not find the right size when there are multi-line items
.TP
.BI "\-\-min\-height=" "HEIGHT"
Minimum height when \fB\-\-height\fR is given in percent (default: 10).
Ignored when \fB\-\-height\fR is not specified.
.BI "\-\-min\-height=" "HEIGHT[+]"
Minimum height when \fB\-\-height\fR is given as a percentage.
Add \fB+\fR to automatically increase the value according to the other
layout options so that the specified number of items are visible in the list
section (default: \fB10+\fR).
Ignored when \fB\-\-height\fR is not specified or set as an absolute value.
.TP
.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

View File

@ -37,7 +37,7 @@ bind '"\e[0n": redraw-current-line' 2> /dev/null
__fzf_defaults() {
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
echo "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
echo "${FZF_DEFAULT_OPTS-} $2"
}

View File

@ -99,7 +99,7 @@ if [[ -o interactive ]]; then
__fzf_defaults() {
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
echo "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
echo "${FZF_DEFAULT_OPTS-} $2"
}

View File

@ -20,7 +20,7 @@ if [[ $- =~ i ]]; then
__fzf_defaults() {
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
echo "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
echo "${FZF_DEFAULT_OPTS-} $2"
}

View File

@ -20,7 +20,7 @@ function fzf_key_bindings
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
test -n "$FZF_TMUX_HEIGHT"; or set FZF_TMUX_HEIGHT 40%
echo "--height $FZF_TMUX_HEIGHT --bind=ctrl-z:ignore" $argv[1]
echo "--height $FZF_TMUX_HEIGHT --min-height 20+ --bind=ctrl-z:ignore" $argv[1]
test -r "$FZF_DEFAULT_OPTS_FILE"; and string collect -N -- <$FZF_DEFAULT_OPTS_FILE
echo $FZF_DEFAULT_OPTS $argv[2]
end

View File

@ -41,7 +41,7 @@ if [[ -o interactive ]]; then
__fzf_defaults() {
# $1: Prepend to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
# $2: Append to FZF_DEFAULT_OPTS_FILE and FZF_DEFAULT_OPTS
echo "--height ${FZF_TMUX_HEIGHT:-40%} --bind=ctrl-z:ignore $1"
echo "--height ${FZF_TMUX_HEIGHT:-40%} --min-height 20+ --bind=ctrl-z:ignore $1"
command cat "${FZF_DEFAULT_OPTS_FILE-}" 2> /dev/null
echo "${FZF_DEFAULT_OPTS-} $2"
}

View File

@ -69,8 +69,9 @@ Usage: fzf [options]
minus the given value.
If prefixed with '~', fzf will determine the height
according to the input size.
--min-height=HEIGHT Minimum height for percent --height is given in percent
(default: 10 or above depending on the other options)
--min-height=HEIGHT[+] Minimum height when --height is given as a percentage.
Add '+' to automatically increase the value
according to the other layout options (default: 10+).
--tmux[=OPTS] Start fzf in a tmux popup (requires tmux 3.3+)
[center|top|bottom|left|right][,SIZE[%]][,SIZE[%]]
[,border-native] (default: center,50%)
@ -673,7 +674,7 @@ func defaultOptions() *Options {
Theme: theme,
Black: false,
Bold: true,
MinHeight: -1,
MinHeight: -10,
Layout: layoutDefault,
Cycle: false,
Wrap: false,
@ -2662,9 +2663,23 @@ func parseOptions(index *int, opts *Options, allArgs []string) error {
return err
}
case "--min-height":
if opts.MinHeight, err = nextInt("height required: HEIGHT"); err != nil {
expr, err := nextString("minimum height required: HEIGHT[+]")
if err != nil {
return err
}
auto := false
if strings.HasSuffix(expr, "+") {
expr = expr[:len(expr)-1]
auto = true
}
num, err := atoi(expr)
if err != nil || num < 0 {
return errors.New("minimum height must be a non-negative integer")
}
if auto {
num *= -1
}
opts.MinHeight = num
case "--no-height":
opts.Height = heightSpec{}
case "--no-margin":
@ -3220,8 +3235,7 @@ func postProcessOptions(opts *Options) error {
// Sets --min-height automatically
if opts.Height.size > 0 && opts.Height.percent && opts.MinHeight < 0 {
// 10 items and 1 prompt line
opts.MinHeight = 10 + 1 + borderLines(opts.BorderShape) + borderLines(opts.ListBorderShape) + borderLines(opts.InputBorderShape)
opts.MinHeight = -opts.MinHeight + 1 + borderLines(opts.BorderShape) + borderLines(opts.ListBorderShape) + borderLines(opts.InputBorderShape)
if len(opts.Header) > 0 {
opts.MinHeight += borderLines(opts.HeaderBorderShape) + len(opts.Header)
}

View File

@ -845,4 +845,35 @@ class TestLayout < TestInteractive
assert lines[1]&.end_with?('1000││')
end
end
def test_min_height_no_auto
tmux.send_keys %(seq 100 | #{FZF} --border sharp --style full:sharp --height 1% --min-height 5), :Enter
block = <<~BLOCK
>
BLOCK
tmux.until { assert_block(block, _1) }
end
def test_min_height_auto
tmux.send_keys %(seq 100 | #{FZF} --style full:sharp --height 1% --min-height 5+), :Enter
block = <<~BLOCK
5
4
3
2
> 1
>
BLOCK
tmux.until { assert_block(block, _1) }
end
end