-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripansi.go
71 lines (56 loc) · 1.62 KB
/
stripansi.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
// package stripansi provides utilities for removing ANSI escape sequences using regular expressions.
package stripansi
import (
"io"
"regexp"
"sync"
)
var re *regexp.Regexp = regexp.MustCompile("[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))")
// Regexp returns a copy of the underlying [regexp.Regexp].
func Regexp() *regexp.Regexp {
return re.Copy()
}
// Bytes removes ANSI escape sequences from the byte slice.
func Bytes(b []byte) []byte {
return re.ReplaceAll(b, nil)
}
// String removes ANSI escape sequences from the string.
func String(s string) string {
return re.ReplaceAllString(s, "")
}
// Writer wraps an [io.Writer] and removes ANSI escape sequences from its output.
type Writer struct {
w io.Writer
mu sync.Mutex
}
// NewWriter creates a new [Writer].
func NewWriter(w io.Writer) *Writer {
return &Writer{w: w}
}
// Write removes ANSI escape sequences and writes to the underlying writer.
func (w *Writer) Write(p []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.w.Write(Bytes(p))
}
// Reader wraps an [io.Reader] and removes ANSI escape sequences from its output.
type Reader struct {
r io.Reader
mu sync.Mutex
}
// NewReader creates a new [Reader].
func NewReader(r io.Reader) *Reader {
return &Reader{r: r}
}
// Read reads from the underlying reader and removes ANSI escape sequences.
func (r *Reader) Read(p []byte) (n int, err error) {
r.mu.Lock()
defer r.mu.Unlock()
n, err = r.r.Read(p)
if err != nil {
return n, err
}
cleaned := Bytes(p[:n])
copy(p, cleaned)
return len(cleaned), nil
}