Skip to content
Myr

Add 'alt-bg' color for striped lines (#4370)

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

Test cases: 1. 'jump' should show alternating background colors even when 'alt-bg' is not defined as before. go run main.go --bind load:jump Two differences: * The alternating lines will not be in bold (was a bug) * The marker column will not be rendered with alternating background color 2. Use alternating background color when 'alt-bg' is set go run main.go --color bg:238,alt-bg:237 go run main.go --color bg:238,alt-bg:237 --highlight-line 3. 'selected-bg' should take precedence go run main.go --color bg:238,alt-bg:237,selected-bg:232 \ --highlight-line --multi --bind 'load:select+up+select+up' 4. Should work with text with ANSI colors declare -f | perl -0777 -pe 's/^}\n/}\0/gm' | bat --plain --language bash --color always | go run main.go --read0 --ansi --reverse --multi \ --color bg:237,alt-bg:238,current-bg:236 --highlight-line --- Close #4354 Fix #4372

authored by Junegunn Choi · committed by GitHub · parent cd6677b · Browse files · 7 files+60 added−19 removed
This commit

State

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

Diff stats

7 files+60 added−19 removed

  • other:+60 added−19 removed

7 files+60 added−19 removed

modified:man/man1/fzf.1+1 added
268 unmodified lines
269 \fBcurrent\-fg (fg+) \fRText (current line)270 \fBcurrent\-bg (bg+) \fRBackground (current line)271 \fBgutter \fRGutter on the left272 \fBcurrent\-hl (hl+) \fRHighlighted substrings (current line)273 \fBalt\-bg \fRAlternate background color to create striped lines274 \fBquery (input\-fg) \fRQuery string275 \fBdisabled \fRQuery string when search is disabled (\fB\-\-disabled\fR)276 \fBinfo \fRInfo line (match counters)277 \fBborder \fRBorder around the window (\fB\-\-border\fR and \fB\-\-preview\fR)
1648 unmodified lines
modified:src/tui/tui.go+12 added
306 unmodified lines
307 dup.attr = dup.attr.Merge(attr)308 return dup309}310311func (p ColorPair) WithBg(bg ColorAttr) ColorPair {312 dup := p313 bgPair := ColorPair{colUndefined, bg.Color, bg.Attr}314 return dup.Merge(bgPair)315}316317func (p ColorPair) MergeAttr(other ColorPair) ColorPair {318 return p.WithAttr(other.attr)319}320
12 unmodified lines
333 Fg ColorAttr334 Bg ColorAttr335 ListFg ColorAttr336 ListBg ColorAttr337 AltBg ColorAttr338 Nth ColorAttr339 SelectedFg ColorAttr340 SelectedBg ColorAttr341 SelectedMatch ColorAttr
399 unmodified lines
741 Fg: ColorAttr{colUndefined, AttrUndefined},742 Bg: ColorAttr{colUndefined, AttrUndefined},743 ListFg: ColorAttr{colUndefined, AttrUndefined},744 ListBg: ColorAttr{colUndefined, AttrUndefined},745 AltBg: ColorAttr{colUndefined, AttrUndefined},746 SelectedFg: ColorAttr{colUndefined, AttrUndefined},747 SelectedBg: ColorAttr{colUndefined, AttrUndefined},748 SelectedMatch: ColorAttr{colUndefined, AttrUndefined},749 DarkBg: ColorAttr{colUndefined, AttrUndefined},
37 unmodified lines
787 Fg: ColorAttr{colDefault, AttrUndefined},788 Bg: ColorAttr{colDefault, AttrUndefined},789 ListFg: ColorAttr{colDefault, AttrUndefined},790 ListBg: ColorAttr{colDefault, AttrUndefined},791 AltBg: ColorAttr{colUndefined, AttrUndefined},792 SelectedFg: ColorAttr{colDefault, AttrUndefined},793 SelectedBg: ColorAttr{colDefault, AttrUndefined},794 SelectedMatch: ColorAttr{colDefault, AttrUndefined},795 DarkBg: ColorAttr{colDefault, AttrUndefined},
37 unmodified lines
833 Fg: ColorAttr{colDefault, AttrUndefined},834 Bg: ColorAttr{colDefault, AttrUndefined},835 ListFg: ColorAttr{colUndefined, AttrUndefined},836 ListBg: ColorAttr{colUndefined, AttrUndefined},837 AltBg: ColorAttr{colUndefined, AttrUndefined},838 SelectedFg: ColorAttr{colUndefined, AttrUndefined},839 SelectedBg: ColorAttr{colUndefined, AttrUndefined},840 SelectedMatch: ColorAttr{colUndefined, AttrUndefined},841 DarkBg: ColorAttr{colBlack, AttrUndefined},
31 unmodified lines
873 Fg: ColorAttr{colDefault, AttrUndefined},874 Bg: ColorAttr{colDefault, AttrUndefined},875 ListFg: ColorAttr{colUndefined, AttrUndefined},876 ListBg: ColorAttr{colUndefined, AttrUndefined},877 AltBg: ColorAttr{colUndefined, AttrUndefined},878 SelectedFg: ColorAttr{colUndefined, AttrUndefined},879 SelectedBg: ColorAttr{colUndefined, AttrUndefined},880 SelectedMatch: ColorAttr{colUndefined, AttrUndefined},881 DarkBg: ColorAttr{236, AttrUndefined},
31 unmodified lines
913 Fg: ColorAttr{colDefault, AttrUndefined},914 Bg: ColorAttr{colDefault, AttrUndefined},915 ListFg: ColorAttr{colUndefined, AttrUndefined},916 ListBg: ColorAttr{colUndefined, AttrUndefined},917 AltBg: ColorAttr{colUndefined, AttrUndefined},918 SelectedFg: ColorAttr{colUndefined, AttrUndefined},919 SelectedBg: ColorAttr{colUndefined, AttrUndefined},920 SelectedMatch: ColorAttr{colUndefined, AttrUndefined},921 DarkBg: ColorAttr{251, AttrUndefined},
172 unmodified lines
modified:src/options.go+2 added
1293 unmodified lines
1294 case "current-fg", "fg+":1295 mergeAttr(&theme.Current)1296 case "current-bg", "bg+":1297 mergeAttr(&theme.DarkBg)1298 case "alt-bg":1299 mergeAttr(&theme.AltBg)1300 case "selected-fg":1301 mergeAttr(&theme.SelectedFg)1302 case "selected-bg":1303 mergeAttr(&theme.SelectedBg)
2199 unmodified lines
modified:src/result_test.go+2 added-2 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, nil, tui.Dark256, colBase, colMatch, tui.AttrUndefined, true)134 colors := item.colorOffsets(offsets, nil, tui.Dark256, colBase, colMatch, tui.AttrUndefined)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)
18 unmodified lines
157 colUnderline := tui.NewColorPair(-1, -1, tui.Underline)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)161 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, attr)162163 // [{[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}}
19 unmodified lines
modified:src/result.go+3 added-11 removed
117 unmodified lines
118func minRank() Result {119 return Result{item: &minItem, points: [4]uint16{math.MaxUint16, 0, 0, 0}}120}121122func (result *Result) colorOffsets(matchOffsets []Offset, nthOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, attrNth tui.Attr, current bool) []colorOffset {122func (result *Result) colorOffsets(matchOffsets []Offset, nthOffsets []Offset, theme *tui.ColorTheme, colBase tui.ColorPair, colMatch tui.ColorPair, attrNth tui.Attr) []colorOffset {123 itemColors := result.item.Colors()124125 // No ANSI codes126 if len(itemColors) == 0 && len(nthOffsets) == 0 {
54 unmodified lines
181 ansiToColorPair := func(ansi ansiOffset, base tui.ColorPair) tui.ColorPair {182 fg := ansi.color.fg183 bg := ansi.color.bg184 if fg == -1 {185 if current {186 fg = theme.Current.Color187 } else {188 fg = theme.Fg.Color189 }185 fg = colBase.Fg()186 }187 if bg == -1 {192 if current {193 bg = theme.DarkBg.Color194 } else {195 bg = theme.Bg.Color196 }188 bg = colBase.Bg()189 }190 return tui.NewColorPair(fg, bg, ansi.color.attr).MergeAttr(base)191 }192 var colors []colorOffset
110 unmodified lines
modified:src/terminal.go+23 added-6 removed
1256 unmodified lines
1257 var offsets []colorOffset1258 printFn := func(window tui.Window, limit int) {1259 if offsets == nil {1260 // tui.Col* are not initialized until renderer.Init()1261 offsets = result.colorOffsets(nil, nil, t.theme, *color, *color, t.nthAttr, false)1261 offsets = result.colorOffsets(nil, nil, t.theme, *color, *color, t.nthAttr)1262 }1263 for limit > 0 {1264 if length > limit {1265 trimmedRunes, _ := t.trimRight(runes, limit)
1524 unmodified lines
2790 item := result.item2791 _, selected := t.selected[item.Index()]2792 label := ""2793 extraWidth := 02794 alt := false2795 altBg := t.theme.AltBg2796 selectedBg := selected && t.theme.SelectedBg != t.theme.ListBg2797 if t.jumping != jumpDisabled {2798 if index < len(t.jumpLabels) {2799 // Striped2800 if !altBg.IsColorDefined() {2801 altBg = t.theme.DarkBg2797 current = index%2 == 02802 alt = index%2 == 02803 } else {2804 alt = index%2 == 12805 }2806 label = t.jumpLabels[index:index+1] + strings.Repeat(" ", util.Max(0, t.pointerLen-1))2807 if t.pointerLen == 0 {2808 extraWidth = 12809 }2810 }2811 } else {2803 } else if current {2804 label = t.pointer2812 if current {2813 label = t.pointer2814 }2815 alt = !selectedBg && altBg.IsColorDefined() && index%2 == 12816 }28172818 // Avoid unnecessary redraw2819 numLines, _ := t.numItemLines(item, maxLine-line+1)
18 unmodified lines
28382839 maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + 1)2840 postTask := func(lineNum int, width int, wrapped bool, forceRedraw bool) {2841 width += extraWidth2831 if (current || selected) && t.highlightLine {2842 if (current || selected || alt) && t.highlightLine {2843 color := tui.ColSelected2844 if current {2845 color = tui.ColCurrent2846 } else if alt {2847 color = color.WithBg(altBg)2848 }2849 fillSpaces := maxWidth - width2850 if wrapped {2851 fillSpaces -= t.wrapSignWidth
89 unmodified lines
2941 } else {2942 base = tui.ColNormal2943 match = tui.ColMatch2944 }2945 if alt {2946 base = base.WithBg(altBg)2947 match = match.WithBg(altBg)2948 }2949 finalLineNum = t.printHighlighted(result, base, match, false, true, line, maxLine, forceRedraw, preTask, postTask)2950 }2951 for i := 0; i < t.gap && finalLineNum < maxLine; i++ {2952 finalLineNum++
90 unmodified lines
3043 }3044 sort.Sort(ByOrder(nthOffsets))3045 }3046 }3030 allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr, current)3047 allOffsets := result.colorOffsets(charOffsets, nthOffsets, t.theme, colBase, colMatch, t.nthAttr)30483049 maxLines := 13050 if t.canSpanMultiLines() {3051 maxLines = maxLineNum - lineNum + 1
3340 unmodified lines
modified:CHANGELOG.md+17 added
1CHANGELOG2=========340.62.05------6- Added `alt-bg` color to create striped lines to visually separate rows7 ```sh8 fzf --color bg:237,alt-bg:238,current-bg:236 --highlight-line910 declare -f | perl -0777 -pe 's/^}\n/}\0/gm' |11 bat --plain --language bash --color always |12 fzf --read0 --ansi --reverse --multi \13 --color bg:237,alt-bg:238,current-bg:236 --highlight-line14 ```15- [fish] Improvements in CTRL-R binding (@bitraid)16 - You can trigger CTRL-R in the middle of a command to insert the selected item17 - You can delete history items with SHIFT-DEL18- Bug fixes and improvements19 - Fixed unnecessary 100ms delay after `reload`20210.61.322------23- Reverted #4351 as it caused `tmux run-shell 'fzf --tmux'` to fail (#4559 #4560)24- More environment variables for child processes (#4356)
2612 unmodified lines