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

very broken but just starting logic for new ParseStyles func

This commit is contained in:
Andrew Arrow 2022-11-07 03:46:07 -08:00
parent 94288e85a1
commit c4fdef993a
2 changed files with 24 additions and 4 deletions

View File

@ -70,11 +70,30 @@ func readStyle(runes []rune, defaultStyle Style) Style {
return style
}
func processToken(token string) {
}
// ParseStyles parses a string for embedded Styles and returns []Cell with the correct styling.
// Uses defaultStyle for any text without an embedded style.
// Syntax is of the form [text](fg:<color>,mod:<attribute>,bg:<color>).
// Ordering does not matter. All fields are optional.
func ParseStyles(s string, defaultStyle Style) []Cell {
//test [blue](fg:blue,bg:white,mod:bold)
cells := []Cell{}
tokens := strings.Split(s, "](")
if len(tokens) == 1 {
// easy case, not styled string
return cells
}
for i := len(tokens) - 1; i > 0; i-- {
processToken(tokens[i])
}
return cells
}
func ParseStyles2(s string, defaultStyle Style) []Cell {
cells := []Cell{}
runes := []rune(s)
state := parserStateDefault

View File

@ -6,8 +6,9 @@ import (
)
func TestParseStyles(t *testing.T) {
cells := ParseStyles("test [blue](fg:blue,bg:white,mod:bold)", NewStyle(ColorWhite))
if len(cells) != 9 {
cells := ParseStyles("test nothing", NewStyle(ColorWhite))
cells = ParseStyles("test [blue](fg:blue,bg:white,mod:bold) and [red](fg:red)", NewStyle(ColorWhite))
if len(cells) != 17 {
t.Fatal("wrong length", len(cells))
}
for i := 0; i < 5; i++ {
@ -21,7 +22,7 @@ func TestParseStyles(t *testing.T) {
t.Fatal("wrong mod", cells[i])
}
}
for i := 5; i < len(cells); i++ {
for i := 5; i < 9; i++ {
if cells[i].Style.Fg != ColorBlue {
t.Fatal("wrong fg color", cells[i])
}
@ -34,7 +35,7 @@ func TestParseStyles(t *testing.T) {
}
text := textFromCells(cells)
if text != "test blue" {
if text != "test blue and red" {
t.Fatal("wrong text", text)
}