mirror of
https://github.com/gizak/termui.git
synced 2025-04-29 13:48:51 +08:00
progress with parsing
This commit is contained in:
parent
70b8f7489a
commit
90c992f2e6
@ -87,6 +87,44 @@ func processToken(token, previous string) (string, string) {
|
|||||||
return styleString, restOfString
|
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 {
|
func PrepareStyles(s string) []PreparedStyle {
|
||||||
items := []PreparedStyle{}
|
items := []PreparedStyle{}
|
||||||
tokens := strings.Split(s, "](")
|
tokens := strings.Split(s, "](")
|
||||||
@ -95,6 +133,8 @@ func PrepareStyles(s string) []PreparedStyle {
|
|||||||
ps := PreparedStyle{s, ""}
|
ps := PreparedStyle{s, ""}
|
||||||
return []PreparedStyle{ps}
|
return []PreparedStyle{ps}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println(strings.Join(tokens, "|"))
|
||||||
return items
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +1,25 @@
|
|||||||
package termui
|
package termui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
|
||||||
"testing"
|
"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) {
|
func TestPrepareStyles(t *testing.T) {
|
||||||
items := PrepareStyles("test no style")
|
items := PrepareStyles("test no style")
|
||||||
if len(items) != 1 {
|
if len(items) != 1 {
|
||||||
t.Fatal("wrong length", len(items))
|
t.Fatal("wrong length", len(items))
|
||||||
}
|
}
|
||||||
items = PrepareStyles("test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red)")
|
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 {
|
if len(items) != 4 {
|
||||||
t.Fatal("wrong length", len(items))
|
t.Fatal("wrong length", len(items))
|
||||||
}
|
}
|
||||||
@ -40,6 +49,7 @@ func TestPrepareStyles(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func TestParseStyles(t *testing.T) {
|
func TestParseStyles(t *testing.T) {
|
||||||
cells := ParseStyles("test nothing", NewStyle(ColorWhite))
|
cells := ParseStyles("test nothing", NewStyle(ColorWhite))
|
||||||
cells = ParseStyles("test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red)", 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))
|
buff = append(buff, string(cell.Rune))
|
||||||
}
|
}
|
||||||
return strings.Join(buff, "")
|
return strings.Join(buff, "")
|
||||||
}
|
}*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user