diff --git a/style_parser.go b/style_parser.go index 8cc26b8..1f18712 100644 --- a/style_parser.go +++ b/style_parser.go @@ -87,6 +87,44 @@ func processToken(token, previous string) (string, string) { return styleString, restOfString } +func lookLeftForBracket(s string) (string, string) { + index := strings.LastIndex(s, "[") + return s[0:index], s[index+1:] +} + +func lookRightForEndStyle(s string) (string, string) { + index := strings.Index(s, ")") + return s[0:index], s[index+1:] +} + +func BreakByStyles(s string) []string { + // "test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red)" + tokens := strings.Split(s, "](") + if len(tokens) == 1 { + return tokens + } + + styleString := "" + remainder := tokens[0] + i := 1 + for { + prefix, item := lookLeftForBracket(remainder) + styleString, remainder = lookRightForEndStyle(tokens[i]) + i++ + fmt.Println(i, prefix) + fmt.Println(i, item) + fmt.Println(i, styleString) + fmt.Println(i, remainder) + if !strings.Contains(remainder, "[") { + break + } + } + + buffer := []string{} + + return buffer +} + func PrepareStyles(s string) []PreparedStyle { items := []PreparedStyle{} tokens := strings.Split(s, "](") @@ -95,6 +133,8 @@ func PrepareStyles(s string) []PreparedStyle { ps := PreparedStyle{s, ""} return []PreparedStyle{ps} } + + fmt.Println(strings.Join(tokens, "|")) return items } diff --git a/style_parser_test.go b/style_parser_test.go index c71e473..a47080e 100644 --- a/style_parser_test.go +++ b/style_parser_test.go @@ -1,16 +1,25 @@ package termui import ( - "strings" "testing" ) +func TestBreakByStyles(t *testing.T) { + items := BreakByStyles("test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red) and maybe even [foo](more)!") + //test [blue|fg:blue,bg:white,mod:bold) and [red|fg:red) + //test|blue|fg:blue,bg:white,mod:bold| and |red|fg:red + if len(items) != 6 { + t.Fatal("wrong length", len(items)) + } +} + func TestPrepareStyles(t *testing.T) { items := PrepareStyles("test no style") if len(items) != 1 { t.Fatal("wrong length", len(items)) } items = PrepareStyles("test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red)") + //test [blue|fg:blue,bg:white,mod:bold) and [red|fg:red) if len(items) != 4 { t.Fatal("wrong length", len(items)) } @@ -40,6 +49,7 @@ func TestPrepareStyles(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)) @@ -100,4 +110,4 @@ func textFromCells(cells []Cell) string { buff = append(buff, string(cell.Rune)) } return strings.Join(buff, "") -} +}*/