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
Provenance
- Commit
3e7f032- Tree
4904189- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
b42f5bf
8 files changed+128 added−36 removed
modified:man/man1/fzf.1+1 added
240 unmodified lines241 \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)246 247 .B ANSI COLORS:248 \fB\-1 \fRDefault terminal foreground/background color249 \fB \fR(or the original color of the text)1551 unmodified linesmodified:src/tui/dummy.go+4 added
9 unmodified lines10 11 var DefaultBorderShape = BorderRounded12 13 func (a Attr) Merge(b Attr) Attr {14+ if b == AttrRegular {15+ return b16+ }17+18 return a | b19 }20 21 const (37 unmodified linesmodified:src/tui/tui.go+10 added
203 unmodified lines204 Color Color205 Attr Attr206 }207 208+func (a ColorAttr) IsColorDefined() bool {209+ return a.Color != colUndefined210+}211+212 func NewColorAttr() ColorAttr {213 return ColorAttr{Color: colUndefined, Attr: AttrUndefined}214 }215 92 unmodified lines308 Fg ColorAttr309 Bg ColorAttr310 ListFg ColorAttr311 ListBg ColorAttr312+ Nth ColorAttr313 SelectedFg ColorAttr314 SelectedBg ColorAttr315 SelectedMatch ColorAttr316 PreviewFg ColorAttr390 unmodified lines707 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 }714 715 func NoColorTheme() *ColorTheme {35 unmodified lines751 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 }758 759 func init() {32 unmodified lines792 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 lines830 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 lines871 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 }878 879 func InitTheme(theme *ColorTheme, baseTheme *ColorTheme, forceBlack bool, hasInputWindow bool, hasHeaderWindow bool) {134 unmodified linesmodified:src/options.go+6 added
1205 unmodified lines1206 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 lines2967 }2968 }2969 }2970 2971+ if opts.Theme.Nth.IsColorDefined() {2972+ return errors.New("only ANSI attributes are allowed for 'nth' (regular, bold, underline, reverse, dim, italic, strikethrough)")2973+ }2974+2975 return nil2976 }2977 2978 // This function can have side-effects and alter some global states.235 unmodified linesmodified:src/result_test.go+26 added-16 removed
129 unmodified lines130 {[2]int32{33, 40}, ansiState{4, 8, tui.Bold, -1, nil}}}}}131 132 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 lines154 assert(9, 35, 40, tui.NewColorPair(4, 8, tui.Bold))155 156 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)158 159+ 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)162+160- // [{[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 lines103 func minRank() Result {104 return Result{item: &minItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}}105 }106 107-func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, current bool) []colorOffset {107+func (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()109 110 // 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 line117 }118 119 // 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 lines128 maxCol = ansi.offset[1]129 }130 }131 132+ type cellInfo struct {133+ index int134+ color bool135+ match bool136+ nth bool137+ }138+132- 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 }145 146 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 }151 152+ 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+ }161+162 // sort.Sort(ByOrder(offsets))163 164 // 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.bg14 unmodified lines187 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 lines209 } 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 linesmodified:src/terminal.go+22 added-2 removed
299 unmodified lines300 ellipsis string301 scrollbar string302 previewScrollbar string303 ansi bool304+ nthAttr tui.Attr305 nth []Range306+ nthCurrent []Range307 tabstop int308 margin [4]sizeSpec309 padding [4]sizeSpec310 unicode bool575 unmodified lines886 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 lines1174 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 lines2720 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)2740 2741 maxLines := 12742 if t.canSpanMultiLines() {2743 maxLines = maxLineNum - lineNum + 11941 unmodified lines4685 } 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 linesmodified:CHANGELOG.md+15 added
80 unmodified lines81 ```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:bold91+92+ # Dim the other parts93+ ls -al | fzf --nth -1 --color nth:regular,fg:dim,current-fg:dim94+95+ # 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