Skip to content
Myr

Allow displaying --nth parts in a different text style

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

Close #4183

authored by Junegunn Choi · committed by Junegunn Choi · parent b42f5bf · Browse files · 8 files+128 added−36 removed
This commit

State

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

Diff stats

8 files+128 added−36 removed

  • other:+128 added−36 removed

8 files+128 added−36 removed

modified:man/man1/fzf.1+1 added
240 unmodified lines
241 \fBpointer \fRPointer to the current line242 \fBmarker \fRMulti\-select marker243 \fBspinner \fRStreaming input indicator244 \fBheader (header\-fg) \fRHeader245 \fBnth \fRParts of the line specified by \fB\-\-nth\fR (only supports attributes)246247.B ANSI COLORS:248 \fB\-1 \fRDefault terminal foreground/background color249 \fB \fR(or the original color of the text)
1551 unmodified lines
modified:src/tui/dummy.go+4 added
9 unmodified lines
1011var DefaultBorderShape = BorderRounded1213func (a Attr) Merge(b Attr) Attr {14 if b == AttrRegular {15 return b16 }1718 return a | b19}2021const (
37 unmodified lines
modified:src/tui/tui.go+10 added
203 unmodified lines
204 Color Color205 Attr Attr206}207208func (a ColorAttr) IsColorDefined() bool {209 return a.Color != colUndefined210}211212func NewColorAttr() ColorAttr {213 return ColorAttr{Color: colUndefined, Attr: AttrUndefined}214}215
92 unmodified lines
308 Fg ColorAttr309 Bg ColorAttr310 ListFg ColorAttr311 ListBg ColorAttr312 Nth ColorAttr313 SelectedFg ColorAttr314 SelectedBg ColorAttr315 SelectedMatch ColorAttr316 PreviewFg ColorAttr
390 unmodified lines
707 InputLabel: ColorAttr{colUndefined, AttrUndefined},708 HeaderBg: ColorAttr{colUndefined, AttrUndefined},709 HeaderBorder: ColorAttr{colUndefined, AttrUndefined},710 HeaderLabel: ColorAttr{colUndefined, AttrUndefined},711 Nth: ColorAttr{colUndefined, AttrUndefined},712 }713}714715func NoColorTheme() *ColorTheme {
35 unmodified lines
751 InputLabel: ColorAttr{colDefault, AttrUndefined},752 HeaderBg: ColorAttr{colDefault, AttrUndefined},753 HeaderBorder: ColorAttr{colDefault, AttrUndefined},754 HeaderLabel: ColorAttr{colDefault, AttrUndefined},755 Nth: ColorAttr{colUndefined, AttrUndefined},756 }757}758759func init() {
32 unmodified lines
792 Scrollbar: ColorAttr{colUndefined, AttrUndefined},793 InputBg: ColorAttr{colUndefined, AttrUndefined},794 InputBorder: ColorAttr{colUndefined, AttrUndefined},795 InputLabel: ColorAttr{colUndefined, AttrUndefined},796 Nth: ColorAttr{colUndefined, AttrUndefined},797 }798 Dark256 = &ColorTheme{799 Colored: true,800 Input: ColorAttr{colDefault, AttrUndefined},
29 unmodified lines
830 Scrollbar: ColorAttr{colUndefined, AttrUndefined},831 InputBg: ColorAttr{colUndefined, AttrUndefined},832 InputBorder: ColorAttr{colUndefined, AttrUndefined},833 InputLabel: ColorAttr{colUndefined, AttrUndefined},834 Nth: ColorAttr{colUndefined, AttrUndefined},835 }836 Light256 = &ColorTheme{837 Colored: true,838 Input: ColorAttr{colDefault, AttrUndefined},
32 unmodified lines
871 InputLabel: ColorAttr{colUndefined, AttrUndefined},872 HeaderBg: ColorAttr{colUndefined, AttrUndefined},873 HeaderBorder: ColorAttr{colUndefined, AttrUndefined},874 HeaderLabel: ColorAttr{colUndefined, AttrUndefined},875 Nth: ColorAttr{colUndefined, AttrUndefined},876 }877}878879func InitTheme(theme *ColorTheme, baseTheme *ColorTheme, forceBlack bool, hasInputWindow bool, hasHeaderWindow bool) {
134 unmodified lines
modified:src/options.go+6 added
1205 unmodified lines
1206 case "selected-fg":1207 mergeAttr(&theme.SelectedFg)1208 case "selected-bg":1209 mergeAttr(&theme.SelectedBg)1210 case "nth":1211 mergeAttr(&theme.Nth)1212 case "gutter":1213 mergeAttr(&theme.Gutter)1214 case "hl":1215 mergeAttr(&theme.Match)
1751 unmodified lines
2967 }2968 }2969 }29702971 if opts.Theme.Nth.IsColorDefined() {2972 return errors.New("only ANSI attributes are allowed for 'nth' (regular, bold, underline, reverse, dim, italic, strikethrough)")2973 }29742975 return nil2976}29772978// This function can have side-effects and alter some global states.
235 unmodified lines
modified:src/result_test.go+26 added-16 removed
129 unmodified lines
130 {[2]int32{33, 40}, ansiState{4, 8, tui.Bold, -1, nil}}}}}131132 colBase := tui.NewColorPair(89, 189, tui.AttrUndefined)133 colMatch := tui.NewColorPair(99, 199, tui.AttrUndefined)134 colors := item.colorOffsets(offsets, tui.Dark256, colBase, colMatch, true)134 colors := item.colorOffsets(offsets, nil, tui.Dark256, colBase, colMatch, tui.AttrUndefined, true)135 assert := func(idx int, b int32, e int32, c tui.ColorPair) {136 o := colors[idx]137 if o.offset[0] != b || o.offset[1] != e || o.color != c {138 t.Error(o, b, e, c)
15 unmodified lines
154 assert(9, 35, 40, tui.NewColorPair(4, 8, tui.Bold))155156 colRegular := tui.NewColorPair(-1, -1, tui.AttrUndefined)157 colUnderline := tui.NewColorPair(-1, -1, tui.Underline)158 colors = item.colorOffsets(offsets, tui.Dark256, colRegular, colUnderline, true)158159 nthOffsets := []Offset{{37, 39}, {42, 45}}160 for _, attr := range []tui.Attr{tui.AttrRegular, tui.StrikeThrough} {161 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, attr, true)162160 // [{[0 5] {1 5 0}} {[5 15] {1 5 8}} {[15 20] {1 5 0}}161 // {[22 25] {2 6 1}} {[25 27] {2 6 9}} {[27 30] {-1 -1 8}}162 // {[30 32] {3 7 8}} {[32 33] {-1 -1 8}} {[33 35] {4 8 9}}163 // {[35 40] {4 8 1}}]164 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))165 assert(1, 5, 15, tui.NewColorPair(1, 5, tui.Underline))166 assert(2, 15, 20, tui.NewColorPair(1, 5, tui.AttrUndefined))167 assert(3, 22, 25, tui.NewColorPair(2, 6, tui.Bold))168 assert(4, 25, 27, tui.NewColorPair(2, 6, tui.Bold|tui.Underline))169 assert(5, 27, 30, colUnderline)170 assert(6, 30, 32, tui.NewColorPair(3, 7, tui.Underline))171 assert(7, 32, 33, colUnderline)172 assert(8, 33, 35, tui.NewColorPair(4, 8, tui.Bold|tui.Underline))173 assert(9, 35, 40, tui.NewColorPair(4, 8, tui.Bold))163 // [{[0 5] {1 5 0}} {[5 15] {1 5 8}} {[15 20] {1 5 0}}164 // {[22 25] {2 6 1}} {[25 27] {2 6 9}} {[27 30] {-1 -1 8}}165 // {[30 32] {3 7 8}} {[32 33] {-1 -1 8}} {[33 35] {4 8 9}}166 // {[35 37] {4 8 1}} {[37 39] {4 8 x|1}} {[39 40] {4 8 x|1}}]167 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))168 assert(1, 5, 15, tui.NewColorPair(1, 5, tui.Underline))169 assert(2, 15, 20, tui.NewColorPair(1, 5, tui.AttrUndefined))170 assert(3, 22, 25, tui.NewColorPair(2, 6, tui.Bold))171 assert(4, 25, 27, tui.NewColorPair(2, 6, tui.Bold|tui.Underline))172 assert(5, 27, 30, colUnderline)173 assert(6, 30, 32, tui.NewColorPair(3, 7, tui.Underline))174 assert(7, 32, 33, colUnderline)175 assert(8, 33, 35, tui.NewColorPair(4, 8, tui.Bold|tui.Underline))176 assert(9, 35, 37, tui.NewColorPair(4, 8, tui.Bold))177 expected := tui.Bold | attr178 if attr == tui.AttrRegular {179 expected = tui.AttrRegular180 }181 assert(10, 37, 39, tui.NewColorPair(4, 8, expected))182 assert(11, 39, 40, tui.NewColorPair(4, 8, tui.Bold))183 }184}
modified:src/result.go+44 added-18 removed
102 unmodified lines
103func minRank() Result {104 return Result{item: &minItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}}105}106107func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, current bool) []colorOffset {107func (result *Result) colorOffsets(matchOffsets []Offset, nthOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, attrNth tui.Attr, current bool) []colorOffset {108 itemColors := result.item.Colors()109110 // No ANSI codes111 if len(itemColors) == 0 {111 if len(itemColors) == 0 && len(nthOffsets) == 0 {112 var offsets []colorOffset113 for _, off := range matchOffsets {114 offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: colMatch, match: true})115 }
1 unmodified line
117 }118119 // Find max column120 var maxCol int32121 for _, off := range matchOffsets {121 for _, off := range append(matchOffsets, nthOffsets...) {122 if off[1] > maxCol {123 maxCol = off[1]124 }125 }
2 unmodified lines
128 maxCol = ansi.offset[1]129 }130 }131132 type cellInfo struct {133 index int134 color bool135 match bool136 nth bool137 }138132 cols := make([]int, maxCol)139 cols := make([]cellInfo, maxCol)140 for colorIndex, ansi := range itemColors {141 for i := ansi.offset[0]; i < ansi.offset[1]; i++ {135 cols[i] = colorIndex + 1 // 1-based index of itemColors142 cols[i] = cellInfo{colorIndex, true, false, false}143 }144 }145146 for _, off := range matchOffsets {147 for i := off[0]; i < off[1]; i++ {141 // Negative of 1-based index of itemColors142 // - The extra -1 means highlighted143 if cols[i] >= 0 {144 cols[i] = cols[i]*-1 - 1145 }148 cols[i].match = true149 }150 }151152 for _, off := range nthOffsets {153 // Exclude the whole line154 if int(off[1])-int(off[0]) == result.item.text.Length() {155 continue156 }157 for i := off[0]; i < off[1]; i++ {158 cols[i].nth = true159 }160 }161162 // sort.Sort(ByOrder(offsets))163164 // Merge offsets165 // ------------ ---- -- ----166 // ++++++++ ++++++++++167 // --++++++++-- --++++++++++---155 curr := 0168 var curr cellInfo = cellInfo{0, false, false, false}169 start := 0170 ansiToColorPair := func(ansi ansiOffset, base tui.ColorPair) tui.ColorPair {171 fg := ansi.color.fg172 bg := ansi.color.bg
14 unmodified lines
187 return tui.NewColorPair(fg, bg, ansi.color.attr).MergeAttr(base)188 }189 var colors []colorOffset190 add := func(idx int) {178 if curr != 0 && idx > start {179 if curr < 0 {191 if (curr.color || curr.nth || curr.match) && idx > start {192 if curr.match {193 color := colMatch194 var url *url182 if curr < -1 && theme.Colored {183 ansi := itemColors[-curr-2]195 if curr.color && theme.Colored {196 ansi := itemColors[curr.index]197 url = ansi.color.url198 origColor := ansiToColorPair(ansi, colMatch)199 // hl or hl+ only sets the foreground color, so colMatch is the200 // combination of either [hl and bg] or [hl+ and bg+].
8 unmodified lines
209 } else {210 color = origColor.MergeNonDefault(color)211 }212 }213 if curr.nth {214 color = color.WithAttr(attrNth)215 }216 colors = append(colors, colorOffset{217 offset: [2]int32{int32(start), int32(idx)}, color: color, match: true, url: url})202 } else {203 ansi := itemColors[curr-1]218 } else if curr.color {219 ansi := itemColors[curr.index]220 color := ansiToColorPair(ansi, colBase)221 if curr.nth {222 color = color.WithAttr(attrNth)223 }224 colors = append(colors, colorOffset{225 offset: [2]int32{int32(start), int32(idx)},206 color: ansiToColorPair(ansi, colBase),226 color: color,227 match: false,228 url: ansi.color.url})229 } else {230 colors = append(colors, colorOffset{231 offset: [2]int32{int32(start), int32(idx)},232 color: colBase.WithAttr(attrNth),233 match: false,234 url: nil})235 }236 }237 }238 for idx, col := range cols {
56 unmodified lines
modified:src/terminal.go+22 added-2 removed
299 unmodified lines
300 ellipsis string301 scrollbar string302 previewScrollbar string303 ansi bool304 nthAttr tui.Attr305 nth []Range306 nthCurrent []Range307 tabstop int308 margin [4]sizeSpec309 padding [4]sizeSpec310 unicode bool
575 unmodified lines
886 gap: opts.Gap,887 header: []string{},888 header0: opts.Header,889 ansi: opts.Ansi,890 nthAttr: opts.Theme.Nth.Attr,891 nth: opts.Nth,892 nthCurrent: opts.Nth,893 tabstop: opts.Tabstop,894 hasStartActions: false,895 hasResultActions: false,896 hasFocusActions: false,
277 unmodified lines
1174 var offsets []colorOffset1175 printFn := func(window tui.Window, limit int) {1176 if offsets == nil {1177 // tui.Col* are not initialized until renderer.Init()1174 offsets = result.colorOffsets(nil, t.theme, *color, *color, false)1178 offsets = result.colorOffsets(nil, nil, t.theme, *color, *color, t.nthAttr, false)1179 }1180 for limit > 0 {1181 if length > limit {1182 trimmedRunes, _ := t.trimRight(runes, limit)
1537 unmodified lines
2720 charOffsets[idx] = offset2721 }2722 sort.Sort(ByOrder(charOffsets))2723 }2724 var nthOffsets []Offset2725 if len(t.nth) > 0 && postTask != nil {2726 var tokens []Token2727 if item.transformed != nil {2728 tokens = item.transformed.tokens2729 } else {2730 tokens = Transform(Tokenize(item.text.ToString(), t.delimiter), t.nthCurrent)2731 }2732 for _, token := range tokens {2733 start := token.prefixLength2734 end := start + int32(token.text.Length())2735 nthOffsets = append(nthOffsets, Offset{int32(start), int32(end)})2736 }2737 sort.Sort(ByOrder(nthOffsets))2738 }2720 allOffsets := result.colorOffsets(charOffsets, t.theme, colBase, colMatch, current)2739 allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr, current)27402741 maxLines := 12742 if t.canSpanMultiLines() {2743 maxLines = maxLineNum - lineNum + 1
1941 unmodified lines
4685 } else {4686 // The default4687 newNth = &t.nth4688 }4689 t.nthCurrent = *newNth4690 // Cycle4691 if len(tokens) > 1 {4692 a.a = strings.Join(append(tokens[1:], tokens[0]), "|")4693 }
1144 unmodified lines
modified:CHANGELOG.md+15 added
80 unmodified lines
81 ```sh82 # Start with --nth 1, then 2, then 3, then back to the default, 183 echo 'foo foobar foobarbaz' | fzf --bind 'space:change-nth(2|3|)' --nth 1 -q foo84 ```85- `--nth` parts of each line can now be rendered in a different text style86 ```sh87 # nth in a different style88 ls -al | fzf --nth -1 --color nth:italic89 ls -al | fzf --nth -1 --color nth:reverse90 ls -al | fzf --nth -1 --color nth:reverse:bold9192 # Dim the other parts93 ls -al | fzf --nth -1 --color nth:regular,fg:dim,current-fg:dim9495 # With 'change-nth'96 ps -ef | fzf --reverse --header-lines 1 --header-border bottom --input-border \97 --color nth:regular,fg:dim,current-fg:dim \98 --nth 8.. --bind 'ctrl-n:change-nth(..|1|2|3|4|5|6|7|)'99 ```100- A single-character delimiter is now treated as a plain string delimiter rather than a regular expression delimiter, even if it's a regular expression meta-character.101 - This means you can just write `--delimiter '|'` instead of escaping it as `--delimiter '\|'`102- Bug fixes103- Bug fixes in fish scripts (thanks to @bitraid)
2330 unmodified lines