mirror of
https://github.com/gizak/termui.git
synced 2025-04-24 13:48:50 +08:00
more cleanup and comments
This commit is contained in:
parent
55ae6181d6
commit
29db86d4f2
120
style_parser.go
120
style_parser.go
@ -5,7 +5,6 @@
|
||||
package termui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -22,10 +21,17 @@ const (
|
||||
|
||||
tokenBeginStyle = '('
|
||||
tokenEndStyle = ')'
|
||||
|
||||
tokenStyleKey = "]("
|
||||
)
|
||||
|
||||
type parserState uint
|
||||
|
||||
type StyleBlock struct {
|
||||
Start int
|
||||
End int
|
||||
}
|
||||
|
||||
const (
|
||||
parserStateDefault parserState = iota
|
||||
parserStateStyleItems
|
||||
@ -71,11 +77,8 @@ func readStyle(runes []rune, defaultStyle Style) Style {
|
||||
return style
|
||||
}
|
||||
|
||||
type StyleBlock struct {
|
||||
Start int
|
||||
End int
|
||||
}
|
||||
|
||||
// this will start at ]( and look backwards to find the [ and forward
|
||||
// to find the ) and record these Start and End indexes in a StyleBlock
|
||||
func findStartEndOfStyle(pos int, runes []rune) StyleBlock {
|
||||
current := pos
|
||||
sb := StyleBlock{0, 0}
|
||||
@ -97,9 +100,11 @@ func findStartEndOfStyle(pos int, runes []rune) StyleBlock {
|
||||
return sb
|
||||
}
|
||||
|
||||
func BreakBlocksIntoStrings(s string) []string {
|
||||
// if are string is "foo [thing](style) foo [more](style)"
|
||||
// this will return "foo ", "[thing](style)", " foo ", "[more](style)"
|
||||
func breakBlocksIntoStrings(s string) []string {
|
||||
buff := []string{}
|
||||
blocks := FindStyleBlocks(s)
|
||||
blocks := findStyleBlocks(s)
|
||||
if len(blocks) == 0 {
|
||||
return buff
|
||||
}
|
||||
@ -121,11 +126,12 @@ func BreakBlocksIntoStrings(s string) []string {
|
||||
return buff
|
||||
}
|
||||
|
||||
func FindStyleBlocks(s string) []StyleBlock {
|
||||
// loop through positions and make [] of StyleBlocks
|
||||
func findStyleBlocks(s string) []StyleBlock {
|
||||
items := []StyleBlock{}
|
||||
runes := []rune(s)
|
||||
|
||||
positions := FindStylePositions(s)
|
||||
positions := findStylePositions(s)
|
||||
for _, pos := range positions {
|
||||
sb := findStartEndOfStyle(pos, runes)
|
||||
items = append(items, sb)
|
||||
@ -133,9 +139,12 @@ func FindStyleBlocks(s string) []StyleBlock {
|
||||
return items
|
||||
}
|
||||
|
||||
func FindStylePositions(s string) []int {
|
||||
fmt.Println(s)
|
||||
index := strings.Index(s, "](")
|
||||
// uses tokenStyleKey ]( which tells us we have both a [text] and a (style)
|
||||
// if are string is "foo [thing](style) foo [more](style)"
|
||||
// this func will return a list of two ints: the index of the first ]( and
|
||||
// the index of the next one
|
||||
func findStylePositions(s string) []int {
|
||||
index := strings.Index(s, tokenStyleKey)
|
||||
if index == -1 {
|
||||
return []int{}
|
||||
}
|
||||
@ -148,7 +157,7 @@ func FindStylePositions(s string) []int {
|
||||
buff = append(buff, index+offset)
|
||||
toProcess = toProcess[index+1:]
|
||||
offset += index + 1
|
||||
index = strings.Index(toProcess, "](")
|
||||
index = strings.Index(toProcess, tokenStyleKey)
|
||||
if index == -1 {
|
||||
break
|
||||
}
|
||||
@ -176,7 +185,7 @@ func extractStyleFromBlock(item string) string {
|
||||
func ParseStyles(s string, defaultStyle Style) []Cell {
|
||||
cells := []Cell{}
|
||||
|
||||
items := BreakBlocksIntoStrings(s)
|
||||
items := breakBlocksIntoStrings(s)
|
||||
if len(items) == 0 {
|
||||
return RunesToStyledCells([]rune(s), defaultStyle)
|
||||
}
|
||||
@ -193,84 +202,3 @@ func ParseStyles(s string, defaultStyle Style) []Cell {
|
||||
}
|
||||
return cells
|
||||
}
|
||||
|
||||
func ParseStyles2(s string, defaultStyle Style) []Cell {
|
||||
cells := []Cell{}
|
||||
runes := []rune(s)
|
||||
state := parserStateDefault
|
||||
styledText := []rune{}
|
||||
styleItems := []rune{}
|
||||
squareCount := 0
|
||||
|
||||
reset := func() {
|
||||
styledText = []rune{}
|
||||
styleItems = []rune{}
|
||||
state = parserStateDefault
|
||||
squareCount = 0
|
||||
}
|
||||
|
||||
rollback := func() {
|
||||
cells = append(cells, RunesToStyledCells(styledText, defaultStyle)...)
|
||||
cells = append(cells, RunesToStyledCells(styleItems, defaultStyle)...)
|
||||
reset()
|
||||
}
|
||||
|
||||
// chop first and last runes
|
||||
chop := func(s []rune) []rune {
|
||||
return s[1 : len(s)-1]
|
||||
}
|
||||
|
||||
for i, _rune := range runes {
|
||||
switch state {
|
||||
case parserStateDefault:
|
||||
if _rune == tokenBeginStyledText {
|
||||
state = parserStateStyledText
|
||||
squareCount = 1
|
||||
styledText = append(styledText, _rune)
|
||||
} else {
|
||||
cells = append(cells, Cell{_rune, defaultStyle})
|
||||
}
|
||||
case parserStateStyledText:
|
||||
switch {
|
||||
case squareCount == 0:
|
||||
switch _rune {
|
||||
case tokenBeginStyle:
|
||||
state = parserStateStyleItems
|
||||
styleItems = append(styleItems, _rune)
|
||||
default:
|
||||
rollback()
|
||||
switch _rune {
|
||||
case tokenBeginStyledText:
|
||||
state = parserStateStyledText
|
||||
squareCount = 1
|
||||
styleItems = append(styleItems, _rune)
|
||||
default:
|
||||
cells = append(cells, Cell{_rune, defaultStyle})
|
||||
}
|
||||
}
|
||||
case len(runes) == i+1:
|
||||
rollback()
|
||||
styledText = append(styledText, _rune)
|
||||
case _rune == tokenBeginStyledText:
|
||||
squareCount++
|
||||
styledText = append(styledText, _rune)
|
||||
case _rune == tokenEndStyledText:
|
||||
squareCount--
|
||||
styledText = append(styledText, _rune)
|
||||
default:
|
||||
styledText = append(styledText, _rune)
|
||||
}
|
||||
case parserStateStyleItems:
|
||||
styleItems = append(styleItems, _rune)
|
||||
if _rune == tokenEndStyle {
|
||||
style := readStyle(chop(styleItems), defaultStyle)
|
||||
cells = append(cells, RunesToStyledCells(chop(styledText), style)...)
|
||||
reset()
|
||||
} else if len(runes) == i+1 {
|
||||
rollback()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cells
|
||||
}
|
||||
|
@ -7,12 +7,12 @@ import (
|
||||
)
|
||||
|
||||
func TestBreakBlocksIntoStrings(t *testing.T) {
|
||||
items := BreakBlocksIntoStrings("test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!")
|
||||
items := breakBlocksIntoStrings("test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!")
|
||||
fmt.Println(strings.Join(items, ""))
|
||||
}
|
||||
|
||||
func TestFindStylePositions(t *testing.T) {
|
||||
items := FindStylePositions("test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!")
|
||||
items := findStylePositions("test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!")
|
||||
if len(items) != 3 {
|
||||
t.Fatal("wrong length", len(items))
|
||||
}
|
||||
@ -28,7 +28,7 @@ func TestFindStylePositions(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFindStyleBlocks(t *testing.T) {
|
||||
items := FindStyleBlocks("test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!")
|
||||
items := findStyleBlocks("test [blue](fg:blue,mod:bold) and [red](fg:red) and maybe even [foo](bg:red)!")
|
||||
if len(items) != 3 {
|
||||
t.Fatal("wrong length", len(items))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user