Add --freeze-right=N option to keep the rightmost N fields visible
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 07d53cb · Browse files · 5 files·+119 added−31 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+119 added−31 removed
- tests:+27 added−0 removed
- other:+92 added−31 removed
Provenance
- Commit
b9f2bf6- Tree
de3d21b- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
07d53cb
5 files changed+119 added−31 removed
modified:man/man1/fzf.1+5 added-1 removed
630 unmodified lines631 .TP632 .BI "\-\-freeze\-left=" "N"633 Number of fields to freeze on the left.634 .TP635+.BI "\-\-freeze\-right=" "N"636+Number of fields to freeze on the right.637+.TP638 .B "\-\-keep\-right"639 Keep the right end of the line visible when it's too long. Effective only when637-the query string is empty.640+the query string is empty. Use \fB\-\-freeze\-right=1\fR instead if you want641+the last field to be always visible even with a non-empty query.642 .TP643 .BI "\-\-scroll\-off=" "LINES"644 Number of screen lines to keep above or below when scrolling to the top or to645 the bottom (default: 3).1540 unmodified linesmodified:src/options.go+7 added-1 removed
103 unmodified lines104 --gap[=N] Render empty lines between each item105 --gap-line[=STR] Draw horizontal line on each gap using the string106 (default: '┈' or '-')107 --freeze-left=N Number of fields to freeze on the left108+ --freeze-right=N Number of fields to freeze on the right109 --keep-right Keep the right end of the line visible on overflow110 --scroll-off=LINES Number of screen lines to keep above or below when111 scrolling to the top or to the bottom (default: 0)112 --no-hscroll Disable horizontal scroll451 unmodified lines564 Case Case565 Normalize bool566 Nth []Range567 FreezeLeft int568+ FreezeRight int569 WithNth func(Delimiter) func([]Token, int32) string570 AcceptNth func(Delimiter) func([]Token, int32) string571 Delimiter Delimiter572 Sort int2129 unmodified lines2702 case "--freeze-left":2703 if opts.FreezeLeft, err = nextInt("number of fields required"); err != nil {2704 return err2705 }2706+ case "--freeze-right":2707+ if opts.FreezeRight, err = nextInt("number of fields required"); err != nil {2708+ return err2709+ }2710 case "--with-nth":2711 str, err := nextString("nth expression required")2712 if err != nil {2713 return err635 unmodified lines3349 if len(opts.JumpLabels) == 0 {3350 return errors.New("empty jump labels")3351 }3352 3347- if opts.FreezeLeft < 0 {3353+ if opts.FreezeLeft < 0 || opts.FreezeRight < 0 {3354 return errors.New("number of fields to freeze must be a non-negative integer")3355 }3356 3357 if validateJumpLabels {443 unmodified linesmodified:src/terminal.go+75 added-29 removed
330 unmodified lines331 scrollbar string332 previewScrollbar string333 ansi bool334 freezeLeft int335+ freezeRight int336 nthAttr tui.Attr337 nth []Range338 nthCurrent []Range339 acceptNth func([]Token, int32) string720 unmodified lines1060 footer: opts.Footer,1061 header0: opts.Header,1062 ansi: opts.Ansi,1063 freezeLeft: opts.FreezeLeft,1064+ freezeRight: opts.FreezeRight,1065 nthAttr: opts.Theme.Nth.Attr,1066 nth: opts.Nth,1067 nthCurrent: opts.Nth,1068 tabstop: opts.Tabstop,2468 unmodified lines3537 }3538 allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr, hidden)3539 3540 // Determine split offset for horizontal scrolling with freeze3539- splitOffset := -13540- if t.hscroll && !t.wrap && t.freezeLeft > 0 {3541- tokens := Tokenize(item.text.ToString(), t.delimiter)3542- if len(tokens) == 0 {3543- splitOffset = 03544- } else {3545- token := tokens[util.Min(t.freezeLeft, len(tokens))-1]3546- splitOffset = int(token.prefixLength) + token.text.Length() - token.text.TrailingWhitespaces()3541+ splitOffset1 := -13542+ splitOffset2 := -13543+ if t.hscroll && !t.wrap {3544+ var tokens []Token3545+ if t.freezeLeft > 0 || t.freezeRight > 0 {3546+ tokens = Tokenize(item.text.ToString(), t.delimiter)3547 }3548+ // 0 1 2| 3 |4 53549+ // -----> <---3550+ if t.freezeLeft > 0 {3551+ if len(tokens) > 0 {3552+ token := tokens[util.Min(t.freezeLeft, len(tokens))-1]3553+ splitOffset1 = int(token.prefixLength) + token.text.Length() - token.text.TrailingWhitespaces()3554+ }3555+ }3556+ if t.freezeRight > 0 {3557+ index := util.Max(t.freezeLeft-1, len(tokens)-t.freezeRight-1)3558+ if index < 0 {3559+ splitOffset2 = 03560+ } else if index >= t.freezeLeft {3561+ token := tokens[index]3562+ splitOffset2 = int(token.prefixLength) + token.text.Length()3563+ }3564+ splitOffset2 = util.Max(splitOffset2, splitOffset1)3565+ }3566 }3567 3568 maxLines := 13569 if t.canSpanMultiLines() {63 unmodified lines3633 allOffsets = append([]colorOffset{dupe}, allOffsets[idx+1:]...)3634 break3635 }3636 }3619- splitOffsetLocal := 03620- if splitOffset >= 0 && splitOffset > from && splitOffset < from+len(line) {3621- splitOffsetLocal = splitOffset - from3637+ splitOffsetLeft := 03638+ if splitOffset1 >= 0 && splitOffset1 > from && splitOffset1 < from+len(line) {3639+ splitOffsetLeft = splitOffset1 - from3640 }3641+ splitOffsetRight := -13642+ if splitOffset2 >= 0 && splitOffset2 >= from && splitOffset2 < from+len(line) {3643+ splitOffsetRight = splitOffset2 - from3644+ }3645 from += len(line)3646 if lineOffset < skipLines {3647 continue3648 }3649 actualLineOffset := lineOffset - skipLines3650 3629- var maxe int3651+ var maxEnd int3652 for _, offset := range offsets {3653 if offset.match {3632- maxe = util.Max(maxe, int(offset.offset[1]))3654+ maxEnd = util.Max(maxEnd, int(offset.offset[1]))3655 }3656 }3657 3658 actualLineNum := lineNum55 unmodified lines3714 } else {3715 wrapped = true3716 }3717 3696- frozen := line[:splitOffsetLocal]3697- rest := line[splitOffsetLocal:]3718+ frozenLeft := line[:splitOffsetLeft]3719+ middle := line[splitOffsetLeft:]3720+ frozenRight := []rune{}3721+ if splitOffsetRight >= splitOffsetLeft {3722+ middle = line[splitOffsetLeft:splitOffsetRight]3723+ frozenRight = line[splitOffsetRight:]3724+ }3725 displayWidthSum := 03726+ todo := [3]func(){}3699- for fidx, runes := range [][]rune{frozen, rest} {3727+ for fidx, runes := range [][]rune{frozenLeft, frozenRight, middle} {3728 if len(runes) == 0 {3729 continue3730 }3703- if splitOffsetLocal > 0 && fidx == 1 {3704- for idx := range offsets {3705- offsets[idx].offset[0] -= int32(splitOffsetLocal)3706- offsets[idx].offset[1] -= int32(splitOffsetLocal)3731+ shift := 03732+ maxe := maxEnd3733+ offs := make([]colorOffset, len(offsets))3734+ for idx := range offsets {3735+ offs[idx] = offsets[idx]3736+ if fidx == 1 && splitOffsetRight > 0 {3737+ shift = splitOffsetRight3738+ } else if fidx == 2 && splitOffsetLeft > 0 {3739+ shift = splitOffsetLeft3740 }3708- maxe -= splitOffsetLocal3741+ offs[idx].offset[0] -= int32(shift)3742+ offs[idx].offset[1] -= int32(shift)3743 }3744+ maxe -= shift3745 displayWidth = t.displayWidthWithLimit(runes, 0, maxWidth)3746 if !t.wrap && displayWidth > maxWidth {3747 ellipsis, ellipsisWidth := util.Truncate(t.ellipsis, maxWidth/2)3748 maxe = util.Constrain(maxe+util.Min(maxWidth/2-ellipsisWidth, t.hscrollOff), 0, len(runes))3749 transformOffsets := func(diff int32, rightTrim bool) {3715- for idx, offset := range offsets {3750+ for idx, offset := range offs {3751 b, e := offset.offset[0], offset.offset[1]3752 el := int32(len(ellipsis))3753 b += el - diff3754 e += el - diff3755 b = util.Max32(b, el)3756 if rightTrim {3757 e = util.Min32(e, int32(maxWidth-ellipsisWidth))3758 }3724- offsets[idx].offset[0] = b3725- offsets[idx].offset[1] = util.Max32(b, e)3759+ offs[idx].offset[0] = b3760+ offs[idx].offset[1] = util.Max32(b, e)3761 }3762 }3763 if t.hscroll {3729- if fidx > 0 && t.keepRight && pos == nil {3764+ if fidx == 1 || fidx == 2 && t.keepRight && pos == nil {3765 trimmed, diff := t.trimLeft(runes, maxWidth, ellipsisWidth)3766 transformOffsets(diff, false)3767 runes = append(ellipsis, trimmed...)3768 } else if fidx == 0 || !t.overflow(runes[:maxe], maxWidth-ellipsisWidth) {18 unmodified lines3787 } else {3788 runes, _ = t.trimRight(runes, maxWidth-ellipsisWidth)3789 runes = append(runes, ellipsis...)3790 3756- for idx, offset := range offsets {3757- offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-len(ellipsis)))3758- offsets[idx].offset[1] = util.Min32(offset.offset[1], int32(maxWidth))3791+ for idx, offset := range offs {3792+ offs[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-len(ellipsis)))3793+ offs[idx].offset[1] = util.Min32(offset.offset[1], int32(maxWidth))3794 }3795 }3796 displayWidth = t.displayWidthWithLimit(runes, 0, displayWidth)3797 }3 unmodified lines3801 color := colBase3802 if hidden {3803 color = color.WithFg(t.theme.Nomatch)3804 }3805+ todo[fidx] = func() {3770- t.printColoredString(t.window, runes, offsets, color)3806+ t.printColoredString(t.window, runes, offs, color)3807+ }3808 } else {3809 break3810 }3811 maxWidth -= displayWidth3812 }3813+ if todo[0] != nil {3814+ todo[0]()3815+ }3816+ if todo[2] != nil {3817+ todo[2]()3818+ }3819+ if todo[1] != nil {3820+ todo[1]()3821+ }3822 if postTask != nil {3823 postTask(actualLineNum, displayWidthSum, wasWrapped, forceRedraw, lbg)3824 } else {3825 t.markOtherLine(actualLineNum)3538 unmodified linesmodified:test/test_core.rb+27 added
1199 unmodified lines1200 tmux.send_keys :Space1201 tmux.until { |lines| assert lines.any_include?('1␊2␊3␊4␊5␊') }1202 end1203 1204+ def test_freeze_left_and_right1205+ tmux.send_keys %[seq 10000 | tr "\n" ' ' | #{FZF} --freeze-left 3 --freeze-right 3 --ellipsis XX], :Enter1206+ tmux.until { |lines| assert_match(/XX9998 9999 10000$/, lines[-3]) }1207+ tmux.send_keys "'1000"1208+ tmux.until { |lines| assert_match(/^> 1 2 3XX.*XX9998 9999 10000$/,lines[-3]) }1209+ end1210+1211+ def test_freeze_right_exceed_range1212+ tmux.send_keys %[seq 10000 | tr "\n" ' ' | #{FZF} --freeze-right 100000 --ellipsis XX], :Enter1213+ ['', "'1000"].each do |query|1214+ tmux.send_keys query1215+ tmux.until { |lines| assert lines.any_include?("> #{query}".strip) }1216+ tmux.until do |lines|1217+ assert_match(/ 9998 9999 10000$/, lines[-3])1218+ assert_equal(1, lines[-3].scan('XX').size)1219+ end1220+ end1221+ end1222+1223+ def test_freeze_right_exceed_range_with_freeze_left1224+ tmux.send_keys %[seq 10000 | tr "\n" ' ' | #{FZF} --freeze-left 3 --freeze-right 100000 --ellipsis XX], :Enter1225+ tmux.until do |lines|1226+ assert_match(/^> 1 2 3XX.*9998 9999 10000$/, lines[-3])1227+ assert_equal(1, lines[-3].scan('XX').size)1228+ end1229+ end1230+1231 def test_backward_eof1232 tmux.send_keys "echo foo | #{FZF} --bind 'backward-eof:reload(seq 100)'", :Enter1233 tmux.until { |lines| lines.item_count == 1 && lines.match_count == 1 }1234 tmux.send_keys 'x'907 unmodified linesmodified:CHANGELOG.md+5 added
11 unmodified lines12 # Used with --keep-right13 git grep --line-number --color=always -- '' |14 fzf --ansi --delimiter : --freeze-left 1 --keep-right15 ```16+- Also added `--freeze-right=N` option to keep the rightmost N columns visible.17+ ```sh18+ fd | fzf --freeze-right 1 --delimiter /19+ fd | fzf --freeze-left 1 --freeze-right 1 --delimiter /20+ ```21 22 0.66.123 ------24 - Bug fixes3087 unmodified lines