-
Notifications
You must be signed in to change notification settings - Fork 1
/
highlighter.go
151 lines (135 loc) · 3.34 KB
/
highlighter.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package logview
import (
"fmt"
"github.com/dlclark/regexp2"
"github.com/gdamore/tcell/v2"
"sort"
"strconv"
"strings"
)
func (lv *LogView) defaultStyleEvent(event *logEventLine) *logEventLine {
event.styleSpans = []styledSpan{
{
start: 0,
end: len(event.Runes),
style: lv.defaultStyle,
},
}
return event
}
type captureGroup struct {
regexp2.Capture
name string
}
type captureGroupSorter []captureGroup
func (cg captureGroupSorter) Len() int {
return len(cg)
}
func (cg captureGroupSorter) Less(i, j int) bool {
return cg[i].Index < cg[j].Index
}
func (cg captureGroupSorter) Swap(i, j int) {
cg[i], cg[j] = cg[j], cg[i]
}
func isInt(text string) bool {
_, err := strconv.Atoi(text)
return err == nil
}
func (lv *LogView) colorize(event *logEventLine) *logEventLine {
if event.order != 0 {
panic(fmt.Errorf("cannot colorize wrapped line"))
}
defaultStyle := lv.defaultStyle
useSpecialBg := false
if lv.highlightLevels && event.Level != LogLevelInfo {
useSpecialBg = true
if event.Level == LogLevelWarning {
defaultStyle = defaultStyle.Background(lv.warningBgColor)
} else {
defaultStyle = defaultStyle.Background(lv.errorBgColor)
}
}
if lv.highlightingEnabled && lv.highlightPattern != nil {
text := event.message()
match, err := lv.highlightPattern.FindStringMatch(text)
if err != nil || match == nil {
return lv.defaultStyleEvent(event)
}
groups := make([]captureGroup, 0)
for match != nil {
for _, gr := range match.Groups() {
if len(gr.Captures) > 0 && !isInt(gr.Name) {
groups = append(groups, captureGroup{
Capture: gr.Capture,
name: gr.Name,
})
}
}
match, err = lv.highlightPattern.FindNextMatch(match)
if err != nil {
return lv.defaultStyleEvent(event)
}
}
sort.Sort(captureGroupSorter(groups))
event.styleSpans = lv.buildSpans([]rune(text), groups, defaultStyle, useSpecialBg)
} else {
event.styleSpans = []styledSpan{
{
start: 0,
end: len(event.Runes),
style: defaultStyle,
},
}
}
return event
}
func (lv *LogView) groupNameToStyle(colorName string) tcell.Style {
style := lv.defaultStyle
colorPair := strings.Split(strings.ToLower(colorName), "_")
if fg, ok := tcell.ColorNames[colorPair[0]]; ok {
style = style.Foreground(fg)
}
if len(colorPair) > 1 {
if bg, ok := tcell.ColorNames[colorPair[1]]; ok {
style = style.Background(bg)
}
}
return style
}
func (lv *LogView) buildSpans(text []rune, groups []captureGroup, defaultStyle tcell.Style, useDefaultBg bool) []styledSpan {
currentPos := 0
spans := make([]styledSpan, 0)
_, dbg, _ := defaultStyle.Decompose()
for _, group := range groups {
if group.Index != currentPos {
runeLen := len(text[currentPos:group.Index])
beforeSpan := styledSpan{
start: currentPos,
end: currentPos + runeLen,
style: defaultStyle,
}
spans = append(spans, beforeSpan)
currentPos = currentPos + runeLen
}
style := lv.groupNameToStyle(group.name)
if useDefaultBg {
style = style.Background(dbg)
}
matched := styledSpan{
start: currentPos,
end: currentPos + group.Length,
style: style,
}
spans = append(spans, matched)
currentPos += group.Length
}
if currentPos < len(text) {
afterSpan := styledSpan{
start: currentPos,
end: len(text) + 1,
style: defaultStyle,
}
spans = append(spans, afterSpan)
}
return spans
}