diff --git a/helper.go b/helper.go index 00d66dd..86906c8 100644 --- a/helper.go +++ b/helper.go @@ -4,7 +4,12 @@ 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" /* ---------------Port from termbox-go --------------------- */ @@ -87,3 +92,59 @@ func strWidth(s string) int { func charWidth(ch rune) int { 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 +} diff --git a/helper_test.go b/helper_test.go index 86d2a8e..5d277de 100644 --- a/helper_test.go +++ b/helper_test.go @@ -63,3 +63,8 @@ func TestTrimStrIfAppropriate(t *testing.T) { assert.Equal(t, "hel…", TrimStrIfAppropriate("hello", 4)) 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")) +} diff --git a/textRender.go b/textRender.go index 2c58b70..04ef135 100644 --- a/textRender.go +++ b/textRender.go @@ -53,7 +53,7 @@ func (r MarkdownTextRenderer) RenderSequence(text string, lastColor, background colorStart, colorEnd := match[4], match[5] contentStart, contentEnd := match[2], match[3] - color := strings.ToUpper(text[colorStart:colorEnd]) + color := StringToAttribute(text[colorStart:colorEnd]) content := text[contentStart:contentEnd] theSequence := ColorSubsequence{color, contentStart - 1, contentEnd - 1} @@ -75,7 +75,7 @@ type RenderedSequence struct { // A ColorSubsequence represents a color for the given text span. type ColorSubsequence struct { - Color string // TODO: use attribute + Color Attribute Start int End int } diff --git a/textRender_test.go b/textRender_test.go index 8ab9151..3eb75f0 100644 --- a/textRender_test.go +++ b/textRender_test.go @@ -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) { - assert.Equal(t, ColorSubsequence{color, start, end}, s) + assert.Equal(t, ColorSubsequence{StringToAttribute(color), start, end}, s) } func TestMarkdownTextRenderer_RenderSequence(t *testing.T) {