Skip to content
Myr

Advanced preview scroll offset expression to better support fixed header

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 b24a2e2 · Browse files · 6 files+67 added−51 removed
This commit

State

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

Diff stats

6 files+67 added−51 removed

  • other:+67 added−51 removed

6 files+67 added−51 removed

modified:man/man1/fzf-tmux.1+1 added-1 removed
19 unmodified lines
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22THE SOFTWARE.23..24.TH fzf-tmux 1 "Feb 2021" "fzf 0.25.1" "fzf-tmux - open fzf in tmux split pane"24.TH fzf-tmux 1 "Mar 2021" "fzf 0.26.0" "fzf-tmux - open fzf in tmux split pane"2526.SH NAME27fzf-tmux - open fzf in tmux split pane28
40 unmodified lines
modified:man/man1/fzf.1+22 added-13 removed
19 unmodified lines
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN22THE SOFTWARE.23..24.TH fzf 1 "Feb 2021" "fzf 0.25.1" "fzf - a command-line fuzzy finder"24.TH fzf 1 "Mar 2021" "fzf 0.26.0" "fzf - a command-line fuzzy finder"2526.SH NAME27fzf - a command-line fuzzy finder28
412 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]][:~HEADER_LINES][:default]"445.BI "--preview-window=" "[POSITION][:SIZE[%]][:rounded|sharp|noborder][:[no]wrap][:[no]follow][:[no]cycle][:[no]hidden][:+SCROLL[OFFSETS][/DENOM]][:~HEADER_LINES][:default]"446447.RS448.B POSITION: (default: right)449 \fBup
29 unmodified lines
479* To change the style of the border of the preview window, specify one of480\fBrounded\fR (border with rounded edges, default), \fBsharp\fR (border with481sharp edges), or \fBnoborder\fR (no border).482483* \fB+SCROLL[-OFFSET]\fR determines the initial scroll offset of the preview484window. \fBSCROLL\fR can be either a numeric integer or a single-field index485expression that refers to a numeric integer. The optional \fB-OFFSET\fR part is486for 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.483* \fB[:+SCROLL[OFFSETS][/DENOM]]\fR determines the initial scroll offset of the484preview window.485486 - \fBSCROLL\fR can be either a numeric integer or a single-field index expression that refers to a numeric integer.487488 - The optional \fBOFFSETS\fR part is for adjusting the base offset. It should be given as a series of signed integers (\fB-INTEGER\fR or \fB+INTEGER\fR).489490 - The final \fB/DENOM\fR part is for specifying a fraction of the preview window height.491492* \fB~HEADER_LINES\fR keeps the top N lines as the fixed header so that they493are always visible.494495* \fBdefault\fR resets all options previously set to the default.
6 unmodified lines
502503 # Initial scroll offset is set to the line number of each line of504 # git grep output *minus* 5 lines (-5)505 git grep --line-number '' |504 fzf --delimiter : --preview 'nl {1}' --preview-window +{2}-5506 fzf --delimiter : --preview 'nl {1}' --preview-window '+{2}-5'507506 # Preview with bat, matching line in the middle of the window (-/2)508 # Preview with bat, matching line in the middle of the window below509 # the fixed header of the top 3 lines510 #511 # ~3 Top 3 lines as the fixed header512 # +{2} Base scroll offset extracted from the second field513 # +3 Extra offset to compensate for the 3-line header514 # /2 Put in the middle of the preview area515 #516 git grep --line-number '' |517 fzf --delimiter : \\509 --preview 'bat --style=numbers --color=always --highlight-line {2} {1}' \\510 --preview-window +{2}-/2\fR518 --preview 'bat --style=full --color=always --highlight-line {2} {1}' \\519 --preview-window '~3:+{2}+3/2'520521 # Display top 3 lines as the fixed header513 fzf --preview 'bat --style=header,grid --color=always {}' --preview-window '~3'522 fzf --preview 'bat --style=full --color=always {}' --preview-window '~3'\fR523.RE524525.SS Scripting526.TP
444 unmodified lines
modified:src/options_test.go+3 added-3 removed
387 unmodified lines
388 if !(opts.Preview.command == "cat {}" &&389 opts.Preview.hidden == true &&390 opts.Preview.wrap == true &&391 opts.Preview.position == posLeft &&392 opts.Preview.scroll == "{1}-/2" &&392 opts.Preview.scroll == "+{1}-/2" &&393 opts.Preview.size.percent == false &&394 opts.Preview.size.size == 15) {395 t.Error(opts.Preview)396 }397 opts = optsFor("--preview-window=up:15:wrap:hidden:+{1}-/2", "--preview-window=down", "--preview-window=cycle")397 opts = optsFor("--preview-window=up:15:wrap:hidden:+{1}+3-1-2/2", "--preview-window=down", "--preview-window=cycle")398 if !(opts.Preview.command == "" &&399 opts.Preview.hidden == true &&400 opts.Preview.wrap == true &&401 opts.Preview.cycle == true &&402 opts.Preview.position == posDown &&403 opts.Preview.scroll == "{1}-/2" &&403 opts.Preview.scroll == "+{1}+3-1-2/2" &&404 opts.Preview.size.percent == false &&405 opts.Preview.size.size == 15) {406 t.Error(opts.Preview.size.size)407 }
50 unmodified lines
modified:src/options.go+4 added-4 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]][:~HEADER_LINES]87 [:+SCROLL[OFFSETS][/DENOM]][:~HEADER_LINES]88 [:default]8990 Scripting91 -q, --query=STR Start the finder with the given query
985 unmodified lines
10771078func 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]*)?$")1081 offsetRegex := regexp.MustCompile(`^(\+{-?[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 "":
34 unmodified lines
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:]1124 opts.scroll = token1125 } else {1126 errorExit("invalid preview window option: " + token)1127 }1128 }
238 unmodified lines
1367 case "--no-preview":1368 opts.Preview.command = ""1369 case "--preview-window":1370 parsePreviewWindow(&opts.Preview,1371 nextString(allArgs, &i, "preview window layout required: [up|down|left|right][:SIZE[%]][:rounded|sharp|noborder][:wrap][:cycle][:hidden][:+SCROLL[-OFFSET]][:~HEADER_LINES][:default]"))1371 nextString(allArgs, &i, "preview window layout required: [up|down|left|right][:SIZE[%]][:rounded|sharp|noborder][:wrap][:cycle][:hidden][:+SCROLL[OFFSETS][/DENOM]][:~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+22 added-30 removed
20 unmodified lines
2122// import "github.com/pkg/profile"2324var placeholder *regexp.Regexp25var numericPrefix *regexp.Regexp25var whiteSuffix *regexp.Regexp26var offsetComponentRegex *regexp.Regexp27var offsetTrimCharsRegex *regexp.Regexp28var activeTempFiles []string2930const ellipsis string = ".."31const clearCode string = "\x1b[2J"3233func init() {34 placeholder = regexp.MustCompile(`\\?(?:{[+sf]*[0-9,-.]*}|{q}|{\+?f?nf?})`)34 numericPrefix = regexp.MustCompile(`^[[:punct:]]*([0-9]+)`)35 whiteSuffix = regexp.MustCompile(`\s*$`)36 offsetComponentRegex = regexp.MustCompile(`([+-][0-9]+)|(-?/[1-9][0-9]*)`)37 offsetTrimCharsRegex = regexp.MustCompile(`[^0-9/+-]`)38 activeTempFiles = []string{}39}4041type jumpMode int
1550 unmodified lines
1592 return replacePlaceholder(1593 template, t.ansi, t.delimiter, t.printsep, forcePlus, input, list)1594}15951594// Ascii to positive integer1595func atopi(s string) int {1596 matches := numericPrefix.FindStringSubmatch(s)1597 if len(matches) < 2 {1598 return 01599 }1600 n, e := strconv.Atoi(matches[1])1601 if e != nil || n < 1 {1602 return 01603 }1604 return n1605}16061596func (t *Terminal) evaluateScrollOffset(list []*Item, height int) int {1608 offsetExpr := t.replacePlaceholder(t.previewOpts.scroll, false, "", list)1609 nums := strings.Split(offsetExpr, "-")1610 switch len(nums) {1611 case 0:1612 return 01613 case 1, 2:1614 base := atopi(nums[0])1615 if base == 0 {1597 offsetExpr := offsetTrimCharsRegex.ReplaceAllString(1598 t.replacePlaceholder(t.previewOpts.scroll, false, "", list), "")15991600 atoi := func(s string) int {1601 n, e := strconv.Atoi(s)1602 if e != nil {1603 return 01617 } else if len(nums) == 1 {1618 return base - 11604 }1605 return n1606 }16071608 base := -11609 for _, component := range offsetComponentRegex.FindAllString(offsetExpr, -1) {1610 if strings.HasPrefix(component, "-/") {1611 component = component[1:]1612 }1620 if nums[1][0] == '/' {1621 denom := atopi(nums[1][1:])1613 if component[0] == '/' {1614 denom := atoi(component[1:])1615 if denom == 0 {1616 return base1617 }1618 return base - height/denom1619 }1627 return base - atopi(nums[1]) - 11628 default:1629 return 01620 base += atoi(component)1621 }1622 return base1623}16241625func replacePlaceholder(template string, stripAnsi bool, delimiter Delimiter, printsep string, forcePlus bool, query string, allItems []*Item) string {1626 current := allItems[:1]
1117 unmodified lines
modified:CHANGELOG.md+15 added
6 unmodified lines
7 ```sh8 # Display top 3 lines as the fixed header9 fzf --preview 'bat --style=header,grid --color=always {}' --preview-window '~3'10 ```11- More advanced preview offset expression to better support the fixed header12 ```sh13 # Preview with bat, matching line in the middle of the window below14 # the fixed header of the top 3 lines15 #16 # ~3 Top 3 lines as the fixed header17 # +{2} Base scroll offset extracted from the second field18 # +3 Extra offset to compensate for the 3-line header19 # /2 Put in the middle of the preview area20 #21 git grep --line-number '' |22 fzf --delimiter : \23 --preview 'bat --style=full --color=always --highlight-line {2} {1}' \24 --preview-window '~3:+{2}+3/2'25 ```26- Added `select` and `deselect` action for unconditionally selecting or27 deselecting a single item in `--multi` mode. Complements `toggle` action.28- Sigificant performance improvement in ANSI code processing29- Bug fixes and improvements
1081 unmodified lines