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

adding new test for PrepareStyles

This commit is contained in:
Andrew Arrow 2022-11-07 04:21:40 -08:00
parent 99a9b416b7
commit 4d1aaf17a7
2 changed files with 56 additions and 6 deletions

View File

@ -5,6 +5,7 @@
package termui
import (
"fmt"
"strings"
)
@ -25,6 +26,11 @@ const (
type parserState uint
type PreparedStyle struct {
Text string
Style string
}
const (
parserStateDefault parserState = iota
parserStateStyleItems
@ -70,13 +76,20 @@ func readStyle(runes []rune, defaultStyle Style) Style {
return style
}
func processToken(token string) string {
func processToken(token, previous string) (string, string) {
fmt.Println("1", token)
index := strings.Index(token, ")")
if index == -1 {
return ""
return "", ""
}
styleString := token[0:index]
return styleString
restOfString := token[index:]
return styleString, restOfString
}
func PrepareStyles(s string) []PreparedStyle {
items := []PreparedStyle{}
return items
}
// ParseStyles parses a string for embedded Styles and returns []Cell with the correct styling.
@ -93,11 +106,17 @@ func ParseStyles(s string, defaultStyle Style) []Cell {
return cells
}
styleString := ""
for i := len(tokens) - 1; i >= 0; i-- {
styleString = processToken(tokens[i])
styleString, rest := processToken(tokens[len(tokens)-1], "")
fmt.Println("2", styleString, rest)
for i := len(tokens) - 2; i >= 0; i-- {
styleString, rest = processToken(tokens[i], styleString)
fmt.Println("3", styleString, rest)
}
//1 fg:red)
//1 fg:blue,bg:white,mod:bold) and [red
//1 test [blue
return cells
}

View File

@ -5,6 +5,37 @@ import (
"testing"
)
func TestPrepareStyles(t *testing.T) {
items := PrepareStyles("test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red)")
if len(items) != 4 {
t.Fatal("wrong length", len(items))
}
if items[0].Text != "test " {
t.Fatal("wrong text", items[0].Text)
}
if items[0].Style != "" {
t.Fatal("wrong text", items[0].Style)
}
if items[1].Text != "blue" {
t.Fatal("wrong text", items[1].Text)
}
if items[1].Style != "fg:blue,bg:white,mod:bold" {
t.Fatal("wrong text", items[1].Style)
}
if items[2].Text != " and " {
t.Fatal("wrong text", items[2].Text)
}
if items[2].Style != "" {
t.Fatal("wrong text", items[2].Style)
}
if items[3].Text != "red" {
t.Fatal("wrong text", items[3].Text)
}
if items[3].Style != "fg:red" {
t.Fatal("wrong text", items[3].Style)
}
}
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))