allocless ansiCuuAndEd func
Vladimir Bauer
3 years ago
| 7 | 7 | "strconv" |
| 8 | 8 | ) |
| 9 | 9 | |
| 10 | // ErrNotTTY not a TeleTYpewriter error. | |
| 11 | var ErrNotTTY = errors.New("not a terminal") | |
| 12 | ||
| 13 | 10 | // https://github.com/dylanaraps/pure-sh-bible#cursor-movement |
| 14 | 11 | const ( |
| 15 | 12 | escOpen = "\x1b[" |
| 16 | 13 | cuuAndEd = "A\x1b[J" |
| 17 | 14 | ) |
| 15 | ||
| 16 | // used by ansiCuuAndEd func | |
| 17 | var escBuf = make([]byte, 8) | |
| 18 | ||
| 19 | // ErrNotTTY not a TeleTYpewriter error. | |
| 20 | var ErrNotTTY = errors.New("not a terminal") | |
| 18 | 21 | |
| 19 | 22 | // Writer is a buffered the writer that updates the terminal. The |
| 20 | 23 | // contents of writer will be flushed when Flush is called. |
| 52 | 55 | return w.termSize(w.fd) |
| 53 | 56 | } |
| 54 | 57 | |
| 58 | // if n > 99 it will allocate because of len(escBuf) = 8 | |
| 55 | 59 | func ansiCuuAndEd(out io.Writer, n int) error { |
| 56 | buf := make([]byte, 8) | |
| 57 | buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(n), 10) | |
| 58 | _, err := out.Write(append(buf, cuuAndEd...)) | |
| 60 | escBuf = strconv.AppendInt(escBuf[:copy(escBuf, escOpen)], int64(n), 10) | |
| 61 | _, err := out.Write(append(escBuf, cuuAndEd...)) | |
| 59 | 62 | return err |
| 60 | 63 | } |