Support preview scroll offset relative to window height
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 1ab4289 · Browse files · 4 files·+32 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Related: https://github.com/junegunn/fzf.vim/issues/1092
Diff stats
4 files+32 added−7 removed
- other:+32 added−7 removed
Provenance
- Commit
1cb19db- Tree
9747349- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
1ab4289
4 files changed+32 added−7 removed
modified:man/man1/fzf.1+10 added-3 removed
392 unmodified lines393 \fB+SCROLL[-OFFSET]\fR determines the initial scroll offset of the preview394 window. \fBSCROLL\fR can be either a numeric integer or a single-field index395 expression that refers to a numeric integer. The optional \fB-OFFSET\fR part is396 for adjusting the base offset so that you can see the text above it. It should397-be given as a numeric integer.397+be given as a numeric integer (\fB-INTEGER\fR), or as a denominator form398+(\fB-/INTEGER\fR) for specifying a fraction of the preview window height.399 400 .RS401 .B POSITION: (default: right)402 \fBup8 unmodified lines411 fzf --preview="head {}" --preview-window=up:30%412 fzf --preview="file {}" --preview-window=down:1413 414 # Initial scroll offset is set to the line number of each line of414- # git grep output *minus* 5 lines415+ # git grep output *minus* 5 lines (-5)416 git grep --line-number '' |416- fzf --delimiter : --preview 'nl {1}' --preview-window +{2}-5\fR417+ fzf --delimiter : --preview 'nl {1}' --preview-window +{2}-5418 419+ # Preview with bat, matching line in the middle of the window (-/2)420+ git grep --line-number '' |421+ fzf --delimiter : \\422+ --preview 'bat --style=numbers --color=always --highlight-line {2} {1}' \\423+ --preview-window +{2}-/2\fR424+425 .RE426 .SS Scripting427 .TP428 .BI "-q, --query=" "STR"414 unmodified linesmodified:src/options.go+1 added-1 removed
993 unmodified lines994 opts.wrap = false995 996 tokens := strings.Split(input, ":")997 sizeRegex := regexp.MustCompile("^[1-9][0-9]*%?$")998- offsetRegex := regexp.MustCompile("^\\+([0-9]+|{-?[0-9]+})(-[0-9]+)?$")998+ offsetRegex := regexp.MustCompile("^\\+([0-9]+|{-?[0-9]+})(-[0-9]+|-/[1-9][0-9]*)?$")999 for _, token := range tokens {1000 switch token {1001 case "":1002 case "hidden":485 unmodified linesmodified:src/terminal.go+11 added-3 removed
1372 unmodified lines1373 }1374 return n1375 }1376 1377-func (t *Terminal) evaluateScrollOffset(list []*Item) int {1377+func (t *Terminal) evaluateScrollOffset(list []*Item, height int) int {1378 offsetExpr := t.replacePlaceholder(t.preview.scroll, false, "", list)1379 nums := strings.Split(offsetExpr, "-")1380 switch len(nums) {1381 case 0:4 unmodified lines1386 return 01387 } else if len(nums) == 1 {1388 return base - 11389 }1390+ if nums[1][0] == '/' {1391+ denom := atopi(nums[1][1:])1392+ if denom == 0 {1393+ return base1394+ }1395+ return base - height/denom1396+ }1397 return base - atopi(nums[1]) - 11398 default:1399 return 01400 }281 unmodified lines1682 })1683 // We don't display preview window if no match1684 if items[0] != nil {1685 command := t.replacePlaceholder(commandTemplate, false, string(t.Input()), items)1686+ height := t.pwindow.Height()1679- offset := t.evaluateScrollOffset(items)1687+ offset := t.evaluateScrollOffset(items, height)1688 cmd := util.ExecCommand(command, true)1689 if t.pwindow != nil {1690 env := os.Environ()1683- lines := fmt.Sprintf("LINES=%d", t.pwindow.Height())1691+ lines := fmt.Sprintf("LINES=%d", height)1692 columns := fmt.Sprintf("COLUMNS=%d", t.pwindow.Width())1693 env = append(env, lines)1694 env = append(env, "FZF_PREVIEW_"+lines)1695 env = append(env, columns)649 unmodified linesmodified:CHANGELOG.md+10 added
1 CHANGELOG2 =========3 4+0.22.15+------6+- Support preview scroll offset relative to window height7+ ```sh8+ git grep --line-number '' |9+ fzf --delimiter : \10+ --preview 'bat --style=numbers --color=always --highlight-line {2} {1}' \11+ --preview-window +{2}-/212+ ```13+14 0.22.015 ------16 - Added more options for `--bind`17 - `backward-eof` event906 unmodified lines