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

broken but good new direction

This commit is contained in:
Andrew Arrow 2022-11-08 06:09:14 -08:00
parent f893b6f7fa
commit 3be455ecc5
2 changed files with 51 additions and 24 deletions

View File

@ -5,6 +5,7 @@
package termui
import (
"fmt"
"strings"
)
@ -70,23 +71,36 @@ 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{}
// 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
@ -97,14 +111,14 @@ func BreakByStyles(s string) []string {
buff = append(buff, prefix)
buff = append(buff, item)
buff = append(buff, styleString)
if !strings.Contains(remainder, "[") {
if !strings.Contains(remainder, "*") {
buff = append(buff, remainder)
break
}
if i > len(tokens)-1 {
break
}
}
}*/
return buff
}

View File

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