Skip to content
Myr

Fix nth attr merge order to respect precedence hierarchy (#4697)

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

* Fix nth attr merge order to respect precedence hierarchy nth attrs were merged ON TOP of current-fg/selected-fg attrs, so nth:regular would clear attrs like underline from current-fg. Fix the merge chain to apply nth BEFORE the line-type overlay: fg < nth < selected-fg < current-fg < hl < selected-hl < current-hl Store raw current-fg and selected-fg attrs in ColorTheme before they get merged with fg/ListFg, then pass them as nthOverlay through printHighlighted to colorOffsets where the correct merge chain is computed: fgAttr.Merge(nthAttr).Merge(nthOverlay). Fix #4687 * Make current-fg inherit from list-fg instead of fg current-fg was inheriting from fg, not list-fg, so setting list-fg had no effect on the current line. e.g. fzf --color fg:dim,list-fg:underline,nth:regular -d / --nth -1 The non-nth part of the current line was dim (from fg) instead of underline (from list-fg). Resolve ListFg/ListBg earlier in InitTheme so Current can inherit from them. * Make selected-fg inherit from list-fg via merge instead of override selected-fg used o() which replaces the attr from list-fg entirely. e.g. with fg:dim,selected-fg:italic, the dim was lost on selected lines because o() replaced dim with italic instead of merging them. Use ColorAttr.Merge() so attrs are combined additively, consistent with how current-fg inherits from list-fg. * Apply selected-fg attrs on current line when item is selected When an item is both current and selected, the current-line rendering ignored selected-fg attrs entirely. e.g. echo foo | fzf --color fg:dim,nth:regular,current-fg:underline,selected-fg:italic --bind result:select --multi The italic from selected-fg was lost. Merge NthSelectedAttr into the overlay and rebuild colBase attr with the correct precedence chain: fg < selected-fg < current-fg.

authored by Junegunn Choi · committed by GitHub · parent 97ac779 · Browse files · 4 files+82 added−22 removed
This commit

State

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

Diff stats

4 files+82 added−22 removed

  • other:+82 added−22 removed

4 files+82 added−22 removed

modified:src/tui/tui.go+18 added-6 removed
445 unmodified lines
446 dup.attr = dup.attr.Merge(attr)447 return dup448}449450func (p ColorPair) WithNewAttr(attr Attr) ColorPair {451 dup := p452 dup.attr = attr453 return dup454}455456func (p ColorPair) WithFg(fg ColorAttr) ColorPair {457 dup := p458 fgPair := ColorPair{fg.Color, colUndefined, colUndefined, fg.Attr}459 return dup.Merge(fgPair)
65 unmodified lines
525 BorderLabel ColorAttr526 ListLabel ColorAttr527 ListBorder ColorAttr528 GapLine ColorAttr529 NthCurrentAttr Attr // raw current-fg attr (before fg merge) for nth overlay530 NthSelectedAttr Attr // raw selected-fg attr (before ListFg inherit) for nth overlay531}532533type Event struct {534 Type EventType
671 unmodified lines
1206 if !baseTheme.Colored && match.IsUndefined() {1207 match.Attr = Underline1208 }1209 theme.Match = o(baseTheme.Match, match)1210 // These colors are not defined in the base themes.1211 // Resolve ListFg/ListBg early so Current and Selected can inherit from them.1212 theme.ListFg = o(theme.Fg, theme.ListFg)1213 theme.ListBg = o(theme.Bg, theme.ListBg)1202 // Inherit from 'fg', so that we don't have to write 'current-fg:dim'1214 // Inherit from 'list-fg', so that we don't have to write 'current-fg:dim'1215 // e.g. fzf --delimiter / --nth -1 --color fg:dim,nth:regular1216 current := theme.Current1217 if !baseTheme.Colored && current.IsUndefined() {1218 current.Attr |= Reverse1219 }1208 theme.Current = theme.Fg.Merge(o(baseTheme.Current, current))1220 resolvedCurrent := o(baseTheme.Current, current)1221 theme.NthCurrentAttr = resolvedCurrent.Attr1222 theme.Current = theme.ListFg.Merge(resolvedCurrent)1223 currentMatch := theme.CurrentMatch1224 if !baseTheme.Colored && currentMatch.IsUndefined() {1225 currentMatch.Attr |= Reverse | Underline1226 }
16 unmodified lines
1243 undefined := NewColorAttr()1244 scrollbarDefined := theme.Scrollbar != undefined1245 previewBorderDefined := theme.PreviewBorder != undefined12461233 // These colors are not defined in the base themes1234 theme.ListFg = o(theme.Fg, theme.ListFg)1235 theme.ListBg = o(theme.Bg, theme.ListBg)1236 theme.SelectedFg = o(theme.ListFg, theme.SelectedFg)1247 theme.NthSelectedAttr = theme.SelectedFg.Attr1248 theme.SelectedFg = theme.ListFg.Merge(theme.SelectedFg)1249 theme.SelectedBg = o(theme.ListBg, theme.SelectedBg)1250 theme.SelectedMatch = o(theme.Match, theme.SelectedMatch)12511252 ghost := theme.Ghost
168 unmodified lines
modified:src/result_test.go+33 added-2 removed
130 unmodified lines
131 {[2]int32{33, 40}, ansiState{4, 8, -1, tui.Bold, -1, nil}}}}}132133 colBase := tui.NewColorPair(89, 189, tui.AttrUndefined)134 colMatch := tui.NewColorPair(99, 199, tui.AttrUndefined)135 colors := item.colorOffsets(offsets, nil, tui.Dark256, colBase, colMatch, tui.AttrUndefined, false)135 colors := item.colorOffsets(offsets, nil, tui.Dark256, colBase, colMatch, tui.AttrUndefined, 0, false)136 assert := func(idx int, b int32, e int32, c tui.ColorPair) {137 o := colors[idx]138 if o.offset[0] != b || o.offset[1] != e || o.color != c {139 t.Error(o, b, e, c)
18 unmodified lines
158 colUnderline := tui.NewColorPair(-1, -1, tui.Underline)159160 nthOffsets := []Offset{{37, 39}, {42, 45}}161 for _, attr := range []tui.Attr{tui.AttrRegular, tui.StrikeThrough} {162 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, attr, false)162 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, attr, 0, false)163164 // [{[0 5] {1 5 0}} {[5 15] {1 5 8}} {[15 20] {1 5 0}}165 // {[22 25] {2 6 1}} {[25 27] {2 6 9}} {[27 30] {-1 -1 8}}166 // {[30 32] {3 7 8}} {[32 33] {-1 -1 8}} {[33 35] {4 8 9}}
14 unmodified lines
181 }182 assert(10, 37, 39, tui.NewColorPair(4, 8, expected))183 assert(11, 39, 40, tui.NewColorPair(4, 8, tui.Bold))184 }185186 // Test nthOverlay: simulates nth:regular with current-fg:underline187 // The overlay (underline) should survive even though nth:regular clears attrs.188 // Precedence: fg < nth < current-fg189 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, tui.AttrRegular, tui.Underline, false)190191 // nth regions should have Underline (from overlay), not cleared by AttrRegular192 // Non-nth regions keep colBase attrs (AttrUndefined)193 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))194 assert(1, 5, 15, tui.NewColorPair(1, 5, tui.Underline))195 assert(2, 15, 20, tui.NewColorPair(1, 5, tui.AttrUndefined))196 assert(3, 22, 25, tui.NewColorPair(2, 6, tui.Bold))197 assert(4, 25, 27, tui.NewColorPair(2, 6, tui.Bold|tui.Underline))198 assert(5, 27, 30, colUnderline)199 assert(6, 30, 32, tui.NewColorPair(3, 7, tui.Underline))200 assert(7, 32, 33, colUnderline)201 assert(8, 33, 35, tui.NewColorPair(4, 8, tui.Bold|tui.Underline))202 assert(9, 35, 37, tui.NewColorPair(4, 8, tui.Bold))203 // nth region within ANSI bold: AttrRegular clears, overlay adds Underline back204 assert(10, 37, 39, tui.NewColorPair(4, 8, tui.Bold|tui.Underline))205 assert(11, 39, 40, tui.NewColorPair(4, 8, tui.Bold))206207 // Test nthOverlay with additive attrs: nth:strikethrough with selected-fg:bold208 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, tui.StrikeThrough, tui.Bold, false)209210 // Non-nth entries unchanged from overlay=0 case211 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))212 assert(5, 27, 30, colUnderline) // match only, no nth213 assert(7, 32, 33, colUnderline) // match only, no nth214 // nth region within ANSI bold: StrikeThrough|Bold merged with ANSI Bold215 assert(10, 37, 39, tui.NewColorPair(4, 8, tui.Bold|tui.StrikeThrough))216}217218func TestRadixSortResults(t *testing.T) {219 sortCriteria = []criterion{byScore, byLength}
54 unmodified lines
modified:src/result.go+9 added-5 removed
126 unmodified lines
127func minRank() Result {128 return Result{item: &minItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}}129}130131func (result *Result) colorOffsets(matchOffsets []Offset, nthOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, attrNth tui.Attr, hidden bool) []colorOffset {131func (result *Result) colorOffsets(matchOffsets []Offset, nthOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, attrNth tui.Attr, nthOverlay tui.Attr, hidden bool) []colorOffset {132 itemColors := result.item.Colors()133134 // No ANSI codes135 if len(itemColors) == 0 && len(nthOffsets) == 0 {
76 unmodified lines
212 bg = colBase.Bg()213 }214 return tui.NewColorPair(fg, bg, ansi.color.attr).WithUl(ansi.color.ul).MergeAttr(base)215 }216 fgAttr := tui.ColNormal.Attr()217 nthAttrFinal := fgAttr.Merge(attrNth).Merge(nthOverlay)218 nthBase := colBase.WithNewAttr(nthAttrFinal)219220 var colors []colorOffset221 add := func(idx int) {222 if curr.fbg >= 0 {223 colors = append(colors, colorOffset{
5 unmodified lines
229 if (curr.color || curr.nth || curr.match) && idx > start {230 if curr.match {231 var color tui.ColorPair232 if curr.nth {229 color = colBase.WithAttr(attrNth).Merge(colMatch)233 color = nthBase.Merge(colMatch)234 } else {235 color = colBase.Merge(colMatch)236 }237 var url *url
11 unmodified lines
249 // echo -e "\x1b[42mfoo\x1b[mbar" | fzf --ansi --color bg+:1,hl+:-1:underline250 if color.Fg().IsDefault() && origColor.HasBg() {251 color = origColor252 if curr.nth {249 color = color.WithAttr(attrNth &^ tui.AttrRegular)253 color = color.WithAttr((attrNth &^ tui.AttrRegular).Merge(nthOverlay))254 }255 } else {256 color = origColor.MergeNonDefault(color)257 }
3 unmodified lines
261 } else if curr.color {262 ansi := itemColors[curr.index]263 base := colBase264 if curr.nth {261 base = base.WithAttr(attrNth)265 base = nthBase266 }267 if hidden {268 base = base.WithFg(theme.Nomatch)269 }
3 unmodified lines
273 color: color,274 match: false,275 url: ansi.color.url})276 } else {273 color := colBase.WithAttr(attrNth)277 color := nthBase278 if hidden {279 color = color.WithFg(theme.Nomatch)280 }281 colors = append(colors, colorOffset{
140 unmodified lines
modified:src/terminal.go+22 added-9 removed
1552 unmodified lines
1553 var offsets []colorOffset1554 printFn := func(window tui.Window, limit int) {1555 if offsets == nil {1556 // tui.Col* are not initialized until renderer.Init()1557 offsets = result.colorOffsets(nil, nil, t.theme, *color, *color, t.nthAttr, false)1557 offsets = result.colorOffsets(nil, nil, t.theme, *color, *color, t.nthAttr, 0, false)1558 }1559 for limit > 0 {1560 if length > limit {1561 trimmedRunes, _ := t.trimRight(runes, limit)
54 unmodified lines
1616 preTask := func(markerClass) int {1617 return 11618 }1619 t.printHighlighted(1620 Result{item: item}, tui.ColPrompt, tui.ColPrompt, false, false, false, line, line, true, preTask, nil)1620 Result{item: item}, tui.ColPrompt, tui.ColPrompt, false, false, false, line, line, true, preTask, nil, 0)1621 })1622 t.wrap = wrap1623 }1624 _, promptLen := t.processTabsStr(trimmed, 0)
1559 unmodified lines
3184 tui.ColFooter, tui.ColFooter, false, false, false, line, line, true,3185 func(markerClass) int {3186 t.footerWindow.Print(indent)3187 return indentSize3188 }, nil)3188 }, nil, 0)3189 }3190 })3191 t.wrap = wrap3192}
75 unmodified lines
3268 tui.ColHeader, tui.ColHeader, false, false, false, line, line, true,3269 func(markerClass) int {3270 t.window.Print(indent)3271 return indentSize3272 }, nil)3272 }, nil, 0)3273 }3274 t.wrap = wrap3275}3276
229 unmodified lines
3506 t.window.CPrint(tui.ColCurrentSelectedEmpty, t.markerEmpty)3507 }3508 return indentSize3509 }3510 finalLineNum = t.printHighlighted(result, tui.ColCurrent, tui.ColCurrentMatch, true, true, !matched, line, maxLine, forceRedraw, preTask, postTask)3510 colCurrent := tui.ColCurrent3511 nthOverlay := t.theme.NthCurrentAttr3512 if selected {3513 nthOverlay = t.theme.NthSelectedAttr.Merge(t.theme.NthCurrentAttr)3514 baseAttr := tui.ColNormal.Attr().Merge(t.theme.NthSelectedAttr).Merge(t.theme.NthCurrentAttr)3515 colCurrent = colCurrent.WithNewAttr(baseAttr)3516 }3517 finalLineNum = t.printHighlighted(result, colCurrent, tui.ColCurrentMatch, true, true, !matched, line, maxLine, forceRedraw, preTask, postTask, nthOverlay)3518 } else {3519 preTask := func(marker markerClass) int {3520 w := t.window.Width() - t.pointerLen3521 if w < 0 {
25 unmodified lines
3547 if alt {3548 base = base.WithBg(altBg)3549 match = match.WithBg(altBg)3550 }3551 var nthOverlay tui.Attr3552 if selected {3553 nthOverlay = t.theme.NthSelectedAttr3554 }3544 finalLineNum = t.printHighlighted(result, base, match, false, true, !matched, line, maxLine, forceRedraw, preTask, postTask)3555 finalLineNum = t.printHighlighted(result, base, match, false, true, !matched, line, maxLine, forceRedraw, preTask, postTask, nthOverlay)3556 }3557 for i := 0; i < t.gap && finalLineNum < maxLine; i++ {3558 finalLineNum++3559 t.renderGapLine(finalLineNum, barRange, i == t.gap-1)
92 unmodified lines
3652func (t *Terminal) overflow(runes []rune, max int) bool {3653 return t.displayWidthWithLimit(runes, 0, max) > max3654}36553645func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMatch tui.ColorPair, current bool, match bool, hidden bool, lineNum int, maxLineNum int, forceRedraw bool, preTask func(markerClass) int, postTask func(int, int, bool, bool, tui.ColorPair)) int {3656func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMatch tui.ColorPair, current bool, match bool, hidden bool, lineNum int, maxLineNum int, forceRedraw bool, preTask func(markerClass) int, postTask func(int, int, bool, bool, tui.ColorPair), nthOverlay tui.Attr) int {3657 var displayWidth int3658 item := result.item3659 matchOffsets := []Offset{}3660 var pos *[]int
32 unmodified lines
3693 if wholeCovered && t.nthAttr&tui.AttrRegular > 0 {3694 // But if 'nth' is set to 'regular', it's a sign that you're applying3695 // a different style to the rest of the string. e.g. 'nth:regular,fg:dim'3696 // In this case, we still need to apply it to clear the style.3686 colBase = colBase.WithAttr(t.nthAttr)3697 fgAttr := tui.ColNormal.Attr()3698 nthAttrFinal := fgAttr.Merge(t.nthAttr).Merge(nthOverlay)3699 colBase = colBase.WithNewAttr(nthAttrFinal)3700 }3701 if !wholeCovered && t.nthAttr > 0 {3702 var tokens []Token3703 if item.transformed != nil && item.transformed.revision == t.resultMerger.revision {
10 unmodified lines
3714 }3715 sort.Sort(ByOrder(nthOffsets))3716 }3717 }3705 allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr, hidden)3718 allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr, nthOverlay, hidden)37193720 // Determine split offset for horizontal scrolling with freeze3721 splitOffset1 := -13722 splitOffset2 := -1
4077 unmodified lines