Skip to content

Commit

Permalink
Add 'show-preview' and 'hide-preview'
Browse files Browse the repository at this point in the history
For cases where 'toggle-preview' is not enough
  • Loading branch information
junegunn committed Jan 31, 2023
1 parent 3ee00f8 commit aa2b9ec
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions man/man1/fzf.1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
15 changes: 13 additions & 2 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ const (
actRefreshPreview
actReplaceQuery
actToggleSort
actShowPreview
actHidePreview
actTogglePreview
actTogglePreviewWrap
actTransformBorderLabel
Expand Down Expand Up @@ -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() {
Expand Down
39 changes: 39 additions & 0 deletions test/test_go.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,45 @@ def test_toggle_preview_without_default_preview_command
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] }
Expand Down

0 comments on commit aa2b9ec

Please sign in to comment.