diff --git a/style_parser.go b/style_parser.go index 5eb33a7..2004b84 100644 --- a/style_parser.go +++ b/style_parser.go @@ -5,6 +5,7 @@ package termui import ( + "fmt" "strings" ) @@ -25,6 +26,11 @@ const ( type parserState uint +type PreparedStyle struct { + Text string + Style string +} + const ( parserStateDefault parserState = iota parserStateStyleItems @@ -70,13 +76,20 @@ func readStyle(runes []rune, defaultStyle Style) Style { return style } -func processToken(token string) string { +func processToken(token, previous string) (string, string) { + fmt.Println("1", token) index := strings.Index(token, ")") if index == -1 { - return "" + return "", "" } styleString := token[0:index] - return styleString + restOfString := token[index:] + return styleString, restOfString +} + +func PrepareStyles(s string) []PreparedStyle { + items := []PreparedStyle{} + return items } // ParseStyles parses a string for embedded Styles and returns []Cell with the correct styling. @@ -93,11 +106,17 @@ func ParseStyles(s string, defaultStyle Style) []Cell { return cells } - styleString := "" - for i := len(tokens) - 1; i >= 0; i-- { - styleString = processToken(tokens[i]) + styleString, rest := processToken(tokens[len(tokens)-1], "") + fmt.Println("2", styleString, rest) + for i := len(tokens) - 2; i >= 0; i-- { + styleString, rest = processToken(tokens[i], styleString) + fmt.Println("3", styleString, rest) } + //1 fg:red) + //1 fg:blue,bg:white,mod:bold) and [red + //1 test [blue + return cells } diff --git a/style_parser_test.go b/style_parser_test.go index 0c8b5b2..c89eaf4 100644 --- a/style_parser_test.go +++ b/style_parser_test.go @@ -5,6 +5,37 @@ import ( "testing" ) +func TestPrepareStyles(t *testing.T) { + items := PrepareStyles("test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red)") + if len(items) != 4 { + t.Fatal("wrong length", len(items)) + } + if items[0].Text != "test " { + t.Fatal("wrong text", items[0].Text) + } + if items[0].Style != "" { + t.Fatal("wrong text", items[0].Style) + } + if items[1].Text != "blue" { + t.Fatal("wrong text", items[1].Text) + } + if items[1].Style != "fg:blue,bg:white,mod:bold" { + t.Fatal("wrong text", items[1].Style) + } + if items[2].Text != " and " { + t.Fatal("wrong text", items[2].Text) + } + if items[2].Style != "" { + t.Fatal("wrong text", items[2].Style) + } + if items[3].Text != "red" { + t.Fatal("wrong text", items[3].Text) + } + if items[3].Style != "fg:red" { + t.Fatal("wrong text", items[3].Style) + } +} + 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))