-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
writer.go
54 lines (44 loc) · 1.01 KB
/
writer.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
// Package writer provides error result writers.
package writer
import (
"io"
"github.com/reviewdog/errorformat"
)
// Writer represents error result writer.
type Writer interface {
Write(*errorformat.Entry) error
}
// BufWriter represents buffered error result writer.
type BufWriter interface {
Writer
Flush() error
}
// trackingWriter tracks the last byte written on every write so
// we can avoid printing a newline if one was already written or
// if there is no output at all.
type trackingWriter struct {
w io.Writer
last byte
}
func newTrackingWriter(w io.Writer) *trackingWriter {
return &trackingWriter{
w: w,
last: '\n',
}
}
func (t *trackingWriter) Write(p []byte) (n int, err error) {
n, err = t.w.Write(p)
if n > 0 {
t.last = p[n-1]
}
return
}
var nl = []byte{'\n'}
// WriteNL writes NL.
func (t *trackingWriter) WriteNL() (int, error) {
return t.w.Write(nl)
}
// NeedNL returns true if the last byte written is not NL.
func (t *trackingWriter) NeedNL() bool {
return t.last != '\n'
}