Fix inconsistent placement of header-lines with border options
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fzf displayed --header-lines inconsistently depending on the presence of borders: # --header and --header-lines co-located seq 10 | fzf --header-lines 3 --header "$(seq 101 103)" --header-first # --header and --header-lines separated seq 10 | fzf --header-lines 3 --header "$(seq 101 103)" --header-first --header-lines-border This commit fixes the inconsistency with the following logic: * If only one of --header or --header-lines is provided, --header-first applies to that single header. * If both are present, --header-first affects only the regular --header, not --header-lines.
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+107 added−46 removed
- tests:+9 added−9 removed
- other:+98 added−37 removed
Provenance
- Commit
39db026- Tree
ef4c791- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
f6c589c
4 files changed+107 added−46 removed
modified:man/man1/fzf.1+2 added-1 removed
999 unmodified lines1000 \fB\-\-with\-nth\fR is set, the lines are transformed just like the other1001 lines that follow.1002 .TP1003 .B "\-\-header\-first"1004-Print header before the prompt line1004+Print header before the prompt line. When both normal header and header lines1005+(\fB\-\-header\-lines\fR) are present, this applies only to the normal header.1006 .TP1007 .BI "\-\-header\-border" [=STYLE]1008 Draw border around the header section1009 943 unmodified linesmodified:src/options.go-9 removed
3216 unmodified lines3217 }3218 3219 if opts.HeaderLinesShape == tui.BorderNone {3220 opts.HeaderLinesShape = tui.BorderPhantom3221- } else if opts.HeaderLinesShape == tui.BorderUndefined {3222- // In reverse-list layout, header lines should be at the top, while3223- // ordinary header should be at the bottom. So let's use a separate3224- // window for the header lines.3225- if opts.Layout == layoutReverseList {3226- opts.HeaderLinesShape = tui.BorderPhantom3227- } else {3228- opts.HeaderLinesShape = tui.BorderNone3229- }3221 }3222 3223 if opts.Pointer == nil {3224 defaultPointer := "▌"276 unmodified linesmodified:src/terminal.go+96 added-27 removed
1201 unmodified lines1202 if t.hasHeaderWindow() {1203 extra += borderLines(t.headerBorderShape)1204 }1205 extra += len(t.header0)1206- if t.hasHeaderLinesWindow() {1207- extra += borderLines(t.headerLinesShape)1206+ if w, shape := t.determineHeaderLinesShape(); w {1207+ extra += borderLines(shape)1208 }1209 extra += t.headerLines1210 }1211 return extra557 unmodified lines1769 return t.inputBorderShape.Visible()1770 }1771 1772 func (t *Terminal) hasHeaderLinesWindow() bool {1773- return t.headerVisible && t.headerLines > 0 && t.headerLinesShape.Visible()1773+ w, _ := t.determineHeaderLinesShape()1774+ return w1775 }1776 1777+func (t *Terminal) determineHeaderLinesShape() (bool, tui.BorderShape) {1778+ if !t.headerVisible || t.headerLines == 0 {1779+ return false, tui.BorderNone1780+ }1781+1782+ // --header-lines-border is set1783+ if t.headerLinesShape != tui.BorderUndefined {1784+ return true, t.headerLinesShape1785+ }1786+1787+ // --header-lines-border is not set, determine if we should use1788+ // the style of --header-border1789+ shape := tui.BorderNone1790+ if len(t.header0) == 0 {1791+ shape = t.headerBorderShape1792+ }1793+ if shape == tui.BorderNone {1794+ shape = tui.BorderPhantom1795+ }1796+1797+ // --layout reverse-list is set1798+ if t.layout == layoutReverseList {1799+ return true, shape1800+ }1801+1802+ // Use header window instead1803+ if len(t.header0) == 0 {1804+ return false, t.headerBorderShape1805+ }1806+1807+ // We have both types of headers, and we want to separate the two1808+ if t.headerFirst {1809+ return true, shape1810+ }1811+1812+ return false, tui.BorderNone1813+}1814+1815 func (t *Terminal) resizeWindows(forcePreview bool, redrawBorder bool) {1816 t.clearNumLinesCache()1817 t.forcePreview = forcePreview1818 screenWidth, screenHeight, marginInt, paddingInt := t.adjustMarginAndPadding()72 unmodified lines1891 availableLines := height1892 shift := 01893 shrink := 01894 hasHeaderWindow := t.hasHeaderWindow()1856- hasHeaderLinesWindow := t.hasHeaderLinesWindow()1895+ hasHeaderLinesWindow, headerLinesShape := t.determineHeaderLinesShape()1896 hasInputWindow := !t.inputless && (t.inputBorderShape.Visible() || hasHeaderWindow || hasHeaderLinesWindow)1897 if hasInputWindow {1898 inputWindowHeight := 21899 if t.noSeparatorLine() {27 unmodified lines1927 }1928 1929 headerLinesHeight := 01930 if hasHeaderLinesWindow {1892- headerLinesHeight = util.Min(availableLines, borderLines(t.headerLinesShape)+t.headerLines)1931+ headerLinesHeight = util.Min(availableLines, borderLines(headerLinesShape)+t.headerLines)1932 if t.layout != layoutDefault {1933 shift += headerLinesHeight1934 shrink += headerLinesHeight1935 } else {249 unmodified lines2185 w := t.wborder2186 if t.wborder == nil {2187 w = t.window2188 }2189+2190 if hasInputWindow {2191 var btop int2152- if hasHeaderWindow && t.headerFirst {2153- if t.layout == layoutReverse {2154- btop = w.Top() - inputBorderHeight - headerLinesHeight2155- } else if t.layout == layoutReverseList {2192+ if (hasHeaderWindow || hasHeaderLinesWindow) && t.headerFirst {2193+ switch t.layout {2194+ case layoutDefault:2195 btop = w.Top() + w.Height()2157- } else {2158- btop = w.Top() + w.Height() + headerLinesHeight2196+ // If both headers are present, the header lines are displayed with the list2197+ if hasHeaderWindow && hasHeaderLinesWindow {2198+ btop += headerLinesHeight2199+ }2200+ case layoutReverse:2201+ btop = w.Top() - inputBorderHeight2202+ if hasHeaderWindow && hasHeaderLinesWindow {2203+ btop -= headerLinesHeight2204+ }2205+ case layoutReverseList:2206+ btop = w.Top() + w.Height()2207 }2208 } else {2161- if t.layout == layoutReverse {2209+ switch t.layout {2210+ case layoutDefault:2211+ btop = w.Top() + w.Height() + headerBorderHeight + headerLinesHeight2212+ case layoutReverse:2213 btop = w.Top() - shrink2163- } else if t.layout == layoutReverseList {2214+ case layoutReverseList:2215 btop = w.Top() + w.Height() + headerBorderHeight2165- } else {2166- btop = w.Top() + w.Height() + headerBorderHeight + headerLinesHeight2216 }2217 }2218 shift := 02219 if !t.inputBorderShape.HasLeft() && t.listBorderShape.HasLeft() {43 unmodified lines2263 2264 // Set up header lines border2265 if hasHeaderLinesWindow {2266 var btop int2267+ // NOTE: We still have to handle --header-first here in case2268+ // --header-lines-border is set. Can't we just use header window instead2269+ // with the style? So we can display header label.2270+ // fzf --header-lines 3 --header-label hello --header-border2271+ // fzf --header-lines 3 --header-label hello --header-lines-border2272+ headerFirst := t.headerFirst && len(t.header0) == 02273+2274+ if headerFirst {2218- if t.layout != layoutDefault {2219- btop = w.Top() - headerLinesHeight2275+ if t.layout == layoutDefault {2276+ btop = w.Top() + w.Height() + inputBorderHeight2277+ } else if t.layout == layoutReverse {2278+ btop = w.Top() - headerLinesHeight - inputBorderHeight2279+ } else {2280+ btop = w.Top() - headerLinesHeight2281+ }2282 } else {2283+ if t.layout != layoutDefault {2284+ btop = w.Top() - headerLinesHeight2285+ } else {2221- btop = w.Top() + w.Height()2286+ btop = w.Top() + w.Height()2287+ }2288 }2289 t.headerLinesBorder = t.tui.NewWindow(2290 btop,2291 w.Left(),2292 w.Width(),2227- headerLinesHeight, tui.WindowHeader, tui.MakeBorderStyle(t.headerLinesShape, t.unicode), true)2228- t.headerLinesWindow = createInnerWindow(t.headerLinesBorder, t.headerLinesShape, tui.WindowHeader, 0)2293+ headerLinesHeight, tui.WindowHeader, tui.MakeBorderStyle(headerLinesShape, t.unicode), true)2294+ t.headerLinesWindow = createInnerWindow(t.headerLinesBorder, headerLinesShape, tui.WindowHeader, 0)2295 }2296 2297 // Print border label2298 t.printLabel(t.wborder, t.listLabel, t.listLabelOpts, t.listLabelLen, t.listBorderShape, false)387 unmodified lines2686 2687 // Check if the header borders are used and header has changed2688 allHeaderLines := t.visibleHeaderLines()2689 primaryHeaderLines := allHeaderLines2624- if t.headerLinesShape.Visible() {2690+ if t.hasHeaderLinesWindow() {2691 primaryHeaderLines -= t.headerLines2692 }2693+ needHeaderLinesWindow := t.hasHeaderLinesWindow()2627- if (t.headerBorderShape.Visible() || t.headerLinesShape.Visible()) &&2694+ if (t.headerBorderShape.Visible() || t.hasHeaderLinesWindow()) &&2695 (t.headerWindow == nil && primaryHeaderLines > 0 || t.headerWindow != nil && primaryHeaderLines != t.headerWindow.Height()) ||2629- t.headerLinesShape.Visible() && (t.headerLinesWindow == nil && t.headerLines > 0 || t.headerLinesWindow != nil && t.headerLines != t.headerLinesWindow.Height()) {2696+ needHeaderLinesWindow && (t.headerLinesWindow == nil || t.headerLinesWindow != nil && t.headerLines != t.headerLinesWindow.Height()) ||2697+ !needHeaderLinesWindow && t.headerLinesWindow != nil {2698 t.printAll()2699 return true2700 }2701 return false5 unmodified lines2707 }2708 2709 t.withWindow(t.headerWindow, func() {2710 var lines []string2643- if !t.headerLinesShape.Visible() {2711+ if !t.hasHeaderLinesWindow() {2712 lines = t.header2713 }2714 t.printHeaderImpl(t.headerWindow, t.headerBorderShape, t.header0, lines)2715 })2648- if t.headerLinesShape.Visible() {2716+ if w, shape := t.determineHeaderLinesShape(); w {2717 t.withWindow(t.headerLinesWindow, func() {2650- t.printHeaderImpl(t.headerLinesWindow, t.headerLinesShape, nil, t.header)2718+ t.printHeaderImpl(t.headerLinesWindow, shape, nil, t.header)2719 })2720 }2721 }2722 3207 unmodified lines5930 return doActions(actionsFor(tui.ClickHeader))5931 }5932 5933 if clicked && t.headerVisible && t.headerLinesWindow != nil && t.headerLinesWindow.Enclose(my, mx) {5934+ _, shape := t.determineHeaderLinesShape()5866- mx -= t.headerLinesWindow.Left() + t.headerIndent(t.headerLinesShape)5935+ mx -= t.headerLinesWindow.Left() + t.headerIndent(shape)5936 my -= t.headerLinesWindow.Top()5937 if mx < 0 {5938 break5939 }551 unmodified linesmodified:test/test_layout.rb+9 added-9 removed
37 unmodified lines38 def test_header_first39 tmux.send_keys "seq 1000 | #{FZF} --header foobar --header-lines 3 --header-first", :Enter40 block = <<~OUTPUT41 > 442- 997/99743- >42 343 244 145+ 997/99746+ >47 foobar48 OUTPUT49 tmux.until { assert_block(block, it) }50 end1 unmodified line52 def test_header_first_reverse53 tmux.send_keys "seq 1000 | #{FZF} --header foobar --header-lines 3 --header-first --reverse --inline-info", :Enter54 block = <<~OUTPUT55 foobar56+ > < 997/99757 158 259 359- > < 997/99760 > 461 OUTPUT62 tmux.until { assert_block(block, it) }63 end83 unmodified lines147 ╭───────148 │149 │ 4150 │ > 3151- │ 2/2152- │ >151 │ 2152 │ 1153+ │ 2/2154+ │ >155 │ foo156 ╰───────157 OUTPUT158 tmux.until { assert_block(before, it) }449 unmodified lines608 block2 = <<~BLOCK609 │ 4610 │ > 3611 ╰──────────612+ 2613+ 1614 98/98 ─615 >616 ╭──────────615- │ 2616- │ 1617 │ hello618 ╰──────────619 BLOCK620 tmux.until { assert_block(block2, it) }44 unmodified lines665 │ 5666 │ 4667 │ > 3668 ╰──────────669+ 98/98 ─670+ >671 ╔══════════672 ║ 2673 ║ 1674 ╚══════════673- 98/98 ─674- >675 BLOCK676 tmux.until { assert_block(block1, it) }677 678 tmux.send_keys :Space358 unmodified lines