diff --git a/cwriter/writer.go b/cwriter/writer.go index cc76da6..095dae1 100644 --- a/cwriter/writer.go +++ b/cwriter/writer.go @@ -6,6 +6,7 @@ "io" "os" "strconv" + "sync" ) // https://github.com/dylanaraps/pure-sh-bible#cursor-movement @@ -15,7 +16,12 @@ ) // used by ansiCuuAndEd func -var escBuf = make([]byte, 8) +var escBuf = sync.Pool{ + New: func() interface{} { + b := make([]byte, 8) + return &b + }, +} // ErrNotTTY not a TeleTYpewriter error. var ErrNotTTY = errors.New("not a terminal") @@ -56,9 +62,12 @@ return w.termSize(w.fd) } -// if n > 99 it will allocate because of len(escBuf) = 8 +// if n > 99 it will allocate because escBuf.Get returns slice of length 8 func ansiCuuAndEd(out io.Writer, n int) error { - escBuf = strconv.AppendInt(escBuf[:copy(escBuf, escOpen)], int64(n), 10) - _, err := out.Write(append(escBuf, cuuAndEd...)) + bufp := escBuf.Get() + buf := *bufp.(*[]byte) + buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(n), 10) + _, err := out.Write(append(buf, cuuAndEd...)) + escBuf.Put(bufp) return err }