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 package termui
import ( import (
"fmt"
"strings" "strings"
) )
@ -70,42 +71,55 @@ func readStyle(runes []rune, defaultStyle Style) Style {
return style return style
} }
func lookLeftForBracket(s string) (string, string) { func lookLeftForBracket(s string) int {
index := strings.LastIndex(s, "[") return strings.LastIndex(s, "[")
return s[0:index], s[index+1:]
} }
func lookRightForEndStyle(s string) (string, string) { func lookRightForEndStyle(s string) int {
index := strings.Index(s, ")") return strings.Index(s, ")")
return s[0:index], s[index+1:]
} }
func BreakByStyles(s string) []string { func BreakByStyles(s string) []string {
fmt.Println(s)
tokens := strings.Split(s, "](") tokens := strings.Split(s, "](")
if len(tokens) == 1 { if len(tokens) == 1 {
return tokens return tokens
} }
buff := []string{} buff := []string{}
styleString := ""
remainder := tokens[0] // test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!
i := 1 for i, token := range tokens {
for { if i%2 == 0 {
prefix, item := lookLeftForBracket(remainder) index := lookLeftForBracket(token)
styleString, remainder = lookRightForEndStyle(tokens[i]) fmt.Println(i, "even", len(token), index)
i++ } else {
buff = append(buff, prefix) index := lookRightForEndStyle(token)
buff = append(buff, item) fmt.Println(i, "odd", len(token), index)
buff = append(buff, styleString)
if !strings.Contains(remainder, "[") {
buff = append(buff, remainder)
break
}
if i > len(tokens)-1 {
break
} }
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 return buff
} }

View File

@ -1,12 +1,18 @@
package termui package termui
import ( import (
"fmt"
"strings" "strings"
"testing" "testing"
) )
func TestBreakByStyles(t *testing.T) { func TestBreakByStylesComplex(t *testing.T) {
items := BreakByStyles("test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!") 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 { if len(items) != 10 {
t.Fatal("wrong length", len(items)) t.Fatal("wrong length", len(items))
} }
@ -15,6 +21,13 @@ func TestBreakByStyles(t *testing.T) {
t.Fatal("wrong text", text) 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) { func TestParseStyles(t *testing.T) {
cells := ParseStyles("test nothing", NewStyle(ColorWhite)) cells := ParseStyles("test nothing", NewStyle(ColorWhite))