-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformat.go
46 lines (35 loc) · 890 Bytes
/
format.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
package color
import (
"regexp"
"strings"
)
// String function recognizes certain patterns in the entered text and formats the text according to these patterns.
func String(text string) string {
pattern := `&[a-zA-Z0-9]+`
regx := regexp.MustCompile(pattern)
matches := regx.FindAllString(text, -1)
replace := func(match string, new string) {
pattern := match + `(\s{1})?`
text = regexp.MustCompile(pattern).ReplaceAllString(text, new)
}
for _, match := range matches {
sgr, exists := sgrParams[strings.ToLower(match[1:])]
if exists {
replace(match, SgrToEscape(sgr))
continue
}
matchLower := strings.ToLower(match)
var target string
back := matchLower[1:3] == "bg"
if back {
target = matchLower[3:]
} else {
target = matchLower[1:]
}
escape, exists := escape(target, back)
if exists {
replace(match, escape)
}
}
return text
}