From 6a75e30941af50a7197ef5e96c07a8605aec9a53 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 24 Jan 2017 00:23:16 +0900 Subject: [PATCH] Allow invisible preview window (--preview-window 0) Close #820 --- CHANGELOG.md | 2 ++ man/man1/fzf.1 | 3 +++ src/options.go | 2 +- src/terminal.go | 32 +++++++++++++++++++------------- test/test_go.rb | 13 +++++++++++++ 5 files changed, 38 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6df93e..db5182b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ CHANGELOG - Added support for composite actions in `--bind`. Multiple actions can be chained using `+` separator. - e.g. `fzf --bind 'ctrl-y:execute(echo -n {} | pbcopy)+abort'` +- `--preview-window` with size 0 is allowed. This is used to make fzf execute + preview command in the background without displaying the result. 0.16.1 ------ diff --git a/man/man1/fzf.1 b/man/man1/fzf.1 index ff5be5e..3ac07a4 100644 --- a/man/man1/fzf.1 +++ b/man/man1/fzf.1 @@ -278,6 +278,9 @@ Determine the layout of the preview window. If the argument ends with \fBtoggle-preview\fR action is triggered. Long lines are truncated by default. Line wrap can be enabled with \fB:wrap\fR flag. +If size is given as 0, preview window will not be visible, but fzf will still +execute the command in the background. + .RS .B POSITION: (default: right) \fBup diff --git a/src/options.go b/src/options.go index eb21ee7..2e96c2c 100644 --- a/src/options.go +++ b/src/options.go @@ -823,7 +823,7 @@ func parsePreviewWindow(opts *previewOpts, input string) { opts.wrap = false tokens := strings.Split(input, ":") - sizeRegex := regexp.MustCompile("^[1-9][0-9]*%?$") + sizeRegex := regexp.MustCompile("^[0-9]+%?$") for _, token := range tokens { switch token { case "hidden": diff --git a/src/terminal.go b/src/terminal.go index 0786b95..d06f752 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -509,9 +509,11 @@ func (t *Terminal) resizeWindows() { } } } + + previewVisible := t.isPreviewEnabled() && t.preview.size.size > 0 minAreaWidth := minWidth minAreaHeight := minHeight - if t.isPreviewEnabled() { + if previewVisible { switch t.preview.position { case posUp, posDown: minAreaHeight *= 2 @@ -531,7 +533,7 @@ func (t *Terminal) resizeWindows() { width := screenWidth - marginInt[1] - marginInt[3] height := screenHeight - marginInt[0] - marginInt[2] - if t.isPreviewEnabled() { + if previewVisible { createPreviewWindow := func(y int, x int, w int, h int) { t.bwindow = t.tui.NewWindow(y, x, w, h, true) pwidth := w - 4 @@ -889,7 +891,7 @@ func numLinesMax(str string, max int) int { } func (t *Terminal) printPreview() { - if !t.isPreviewEnabled() { + if !t.hasPreviewWindow() { return } t.pwindow.Erase() @@ -974,7 +976,7 @@ func (t *Terminal) printAll() { func (t *Terminal) refresh() { if !t.suppress { - if t.isPreviewEnabled() { + if t.hasPreviewWindow() { t.tui.RefreshWindows([]tui.Window{t.bwindow, t.pwindow, t.window}) } else { t.tui.RefreshWindows([]tui.Window{t.window}) @@ -1107,12 +1109,16 @@ func (t *Terminal) executeCommand(template string, items []*Item) { t.refresh() } -func (t *Terminal) hasPreviewWindow() bool { +func (t *Terminal) hasPreviewer() bool { return t.previewBox != nil } func (t *Terminal) isPreviewEnabled() bool { - return t.previewBox != nil && t.previewer.enabled + return t.hasPreviewer() && t.previewer.enabled +} + +func (t *Terminal) hasPreviewWindow() bool { + return t.pwindow != nil && t.isPreviewEnabled() } func (t *Terminal) currentItem() *Item { @@ -1174,7 +1180,7 @@ func (t *Terminal) Loop() { }() } - if t.hasPreviewWindow() { + if t.hasPreviewer() { go func() { for { var request *Item @@ -1358,7 +1364,7 @@ func (t *Terminal) Loop() { t.mutex.Unlock() return false case actTogglePreview: - if t.hasPreviewWindow() { + if t.hasPreviewer() { t.previewer.enabled = !t.previewer.enabled t.tui.Clear() t.resizeWindows() @@ -1374,19 +1380,19 @@ func (t *Terminal) Loop() { t.mutex.Unlock() return false case actPreviewUp: - if t.isPreviewEnabled() { + if t.hasPreviewWindow() { scrollPreview(-1) } case actPreviewDown: - if t.isPreviewEnabled() { + if t.hasPreviewWindow() { scrollPreview(1) } case actPreviewPageUp: - if t.isPreviewEnabled() { + if t.hasPreviewWindow() { scrollPreview(-t.pwindow.Height()) } case actPreviewPageDown: - if t.isPreviewEnabled() { + if t.hasPreviewWindow() { scrollPreview(t.pwindow.Height()) } case actBeginningOfLine: @@ -1563,7 +1569,7 @@ func (t *Terminal) Loop() { } t.vmove(me.S) req(reqList) - } else if t.isPreviewEnabled() && t.pwindow.Enclose(my, mx) { + } else if t.hasPreviewWindow() && t.pwindow.Enclose(my, mx) { scrollPreview(-me.S) } } else if t.window.Enclose(my, mx) { diff --git a/test/test_go.rb b/test/test_go.rb index 6f2deb1..cdd96d1 100644 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -1224,6 +1224,19 @@ class TestGoFZF < TestBase tmux.send_keys '?' tmux.until { |lines| lines[-1] == '> 555' } end + + def test_preview_size_0 + File.unlink tempname rescue nil + tmux.send_keys %[seq 100 | #{FZF} --reverse --preview 'echo {} >> #{tempname}; echo ' --preview-window 0], :Enter + tmux.until { |lines| lines.item_count == 100 && lines[1] == ' 100/100' && lines[2] == '> 1' } + tmux.until { |_| %w[1] == File.readlines(tempname).map(&:chomp) } + tmux.send_keys :Down + tmux.until { |lines| lines[3] == '> 2' } + tmux.until { |_| %w[1 2] == File.readlines(tempname).map(&:chomp) } + tmux.send_keys :Down + tmux.until { |lines| lines[4] == '> 3' } + tmux.until { |_| %w[1 2 3] == File.readlines(tempname).map(&:chomp) } + end end module TestShell