Supporting any color now in TextView. Resolves #12

This commit is contained in:
Oliver 2018-01-12 09:09:01 +01:00
parent 5f33d0cbc0
commit d74b71b48d
4 changed files with 8 additions and 30 deletions

View File

@ -67,7 +67,7 @@ func main() {
for index, slide := range slides {
title, primitive := slide(nextSlide)
pages.AddPage(strconv.Itoa(index), primitive, true, index == currentSlide)
fmt.Fprintf(info, `%d ["%d"][cyan]%s[white][""] `, index+1, index, title)
fmt.Fprintf(info, `%d ["%d"][darkcyan]%s[white][""] `, index+1, index, title)
}
// Create the main layout.

View File

@ -31,7 +31,7 @@ func main() {
go func() {
for _, word := range strings.Split(corporate, " ") {
if word == "the" {
word = "[red]the[white]"
word = "[#ff0000]the[white]"
}
if word == "to" {
word = fmt.Sprintf(`["%d"]to[""]`, numSelections)

View File

@ -11,21 +11,9 @@ import (
runewidth "github.com/mattn/go-runewidth"
)
// textColors maps color strings which may be embedded in text sent to a
// TextView to their tcell counterparts.
var textColors = map[string]tcell.Color{
"red": tcell.ColorRed,
"white": tcell.ColorWhite,
"yellow": tcell.ColorYellow,
"blue": tcell.ColorBlue,
"green": tcell.ColorGreen,
"cyan": tcell.ColorDarkCyan,
"magenta": tcell.ColorDarkMagenta,
}
// Regular expressions commonly used throughout the TextView class.
var (
colorPattern *regexp.Regexp // Initialized in the init() function.
colorPattern = regexp.MustCompile(`\[([a-zA-Z]+|#[0-9a-zA-Z]{6})\]`)
regionPattern = regexp.MustCompile(`\["([a-zA-Z0-9_,;: \-\.]*)"\]`)
)
@ -72,8 +60,9 @@ type textViewIndex struct {
//
// This is a [red]warning[white]!
//
// will print the word "warning" in red. The following colors are currently
// supported: white, yellow, blue, green, red, cyan, magenta.
// will print the word "warning" in red. You can provide W3C color names or
// hex strings starting with "#", followed by 6 hexadecimal digits. See
// tcell.GetColor() for more information.
//
// Regions and Highlights
//
@ -489,7 +478,7 @@ func (t *TextView) reindexBuffer(width int) {
// Skip any color tags.
if currentTag < len(colorTags) && pos >= colorTagIndices[currentTag][0] && pos < colorTagIndices[currentTag][1] {
if pos == colorTagIndices[currentTag][1]-1 {
color = textColors[colorTags[currentTag][1]]
color = tcell.GetColor(colorTags[currentTag][1])
currentTag++
}
continue
@ -631,7 +620,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 = textColors[colorTags[currentTag][1]]
color = tcell.GetColor(colorTags[currentTag][1])
currentTag++
}
continue

11
util.go
View File

@ -2,7 +2,6 @@ package tview
import (
"math"
"regexp"
"strconv"
"strings"
@ -75,16 +74,6 @@ func init() {
return len([]rune(text)) <= maxLength
}
}
// Regular expressions.
var colors string
for color := range textColors {
if len(colors) > 0 {
colors += "|"
}
colors += color
}
colorPattern = regexp.MustCompile(`\[(` + colors + `)\]`)
}
// Print prints text onto the screen into the given box at (x,y,maxWidth,1),