Add --freeze-left=N option to keep the leftmost 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 ead534a · Browse files · 5 files·+126 added−52 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+126 added−52 removed
- tests:+11 added−0 removed
- other:+115 added−52 removed
Provenance
- Commit
07d53cb- Tree
0d64813- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
ead534a
5 files changed+126 added−52 removed
modified:man/man1/fzf.1+3 added
627 unmodified lines628 .BI "\-\-gap\-line" "[=STR]"629 The given string will be repeated to draw a horizontal line on each gap630 (default: '┈' or '\-' depending on \fB\-\-no\-unicode\fR).631 .TP632+.BI "\-\-freeze\-left=" "N"633+Number of fields to freeze on the left.634+.TP635 .B "\-\-keep\-right"636 Keep the right end of the line visible when it's too long. Effective only when637 the query string is empty.638 .TP1543 unmodified linesmodified:src/options.go+10 added
102 unmodified lines103 --tac Reverse the order of the input104 --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 --keep-right Keep the right end of the line visible on overflow109 --scroll-off=LINES Number of screen lines to keep above or below when110 scrolling to the top or to the bottom (default: 0)111 --no-hscroll Disable horizontal scroll450 unmodified lines562 Inputless bool563 Case Case564 Normalize bool565 Nth []Range566+ FreezeLeft int567 WithNth func(Delimiter) func([]Token, int32) string568 AcceptNth func(Delimiter) func([]Token, int32) string569 Delimiter Delimiter570 Sort int2125 unmodified lines2696 }2697 if opts.Nth, err = splitNth(str); err != nil {2698 return err2699 }2700+ case "--freeze-left":2701+ if opts.FreezeLeft, err = nextInt("number of fields required"); err != nil {2702+ return err2703+ }2704 case "--with-nth":2705 str, err := nextString("nth expression required")2706 if err != nil {2707 return err635 unmodified lines3343 if len(opts.JumpLabels) == 0 {3344 return errors.New("empty jump labels")3345 }3346 3347+ if opts.FreezeLeft < 0 {3348+ return errors.New("number of fields to freeze must be a non-negative integer")3349+ }3350+3351 if validateJumpLabels {3352 for _, r := range opts.JumpLabels {3353 if r < 32 || r > 126 {3354 return errors.New("non-ascii jump labels are not allowed")440 unmodified linesmodified:src/terminal.go+89 added-52 removed
329 unmodified lines330 ellipsis string331 scrollbar string332 previewScrollbar string333 ansi bool334+ freezeLeft int335 nthAttr tui.Attr336 nth []Range337 nthCurrent []Range338 acceptNth func([]Token, int32) string719 unmodified lines1058 header: []string{},1059 footer: opts.Footer,1060 header0: opts.Header,1061 ansi: opts.Ansi,1062+ freezeLeft: opts.FreezeLeft,1063 nthAttr: opts.Theme.Nth.Attr,1064 nth: opts.Nth,1065 nthCurrent: opts.Nth,1066 tabstop: opts.Tabstop,2467 unmodified lines3534 }3535 }3536 allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr, hidden)3537 3538+ // 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()3547+ }3548+ }3549+3550 maxLines := 13551 if t.canSpanMultiLines() {3552 maxLines = maxLineNum - lineNum + 13553 }61 unmodified lines3615 allOffsets = append([]colorOffset{dupe}, allOffsets[idx+1:]...)3616 break3617 }3618 }3619+ splitOffsetLocal := 03620+ if splitOffset >= 0 && splitOffset > from && splitOffset < from+len(line) {3621+ splitOffsetLocal = splitOffset - from3622+ }3623 from += len(line)3624 if lineOffset < skipLines {3625 continue3626 }65 unmodified lines3692 } else {3693 wrapped = true3694 }3695 3678- displayWidth = t.displayWidthWithLimit(line, 0, maxWidth)3679- if !t.wrap && displayWidth > maxWidth {3680- ellipsis, ellipsisWidth := util.Truncate(t.ellipsis, maxWidth/2)3681- maxe = util.Constrain(maxe+util.Min(maxWidth/2-ellipsisWidth, t.hscrollOff), 0, len(line))3682- transformOffsets := func(diff int32, rightTrim bool) {3683- for idx, offset := range offsets {3684- b, e := offset.offset[0], offset.offset[1]3685- el := int32(len(ellipsis))3686- b += el - diff3687- e += el - diff3688- b = util.Max32(b, el)3689- if rightTrim {3690- e = util.Min32(e, int32(maxWidth-ellipsisWidth))3691- }3692- offsets[idx].offset[0] = b3693- offsets[idx].offset[1] = util.Max32(b, e)3696+ frozen := line[:splitOffsetLocal]3697+ rest := line[splitOffsetLocal:]3698+ displayWidthSum := 03699+ for fidx, runes := range [][]rune{frozen, rest} {3700+ if len(runes) == 0 {3701+ continue3702+ }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)3707 }3708+ maxe -= splitOffsetLocal3709 }3696- if t.hscroll {3697- if t.keepRight && pos == nil {3698- trimmed, diff := t.trimLeft(line, maxWidth, ellipsisWidth)3699- transformOffsets(diff, false)3700- line = append(ellipsis, trimmed...)3701- } else if !t.overflow(line[:maxe], maxWidth-ellipsisWidth) {3702- // Stri..3703- line, _ = t.trimRight(line, maxWidth-ellipsisWidth)3704- line = append(line, ellipsis...)3705- } else {3706- // Stri..3707- rightTrim := false3708- if t.overflow(line[maxe:], ellipsisWidth) {3709- line = append(line[:maxe], ellipsis...)3710- rightTrim = true3710+ displayWidth = t.displayWidthWithLimit(runes, 0, maxWidth)3711+ if !t.wrap && displayWidth > maxWidth {3712+ ellipsis, ellipsisWidth := util.Truncate(t.ellipsis, maxWidth/2)3713+ maxe = util.Constrain(maxe+util.Min(maxWidth/2-ellipsisWidth, t.hscrollOff), 0, len(runes))3714+ transformOffsets := func(diff int32, rightTrim bool) {3715+ for idx, offset := range offsets {3716+ b, e := offset.offset[0], offset.offset[1]3717+ el := int32(len(ellipsis))3718+ b += el - diff3719+ e += el - diff3720+ b = util.Max32(b, el)3721+ if rightTrim {3722+ e = util.Min32(e, int32(maxWidth-ellipsisWidth))3723+ }3724+ offsets[idx].offset[0] = b3725+ offsets[idx].offset[1] = util.Max32(b, e)3726 }3712- // ..ri..3713- var diff int323714- line, diff = t.trimLeft(line, maxWidth, ellipsisWidth)3715-3716- // Transform offsets3717- transformOffsets(diff, rightTrim)3718- line = append(ellipsis, line...)3727 }3720- } else {3721- line, _ = t.trimRight(line, maxWidth-ellipsisWidth)3722- line = append(line, ellipsis...)3728+ if t.hscroll {3729+ if fidx > 0 && t.keepRight && pos == nil {3730+ trimmed, diff := t.trimLeft(runes, maxWidth, ellipsisWidth)3731+ transformOffsets(diff, false)3732+ runes = append(ellipsis, trimmed...)3733+ } else if fidx == 0 || !t.overflow(runes[:maxe], maxWidth-ellipsisWidth) {3734+ // Stri..3735+ runes, _ = t.trimRight(runes, maxWidth-ellipsisWidth)3736+ runes = append(runes, ellipsis...)3737+ } else {3738+ // Stri..3739+ rightTrim := false3740+ if t.overflow(runes[maxe:], ellipsisWidth) {3741+ runes = append(runes[:maxe], ellipsis...)3742+ rightTrim = true3743+ }3744+ // ..ri..3745+ var diff int323746+ runes, diff = t.trimLeft(runes, maxWidth, ellipsisWidth)3747 3748+ // Transform offsets3749+ transformOffsets(diff, rightTrim)3750+ runes = append(ellipsis, runes...)3751+ }3752+ } else {3753+ runes, _ = t.trimRight(runes, maxWidth-ellipsisWidth)3754+ runes = append(runes, ellipsis...)3755+3724- for idx, offset := range offsets {3725- offsets[idx].offset[0] = util.Min32(offset.offset[0], int32(maxWidth-len(ellipsis)))3726- offsets[idx].offset[1] = util.Min32(offset.offset[1], int32(maxWidth))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))3759+ }3760 }3761+ displayWidth = t.displayWidthWithLimit(runes, 0, displayWidth)3762 }3729- displayWidth = t.displayWidthWithLimit(line, 0, displayWidth)3730- }3763+ displayWidthSum += displayWidth3764 3732- if maxWidth > 0 {3733- color := colBase3734- if hidden {3735- color = color.WithFg(t.theme.Nomatch)3765+ if maxWidth > 0 {3766+ color := colBase3767+ if hidden {3768+ color = color.WithFg(t.theme.Nomatch)3769+ }3770+ t.printColoredString(t.window, runes, offsets, color)3771+ } else {3772+ break3773 }3737- t.printColoredString(t.window, line, offsets, color)3774+ maxWidth -= displayWidth3775 }3776 if postTask != nil {3740- postTask(actualLineNum, displayWidth, wasWrapped, forceRedraw, lbg)3777+ postTask(actualLineNum, displayWidthSum, wasWrapped, forceRedraw, lbg)3778 } else {3779 t.markOtherLine(actualLineNum)3780 }3781 lineNum += 13536 unmodified linesmodified:test/test_core.rb+11 added
1188 unmodified lines1189 tmux.send_keys :Space1190 tmux.until { |lines| assert lines.any_include?('9999␊10000') }1191 end1192 1193+ def test_freeze_left_keep_right1194+ tmux.send_keys %[seq 10000 | #{FZF} --read0 --delimiter "\n" --freeze-left 3 --keep-right --ellipsis XX --no-multi-line --bind space:toggle-multi-line], :Enter1195+ tmux.until { |lines| assert_match(/^> 1␊2␊3XX.*10000␊$/, lines[-3]) }1196+ tmux.send_keys '5'1197+ tmux.until { |lines| assert_match(/^> 1␊2␊3␊4␊5␊.*XX$/, lines[-3]) }1198+ tmux.send_keys :Space1199+ tmux.until { |lines| assert lines.any_include?('> 1') }1200+ tmux.send_keys :Space1201+ tmux.until { |lines| assert lines.any_include?('1␊2␊3␊4␊5␊') }1202+ end1203+1204 def test_backward_eof1205 tmux.send_keys "echo foo | #{FZF} --bind 'backward-eof:reload(seq 100)'", :Enter1206 tmux.until { |lines| lines.item_count == 1 && lines.match_count == 1 }1207 tmux.send_keys 'x'907 unmodified linesmodified:CHANGELOG.md+13 added
1 CHANGELOG2 =========3 4+0.67.05+------6+- Added `--freeze-left=N` option to keep the leftmost N columns visible.7+ ```sh8+ # Keeps the file name column fixed and always visible9+ git grep --line-number --color=always -- '' |10+ fzf --ansi --delimiter : --freeze-left 111+12+ # Used with --keep-right13+ git grep --line-number --color=always -- '' |14+ fzf --ansi --delimiter : --freeze-left 1 --keep-right15+ ```16+17 0.66.118 ------19 - Bug fixes20 - Fixed a bug preventing 'ctrl-h' from being bound to an action (#4556)3086 unmodified lines