Always execute preview command if {q} is in the template

Even when {q} is empty. Because, why not?

While this can be seen as a breaking change, there is an easy workaround
to keep the old behavior.

    # This will show // even when the query is empty
    : | fzf --preview 'echo /{q}/'

    # But if you don't want it,
    : | fzf --preview '[ -n {q} ] || exit; echo /{q}/'

Close #2759
pull/3110/head
Junegunn Choi 1 year ago
parent 6c37177cf5
commit d649f5d826
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

@ -68,12 +68,22 @@ CHANGELOG
# Can only type numbers # Can only type numbers
fzf --bind 'change:transform-query(sed 's/[^0-9]//g' <<< {q})' fzf --bind 'change:transform-query(sed 's/[^0-9]//g' <<< {q})'
``` ```
- Improvements
- `put` action can optionally take an argument string - `put` action can optionally take an argument string
```sh ```sh
# a will put 'alpha' on the prompt, ctrl-b will put 'bravo' # a will put 'alpha' on the prompt, ctrl-b will put 'bravo'
fzf --bind 'a:put+put(lpha),ctrl-b:put(bravo)' fzf --bind 'a:put+put(lpha),ctrl-b:put(bravo)'
``` ```
- Behavior changes
- fzf will always execute the preview command if the command template
contains `{q}` even when it's empty. If you prefer the old behavior,
you'll have to check if `{q}` is empty in your command.
```sh
# This will show // even when the query is empty
: | fzf --preview 'echo /{q}/'
# But if you don't want it,
: | fzf --preview '[ -n {q} ] || exit; echo /{q}/'
```
- `double-click` will behave the same as `enter` unless otherwise specified, - `double-click` will behave the same as `enter` unless otherwise specified,
so you don't have to repeat the same action twice in `--bind` in most cases. so you don't have to repeat the same action twice in `--bind` in most cases.
```sh ```sh

@ -536,7 +536,8 @@ e.g.
Note that you can escape a placeholder pattern by prepending a backslash. Note that you can escape a placeholder pattern by prepending a backslash.
Preview window will be updated even when there is no match for the current Preview window will be updated even when there is no match for the current
query if any of the placeholder expressions evaluates to a non-empty string. query if any of the placeholder expressions evaluates to a non-empty string
or \fB{q}\fR is in the command template.
Since 0.24.0, fzf can render partial preview content before the preview command Since 0.24.0, fzf can render partial preview content before the preview command
completes. ANSI escape sequence for clearing the display (\fBCSI 2 J\fR) is completes. ANSI escape sequence for clearing the display (\fBCSI 2 J\fR) is

@ -2096,7 +2096,7 @@ func (t *Terminal) currentItem() *Item {
func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item) { func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item) {
current := t.currentItem() current := t.currentItem()
slot, plus, query := hasPreviewFlags(template) slot, plus, query := hasPreviewFlags(template)
if !(!slot || query && len(t.input) > 0 || (forcePlus || plus) && len(t.selected) > 0) { if !(!slot || query || (forcePlus || plus) && len(t.selected) > 0) {
return current != nil, []*Item{current, current} return current != nil, []*Item{current, current}
} }

@ -1555,13 +1555,13 @@ class TestGoFZF < TestBase
end end
def test_preview_q_no_match def test_preview_q_no_match
tmux.send_keys %(: | #{FZF} --preview 'echo foo {q}'), :Enter tmux.send_keys %(: | #{FZF} --preview 'echo foo {q} foo'), :Enter
tmux.until { |lines| assert_equal 0, lines.match_count } tmux.until { |lines| assert_equal 0, lines.match_count }
tmux.until { |lines| refute_includes lines[1], ' foo ' } tmux.until { |lines| assert_includes lines[1], ' foo foo' }
tmux.send_keys 'bar' tmux.send_keys 'bar'
tmux.until { |lines| assert_includes lines[1], ' foo bar ' } tmux.until { |lines| assert_includes lines[1], ' foo bar foo' }
tmux.send_keys 'C-u' tmux.send_keys 'C-u'
tmux.until { |lines| refute_includes lines[1], ' foo ' } tmux.until { |lines| assert_includes lines[1], ' foo foo' }
end end
def test_preview_q_no_match_with_initial_query def test_preview_q_no_match_with_initial_query

Loading…
Cancel
Save