From aa2b9ec47652e84f320dad512c179feb46c8364c Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 31 Jan 2023 17:33:53 +0900 Subject: [PATCH] Add 'show-preview' and 'hide-preview' For cases where 'toggle-preview' is not enough --- CHANGELOG.md | 5 ++++- man/man1/fzf.1 | 2 ++ src/options.go | 4 ++++ src/terminal.go | 15 +++++++++++++-- test/test_go.rb | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0378ba1e..dc11c49b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,11 @@ CHANGELOG ========= -0.37.1 +0.38.0 ------ +- New actions + - `show-preview` + - `hide-preview` - Bug fixes - `--preview-window 0,hidden` should not execute the preview command until `toggle-preview` action is triggered diff --git a/man/man1/fzf.1 b/man/man1/fzf.1 index 299c30e1..3ce32bec 100644 --- a/man/man1/fzf.1 +++ b/man/man1/fzf.1 @@ -1036,6 +1036,7 @@ A key or an event can be bound to one or more of the following actions. \fBpage-up\fR \fIpgup\fR \fBhalf-page-down\fR \fBhalf-page-up\fR + \fBhide-preview\fR \fBpos(...)\fR (move cursor to the numeric position; negative number to count from the end) \fBprev-history\fR (\fIctrl-p\fR on \fB--history\fR) \fBprev-selected\fR (move to the previous selected item) @@ -1058,6 +1059,7 @@ A key or an event can be bound to one or more of the following actions. \fBreplace-query\fR (replace query string with the current selection) \fBselect\fR \fBselect-all\fR (select all matches) + \fBshow-preview\fR \fBtoggle\fR (\fIright-click\fR) \fBtoggle-all\fR (toggle all matches) \fBtoggle+down\fR \fIctrl-i (tab)\fR diff --git a/src/options.go b/src/options.go index 86a168c0..e30ba2c5 100644 --- a/src/options.go +++ b/src/options.go @@ -1111,6 +1111,10 @@ func parseActionList(masked string, original string, prevActions []*action, putA appendAction(actPrevSelected) case "next-selected": appendAction(actNextSelected) + case "show-preview": + appendAction(actShowPreview) + case "hide-preview": + appendAction(actHidePreview) case "toggle-preview": appendAction(actTogglePreview) case "toggle-preview-wrap": diff --git a/src/terminal.go b/src/terminal.go index 8308f63d..b95686ca 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -350,6 +350,8 @@ const ( actRefreshPreview actReplaceQuery actToggleSort + actShowPreview + actHidePreview actTogglePreview actTogglePreviewWrap actTransformBorderLabel @@ -2866,8 +2868,17 @@ func (t *Terminal) Loop() { case actInvalid: t.mutex.Unlock() return false - case actTogglePreview: - if t.hasPreviewWindow() || len(t.previewOpts.command) > 0 { + case actTogglePreview, actShowPreview, actHidePreview: + var act bool + switch a.t { + case actShowPreview: + act = !t.hasPreviewWindow() && len(t.previewOpts.command) > 0 + case actHidePreview: + act = t.hasPreviewWindow() + case actTogglePreview: + act = t.hasPreviewWindow() || len(t.previewOpts.command) > 0 + } + if act { t.activePreviewOpts.Toggle() updatePreviewWindow(false) if t.isPreviewEnabled() { diff --git a/test/test_go.rb b/test/test_go.rb index 9d95a0a1..1816191d 100755 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -1495,6 +1495,45 @@ class TestGoFZF < TestBase end end + def test_show_and_hide_preview + tmux.send_keys %(seq 100 | #{FZF} --preview-window hidden,border-bold --preview 'echo [{}]' --bind 'a:show-preview,b:hide-preview'), :Enter + + # Hidden by default + tmux.until do |lines| + assert_equal 100, lines.match_count + refute_includes lines[1], '┃ [1]' + end + + # Show + tmux.send_keys :a + tmux.until { |lines| assert_includes lines[1], '┃ [1]' } + + # Already shown + tmux.send_keys :a + tmux.send_keys :Up + tmux.until { |lines| assert_includes lines[1], '┃ [2]' } + + # Hide + tmux.send_keys :b + tmux.send_keys :Up + tmux.until do |lines| + assert_includes lines, '> 3' + refute_includes lines[1], '┃ [3]' + end + + # Already hidden + tmux.send_keys :b + tmux.send_keys :Up + tmux.until do |lines| + assert_includes lines, '> 4' + refute_includes lines[1], '┃ [4]' + end + + # Show it again + tmux.send_keys :a + tmux.until { |lines| assert_includes lines[1], '┃ [4]' } + end + def test_preview_hidden tmux.send_keys %(seq 1000 | #{FZF} --preview 'echo {{}-{}-$FZF_PREVIEW_LINES-$FZF_PREVIEW_COLUMNS}' --preview-window down:1:hidden --bind ?:toggle-preview), :Enter tmux.until { |lines| assert_equal '>', lines[-1] }