Support full-line background color in the list section
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #4432
authored by Junegunn Choi · committed by Junegunn Choi · parent 8d81730 · Browse files · 7 files·+106 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
7 files+106 added−22 removed
- other:+106 added−22 removed
Provenance
- Commit
4811e52- Tree
da7d88e- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
8d81730
7 files changed+106 added−22 removed
modified:src/tui/dummy.go+1 added
22 unmodified lines23 AttrUndefined = Attr(0)24 AttrRegular = Attr(1 << 8)25 AttrClear = Attr(1 << 9)26 BoldForce = Attr(1 << 10)27+ FullBg = Attr(1 << 11)28 29 Bold = Attr(1)30 Dim = Attr(1 << 1)31 Italic = Attr(1 << 2)33 unmodified linesmodified:src/tui/tui.go+8 added
271 unmodified lines272 func NewColorPair(fg Color, bg Color, attr Attr) ColorPair {273 return ColorPair{fg, bg, attr}274 }275 276+func NoColorPair() ColorPair {277+ return ColorPair{-1, -1, 0}278+}279+280 func (p ColorPair) Fg() Color {281 return p.fg282 }283 4 unmodified lines288 func (p ColorPair) Attr() Attr {289 return p.attr290 }291 292+func (p ColorPair) IsFullBgMarker() bool {293+ return p.attr&FullBg > 0294+}295+296 func (p ColorPair) HasBg() bool {297 return p.attr&Reverse == 0 && p.bg != colDefault ||298 p.attr&Reverse > 0 && p.fg != colDefault299 }852 unmodified linesmodified:src/ansi_test.go+1 added-1 removed
20 unmodified lines21 // - https://web.archive.org/web/20090227051140/http://ascii-table.com/ansi-escape-sequences-vt-100.php22 // (archived from http://ascii-table.com/ansi-escape-sequences-vt-100.php)23 // - http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html24 // - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html25-var ansiRegexReference = regexp.MustCompile("(?:\x1b[\\[()][0-9;:]*[a-zA-Z@]|\x1b][0-9][;:][[:print:]]+(?:\x1b\\\\|\x07)|\x1b.|[\x0e\x0f]|.\x08)")25+var ansiRegexReference = regexp.MustCompile("(?:\x1b[\\[()][0-9;:]*[a-zA-Z@]|\x1b][0-9][;:][[:print:]]+(?:\x1b\\\\|\x07)|\x1b.|[\x0e\x0f]|.\x08|\n)")26 27 func testParserReference(t testing.TB, str string) {28 t.Helper()29 424 unmodified linesmodified:src/ansi.go+30 added-4 removed
154 unmodified lines155 156 // nextAnsiEscapeSequence returns the ANSI escape sequence and is equivalent to157 // calling FindStringIndex() on the below regex (which was originally used):158 //159-// "(?:\x1b[\\[()][0-9;:?]*[a-zA-Z@]|\x1b][0-9]+[;:][[:print:]]+(?:\x1b\\\\|\x07)|\x1b.|[\x0e\x0f]|.\x08)"159+// "(?:\x1b[\\[()][0-9;:?]*[a-zA-Z@]|\x1b][0-9]+[;:][[:print:]]+(?:\x1b\\\\|\x07)|\x1b.|[\x0e\x0f]|.\x08|\n)"160 func nextAnsiEscapeSequence(s string) (int, int) {161 // fast check for ANSI escape sequences162 i := 0163 for ; i < len(s); i++ {164 switch s[i] {165- case '\x0e', '\x0f', '\x1b', '\x08':165+ case '\x0e', '\x0f', '\x1b', '\x08', '\n':166 // We ignore the fact that '\x08' cannot be the first char167 // in the string and be an escape sequence for the sake of168 // speed and simplicity.169 goto Loop3 unmodified lines173 174 Loop:175 for ; i < len(s); i++ {176 switch s[i] {177+ case '\n':178+ // match: `\n`179+ return i, i + 1180 case '\x08':181 // backtrack to match: `.\x08`182 if i > 0 && s[i-1] != '\n' {183 if s[i-1] < utf8.RuneSelf {83 unmodified lines267 }268 output.WriteString(prev)269 }270 271+ code := str[start:idx]268- newState := interpretCode(str[start:idx], state)269- if !newState.equals(state) {272+ newState := interpretCode(code, state)273+ if code == "\n" || !newState.equals(state) {274 if state != nil {275 // Update last offset276 (&offsets[len(offsets)-1]).offset[1] = int32(runeCount)277 }278 279+ if code == "\n" {280+ output.WriteRune('\n')281+ // Full-background marker282+ if newState.lbg >= 0 {283+ marker := newState284+ marker.attr |= tui.FullBg285+ offsets = append(offsets, ansiOffset{286+ [2]int32{int32(runeCount), int32(runeCount)},287+ marker,288+ })289+ }290+ }291+292 if newState.colored() {293 // Append new offset294 if pstate == nil {295 pstate = &ansiState{}69 unmodified lines365 return -1, remaining366 }367 368 func interpretCode(ansiCode string, prevState *ansiState) ansiState {369+ if ansiCode == "\n" {370+ if prevState != nil {371+ return *prevState372+ }373+ return ansiState{-1, -1, 0, -1, nil}374+ }375+376 var state ansiState377 if prevState == nil {378 state = ansiState{-1, -1, 0, -1, nil}379 } else {78 unmodified lines458 case 0:459 state.fg = -1460 state.bg = -1461 state.attr = 0462+ state.lbg = -1463 state256 = 0464 default:465 if num >= 30 && num <= 37 {466 state.fg = tui.Color(num - 30)34 unmodified lines501 if count == 0 {502 state.fg = -1503 state.bg = -1504 state.attr = 0505+ state.lbg = -1506 }507 508 if state256 > 0 {509 *ptr = -13 unmodified linesmodified:src/core.go+11 added
4 unmodified lines5 "os"6 "sync"7 "time"8 9+ "github.com/junegunn/fzf/src/tui"10 "github.com/junegunn/fzf/src/util"11 )12 13 /*64 unmodified lines78 ansiProcessor = func(data []byte) (util.Chars, *[]ansiOffset) {79 prevLineAnsiState = lineAnsiState80 trimmed, offsets, newState := extractColor(byteString(data), lineAnsiState, nil)81 lineAnsiState = newState82+83+ // Full line background is found. Add a special marker.84+ if !opts.ReadZero && offsets != nil && newState != nil && len(*offsets) > 0 && newState.lbg >= 0 {85+ marker := (*offsets)[len(*offsets)-1]86+ marker.offset[0] = marker.offset[1]87+ marker.color.bg = newState.lbg88+ marker.color.attr = marker.color.attr | tui.FullBg89+ newOffsets := append(*offsets, marker)90+ offsets = &newOffsets91+ }92 return util.ToChars(stringBytes(trimmed)), offsets93 }94 }95 437 unmodified linesmodified:src/result.go+23 added-4 removed
17 unmodified lines18 match bool19 url *url20 }21 22+func (co colorOffset) IsFullBgMarker(at int32) bool {23+ return at == co.offset[0] && at == co.offset[1] && co.color.Attr()&tui.FullBg > 024+}25+26 type Result struct {27 item *Item28 points [4]uint1629 }122 unmodified lines152 index int153 color bool154 match bool155 nth bool156+ fbg tui.Color157 }158 154- cols := make([]cellInfo, maxCol)159+ cols := make([]cellInfo, maxCol+1)160+ for idx := range cols {161+ cols[idx].fbg = -1162+ }163 for colorIndex, ansi := range itemColors {164+ if ansi.offset[0] == ansi.offset[1] && ansi.color.attr&tui.FullBg > 0 {165+ cols[ansi.offset[0]].fbg = ansi.color.lbg166+ } else {156- for i := ansi.offset[0]; i < ansi.offset[1]; i++ {157- cols[i] = cellInfo{colorIndex, true, false, false}167+ for i := ansi.offset[0]; i < ansi.offset[1]; i++ {168+ cols[i] = cellInfo{colorIndex, true, false, false, cols[i].fbg}169+ }170 }171 }172 173 for _, off := range matchOffsets {13 unmodified lines187 // Merge offsets188 // ------------ ---- -- ----189 // ++++++++ ++++++++++190 // --++++++++-- --++++++++++---179- var curr cellInfo = cellInfo{0, false, false, false}191+ curr := cellInfo{0, false, false, false, -1}192 start := 0193 ansiToColorPair := func(ansi ansiOffset, base tui.ColorPair) tui.ColorPair {194 if !theme.Colored {195 return tui.NewColorPair(-1, -1, ansi.color.attr).MergeAttr(base)9 unmodified lines205 return tui.NewColorPair(fg, bg, ansi.color.attr).MergeAttr(base)206 }207 var colors []colorOffset208 add := func(idx int) {209+ if curr.fbg >= 0 {210+ colors = append(colors, colorOffset{211+ offset: [2]int32{int32(start), int32(start)},212+ color: tui.NewColorPair(-1, curr.fbg, tui.FullBg),213+ match: false,214+ url: nil})215+ }216 if (curr.color || curr.nth || curr.match) && idx > start {217 if curr.match {218 var color tui.ColorPair219 if curr.nth {106 unmodified linesmodified:src/terminal.go+32 added-13 removed
3153 unmodified lines3154 return line + numLines - 13155 }3156 3157 maxWidth := t.window.Width() - (t.pointerLen + t.markerLen + t.barCol())3158- postTask := func(lineNum int, width int, wrapped bool, forceRedraw bool) {3158+ postTask := func(lineNum int, width int, wrapped bool, forceRedraw bool, lbg tui.ColorPair) {3159 width += extraWidth3160- if (current || selected || alt) && t.highlightLine {3160+ if (current || selected || alt) && t.highlightLine || lbg.IsFullBgMarker() {3161 color := tui.ColSelected3162+ if lbg.IsFullBgMarker() {3163+ color = lbg3162- if current {3164+ } else if current {3165 color = tui.ColCurrent3166 } else if alt {3167 color = color.WithBg(altBg)3168 }143 unmodified lines3312 func (t *Terminal) overflow(runes []rune, max int) bool {3313 return t.displayWidthWithLimit(runes, 0, max) > max3314 }3315 3314-func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMatch tui.ColorPair, current bool, match bool, lineNum int, maxLineNum int, forceRedraw bool, preTask func(markerClass) int, postTask func(int, int, bool, bool)) int {3316+func (t *Terminal) printHighlighted(result Result, colBase tui.ColorPair, colMatch tui.ColorPair, current bool, match bool, lineNum int, maxLineNum int, forceRedraw bool, preTask func(markerClass) int, postTask func(int, int, bool, bool, tui.ColorPair)) int {3317 var displayWidth int3318 item := result.item3319 matchOffsets := []Offset{}3320 var pos *[]int76 unmodified lines3397 for lineOffset := 0; lineOffset < len(lines) && (lineNum <= maxLineNum || maxLineNum == 0); lineOffset++ {3398 line := lines[lineOffset]3399 finalLineNum = lineNum3400 offsets := []colorOffset{}3399- for _, offset := range allOffsets {3400- if offset.offset[0] >= int32(from+len(line)) {3401- allOffsets = allOffsets[len(offsets):]3401+ lbg := tui.NoColorPair()3402+ var lineLen int3403+ for idx, offset := range allOffsets {3404+ lineLen = len(line)3405+ if lineLen > 0 && line[lineLen-1] == '\n' {3406+ lineLen--3407+ }3408+ lineEnd := int32(from + lineLen)3409+ if offset.offset[0] >= lineEnd {3410+ if offset.IsFullBgMarker(lineEnd) {3411+ lbg = offset.color3412+ }3413+ allOffsets = allOffsets[idx:]3414 break3415 }3416 3417 if offset.offset[0] < int32(from) {3418 continue3419 }3420 3409- if offset.offset[1] < int32(from+len(line)) {3421+ if offset.offset[1] < lineEnd {3422 offset.offset[0] -= int32(from)3423 offset.offset[1] -= int32(from)3424 offsets = append(offsets, offset)3425 } else {3426+ if idx < len(allOffsets)-1 {3427+ next := allOffsets[idx+1]3428+ if next.IsFullBgMarker(lineEnd) {3429+ lbg = next.color3430+ idx++3431+ }3432+ }3433 dupe := offset3415- dupe.offset[0] = int32(from + len(line))3434+ dupe.offset[0] = lineEnd3435 3436 offset.offset[0] -= int32(from)3418- offset.offset[1] = int32(from + len(line))3437+ offset.offset[1] = lineEnd3438 offsets = append(offsets, offset)3439 3421- allOffsets = append([]colorOffset{dupe}, allOffsets[len(offsets):]...)3440+ allOffsets = append([]colorOffset{dupe}, allOffsets[idx+1:]...)3441 break3442 }3443 }3425- from += len(line)3444+ from += lineLen3445 if lineOffset < skipLines {3446 continue3447 }3448 actualLineOffset := lineOffset - skipLines122 unmodified lines3571 if maxWidth > 0 {3572 t.printColoredString(t.window, line, offsets, colBase)3573 }3574 if postTask != nil {3556- postTask(actualLineNum, displayWidth, wasWrapped, forceRedraw)3575+ postTask(actualLineNum, displayWidth, wasWrapped, forceRedraw, lbg)3576 } else {3577 t.markOtherLine(actualLineNum)3578 }3579 lineNum += 13339 unmodified lines