1
0
mirror of https://github.com/gizak/termui.git synced 2025-04-24 13:48:50 +08:00

progress with parsing

This commit is contained in:
Andrew Arrow 2022-11-07 08:26:51 -08:00
parent 70b8f7489a
commit 90c992f2e6
2 changed files with 52 additions and 2 deletions

View File

@ -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
}

View File

@ -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, "")
}
}*/