Allow toggling of alternative preview window layout that is hidden

Fix #3113
This commit is contained in:
Junegunn Choi 2023-01-07 11:21:52 +09:00
parent 3b2244077d
commit 23d8b78ce1
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
3 changed files with 107 additions and 71 deletions

View File

@ -207,6 +207,14 @@ type previewOpts struct {
alternative *previewOpts alternative *previewOpts
} }
func (o *previewOpts) Visible() bool {
return o.size.size > 0 || o.alternative != nil && o.alternative.size.size > 0
}
func (o *previewOpts) Toggle() {
o.hidden = !o.hidden
}
func parseLabelPosition(opts *labelOpts, arg string) { func parseLabelPosition(opts *labelOpts, arg string) {
opts.column = 0 opts.column = 0
opts.bottom = false opts.bottom = false

View File

@ -195,6 +195,7 @@ type Terminal struct {
reqBox *util.EventBox reqBox *util.EventBox
initialPreviewOpts previewOpts initialPreviewOpts previewOpts
previewOpts previewOpts previewOpts previewOpts
activePreviewOpts *previewOpts
previewer previewer previewer previewer
previewed previewed previewed previewed
previewBox *util.EventBox previewBox *util.EventBox
@ -241,7 +242,6 @@ const (
reqJump reqJump
reqRefresh reqRefresh
reqReinit reqReinit
reqRedraw
reqFullRedraw reqFullRedraw
reqClose reqClose
reqPrintQuery reqPrintQuery
@ -494,7 +494,6 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
delay = initialDelay delay = initialDelay
} }
var previewBox *util.EventBox var previewBox *util.EventBox
showPreviewWindow := len(opts.Preview.command) > 0 && !opts.Preview.hidden
// We need to start previewer if HTTP server is enabled even when --preview option is not specified // We need to start previewer if HTTP server is enabled even when --preview option is not specified
if len(opts.Preview.command) > 0 || hasPreviewAction(opts) || opts.ListenPort > 0 { if len(opts.Preview.command) > 0 || hasPreviewAction(opts) || opts.ListenPort > 0 {
previewBox = util.NewEventBox() previewBox = util.NewEventBox()
@ -602,7 +601,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
reqBox: util.NewEventBox(), reqBox: util.NewEventBox(),
initialPreviewOpts: opts.Preview, initialPreviewOpts: opts.Preview,
previewOpts: opts.Preview, previewOpts: opts.Preview,
previewer: previewer{0, []string{}, 0, showPreviewWindow, false, true, false, "", []bool{}}, previewer: previewer{0, []string{}, 0, len(opts.Preview.command) > 0, false, true, false, "", []bool{}},
previewed: previewed{0, 0, 0, false}, previewed: previewed{0, 0, 0, false},
previewBox: previewBox, previewBox: previewBox,
eventBox: eventBox, eventBox: eventBox,
@ -985,7 +984,7 @@ func (t *Terminal) adjustMarginAndPadding() (int, int, [4]int, [4]int) {
if t.noInfoLine() { if t.noInfoLine() {
minAreaHeight -= 1 minAreaHeight -= 1
} }
if t.isPreviewVisible() { if t.mayNeedPreviewWindow() {
minPreviewHeight := 1 + borderLines(t.previewOpts.border) minPreviewHeight := 1 + borderLines(t.previewOpts.border)
minPreviewWidth := 5 minPreviewWidth := 5
switch t.previewOpts.position { switch t.previewOpts.position {
@ -1003,7 +1002,7 @@ func (t *Terminal) adjustMarginAndPadding() (int, int, [4]int, [4]int) {
return screenWidth, screenHeight, marginInt, paddingInt return screenWidth, screenHeight, marginInt, paddingInt
} }
func (t *Terminal) resizeWindows() { func (t *Terminal) resizeWindows(forcePreview bool) {
screenWidth, screenHeight, marginInt, paddingInt := t.adjustMarginAndPadding() screenWidth, screenHeight, marginInt, paddingInt := t.adjustMarginAndPadding()
width := screenWidth - marginInt[1] - marginInt[3] width := screenWidth - marginInt[1] - marginInt[3]
height := screenHeight - marginInt[0] - marginInt[2] height := screenHeight - marginInt[0] - marginInt[2]
@ -1067,9 +1066,13 @@ func (t *Terminal) resizeWindows() {
// Set up preview window // Set up preview window
noBorder := tui.MakeBorderStyle(tui.BorderNone, t.unicode) noBorder := tui.MakeBorderStyle(tui.BorderNone, t.unicode)
if t.isPreviewVisible() { if t.mayNeedPreviewWindow() {
var resizePreviewWindows func(previewOpts previewOpts) var resizePreviewWindows func(previewOpts *previewOpts)
resizePreviewWindows = func(previewOpts previewOpts) { resizePreviewWindows = func(previewOpts *previewOpts) {
t.activePreviewOpts = previewOpts
if previewOpts.size.size == 0 {
return
}
hasThreshold := previewOpts.threshold > 0 && previewOpts.alternative != nil hasThreshold := previewOpts.threshold > 0 && previewOpts.alternative != nil
createPreviewWindow := func(y int, x int, w int, h int) { createPreviewWindow := func(y int, x int, w int, h int) {
pwidth := w pwidth := w
@ -1124,9 +1127,19 @@ func (t *Terminal) resizeWindows() {
case posUp, posDown: case posUp, posDown:
pheight := calculateSize(height, previewOpts.size, minHeight, minPreviewHeight, verticalPad) pheight := calculateSize(height, previewOpts.size, minHeight, minPreviewHeight, verticalPad)
if hasThreshold && pheight < previewOpts.threshold { if hasThreshold && pheight < previewOpts.threshold {
if !previewOpts.alternative.hidden { t.activePreviewOpts = previewOpts.alternative
resizePreviewWindows(*previewOpts.alternative) if forcePreview {
previewOpts.alternative.hidden = false
} }
if !previewOpts.alternative.hidden {
resizePreviewWindows(previewOpts.alternative)
}
return
}
if forcePreview {
previewOpts.hidden = false
}
if previewOpts.hidden {
return return
} }
// Put scrollbar closer to the right border for consistent look // Put scrollbar closer to the right border for consistent look
@ -1145,9 +1158,19 @@ func (t *Terminal) resizeWindows() {
case posLeft, posRight: case posLeft, posRight:
pwidth := calculateSize(width, previewOpts.size, minWidth, 5, 4) pwidth := calculateSize(width, previewOpts.size, minWidth, 5, 4)
if hasThreshold && pwidth < previewOpts.threshold { if hasThreshold && pwidth < previewOpts.threshold {
if !previewOpts.alternative.hidden { t.activePreviewOpts = previewOpts.alternative
resizePreviewWindows(*previewOpts.alternative) if forcePreview {
previewOpts.alternative.hidden = false
} }
if !previewOpts.alternative.hidden {
resizePreviewWindows(previewOpts.alternative)
}
return
}
if forcePreview {
previewOpts.hidden = false
}
if previewOpts.hidden {
return return
} }
if previewOpts.position == posLeft { if previewOpts.position == posLeft {
@ -1171,7 +1194,7 @@ func (t *Terminal) resizeWindows() {
} }
} }
} }
resizePreviewWindows(t.previewOpts) resizePreviewWindows(&t.previewOpts)
} }
// Without preview window // Without preview window
@ -1857,7 +1880,7 @@ func (t *Terminal) processTabs(runes []rune, prefixWidth int) (string, int) {
} }
func (t *Terminal) printAll() { func (t *Terminal) printAll() {
t.resizeWindows() t.resizeWindows(false)
t.printList() t.printList()
t.printPrompt() t.printPrompt()
t.printInfo() t.printInfo()
@ -2142,10 +2165,8 @@ func replacePlaceholder(template string, stripAnsi bool, delimiter Delimiter, pr
}) })
} }
func (t *Terminal) redraw(clear bool) { func (t *Terminal) redraw() {
if clear {
t.tui.Clear() t.tui.Clear()
}
t.tui.Refresh() t.tui.Refresh()
t.printAll() t.printAll()
} }
@ -2168,7 +2189,7 @@ func (t *Terminal) executeCommand(template string, forcePlus bool, background bo
t.tui.Pause(true) t.tui.Pause(true)
cmd.Run() cmd.Run()
t.tui.Resume(true, false) t.tui.Resume(true, false)
t.redraw(true) t.redraw()
t.refresh() t.refresh()
} else { } else {
t.tui.Pause(false) t.tui.Pause(false)
@ -2193,16 +2214,17 @@ func (t *Terminal) hasPreviewer() bool {
return t.previewBox != nil return t.previewBox != nil
} }
func (t *Terminal) isPreviewEnabled() bool { func (t *Terminal) mayNeedPreviewWindow() bool {
return t.hasPreviewer() && t.previewer.enabled return t.hasPreviewer() && t.previewer.enabled && t.previewOpts.Visible()
} }
func (t *Terminal) isPreviewVisible() bool { // Check if previewer is currently in action (invisible previewer with size 0 or visible previewer)
return t.isPreviewEnabled() && t.previewOpts.size.size > 0 func (t *Terminal) isPreviewEnabled() bool {
return t.hasPreviewer() && t.previewer.enabled && (!t.previewOpts.Visible() || t.pwindow != nil)
} }
func (t *Terminal) hasPreviewWindow() bool { func (t *Terminal) hasPreviewWindow() bool {
return t.pwindow != nil && t.isPreviewEnabled() return t.pwindow != nil
} }
func (t *Terminal) currentItem() *Item { func (t *Terminal) currentItem() *Item {
@ -2306,7 +2328,7 @@ func (t *Terminal) Loop() {
pad := fitpad.pad pad := fitpad.pad
t.tui.Resize(func(termHeight int) int { t.tui.Resize(func(termHeight int) int {
contentHeight := fit + t.extraLines() contentHeight := fit + t.extraLines()
if t.hasPreviewer() { if t.mayNeedPreviewWindow() {
if t.previewOpts.aboveOrBelow() { if t.previewOpts.aboveOrBelow() {
if t.previewOpts.size.percent { if t.previewOpts.size.percent {
newContentHeight := int(float64(contentHeight) * 100. / (100. - t.previewOpts.size.size)) newContentHeight := int(float64(contentHeight) * 100. / (100. - t.previewOpts.size.size))
@ -2354,7 +2376,7 @@ func (t *Terminal) Loop() {
t.mutex.Lock() t.mutex.Lock()
t.initFunc() t.initFunc()
t.resizeWindows() t.resizeWindows(false)
t.printPrompt() t.printPrompt()
t.printInfo() t.printInfo()
t.printHeader() t.printHeader()
@ -2598,11 +2620,9 @@ func (t *Terminal) Loop() {
t.suppress = false t.suppress = false
case reqReinit: case reqReinit:
t.tui.Resume(t.fullscreen, t.sigstop) t.tui.Resume(t.fullscreen, t.sigstop)
t.redraw(true) t.redraw()
case reqRedraw:
t.redraw(false)
case reqFullRedraw: case reqFullRedraw:
t.redraw(true) t.redraw()
case reqClose: case reqClose:
exit(func() int { exit(func() int {
if t.output() { if t.output() {
@ -2701,15 +2721,9 @@ func (t *Terminal) Loop() {
} }
} }
} }
togglePreview := func(enabled bool) bool { updatePreviewWindow := func(forcePreview bool) {
if t.previewer.enabled != enabled { t.resizeWindows(forcePreview)
t.previewer.enabled = enabled
// We need to immediately update t.pwindow so we don't use reqRedraw
t.resizeWindows()
req(reqPrompt, reqList, reqInfo, reqHeader) req(reqPrompt, reqList, reqInfo, reqHeader)
return true
}
return false
} }
toggle := func() bool { toggle := func() bool {
current := t.currentItem() current := t.currentItem()
@ -2773,8 +2787,13 @@ func (t *Terminal) Loop() {
return false return false
case actTogglePreview: case actTogglePreview:
if t.hasPreviewer() { if t.hasPreviewer() {
togglePreview(!t.previewer.enabled) if t.activePreviewOpts != nil {
if t.previewer.enabled { t.activePreviewOpts.Toggle()
} else if !t.previewOpts.Visible() {
t.previewer.enabled = !t.previewer.enabled
}
updatePreviewWindow(false)
if t.isPreviewEnabled() {
valid, list := t.buildPlusList(t.previewOpts.command, false, false) valid, list := t.buildPlusList(t.previewOpts.command, false, false)
if valid { if valid {
t.cancelPreview() t.cancelPreview()
@ -2848,7 +2867,8 @@ func (t *Terminal) Loop() {
t.prompt, t.promptLen = t.parsePrompt(a.a) t.prompt, t.promptLen = t.parsePrompt(a.a)
req(reqPrompt) req(reqPrompt)
case actPreview: case actPreview:
togglePreview(true) t.previewer.enabled = true
updatePreviewWindow(true)
refreshPreview(a.a) refreshPreview(a.a)
case actRefreshPreview: case actRefreshPreview:
refreshPreview(t.previewOpts.command) refreshPreview(t.previewOpts.command)
@ -2910,8 +2930,9 @@ func (t *Terminal) Loop() {
req(reqList, reqInfo) req(reqList, reqInfo)
} }
case actClose: case actClose:
if t.isPreviewEnabled() { if t.hasPreviewWindow() {
togglePreview(false) t.activePreviewOpts.Toggle()
updatePreviewWindow(false)
} else { } else {
req(reqQuit) req(reqQuit)
} }
@ -3258,7 +3279,8 @@ func (t *Terminal) Loop() {
} }
case actChangePreview: case actChangePreview:
if t.previewOpts.command != a.a { if t.previewOpts.command != a.a {
togglePreview(len(a.a) > 0) t.previewer.enabled = len(a.a) > 0
updatePreviewWindow(false)
t.previewOpts.command = a.a t.previewOpts.command = a.a
refreshPreview(t.previewOpts.command) refreshPreview(t.previewOpts.command)
} }
@ -3275,15 +3297,14 @@ func (t *Terminal) Loop() {
a.a = strings.Join(append(tokens[1:], tokens[0]), "|") a.a = strings.Join(append(tokens[1:], tokens[0]), "|")
} }
if t.previewOpts.hidden {
togglePreview(false)
} else {
// Full redraw // Full redraw
if !currentPreviewOpts.sameLayout(t.previewOpts) { if !currentPreviewOpts.sameLayout(t.previewOpts) {
if togglePreview(true) { wasHidden := t.pwindow == nil
updatePreviewWindow(false)
if wasHidden && t.pwindow != nil {
refreshPreview(t.previewOpts.command) refreshPreview(t.previewOpts.command)
} else { } else {
req(reqRedraw) req(reqPreviewRefresh)
} }
} else if !currentPreviewOpts.sameContentLayout(t.previewOpts) { } else if !currentPreviewOpts.sameContentLayout(t.previewOpts) {
t.previewed.version = 0 t.previewed.version = 0
@ -3294,7 +3315,6 @@ func (t *Terminal) Loop() {
if t.hasPreviewWindow() && currentPreviewOpts.scroll != t.previewOpts.scroll { if t.hasPreviewWindow() && currentPreviewOpts.scroll != t.previewOpts.scroll {
scrollPreviewTo(t.evaluateScrollOffset()) scrollPreviewTo(t.evaluateScrollOffset())
} }
}
case actNextSelected, actPrevSelected: case actNextSelected, actPrevSelected:
if len(t.selected) > 0 { if len(t.selected) > 0 {
total := t.merger.Length() total := t.merger.Length()

View File

@ -1502,7 +1502,7 @@ class TestGoFZF < TestBase
rescue StandardError rescue StandardError
nil nil
end end
tmux.send_keys %(seq 100 | #{FZF} --reverse --preview 'echo {} >> #{tempname}; echo ' --preview-window 0), :Enter tmux.send_keys %(seq 100 | #{FZF} --reverse --preview 'echo {} >> #{tempname}; echo ' --preview-window 0 --bind space:toggle-preview), :Enter
tmux.until do |lines| tmux.until do |lines|
assert_equal 100, lines.item_count assert_equal 100, lines.item_count
assert_equal ' 100/100', lines[1] assert_equal ' 100/100', lines[1]
@ -1512,17 +1512,17 @@ class TestGoFZF < TestBase
assert_path_exists tempname assert_path_exists tempname
assert_equal %w[1], File.readlines(tempname, chomp: true) assert_equal %w[1], File.readlines(tempname, chomp: true)
end end
tmux.send_keys :Down tmux.send_keys :Space, :Down, :Down
tmux.until { |lines| assert_equal '> 2', lines[3] }
wait do
assert_path_exists tempname
assert_equal %w[1 2], File.readlines(tempname, chomp: true)
end
tmux.send_keys :Down
tmux.until { |lines| assert_equal '> 3', lines[4] } tmux.until { |lines| assert_equal '> 3', lines[4] }
wait do wait do
assert_path_exists tempname assert_path_exists tempname
assert_equal %w[1 2 3], File.readlines(tempname, chomp: true) assert_equal %w[1], File.readlines(tempname, chomp: true)
end
tmux.send_keys :Space, :Down
tmux.until { |lines| assert_equal '> 4', lines[5] }
wait do
assert_path_exists tempname
assert_equal %w[1 3 4], File.readlines(tempname, chomp: true)
end end
end end
@ -2405,7 +2405,7 @@ class TestGoFZF < TestBase
end end
def test_start_event def test_start_event
tmux.send_keys 'seq 100 | fzf --multi --sync --preview-window border-none --bind "start:select-all+last+preview(echo welcome)"', :Enter tmux.send_keys 'seq 100 | fzf --multi --sync --preview-window hidden:border-none --bind "start:select-all+last+preview(echo welcome)"', :Enter
tmux.until do |lines| tmux.until do |lines|
assert_match(/>100.*welcome/, lines[0]) assert_match(/>100.*welcome/, lines[0])
assert_includes(lines[-2], '100/100 (100)') assert_includes(lines[-2], '100/100 (100)')
@ -2495,6 +2495,14 @@ class TestGoFZF < TestBase
tmux.until { |lines| assert_equal 100, lines.item_count } tmux.until { |lines| assert_equal 100, lines.item_count }
tmux.until { |lines| assert_equal 'hundred> yo', lines[-1] } tmux.until { |lines| assert_equal 'hundred> yo', lines[-1] }
end end
def test_toggle_alternative_preview_window
tmux.send_keys "seq 10 | #{FZF} --bind space:toggle-preview --preview-window '<100000(hidden,up,border-none)' --preview 'echo /{}/{}/'", :Enter
tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.until { |lines| refute_includes lines, '/1/1/' }
tmux.send_keys :Space
tmux.until { |lines| assert_includes lines, '/1/1/' }
end
end end
module TestShell module TestShell