mirror of https://github.com/gizak/termui.git
ColorSubsequence.Color is now an attribute
Added StringToAttribute method in helper.go
This commit is contained in:
parent
31f6e9a66d
commit
a267dd583e
63
helper.go
63
helper.go
|
@ -4,7 +4,12 @@
|
||||||
|
|
||||||
package termui
|
package termui
|
||||||
|
|
||||||
import tm "github.com/nsf/termbox-go"
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
tm "github.com/nsf/termbox-go"
|
||||||
|
)
|
||||||
import rw "github.com/mattn/go-runewidth"
|
import rw "github.com/mattn/go-runewidth"
|
||||||
|
|
||||||
/* ---------------Port from termbox-go --------------------- */
|
/* ---------------Port from termbox-go --------------------- */
|
||||||
|
@ -87,3 +92,59 @@ func strWidth(s string) int {
|
||||||
func charWidth(ch rune) int {
|
func charWidth(ch rune) int {
|
||||||
return rw.RuneWidth(ch)
|
return rw.RuneWidth(ch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var whiteSpaceRegex = regexp.MustCompile(`\s`)
|
||||||
|
|
||||||
|
// StringToAttribute converts text to a termui attribute. You may specifiy more
|
||||||
|
// then one attribute like that: "BLACK, BOLD, ...". All whitespaces
|
||||||
|
// are ignored.
|
||||||
|
func StringToAttribute(text string) Attribute {
|
||||||
|
text = whiteSpaceRegex.ReplaceAllString(strings.ToLower(text), "")
|
||||||
|
attributes := strings.Split(text, ",")
|
||||||
|
result := Attribute(0)
|
||||||
|
|
||||||
|
for _, theAttribute := range attributes {
|
||||||
|
var match Attribute
|
||||||
|
switch theAttribute {
|
||||||
|
case "reset", "default":
|
||||||
|
match = ColorDefault
|
||||||
|
|
||||||
|
case "black":
|
||||||
|
match = ColorBlack
|
||||||
|
|
||||||
|
case "red":
|
||||||
|
match = ColorRed
|
||||||
|
|
||||||
|
case "green":
|
||||||
|
match = ColorGreen
|
||||||
|
|
||||||
|
case "yellow":
|
||||||
|
match = ColorYellow
|
||||||
|
|
||||||
|
case "blue":
|
||||||
|
match = ColorBlue
|
||||||
|
|
||||||
|
case "magenta":
|
||||||
|
match = ColorMagenta
|
||||||
|
|
||||||
|
case "cyan":
|
||||||
|
match = ColorCyan
|
||||||
|
|
||||||
|
case "white":
|
||||||
|
match = ColorWhite
|
||||||
|
|
||||||
|
case "bold":
|
||||||
|
match = AttrBold
|
||||||
|
|
||||||
|
case "underline":
|
||||||
|
match = AttrUnderline
|
||||||
|
|
||||||
|
case "reverse":
|
||||||
|
match = AttrReverse
|
||||||
|
}
|
||||||
|
|
||||||
|
result |= match
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
|
@ -63,3 +63,8 @@ func TestTrimStrIfAppropriate(t *testing.T) {
|
||||||
assert.Equal(t, "hel…", TrimStrIfAppropriate("hello", 4))
|
assert.Equal(t, "hel…", TrimStrIfAppropriate("hello", 4))
|
||||||
assert.Equal(t, "h…", TrimStrIfAppropriate("hello", 2))
|
assert.Equal(t, "h…", TrimStrIfAppropriate("hello", 2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStringToAttribute(t *testing.T) {
|
||||||
|
assert.Equal(t, ColorRed, StringToAttribute("ReD"))
|
||||||
|
assert.Equal(t, ColorRed|AttrBold, StringToAttribute("RED, bold"))
|
||||||
|
}
|
||||||
|
|
|
@ -53,7 +53,7 @@ func (r MarkdownTextRenderer) RenderSequence(text string, lastColor, background
|
||||||
colorStart, colorEnd := match[4], match[5]
|
colorStart, colorEnd := match[4], match[5]
|
||||||
contentStart, contentEnd := match[2], match[3]
|
contentStart, contentEnd := match[2], match[3]
|
||||||
|
|
||||||
color := strings.ToUpper(text[colorStart:colorEnd])
|
color := StringToAttribute(text[colorStart:colorEnd])
|
||||||
content := text[contentStart:contentEnd]
|
content := text[contentStart:contentEnd]
|
||||||
theSequence := ColorSubsequence{color, contentStart - 1, contentEnd - 1}
|
theSequence := ColorSubsequence{color, contentStart - 1, contentEnd - 1}
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ type RenderedSequence struct {
|
||||||
|
|
||||||
// A ColorSubsequence represents a color for the given text span.
|
// A ColorSubsequence represents a color for the given text span.
|
||||||
type ColorSubsequence struct {
|
type ColorSubsequence struct {
|
||||||
Color string // TODO: use attribute
|
Color Attribute
|
||||||
Start int
|
Start int
|
||||||
End int
|
End int
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ func assertRenderSequence(t *testing.T, sequence RenderedSequence, last, backgro
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertColorSubsequence(t *testing.T, s ColorSubsequence, color string, start, end int) {
|
func assertColorSubsequence(t *testing.T, s ColorSubsequence, color string, start, end int) {
|
||||||
assert.Equal(t, ColorSubsequence{color, start, end}, s)
|
assert.Equal(t, ColorSubsequence{StringToAttribute(color), start, end}, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarkdownTextRenderer_RenderSequence(t *testing.T) {
|
func TestMarkdownTextRenderer_RenderSequence(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue