From 3e7f032ec200f00f92fa53f9aecde4b989c7dd66 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 16 Jan 2025 01:38:45 +0900 Subject: Allow displaying --nth parts in a different text style Close #4183 --- src/result.go | 62 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 18 deletions(-) (limited to 'src/result.go') diff --git a/src/result.go b/src/result.go index f10db19b..ada31d59 100644 --- a/src/result.go +++ b/src/result.go @@ -104,11 +104,11 @@ func minRank() Result { return Result{item: &minItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}} } -func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, current bool) []colorOffset { +func (result *Result) colorOffsets(matchOffsets []Offset, nthOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, attrNth tui.Attr, current bool) []colorOffset { itemColors := result.item.Colors() // No ANSI codes - if len(itemColors) == 0 { + if len(itemColors) == 0 && len(nthOffsets) == 0 { var offsets []colorOffset for _, off := range matchOffsets { offsets = append(offsets, colorOffset{offset: [2]int32{off[0], off[1]}, color: colMatch, match: true}) @@ -118,7 +118,7 @@ func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, // Find max column var maxCol int32 - for _, off := range matchOffsets { + for _, off := range append(matchOffsets, nthOffsets...) { if off[1] > maxCol { maxCol = off[1] } @@ -129,20 +129,33 @@ func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, } } - cols := make([]int, maxCol) + type cellInfo struct { + index int + color bool + match bool + nth bool + } + + cols := make([]cellInfo, maxCol) for colorIndex, ansi := range itemColors { for i := ansi.offset[0]; i < ansi.offset[1]; i++ { - cols[i] = colorIndex + 1 // 1-based index of itemColors + cols[i] = cellInfo{colorIndex, true, false, false} } } for _, off := range matchOffsets { for i := off[0]; i < off[1]; i++ { - // Negative of 1-based index of itemColors - // - The extra -1 means highlighted - if cols[i] >= 0 { - cols[i] = cols[i]*-1 - 1 - } + cols[i].match = true + } + } + + for _, off := range nthOffsets { + // Exclude the whole line + if int(off[1])-int(off[0]) == result.item.text.Length() { + continue + } + for i := off[0]; i < off[1]; i++ { + cols[i].nth = true } } @@ -152,7 +165,7 @@ func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, // ------------ ---- -- ---- // ++++++++ ++++++++++ // --++++++++-- --++++++++++--- - curr := 0 + var curr cellInfo = cellInfo{0, false, false, false} start := 0 ansiToColorPair := func(ansi ansiOffset, base tui.ColorPair) tui.ColorPair { fg := ansi.color.fg @@ -175,12 +188,12 @@ func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, } var colors []colorOffset add := func(idx int) { - if curr != 0 && idx > start { - if curr < 0 { + if (curr.color || curr.nth || curr.match) && idx > start { + if curr.match { color := colMatch var url *url - if curr < -1 && theme.Colored { - ansi := itemColors[-curr-2] + if curr.color && theme.Colored { + ansi := itemColors[curr.index] url = ansi.color.url origColor := ansiToColorPair(ansi, colMatch) // hl or hl+ only sets the foreground color, so colMatch is the @@ -197,15 +210,28 @@ func (result *Result) colorOffsets(matchOffsets []Offset, theme *tui.ColorTheme, color = origColor.MergeNonDefault(color) } } + if curr.nth { + color = color.WithAttr(attrNth) + } colors = append(colors, colorOffset{ offset: [2]int32{int32(start), int32(idx)}, color: color, match: true, url: url}) - } else { - ansi := itemColors[curr-1] + } else if curr.color { + ansi := itemColors[curr.index] + color := ansiToColorPair(ansi, colBase) + if curr.nth { + color = color.WithAttr(attrNth) + } colors = append(colors, colorOffset{ offset: [2]int32{int32(start), int32(idx)}, - color: ansiToColorPair(ansi, colBase), + color: color, match: false, url: ansi.color.url}) + } else { + colors = append(colors, colorOffset{ + offset: [2]int32{int32(start), int32(idx)}, + color: colBase.WithAttr(attrNth), + match: false, + url: nil}) } } } -- cgit v1.2.3