aboutsummaryrefslogtreecommitdiff
path: root/vendor/maunium.net/go/tview/textview.go
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-04-30 10:55:37 +0300
committerTulir Asokan <tulir@maunium.net>2018-04-30 10:55:37 +0300
commite48ff5bea4725d39818f24fa76b5ae74971f23a3 (patch)
tree549b8688ef59f7096a20544cd01ad75ec111c0e4 /vendor/maunium.net/go/tview/textview.go
parent576bab9e2e9589942d4cac8742fa1b54e8b237f9 (diff)
Update dependencies
Diffstat (limited to 'vendor/maunium.net/go/tview/textview.go')
-rw-r--r--vendor/maunium.net/go/tview/textview.go93
1 files changed, 58 insertions, 35 deletions
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.