-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
76 lines (65 loc) · 1.39 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package ansi
import (
"bytes"
"io"
"unicode/utf8"
)
type Writer struct {
Forward io.Writer
ansi bool
ansiseq bytes.Buffer
lastseq bytes.Buffer
seqchanged bool
runeBuf []byte
}
// Write is used to write content to the ANSI buffer.
func (w *Writer) Write(b []byte) (int, error) {
for _, c := range string(b) {
if c == Marker {
// ANSI escape sequence
w.ansi = true
w.seqchanged = true
_, _ = w.ansiseq.WriteRune(c)
} else if w.ansi {
_, _ = w.ansiseq.WriteRune(c)
if IsTerminator(c) {
// ANSI sequence terminated
w.ansi = false
if bytes.HasSuffix(w.ansiseq.Bytes(), []byte("[0m")) {
// reset sequence
w.lastseq.Reset()
w.seqchanged = false
} else if c == 'm' {
// color code
_, _ = w.lastseq.Write(w.ansiseq.Bytes())
}
_, _ = w.ansiseq.WriteTo(w.Forward)
}
} else {
_, err := w.writeRune(c)
if err != nil {
return 0, err
}
}
}
return len(b), nil
}
func (w *Writer) writeRune(r rune) (int, error) {
if w.runeBuf == nil {
w.runeBuf = make([]byte, utf8.UTFMax)
}
n := utf8.EncodeRune(w.runeBuf, r)
return w.Forward.Write(w.runeBuf[:n])
}
func (w *Writer) LastSequence() string {
return w.lastseq.String()
}
func (w *Writer) ResetAnsi() {
if !w.seqchanged {
return
}
_, _ = w.Forward.Write([]byte("\x1b[0m"))
}
func (w *Writer) RestoreAnsi() {
_, _ = w.Forward.Write(w.lastseq.Bytes())
}