-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_lexemes.go
41 lines (33 loc) · 878 Bytes
/
dump_lexemes.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
package scold
import (
"github.com/logrusorgru/aurora"
"strings"
)
// AltLineFeed is the representation of LF in textual form, that replaces LF
// when it's colorized.
const AltLineFeed = "\\n"
// DumpLexemes is used to transform array of possibly colorized lexemes into a
// human readable format. The lexemes are separated by spaces. There are no
// trailing spaces. Colorized newlines are replaced by printable AltLineFeed
// string + a newline.
func DumpLexemes(xms []RichText, color aurora.Color) string {
var str strings.Builder
x := 0
for _, xm := range xms {
if x != 0 && xm.Str != "\n" {
str.WriteRune(' ')
}
if xm.Str == "\n" {
x = -1
if xm.Colorful() {
str.WriteString(Au.Colorize(AltLineFeed, color).String())
str.WriteRune('\n')
x++
continue
}
}
str.WriteString(xm.Colorize(color))
x++
}
return str.String()
}