Skip to content
Myr

Add support for preview window header

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #2373 # Display top 3 lines as the fixed header fzf --preview 'bat --style=header,grid --color=always {}' --preview-window '~3'

authored by Junegunn Choi · committed by Junegunn Choi · parent 7310370 · Browse files · 5 files+87 added−29 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+87 added−29 removed

  • tests:+26 added−0 removed
  • other:+61 added−29 removed

5 files+87 added−29 removed

modified:man/man1/fzf.1+6 added-1 removed
440 unmodified lines
441 sleep 0.01442 done'\fR443.RE444.TP445.BI "--preview-window=" "[POSITION][:SIZE[%]][:rounded|sharp|noborder][:[no]wrap][:[no]follow][:[no]cycle][:[no]hidden][:+SCROLL[-OFFSET]][:default]"445.BI "--preview-window=" "[POSITION][:SIZE[%]][:rounded|sharp|noborder][:[no]wrap][:[no]follow][:[no]cycle][:[no]hidden][:+SCROLL[-OFFSET]][:~HEADER_LINES][:default]"446447.RS448.B POSITION: (default: right)449 \fBup
36 unmodified lines
486for adjusting the base offset so that you can see the text above it. It should487be given as a numeric integer (\fB-INTEGER\fR), or as a denominator form488(\fB-/INTEGER\fR) for specifying a fraction of the preview window height.489490* \fB~HEADER_LINES\fR keeps the top N lines as the fixed header so that they491are always visible.492493* \fBdefault\fR resets all options previously set to the default.494495.RS496e.g.
11 unmodified lines
508 fzf --delimiter : \\509 --preview 'bat --style=numbers --color=always --highlight-line {2} {1}' \\510 --preview-window +{2}-/2\fR511512 # Display top 3 lines as the fixed header513 fzf --preview 'bat --style=header,grid --color=always {}' --preview-window '~3'514.RE515516.SS Scripting517.TP
444 unmodified lines
modified:src/options.go+17 added-13 removed
82 unmodified lines
83 --preview-window=OPT Preview window layout (default: right:50%)84 [up|down|left|right][:SIZE[%]]85 [:[no]wrap][:[no]cycle][:[no]follow][:[no]hidden]86 [:rounded|sharp|noborder]87 [:+SCROLL[-OFFSET]]87 [:+SCROLL[-OFFSET]][:~HEADER_LINES]88 [:default]8990 Scripting91 -q, --query=STR Start the finder with the given query
68 unmodified lines
160 infoHidden161)162163type previewOpts struct {164 command string165 position windowPosition166 size sizeSpec167 scroll string168 hidden bool169 wrap bool170 cycle bool171 follow bool172 border tui.BorderShape164 command string165 position windowPosition166 size sizeSpec167 scroll string168 hidden bool169 wrap bool170 cycle bool171 follow bool172 border tui.BorderShape173 headerLines int174}175176// Options stores the values of command-line options177type Options struct {
53 unmodified lines
231 Version bool232}233234func defaultPreviewOpts(command string) previewOpts {234 return previewOpts{command, posRight, sizeSpec{50, true}, "", false, false, false, false, tui.BorderRounded}235 return previewOpts{command, posRight, sizeSpec{50, true}, "", false, false, false, false, tui.BorderRounded, 0}236}237238func defaultOptions() *Options {239 return &Options{
838 unmodified lines
1078func parsePreviewWindow(opts *previewOpts, input string) {1079 tokens := strings.Split(input, ":")1080 sizeRegex := regexp.MustCompile("^[0-9]+%?$")1081 offsetRegex := regexp.MustCompile("^\\+([0-9]+|{-?[0-9]+})(-[0-9]+|-/[1-9][0-9]*)?$")1082 headerRegex := regexp.MustCompile("^~(0|[1-9][0-9]*)$")1083 for _, token := range tokens {1084 switch token {1085 case "":1086 case "default":
28 unmodified lines
1115 opts.follow = true1116 case "nofollow":1117 opts.follow = false1118 default:1117 if sizeRegex.MatchString(token) {1119 if headerRegex.MatchString(token) {1120 opts.headerLines = atoi(token[1:])1121 } else if sizeRegex.MatchString(token) {1122 opts.size = parseSize(token, 99, "window size")1123 } else if offsetRegex.MatchString(token) {1124 opts.scroll = token[1:]1125 } else {
241 unmodified lines
1367 case "--no-preview":1368 opts.Preview.command = ""1369 case "--preview-window":1370 parsePreviewWindow(&opts.Preview,1367 nextString(allArgs, &i, "preview window layout required: [up|down|left|right][:SIZE[%]][:rounded|sharp|noborder][:wrap][:cycle][:hidden][:+SCROLL[-OFFSET]][:default]"))1371 nextString(allArgs, &i, "preview window layout required: [up|down|left|right][:SIZE[%]][:rounded|sharp|noborder][:wrap][:cycle][:hidden][:+SCROLL[-OFFSET]][:~HEADER_LINES][:default]"))1372 case "--height":1373 opts.Height = parseHeight(nextString(allArgs, &i, "height required: HEIGHT[%]"))1374 case "--min-height":1375 opts.MinHeight = nextInt(allArgs, &i, "height required: HEIGHT")
234 unmodified lines
modified:src/terminal.go+30 added-14 removed
1293 unmodified lines
1294 }1295 }1296}12971298func (t *Terminal) renderPreviewText(unchanged bool) {1299 maxWidth := t.pwindow.Width()1300 lineNo := -t.previewer.offset1301 height := t.pwindow.Height()1298func (t *Terminal) renderPreviewArea(unchanged bool) {1299 if unchanged {1303 t.pwindow.MoveAndClear(0, 0)1300 t.pwindow.MoveAndClear(0, 0) // Clear scroll offset display1301 } else {1302 t.previewed.filled = false1303 t.pwindow.Erase()1304 }13051306 height := t.pwindow.Height()1307 header := []string{}1308 body := t.previewer.lines1309 headerLines := t.previewOpts.headerLines1310 // Do not enable preview header lines if it's value is too large1311 if headerLines > 0 && headerLines < util.Min(len(body), height) {1312 header = t.previewer.lines[0:headerLines]1313 body = t.previewer.lines[headerLines:]1314 // Always redraw header1315 t.renderPreviewText(height, header, 0, false)1316 t.pwindow.MoveAndClear(t.pwindow.Y(), 0)1317 }1318 t.renderPreviewText(height, body, -t.previewer.offset+headerLines, unchanged)13191320 if !unchanged {1321 t.pwindow.FinishFill()1322 }1323}13241325func (t *Terminal) renderPreviewText(height int, lines []string, lineNo int, unchanged bool) {1326 maxWidth := t.pwindow.Width()1327 var ansi *ansiState1309 for _, line := range t.previewer.lines {1328 for _, line := range lines {1329 var lbg tui.Color = -11330 if ansi != nil {1331 ansi.lbg = -11332 }
39 unmodified lines
1372 }1373 }1374 lineNo++1375 }1357 if !unchanged {1358 t.pwindow.FinishFill()1359 }1376}13771378func (t *Terminal) printPreview() {1379 if !t.hasPreviewWindow() {
4 unmodified lines
1384 unchanged := (t.previewed.filled || numLines == t.previewed.numLines) &&1385 t.previewer.version == t.previewed.version &&1386 t.previewer.offset == t.previewed.offset1387 t.previewer.scrollable = t.previewer.offset > 0 || numLines > height1372 t.renderPreviewText(unchanged)1388 t.renderPreviewArea(unchanged)1389 t.renderPreviewSpinner()1390 t.previewed.numLines = numLines1391 t.previewed.version = t.previewer.version1392 t.previewed.offset = t.previewer.offset
4 unmodified lines
1397 return1398 }13991400 t.previewer.scrollable = false1385 t.renderPreviewText(true)1401 t.renderPreviewArea(true)14021403 message := t.trimMessage("Loading ..", t.pwindow.Width())1404 pos := t.pwindow.Width() - len(message)1405 t.pwindow.Move(0, pos)
538 unmodified lines
1944 initialOffset := 01945 cmd := util.ExecCommand(command, true)1946 if pwindow != nil {1947 height := pwindow.Height()1932 initialOffset = util.Max(0, t.evaluateScrollOffset(items, height))1948 initialOffset = util.Max(0, t.evaluateScrollOffset(items, util.Max(0, height-t.previewOpts.headerLines)))1949 env := os.Environ()1950 lines := fmt.Sprintf("LINES=%d", height)1951 columns := fmt.Sprintf("COLUMNS=%d", pwindow.Width())1952 env = append(env, lines)
194 unmodified lines
2147 t.previewer.spinner = result.spinner2148 if t.previewer.following {2149 t.previewer.offset = len(t.previewer.lines) - t.pwindow.Height()2150 } else if result.offset >= 0 {2135 t.previewer.offset = util.Constrain(result.offset, 0, len(t.previewer.lines)-1)2151 t.previewer.offset = util.Constrain(result.offset, t.previewOpts.headerLines, len(t.previewer.lines)-1)2152 }2153 t.printPreview()2154 case reqPreviewRefresh:2155 t.printPreview()
64 unmodified lines
2220 numLines := len(t.previewer.lines)2221 if t.previewOpts.cycle {2222 newOffset = (newOffset + numLines) % numLines2223 }2208 newOffset = util.Constrain(newOffset, 0, numLines-1)2224 newOffset = util.Constrain(newOffset, t.previewOpts.headerLines, numLines-1)2225 if t.previewer.offset != newOffset {2226 t.previewer.offset = newOffset2227 req(reqPreviewRefresh)2228 }
523 unmodified lines
modified:test/test_go.rb+26 added
2014 unmodified lines
2015 rescue StandardError2016 nil2017 end2018 end20192020 def test_preview_header2021 tmux.send_keys "seq 100 | #{FZF} --bind ctrl-k:preview-up+preview-up,ctrl-j:preview-down+preview-down+preview-down --preview 'seq 1000' --preview-window 'top:+{1}:~3'", :Enter2022 tmux.until { |lines| assert_equal 100, lines.item_count }2023 top5 = ->(lines) { lines.drop(1).take(5).map { |s| s[/[0-9]+/] } }2024 tmux.until do |lines|2025 assert_includes lines[1], '4/1000'2026 assert_equal(%w[1 2 3 4 5], top5[lines])2027 end2028 tmux.send_keys '55'2029 tmux.until do |lines|2030 assert_equal 1, lines.match_count2031 assert_equal(%w[1 2 3 55 56], top5[lines])2032 end2033 tmux.send_keys 'C-J'2034 tmux.until do |lines|2035 assert_equal(%w[1 2 3 58 59], top5[lines])2036 end2037 tmux.send_keys :BSpace2038 tmux.until do |lines|2039 assert_equal 19, lines.match_count2040 assert_equal(%w[1 2 3 5 6], top5[lines])2041 end2042 tmux.send_keys 'C-K'2043 tmux.until { |lines| assert_equal(%w[1 2 3 4 5], top5[lines]) }2044 end2045end20462047module TestShell2048 def setup
478 unmodified lines
modified:CHANGELOG.md+8 added-1 removed
1CHANGELOG2=========340.25.240.26.05------6- Added support for fixed header in preview window7 ```sh8 # Display top 3 lines as the fixed header9 fzf --preview 'bat --style=header,grid --color=always {}' --preview-window '~3'10 ```11- Added `select` and `deselect` action for unconditionally selecting or12 deselecting a single item in `--multi` mode. Complements `toggle` action.13- Sigificant performance improvement in ANSI code processing14- Bug fixes and improvements15- Built with Go 1.1616170.25.118------
1077 unmodified lines