Allow invisible preview window (--preview-window 0)

Close #820
This commit is contained in:
Junegunn Choi 2017-01-24 00:23:16 +09:00
parent a3244c4892
commit 6a75e30941
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
5 changed files with 38 additions and 14 deletions

View File

@ -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
------

View File

@ -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

View File

@ -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":

View File

@ -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) {

View File

@ -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