Codebase list golang-github-vbauerster-mpb / c61dc34
wrap escBuf with sync.Pool Vladimir Bauer 3 years ago
1 changed file(s) with 13 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
55 "io"
66 "os"
77 "strconv"
8 "sync"
89 )
910
1011 // https://github.com/dylanaraps/pure-sh-bible#cursor-movement
1415 )
1516
1617 // used by ansiCuuAndEd func
17 var escBuf = make([]byte, 8)
18 var escBuf = sync.Pool{
19 New: func() interface{} {
20 b := make([]byte, 8)
21 return &b
22 },
23 }
1824
1925 // ErrNotTTY not a TeleTYpewriter error.
2026 var ErrNotTTY = errors.New("not a terminal")
5561 return w.termSize(w.fd)
5662 }
5763
58 // if n > 99 it will allocate because of len(escBuf) = 8
64 // if n > 99 it will allocate because escBuf.Get returns slice of length 8
5965 func ansiCuuAndEd(out io.Writer, n int) error {
60 escBuf = strconv.AppendInt(escBuf[:copy(escBuf, escOpen)], int64(n), 10)
61 _, err := out.Write(append(escBuf, cuuAndEd...))
66 bufp := escBuf.Get()
67 buf := *bufp.(*[]byte)
68 buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(n), 10)
69 _, err := out.Write(append(buf, cuuAndEd...))
70 escBuf.Put(bufp)
6271 return err
6372 }