mirror of
https://github.com/rivo/tview.git
synced 2025-04-26 13:49:06 +08:00
Supporting any color now in TextView. Resolves #12
This commit is contained in:
parent
5f33d0cbc0
commit
d74b71b48d
@ -67,7 +67,7 @@ func main() {
|
|||||||
for index, slide := range slides {
|
for index, slide := range slides {
|
||||||
title, primitive := slide(nextSlide)
|
title, primitive := slide(nextSlide)
|
||||||
pages.AddPage(strconv.Itoa(index), primitive, true, index == currentSlide)
|
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.
|
// Create the main layout.
|
||||||
|
@ -31,7 +31,7 @@ func main() {
|
|||||||
go func() {
|
go func() {
|
||||||
for _, word := range strings.Split(corporate, " ") {
|
for _, word := range strings.Split(corporate, " ") {
|
||||||
if word == "the" {
|
if word == "the" {
|
||||||
word = "[red]the[white]"
|
word = "[#ff0000]the[white]"
|
||||||
}
|
}
|
||||||
if word == "to" {
|
if word == "to" {
|
||||||
word = fmt.Sprintf(`["%d"]to[""]`, numSelections)
|
word = fmt.Sprintf(`["%d"]to[""]`, numSelections)
|
||||||
|
23
textview.go
23
textview.go
@ -11,21 +11,9 @@ import (
|
|||||||
runewidth "github.com/mattn/go-runewidth"
|
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.
|
// Regular expressions commonly used throughout the TextView class.
|
||||||
var (
|
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_,;: \-\.]*)"\]`)
|
regionPattern = regexp.MustCompile(`\["([a-zA-Z0-9_,;: \-\.]*)"\]`)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -72,8 +60,9 @@ type textViewIndex struct {
|
|||||||
//
|
//
|
||||||
// This is a [red]warning[white]!
|
// This is a [red]warning[white]!
|
||||||
//
|
//
|
||||||
// will print the word "warning" in red. The following colors are currently
|
// will print the word "warning" in red. You can provide W3C color names or
|
||||||
// supported: white, yellow, blue, green, red, cyan, magenta.
|
// hex strings starting with "#", followed by 6 hexadecimal digits. See
|
||||||
|
// tcell.GetColor() for more information.
|
||||||
//
|
//
|
||||||
// Regions and Highlights
|
// Regions and Highlights
|
||||||
//
|
//
|
||||||
@ -489,7 +478,7 @@ func (t *TextView) reindexBuffer(width int) {
|
|||||||
// Skip any color tags.
|
// Skip any color tags.
|
||||||
if currentTag < len(colorTags) && pos >= colorTagIndices[currentTag][0] && pos < colorTagIndices[currentTag][1] {
|
if currentTag < len(colorTags) && pos >= colorTagIndices[currentTag][0] && pos < colorTagIndices[currentTag][1] {
|
||||||
if pos == colorTagIndices[currentTag][1]-1 {
|
if pos == colorTagIndices[currentTag][1]-1 {
|
||||||
color = textColors[colorTags[currentTag][1]]
|
color = tcell.GetColor(colorTags[currentTag][1])
|
||||||
currentTag++
|
currentTag++
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
@ -631,7 +620,7 @@ func (t *TextView) Draw(screen tcell.Screen) {
|
|||||||
// Get the color.
|
// Get the color.
|
||||||
if currentTag < len(colorTags) && pos >= colorTagIndices[currentTag][0] && pos < colorTagIndices[currentTag][1] {
|
if currentTag < len(colorTags) && pos >= colorTagIndices[currentTag][0] && pos < colorTagIndices[currentTag][1] {
|
||||||
if pos == colorTagIndices[currentTag][1]-1 {
|
if pos == colorTagIndices[currentTag][1]-1 {
|
||||||
color = textColors[colorTags[currentTag][1]]
|
color = tcell.GetColor(colorTags[currentTag][1])
|
||||||
currentTag++
|
currentTag++
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
|
11
util.go
11
util.go
@ -2,7 +2,6 @@ package tview
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -75,16 +74,6 @@ func init() {
|
|||||||
return len([]rune(text)) <= maxLength
|
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),
|
// Print prints text onto the screen into the given box at (x,y,maxWidth,1),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user