-
Notifications
You must be signed in to change notification settings - Fork 7
/
text.go
84 lines (70 loc) · 2.16 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package console
import (
"regexp"
"strings"
"github.com/BigJk/ramen/concolor"
"github.com/BigJk/ramen/t"
)
var colorSectionRegex = regexp.MustCompile("\\[\\[(([bf]):(#[0-9a-zA-Z]+))(\\|(([bf]):(#[0-9a-zA-Z]+)))?\\]\\]")
// ColorSection represents a colorized section in a text.
type ColorSection struct {
Index int
Transformer []t.Transformer
}
// ColorSections represents a slice of color sections.
type ColorSections []*ColorSection
// GetCurrent gets the current color section for the given index in a string.
func (cs ColorSections) GetCurrent(index int) *ColorSection {
for i := len(cs) - 1; i >= 0; i-- {
if index >= cs[i].Index {
return cs[i]
}
}
return nil
}
// GetCurrentTransformer gets the transformers for the current color section
// for the given index in a string.
func (cs ColorSections) GetCurrentTransformer(index int) []t.Transformer {
c := cs.GetCurrent(index)
if c != nil {
return c.Transformer
}
return []t.Transformer{}
}
// ParseColoredText parses the coloring annotations in a string and returns the cleaned string
// and the parsed color sections.
func ParseColoredText(text string) (string, ColorSections) {
var results []*ColorSection
for {
match := colorSectionRegex.FindStringSubmatch(text)
if len(match) <= 1 {
break
}
var cs ColorSection
cs.Index = strings.Index(text, match[0])
text = strings.Replace(text, match[0], "", 1)
if len(match[4]) == 0 {
switch match[2] {
case "f":
cs.Transformer = append(cs.Transformer, t.Foreground(concolor.MustHex(match[3])))
case "b":
cs.Transformer = append(cs.Transformer, t.Background(concolor.MustHex(match[3])))
}
} else if len(match) == 8 {
switch match[2] {
case "f":
cs.Transformer = append(cs.Transformer, t.Foreground(concolor.MustHex(match[3])))
case "b":
cs.Transformer = append(cs.Transformer, t.Background(concolor.MustHex(match[3])))
}
switch match[6] {
case "f":
cs.Transformer = append(cs.Transformer, t.Foreground(concolor.MustHex(match[7])))
case "b":
cs.Transformer = append(cs.Transformer, t.Background(concolor.MustHex(match[7])))
}
}
results = append(results, &cs)
}
return text, results
}