forked from greut/eclint
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.go
124 lines (99 loc) · 2.72 KB
/
print.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
package eclint
import (
"bytes"
"context"
"errors"
"fmt"
"strconv"
"github.com/go-logr/logr"
"github.com/logrusorgru/aurora"
)
// PrintErrors is the rich output of the program.
func PrintErrors(ctx context.Context, opt *Option, filename string, errs []error) error { //nolint:gocognit
counter := 0
log := logr.FromContextOrDiscard(ctx)
stdout := opt.Stdout
au := aurora.NewAurora(opt.IsTerminal && !opt.NoColors)
for _, err := range errs {
if err != nil { //nolint:nestif
if counter == 0 && !opt.Summary {
fmt.Fprintf(stdout, "%s:\n", au.Magenta(filename).Bold())
}
var ve ValidationError
if ok := errors.As(err, &ve); ok {
log.V(4).Info("lint error", "error", ve)
if !opt.Summary {
vi := au.Green(strconv.Itoa(ve.Index + 1)).Bold()
vp := au.Green(strconv.Itoa(ve.Position + 1)).Bold()
fmt.Fprintf(stdout, "%s:%s: %s\n", vi, vp, ve.Message)
l, err := errorAt(au, ve.Line, ve.Position)
if err != nil {
log.Error(err, "line formatting failure", "error", ve)
return err
}
fmt.Fprintln(stdout, l)
}
} else {
log.V(2).Info("lint error", "filename", filename, "error", err.Error())
fmt.Fprintln(stdout, err)
}
counter++
if opt.ShowErrorQuantity > 0 && counter >= opt.ShowErrorQuantity && len(errs) > counter {
fmt.Fprintf(
stdout,
" ... skipping at most %s errors\n",
au.BrightRed(strconv.Itoa(len(errs)-counter)),
)
break
}
}
}
if counter > 0 {
if !opt.Summary {
fmt.Fprintln(stdout, "")
} else {
fmt.Fprintf(stdout, "%s: %d errors\n", au.Magenta(filename), counter)
}
}
return nil
}
// errorAt highlights the ValidationError position within the line.
func errorAt(au aurora.Aurora, line []byte, position int) (string, error) {
b := bytes.NewBuffer(make([]byte, len(line)))
if position > len(line)-1 {
position = len(line) - 1
}
for i := 0; i < position; i++ {
if line[i] != cr && line[i] != lf {
if err := b.WriteByte(line[i]); err != nil {
return "", fmt.Errorf("error writing byte: %w", err)
}
}
}
// Rewind the 0x10xxxxxx that are UTF-8 continuation markers
for i := position; i > 0; i-- {
if (line[i] >> 6) != 0b10 {
break
}
position--
}
// XXX this will break every non latin1 line.
s := " "
if position < len(line)-1 {
s = string(line[position : position+1])
}
if _, err := b.WriteString(au.White(s).BgRed().String()); err != nil {
return "", fmt.Errorf("error writing string: %w", err)
}
for i := position + 1; i < len(line); i++ {
if line[i] != cr && line[i] != lf {
if err := b.WriteByte(line[i]); err != nil {
return "", fmt.Errorf("error writing byte: %w", err)
}
if (line[i] >> 6) == 0b10 {
i++
}
}
}
return b.String(), nil
}