From e48ff5bea4725d39818f24fa76b5ae74971f23a3 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 30 Apr 2018 10:55:37 +0300 Subject: Update dependencies --- vendor/maunium.net/go/tview/textview.go | 93 ++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 35 deletions(-) (limited to 'vendor/maunium.net/go/tview/textview.go') diff --git a/vendor/maunium.net/go/tview/textview.go b/vendor/maunium.net/go/tview/textview.go index 8de0121..16b9dfb 100644 --- a/vendor/maunium.net/go/tview/textview.go +++ b/vendor/maunium.net/go/tview/textview.go @@ -8,6 +8,7 @@ import ( "unicode/utf8" "maunium.net/go/tcell" + "github.com/lucasb-eyer/go-colorful" runewidth "github.com/mattn/go-runewidth" ) @@ -17,12 +18,14 @@ var TabSize = 4 // textViewIndex contains information about each line displayed in the text // view. type textViewIndex struct { - Line int // The index into the "buffer" variable. - Pos int // The index into the "buffer" string (byte position). - NextPos int // The (byte) index of the next character in this buffer line. - Width int // The screen width of this line. - Color tcell.Color // The starting color. - Region string // The starting region ID. + Line int // The index into the "buffer" variable. + Pos int // The index into the "buffer" string (byte position). + NextPos int // The (byte) index of the next character in this buffer line. + Width int // The screen width of this line. + ForegroundColor string // The starting foreground color ("" = don't change, "-" = reset). + BackgroundColor string // The starting background color ("" = don't change, "-" = reset). + Attributes string // The starting attributes ("" = don't change, "-" = reset). + Region string // The starting region ID. } // TextView is a box which displays text. It implements the io.Writer interface @@ -499,7 +502,6 @@ func (t *TextView) reindexBuffer(width int) { // Initial states. regionID := "" var highlighted bool - color := t.textColor // Go through each line in the buffer. for bufferIndex, str := range t.buffer { @@ -507,11 +509,10 @@ func (t *TextView) reindexBuffer(width int) { var ( colorTagIndices [][]int colorTags [][]string + escapeIndices [][]int ) if t.dynamicColors { - colorTagIndices = colorPattern.FindAllStringIndex(str, -1) - colorTags = colorPattern.FindAllStringSubmatch(str, -1) - str = colorPattern.ReplaceAllString(str, "") + colorTagIndices, colorTags, escapeIndices, str, _ = decomposeString(str) } // Find all regions in this line. Then remove them. @@ -523,13 +524,11 @@ func (t *TextView) reindexBuffer(width int) { regionIndices = regionPattern.FindAllStringIndex(str, -1) regions = regionPattern.FindAllStringSubmatch(str, -1) str = regionPattern.ReplaceAllString(str, "") - } - - // Find all replace tags in this line. Then replace them. - var escapeIndices [][]int - if t.dynamicColors || t.regions { - escapeIndices = escapePattern.FindAllStringIndex(str, -1) - str = escapePattern.ReplaceAllString(str, "[$1$2]") + if !t.dynamicColors { + // We haven't detected escape tags yet. Do it now. + escapeIndices = escapePattern.FindAllStringIndex(str, -1) + str = escapePattern.ReplaceAllString(str, "[$1$2]") + } } // Split the line if required. @@ -559,13 +558,18 @@ func (t *TextView) reindexBuffer(width int) { } // Create index from split lines. - var originalPos, colorPos, regionPos, escapePos int + var ( + originalPos, colorPos, regionPos, escapePos int + foregroundColor, backgroundColor, attributes string + ) for _, splitLine := range splitLines { line := &textViewIndex{ - Line: bufferIndex, - Pos: originalPos, - Color: color, - Region: regionID, + Line: bufferIndex, + Pos: originalPos, + ForegroundColor: foregroundColor, + BackgroundColor: backgroundColor, + Attributes: attributes, + Region: regionID, } // Shift original position with tags. @@ -574,7 +578,7 @@ func (t *TextView) reindexBuffer(width int) { if colorPos < len(colorTagIndices) && colorTagIndices[colorPos][0] <= originalPos+lineLength { // Process color tags. originalPos += colorTagIndices[colorPos][1] - colorTagIndices[colorPos][0] - color = tcell.GetColor(colorTags[colorPos][1]) + foregroundColor, backgroundColor, attributes = styleFromTag(foregroundColor, backgroundColor, attributes, colorTags[colorPos]) colorPos++ } else if regionPos < len(regionIndices) && regionIndices[regionPos][0] <= originalPos+lineLength { // Process region tags. @@ -712,6 +716,7 @@ func (t *TextView) Draw(screen tcell.Screen) { } // Draw the buffer. + defaultStyle := tcell.StyleDefault.Foreground(t.textColor) for line := t.lineOffset; line < len(t.index); line++ { // Are we done? if line-t.lineOffset >= height { @@ -721,17 +726,19 @@ func (t *TextView) Draw(screen tcell.Screen) { // Get the text for this line. index := t.index[line] text := t.buffer[index.Line][index.Pos:index.NextPos] - color := index.Color + foregroundColor := index.ForegroundColor + backgroundColor := index.BackgroundColor + attributes := index.Attributes regionID := index.Region // Get color tags. var ( colorTagIndices [][]int colorTags [][]string + escapeIndices [][]int ) if t.dynamicColors { - colorTagIndices = colorPattern.FindAllStringIndex(text, -1) - colorTags = colorPattern.FindAllStringSubmatch(text, -1) + colorTagIndices, colorTags, escapeIndices, _, _ = decomposeString(text) } // Get regions. @@ -742,12 +749,9 @@ func (t *TextView) Draw(screen tcell.Screen) { if t.regions { regionIndices = regionPattern.FindAllStringIndex(text, -1) regions = regionPattern.FindAllStringSubmatch(text, -1) - } - - // Get escape tags. - var escapeIndices [][]int - if t.dynamicColors || t.regions { - escapeIndices = escapePattern.FindAllStringIndex(text, -1) + if !t.dynamicColors { + escapeIndices = escapePattern.FindAllStringIndex(text, -1) + } } // Calculate the position of the line. @@ -770,7 +774,7 @@ func (t *TextView) Draw(screen tcell.Screen) { // Get the color. if currentTag < len(colorTags) && pos >= colorTagIndices[currentTag][0] && pos < colorTagIndices[currentTag][1] { if pos == colorTagIndices[currentTag][1]-1 { - color = tcell.GetColor(colorTags[currentTag][1]) + foregroundColor, backgroundColor, attributes = styleFromTag(foregroundColor, backgroundColor, attributes, colorTags[currentTag]) currentTag++ } continue @@ -811,12 +815,31 @@ func (t *TextView) Draw(screen tcell.Screen) { break } + // Mix the existing style with the new style. + _, _, existingStyle, _ := screen.GetContent(x+posX, y+line-t.lineOffset) + _, background, _ := existingStyle.Decompose() + style := overlayStyle(background, defaultStyle, foregroundColor, backgroundColor, attributes) + // Do we highlight this character? - style := tcell.StyleDefault.Background(t.backgroundColor).Foreground(color) + var highlighted bool if len(regionID) > 0 { if _, ok := t.highlights[regionID]; ok { - style = tcell.StyleDefault.Background(color).Foreground(t.backgroundColor) + highlighted = true + } + } + if highlighted { + fg, bg, _ := style.Decompose() + if bg == tcell.ColorDefault { + r, g, b := fg.RGB() + c := colorful.Color{R: float64(r) / 255, G: float64(g) / 255, B: float64(b) / 255} + _, _, li := c.Hcl() + if li < .5 { + bg = tcell.ColorWhite + } else { + bg = tcell.ColorBlack + } } + style = style.Background(fg).Foreground(bg) } // Draw the character. -- cgit v1.2.3