Codebase list golang-github-vbauerster-mpb / e2318cb
free list without sync.Pool Vladimir Bauer 3 years ago
4 changed file(s) with 12 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
3636 }
3737
3838 func BenchmarkWithCurrentImpl(b *testing.B) {
39 w := New(out)
40 b.ResetTimer()
3941 for i := 0; i < b.N; i++ {
40 _ = ansiCuuAndEd(out, lines)
42 _ = w.ew.ansiCuuAndEd(out, lines)
4143 }
4244 }
55 "io"
66 "os"
77 "strconv"
8 "sync"
98 )
109
1110 // https://github.com/dylanaraps/pure-sh-bible#cursor-movement
1413 cuuAndEd = "A\x1b[J"
1514 )
1615
17 // used by ansiCuuAndEd func
18 var escBuf = sync.Pool{
19 New: func() interface{} {
20 b := make([]byte, 8)
21 return &b
22 },
23 }
24
2516 // ErrNotTTY not a TeleTYpewriter error.
2617 var ErrNotTTY = errors.New("not a terminal")
2718
2920 // contents of writer will be flushed when Flush is called.
3021 type Writer struct {
3122 *bytes.Buffer
23 ew escWriter
3224 out io.Writer
3325 lines int // used by writer_windows only
3426 fd int
4032 func New(out io.Writer) *Writer {
4133 w := &Writer{
4234 Buffer: new(bytes.Buffer),
35 ew: escWriter(make([]byte, 8, 16)),
4336 out: out,
4437 termSize: func(_ int) (int, int, error) {
4538 return -1, -1, ErrNotTTY
6255 return w.termSize(w.fd)
6356 }
6457
65 // if n > 99 it will allocate because escBuf.Get returns slice of length 8
66 func ansiCuuAndEd(out io.Writer, n int) error {
67 bufp := escBuf.Get()
68 buf := *bufp.(*[]byte)
69 buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(n), 10)
70 _, err := out.Write(append(buf, cuuAndEd...))
71 escBuf.Put(bufp)
58 type escWriter []byte
59
60 func (b escWriter) ansiCuuAndEd(out io.Writer, n int) error {
61 b = strconv.AppendInt(b[:copy(b, escOpen)], int64(n), 10)
62 _, err := out.Write(append(b, cuuAndEd...))
7263 return err
7364 }
1010 _, err := w.WriteTo(w.out)
1111 // some terminals interpret 'cursor up 0' as 'cursor up 1'
1212 if err == nil && lines > 0 {
13 err = ansiCuuAndEd(w, lines)
13 err = w.ew.ansiCuuAndEd(w, lines)
1414 }
1515 return err
1616 }
3030 func (w *Writer) clearLines(n int) error {
3131 if !w.terminal {
3232 // hope it's cygwin or similar
33 return ansiCuuAndEd(w.out, n)
33 return w.ew.ansiCuuAndEd(w.out, n)
3434 }
3535
3636 var info windows.ConsoleScreenBufferInfo