Hide separator next to preview window at 'next' position when decidable
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Decision must not depend on threshold chain resolution (computePreviewSize consults noSeparatorLine); separator is hidden only when every spec in the chain displays a 'next' preview window with a border line facing the input section. Otherwise err on the side of showing it. - Without active previewer, preview position is resolved from first spec alone, so 'next' in a threshold alternative does not create input window and cannot hide the separator - Empty preview command is undecidable; preview(...) action can display ad-hoc window at 'next' position at any time - change-preview-window relayouts or repaints when separator visibility changes even if the active spec is unchanged, without dropping preview window forced by preview(...) action
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+239 added−28 removed
- tests:+162 added−2 removed
- other:+77 added−26 removed
Provenance
- Commit
e58c5e6- Tree
8a0bd65- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
7fe4831
3 files changed+239 added−28 removed
modified:src/terminal.go+74 added-26 removed
1724 unmodified lines1725 }1726 return shape.HasTop()1727 }1728 1729-// The default horizontal separator is redundant if the input section is1730-// already visually separated from the list section by a border line1729+// Whether a border line is already drawn between the info line and the1730+// section next to it on the list side, making the default horizontal1731+// separator redundant1732 func (t *Terminal) separatedByBorder() bool {1733 // The input border itself draws a line on the side facing the list section1734 if inputBorderFacesList(t.layout, t.inputBorderShape) {1735 return true1736 }1737+ // A preview window at 'next' position sits right next to the input1738+ // section, in front of the sections handled below1739+ anyNext := false1740 for po := &t.previewOpts; po != nil; po = po.alternative {1741 if po.position == posNext {1738- // A preview window at 'next' position sits right next to the input1739- // section instead. Keep the separator, as the visibility and the1740- // border of the preview window can change at runtime.1741- //1742- // The whole chain is checked because which spec is active depends1743- // on the terminal size, and the resolution (computePreviewSize in1744- // resizeWindows) itself consults noSeparatorLine, so the decision1745- // here cannot depend on its outcome. When a spec other than 'next'1746- // is active, this errs on the side of showing the separator.1742+ anyNext = true1743+ break1744+ }1745+ }1746+ if anyNext && t.hasPreviewer() {1747+ // The separator is hidden only when every spec in the threshold chain1748+ // displays a preview window at 'next' position with a border line1749+ // facing the input section. We cannot single out the active spec;1750+ // which one is active depends on the terminal size, and the1751+ // resolution (computePreviewSize in resizeWindows) itself consults1752+ // noSeparatorLine. When not every spec qualifies, err on the side of1753+ // showing the separator.1754+ if len(t.previewOpts.command) == 0 {1755+ // No preview command, so no preview window is normally shown, but1756+ // the preview(...) action can still display one at any time1757 return false1758 }1759+ // NOTE: resizeWindows can clear 'hidden' mid-layout when the1760+ // preview(...) action forces the window, so the frame being laid out1761+ // can briefly disagree with this decision until the next relayout1762+ for po := &t.previewOpts; po != nil; po = po.alternative {1763+ if po.position != posNext || po.hidden || po.size.size == 0 || !t.borderFacingInput(po.Border(t.layout)) {1764+ return false1765+ }1766+ }1767+ return true1768 }1769 hasHeaderLinesWindow, headerLinesShape := t.determineHeaderLinesShape()1770 hasHeaderWindow := t.hasHeaderWindow()1752- if !t.inputBorderShape.Visible() && !hasHeaderWindow && !hasHeaderLinesWindow {1753- // No separate window is created for the input section in this case1754- // (see hasInputWindow in resizeWindows), i.e. the input section is1755- // embedded in the list window and no border separates the two1771+ // Mirror of hasInputWindow in resizeWindows; a 'next' preview position1772+ // gives the input section its own window even when no preview window is1773+ // shown. A 'next' spec here implies there is no previewer (the block1774+ // above returns otherwise), and without a previewer the position is1775+ // resolved from the first spec alone, so a 'next' in a threshold1776+ // alternative does not count.1777+ hasInputWindow := t.previewOpts.position == posNext || t.inputBorderShape.Visible() || hasHeaderWindow || hasHeaderLinesWindow1778+ if !hasInputWindow {1779+ // The input section is embedded in the list window and no border1780+ // separates the two1781 return false1782 }1783 1784 // The input section has its own window. Determine which section it is1785 // facing, and check if the border of that section draws a line toward it.1786 // NOTE: hasHeaderWindow can be true with no header content when the input1787 // border is visible (see Terminal.hasHeaderWindow). An empty header window1788 // takes no space, so it cannot be the facing section.1764- hasHeaderSection := hasHeaderWindow && len(t.header0)+t.headerLines > 01789+ hasHeaderSection := hasHeaderWindow && t.visibleHeaderLines() > 01790 if hasHeaderSection && !t.headerFirst && t.headerBorderShape != tui.BorderInline {1766- // Header section; an inline header is not a separate section, it is1767- // drawn inside the list border, so it falls through to the branches1768- // below1791+ // Header section is facing the input section. An inline header is1792+ // excluded; it is drawn inside the list border and is covered by the1793+ // branches below.1794 return t.borderFacingInput(t.headerBorderShape)1795 }1771- // The header lines section is next to the input section, except in1796+ // Header lines section is facing the input section, except in1797 // reverse-list layout where it is on the other side of the list section,1773- // or with --header-first and no header section where it is moved past the1774- // input section1798+ // or with --header-first and no header section where it is moved past1799+ // the input section1800 if hasHeaderLinesWindow && t.layout != layoutReverseList && (hasHeaderWindow || !t.headerFirst) {1801 return t.borderFacingInput(headerLinesShape)1802 }1778- // List section1803+ // The input section sits right next to the list section1804 return t.borderFacingInput(t.listBorderShape)1805 }1806 1782-// Whether the shape draws a horizontal line facing the info line when the1783-// section is placed between the input section and the list section1807+// Whether the border of the section next to the input section on the list1808+// side draws a horizontal line facing the info line1809 func (t *Terminal) borderFacingInput(shape tui.BorderShape) bool {1810 if shape == tui.BorderInline {1811 // Embedded between the horizontal lines of the list border1812 return true6463 unmodified lines8276 }8277 case actChangePreviewWindow:8278 // NOTE: We intentionally use "previewOpts" instead of "activePreviewOpts" here8279 currentPreviewOpts := t.previewOpts8280+ wasNoSeparatorLine := t.noSeparatorLine()8281+ wasSeparatorLength := t.separatorLength()8282 8283 // Reset preview options and apply the additional options8284 t.previewOpts = t.initialPreviewOpts8285 t.previewOpts.command = currentPreviewOpts.command12 unmodified lines8298 if len(tokens) > 1 {8299 a.a = strings.Join(append(tokens[1:], tokens[0]), "|")8300 }8301 8302+ // Do not drop a preview window forced by the preview(...) action8303+ keepForcedPreview := t.hasPreviewWindow() && !t.activePreviewOpts.hidden8304+8305+ // The default separator is decided from the whole threshold8306+ // chain (separatedByBorder), which compare does not fully8307+ // inspect, so it can change even when compare finds no8308+ // layout difference8309+ checkSeparator := func() {8310+ if t.noSeparatorLine() != wasNoSeparatorLine {8311+ // Number of lines changed; relayout8312+ updatePreviewWindow(keepForcedPreview)8313+ req(reqPreviewRefresh)8314+ } else if t.separatorLength() != wasSeparatorLength {8315+ // Only the content of the info line changed8316+ req(reqInfo)8317+ }8318+ }8319+8320 // Full redraw8321 switch currentPreviewOpts.compare(t.activePreviewOpts, &t.previewOpts) {8322+ case previewOptsSame:8323+ checkSeparator()8324 case previewOptsDifferentLayout:8325 // Preview command can be running in the background if the size of8326 // the preview window is 0 but not 'hidden'8327 wasHidden := currentPreviewOpts.hidden8328 8329 // FIXME: One-time preview window can't reappear once hidden8330 // fzf --bind space:preview:ls --bind 'enter:change-preview-window:down|left|up|hidden|'8284- updatePreviewWindow(t.hasPreviewWindow() && !t.activePreviewOpts.hidden)8331+ updatePreviewWindow(keepForcedPreview)8332 if wasHidden && t.hasPreviewWindow() {8333 // Restart8334 refreshPreview(t.previewOpts.command)8335 } else if t.activePreviewOpts.hidden {5 unmodified lines8341 }8342 case previewOptsDifferentContentLayout:8343 t.previewed.version = 08344 req(reqPreviewRefresh)8345+ checkSeparator()8346 }8347 8348 // Adjust scroll offset8349 if t.hasPreviewWindow() && currentPreviewOpts.scroll != t.activePreviewOpts.scroll {361 unmodified linesmodified:test/test_layout.rb+162 added-2 removed
246 unmodified lines247 # https://github.com/junegunn/fzf/issues/4798248 tmux.send_keys %(seq 5 | #{FZF} --layout=reverse --preview 'echo PREVIEW' --preview-window=next:3 --prompt='line2$ > '), :Enter249 expected = <<~OUTPUT250 line2$ >251- 5/5 ───251+ 5/5252 ╭────────253 │ PREVIEW254 │255 │11 unmodified lines267 │ PREVIEW268 │269 │270 ╰────────271- 5/5 ───271+ 5/5272 >273 OUTPUT274 tmux.until { assert_block(expected, it) }275 end1029 unmodified lines1305 1306 # Same when 'next' position comes from a threshold alternative1307 tmux.send_keys %(seq 100 | #{FZF} --header foo --header-border rounded --no-list-border --preview 'echo hi' --preview-window 'up,40%,<9999(next,1,border-none)'), :Enter1308 tmux.until { assert_block(block, it) }1309+ teardown1310+ setup1311+1312+ # A border of the preview window at 'next' position facing the input1313+ # section hides the separator1314+ tmux.send_keys %(seq 100 | #{FZF} --preview : --preview-window next,3), :Enter1315+ block = <<~BLOCK1316+ ╰────────────1317+ 100/1001318+ >1319+ BLOCK1320+ tmux.until { assert_block(block, it) }1321+ teardown1322+ setup1323+1324+ # Same when every spec in the threshold chain is a bordered preview1325+ # window at 'next' position1326+ tmux.send_keys %(seq 100 | #{FZF} --preview : --preview-window 'next,3,<15(next,3)'), :Enter1327+ tmux.until { assert_block(block, it) }1328 end1329 1330+ def test_separator_with_preview_next_toggle1331+ tmux.send_keys %(seq 100 | #{FZF} --preview 'echo hi' --preview-window next,3,hidden --bind space:toggle-preview), :Enter1332+ hidden = <<~BLOCK1333+ > 11334+ 100/100 ──1335+ >1336+ BLOCK1337+ tmux.until { assert_block(hidden, it) }1338+1339+ tmux.send_keys :Space1340+ shown = <<~BLOCK1341+ │ hi1342+ │1343+ │1344+ ╰────────────1345+ 100/1001346+ >1347+ BLOCK1348+ tmux.until { assert_block(shown, it) }1349+1350+ tmux.send_keys :Space1351+ tmux.until { assert_block(hidden, it) }1352+ end1353+1354+ def test_separator_with_preview_next_alternative_only1355+ # 'next' appears only in a threshold alternative and there is no1356+ # previewer, so the input section stays embedded in the list window and1357+ # needs the separator1358+ tmux.send_keys %(seq 100 | #{FZF} --preview-window '<9999(next)' --list-border rounded), :Enter1359+ block = <<~BLOCK1360+ │ > 11361+ │ 100/100 ──1362+ │ >1363+ ╰─────────────1364+ BLOCK1365+ tmux.until { assert_block(block, it) }1366+ end1367+1368+ def test_separator_with_preview_action_at_next1369+ # preview(...) action can display an ad-hoc preview window at 'next'1370+ # position at any time; the list border must not hide the separator1371+ tmux.send_keys %(seq 100 | #{FZF} --list-border rounded --preview-window 'next,border-left,5' --bind 'space:preview(echo hi)'), :Enter1372+ block = <<~BLOCK1373+ ╰─────────────1374+ 100/100 ──1375+ >1376+ BLOCK1377+ tmux.until { assert_block(block, it) }1378+1379+ tmux.send_keys :Space1380+ block = <<~BLOCK1381+ │ hi1382+ │1383+ │1384+ │1385+ │1386+ 100/100 ──1387+ >1388+ BLOCK1389+ tmux.until { assert_block(block, it) }1390+ end1391+1392+ def test_separator_change_preview_window_keeps_forced_preview1393+ # Relayout triggered by the separator visibility change must not drop a1394+ # preview window opened via the preview(...) action1395+ tmux.send_keys %(seq 100 | #{FZF} --info hidden --input-border left --list-border --preview-window 'up,50%,<5(next)' --bind 'space:preview(echo AD-HOC)' --bind 'x:change-preview-window(up,50%,<5(up))'), :Enter1396+ tmux.until { |lines| assert_includes lines.join, '│ ─' }1397+1398+ tmux.send_keys :Space1399+ tmux.until do |lines|1400+ assert_includes lines.join, 'AD-HOC'1401+ assert_includes lines.join, '│ ─'1402+ end1403+1404+ # Only the inactive threshold alternative changes; separator is hidden1405+ # by the list border, and the forced preview window must survive1406+ tmux.send_keys :x1407+ tmux.until do |lines|1408+ assert_includes lines.join, 'AD-HOC'1409+ refute_includes lines.join, '│ ─'1410+ end1411+ end1412+1413+ def test_separator_with_change_preview_window_chain1414+ # change-preview-window that only alters a non-active spec must still1415+ # trigger a relayout when it changes the separator visibility1416+ tmux.send_keys %(seq 100 | #{FZF} --info hidden --layout reverse --preview 'echo hi' --preview-window 'next,3,border-none,<100(next,3,border-top)' --bind 'space:change-preview-window(next,3,border-top,<100(next,3,border-top))'), :Enter1417+ block = <<~BLOCK1418+ >1419+ ──────────1420+ ──────────1421+ hi1422+ BLOCK1423+ tmux.until { assert_block(block, it) }1424+1425+ tmux.send_keys :Space1426+ block = <<~BLOCK1427+ >1428+ ──────────1429+ hi1430+ BLOCK1431+ tmux.until { assert_block(block, it) }1432+ teardown1433+ setup1434+1435+ # Also when only the drawn bar changes; the info line row count is1436+ # constant with the default info style1437+ tmux.send_keys %(seq 100 | #{FZF} --layout reverse --preview 'echo hi' --preview-window 'next,3,border-none,<100(next,3,border-top)' --bind 'space:change-preview-window(next,3,border-top,<100(next,3,border-top))'), :Enter1438+ block = <<~BLOCK1439+ >1440+ 100/100 ──1441+ BLOCK1442+ tmux.until { assert_block(block, it) }1443+1444+ tmux.send_keys :Space1445+ block = <<~BLOCK1446+ >1447+ 100/1001448+ ────────────1449+ BLOCK1450+ tmux.until { assert_block(block, it) }1451+ teardown1452+ setup1453+1454+ # Also when compare finds a content-layout difference (scroll offset)1455+ tmux.send_keys %(seq 100 | #{FZF} --info hidden --preview 'echo hi' --preview-window 'next,3,<2(up,3)' --bind 'space:change-preview-window(next,3,+1,<2(next,3))'), :Enter1456+ block = <<~BLOCK1457+ ╰────────────1458+ ─────────────1459+ >1460+ BLOCK1461+ tmux.until { assert_block(block, it) }1462+1463+ tmux.send_keys :Space1464+ block = <<~BLOCK1465+ ╰────────────1466+ >1467+ BLOCK1468+ tmux.until { assert_block(block, it) }1469+ end1470+1471 def test_separator_with_inline_header_border1472 # Inline header is drawn inside the list border; bare header lines sit1473 # next to the input section, so the separator is shown1474 tmux.send_keys %(seq 100 | #{FZF} --list-border rounded --header foo --header-lines 2 --header-border inline --header-lines-border none), :Enter544 unmodified linesmodified:CHANGELOG.md+3 added
9 unmodified lines10 11 # Separator shown; no border separates the input section from the list section12 fzf --style full --input-border none --header foo --no-header-border13 14+ # No separator below the border of the preview window at 'next' position15+ fzf --preview : --preview-window next16+17 # Conversely, separator is now shown when the input border does not draw18 # a line facing the list section19 fzf --input-border bottom20 ```3377 unmodified lines