-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
319 lines (261 loc) · 6.75 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"os"
"runtime/debug"
"strconv"
"strings"
"github.com/charmbracelet/lipgloss"
)
const (
WHITE = "#FFF"
BLACK = "#000"
GRAY = "#AAA"
RED = "#E03C32"
ORANGE = "#FF8C01"
YELLOW = "#FFE733"
LIGHT_GREEN = "#7BB662"
GREEN = "#006B3D"
DARK_GREEN = "#024E1B"
BRIGHT_GREEN = "#00D700"
FUCHSIA = "#FF00FF"
)
var (
brightGreenStyle = NewStyleWithFG(BRIGHT_GREEN)
yellowStyle = NewStyleWithFG(YELLOW)
redColor = NewStyleWithFG(RED)
fuchsiaColor = NewStyleWithFG(FUCHSIA)
covNopeStyle = NewStyleWithFG(BLACK).
Background(lipgloss.Color(GRAY))
covNope = NewCoverageColor(0, 0, BLACK, GRAY)
covColors = []*CoverageColor{
NewCoverageColor(0, 30, BLACK, RED),
NewCoverageColor(30, 50, BLACK, ORANGE),
NewCoverageColor(50, 70, BLACK, YELLOW),
NewCoverageColor(70, 80, BLACK, LIGHT_GREEN),
NewCoverageColor(80, 90, WHITE, GREEN),
NewCoverageColor(90, 100, WHITE, DARK_GREEN),
}
)
type CoverageColor struct {
start float64
end float64
style lipgloss.Style
}
func (c *CoverageColor) Color(v string) string {
return c.style.Render(v)
}
func NewCoverageColor(start, end float64, fgColor, bgColor string) *CoverageColor {
return &CoverageColor{
start: start,
end: end,
style: NewStyleWithFG(fgColor).
Background(lipgloss.Color(bgColor)),
}
}
func NewStyleWithFG(fgColor string) lipgloss.Style {
return lipgloss.NewStyle().Foreground(lipgloss.Color(fgColor))
}
func main() {
checkPipe()
// read lines and fail if something happened
lines, err := readLines()
if err != nil {
log.Fatal(err)
}
noTestFileLines := []string{}
colorizedLines := []string{}
for _, line := range lines {
// append to write [no test files] at the end
if strings.Contains(line, "[no test files]") {
line = reorderNoTestLine(line)
line := colorizeLine(line)
noTestFileLines = append(noTestFileLines, line)
continue
}
// reorder columns for coverage lines
if strings.Contains(line, "coverage:") {
line = reorderCoverageLine(line)
}
line := colorizeLine(line)
colorizedLines = append(colorizedLines, line)
}
// print colorized lines
for _, line := range colorizedLines {
fmt.Println(line)
}
// print [no test files] lines
for _, line := range noTestFileLines {
fmt.Println(line)
}
}
func checkPipe() {
// check if there is something to read on STDIN
stat, err := os.Stdin.Stat()
if err != nil {
log.Fatal(err)
}
isPipe := (stat.Mode() & os.ModeCharDevice) == 0
if !isPipe {
help := `Gocol colorize your coverage
Version: %s
Usage:
go test -cover ./... | gocol
`
version := "<unknown>"
if info, ok := debug.ReadBuildInfo(); ok {
if info.Main.Version != "" {
version = info.Main.Version
}
}
fmt.Printf(help, version)
os.Exit(0)
}
}
func readLines() ([]string, error) {
var lines []string
var line string
var err error
// read until EOF
reader := bufio.NewReader(os.Stdin)
for err == nil {
line, err = reader.ReadString('\n')
line = strings.TrimSpace(line)
if line != "" {
lines = append(lines, line)
}
}
if errors.Is(err, io.EOF) {
return lines, nil
}
return nil, err
}
func reorderCoverageLine(line string) string {
splitted := strings.Split(line, "\t")
if len(splitted) > 3 {
covColumn := splitted[3]
// keep the [no tests to run] at the end
if strings.HasSuffix(covColumn, " [no tests to run]") {
covColumn = strings.TrimSuffix(covColumn, " [no tests to run]")
splitted = append(splitted, "[no tests to run]")
}
// switch package and coverage
splitted[3], splitted[1] = splitted[1], covColumn
}
return strings.Join(splitted, "\t")
}
func reorderNoTestLine(line string) string {
splitted := strings.Split(line, "\t")
if len(splitted) == 3 {
covColumn := splitted[2]
// switch [no test files] and package
splitted[2], splitted[1] = splitted[1], covColumn
}
return strings.Join(splitted, "\t")
}
func colorizeLine(line string) string {
if strings.Contains(line, "coverage:") {
return colorizeCoverageLine(line)
}
if strings.HasPrefix(line, "?") {
line = colorizeMatch(line, "?", fuchsiaColor)
line = colorizeMatchBg(line, "[no test files]", covNopeStyle)
return line
}
trimmed := line
if strings.HasPrefix(line, "--- ") {
trimmed = strings.TrimPrefix(line, "--- ")
}
style := lipgloss.NewStyle()
switch {
case strings.HasPrefix(trimmed, "PASS"):
style = brightGreenStyle
case strings.HasPrefix(trimmed, "SKIP"):
style = yellowStyle
case strings.HasPrefix(trimmed, "FAIL"):
style = redColor
}
return style.Render(line)
}
func colorizeCoverageLine(line string) string {
line = colorizeMatch(line, "ok", brightGreenStyle)
line = colorizeMatch(line, "(cached)", yellowStyle)
line = colorizeMatch(line, "[no tests to run]", fuchsiaColor)
line = colorizeCoverage(line)
return line
}
func colorizeMatch(line, match string, style lipgloss.Style) string {
index := strings.Index(line, match)
if index > -1 {
return strings.Replace(line, match, style.Render(match), 1)
}
return line
}
func colorizeMatchBg(line, match string, style lipgloss.Style) string {
index := strings.Index(line, match)
if index > -1 {
return strings.Replace(line, match, style.Render(match), 1)
}
return line
}
func colorizeCoverage(line string) string {
coverageIndex := strings.Index(line, "coverage:")
if coverageIndex == -1 {
return line
}
// fix different percentage lenght
// if strings.Contains(line, "coverage: 0.0%") {
// line = strings.Replace(line, "coverage: 0.0%", "coverage: 0.0%", 1)
// } else if !strings.Contains(line, "coverage: 100.0%") {
// line = strings.Replace(line, "coverage: ", "coverage: ", 1)
// }
statementsIndex := strings.Index(line, "statements")
if statementsIndex == -1 {
return line
}
end := statementsIndex + len("statements")
// fix different percentage lenght
switch end - coverageIndex {
case 28:
line = strings.Replace(line, "coverage: ", "coverage: ", 1)
end += 2
case 29:
line = strings.Replace(line, "coverage: ", "coverage: ", 1)
end += 1
}
percentage, err := findPercentageValue(line)
if err != nil {
fmt.Println(err)
return line
}
covColor := getCoverageColor(percentage)
return line[:coverageIndex] +
covColor.Color(line[coverageIndex:end]) +
line[end:]
}
func findPercentageValue(line string) (float64, error) {
percentageStr := ""
for _, field := range strings.Fields(line) {
if strings.HasSuffix(field, "%") {
percentageStr = strings.TrimSuffix(field, "%")
break
}
}
return strconv.ParseFloat(percentageStr, 32)
}
func getCoverageColor(coverage float64) *CoverageColor {
if coverage == 0 {
return covNope
}
for _, covCol := range covColors {
if coverage >= covCol.start &&
(coverage < covCol.end || covCol.end == 100) {
return covCol
}
}
return covNope
}