From 3be455ecc53b2de8d6f0a2990e9cf1290669c5e5 Mon Sep 17 00:00:00 2001 From: Andrew Arrow Date: Tue, 8 Nov 2022 06:09:14 -0800 Subject: [PATCH] broken but good new direction --- style_parser.go | 58 +++++++++++++++++++++++++++----------------- style_parser_test.go | 17 +++++++++++-- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/style_parser.go b/style_parser.go index 718c0bc..fcab742 100644 --- a/style_parser.go +++ b/style_parser.go @@ -5,6 +5,7 @@ package termui import ( + "fmt" "strings" ) @@ -70,42 +71,55 @@ func readStyle(runes []rune, defaultStyle Style) Style { return style } -func lookLeftForBracket(s string) (string, string) { - index := strings.LastIndex(s, "[") - return s[0:index], s[index+1:] +func lookLeftForBracket(s string) int { + return strings.LastIndex(s, "[") } -func lookRightForEndStyle(s string) (string, string) { - index := strings.Index(s, ")") - return s[0:index], s[index+1:] +func lookRightForEndStyle(s string) int { + return strings.Index(s, ")") } func BreakByStyles(s string) []string { + fmt.Println(s) tokens := strings.Split(s, "](") if len(tokens) == 1 { return tokens } buff := []string{} - styleString := "" - remainder := tokens[0] - i := 1 - for { - prefix, item := lookLeftForBracket(remainder) - styleString, remainder = lookRightForEndStyle(tokens[i]) - i++ - buff = append(buff, prefix) - buff = append(buff, item) - buff = append(buff, styleString) - if !strings.Contains(remainder, "[") { - buff = append(buff, remainder) - break - } - if i > len(tokens)-1 { - break + + // test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)! + for i, token := range tokens { + if i%2 == 0 { + index := lookLeftForBracket(token) + fmt.Println(i, "even", len(token), index) + } else { + index := lookRightForEndStyle(token) + fmt.Println(i, "odd", len(token), index) } + buff = append(buff, token) } + /* + styleString := "" + remainder := tokens[0] + i := 1 + for { + prefix, item := lookLeftForBracket(remainder) + styleString, remainder = lookRightForEndStyle(tokens[i]) + i++ + buff = append(buff, prefix) + buff = append(buff, item) + buff = append(buff, styleString) + if !strings.Contains(remainder, "*") { + buff = append(buff, remainder) + break + } + if i > len(tokens)-1 { + break + } + }*/ + return buff } diff --git a/style_parser_test.go b/style_parser_test.go index 8618d78..9be4b5e 100644 --- a/style_parser_test.go +++ b/style_parser_test.go @@ -1,12 +1,18 @@ package termui import ( + "fmt" "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](bg:red)!") +func TestBreakByStylesComplex(t *testing.T) { + items := BreakByStyles("test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!") + // "test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!" + // 01234567890123456789012345678 + // 0- 4 normal + // 5-28 style + // 29- if len(items) != 10 { t.Fatal("wrong length", len(items)) } @@ -15,6 +21,13 @@ func TestBreakByStyles(t *testing.T) { t.Fatal("wrong text", text) } } +func TestBreakByStylesSimpler(t *testing.T) { + items := BreakByStyles("[blue](fg:blue) [1]") + // [blue](fg:blue) [1] + // 012345678901234 + // 0-14, rest + fmt.Println(items, len(items)) +} func TestParseStyles(t *testing.T) { cells := ParseStyles("test nothing", NewStyle(ColorWhite))