From eaf7f8d6e68cfc019a42d90f91370a3c4db16d82 Mon Sep 17 00:00:00 2001 From: Andrew Arrow Date: Mon, 7 Nov 2022 11:30:57 -0800 Subject: [PATCH] clean up and better order --- style_parser.go | 20 ++++---------------- style_parser_test.go | 4 ++++ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/style_parser.go b/style_parser.go index 9af7210..718c0bc 100644 --- a/style_parser.go +++ b/style_parser.go @@ -5,7 +5,6 @@ package termui import ( - "fmt" "strings" ) @@ -71,17 +70,6 @@ func readStyle(runes []rune, defaultStyle Style) Style { return style } -func processToken(token, previous string) (string, string) { - fmt.Println("1", token) - index := strings.Index(token, ")") - if index == -1 { - return "", "" - } - styleString := token[0:index] - restOfString := token[index:] - return styleString, restOfString -} - func lookLeftForBracket(s string) (string, string) { index := strings.LastIndex(s, "[") return s[0:index], s[index+1:] @@ -113,6 +101,9 @@ func BreakByStyles(s string) []string { buff = append(buff, remainder) break } + if i > len(tokens)-1 { + break + } } return buff @@ -139,9 +130,7 @@ func containsColorOrMod(s string) bool { func ParseStyles(s string, defaultStyle Style) []Cell { cells := []Cell{} - fmt.Println("11") items := BreakByStyles(s) - fmt.Println("11", len(items)) if len(items) == 1 { runes := []rune(s) for _, _rune := range runes { @@ -150,13 +139,12 @@ func ParseStyles(s string, defaultStyle Style) []Cell { return cells } - //test blue fg:blue,bg:white,mod:bold and red fg:red and maybe even foo bg:red ! style := defaultStyle for i := len(items) - 1; i > -1; i-- { if containsColorOrMod(items[i]) { style = readStyle([]rune(items[i]), defaultStyle) } else { - cells = append(cells, RunesToStyledCells([]rune(items[i]), style)...) + cells = append(RunesToStyledCells([]rune(items[i]), style), cells...) style = defaultStyle } } diff --git a/style_parser_test.go b/style_parser_test.go index 8618d78..9d9893a 100644 --- a/style_parser_test.go +++ b/style_parser_test.go @@ -1,6 +1,7 @@ package termui import ( + "fmt" "strings" "testing" ) @@ -19,6 +20,9 @@ func TestBreakByStyles(t *testing.T) { func TestParseStyles(t *testing.T) { cells := ParseStyles("test nothing", NewStyle(ColorWhite)) cells = ParseStyles("test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red)", NewStyle(ColorWhite)) + fmt.Println(cells) + text2 := textFromCells(cells) + fmt.Println(text2) if len(cells) != 17 { t.Fatal("wrong length", len(cells)) }