Codebase list golang-github-vbauerster-mpb / c49446b
optimize cuuAndEd write Vladimir Bauer 5 years ago
4 changed file(s) with 56 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
0 package cwriter
1
2 import (
3 "bytes"
4 "fmt"
5 "io/ioutil"
6 "strconv"
7 "testing"
8 )
9
10 func BenchmarkWithFprintf(b *testing.B) {
11 cuuAndEd := "\x1b[%dA\x1b[J"
12 for i := 0; i < b.N; i++ {
13 fmt.Fprintf(ioutil.Discard, cuuAndEd, 4)
14 }
15 }
16
17 func BenchmarkWithJoin(b *testing.B) {
18 bCuuAndEd := [][]byte{[]byte("\x1b["), []byte("A\x1b[J")}
19 for i := 0; i < b.N; i++ {
20 ioutil.Discard.Write(bytes.Join(bCuuAndEd, []byte(strconv.Itoa(4))))
21 }
22 }
23
24 func BenchmarkWithAppend(b *testing.B) {
25 escOpen := []byte("\x1b[")
26 cuuAndEd := []byte("A\x1b[J")
27 for i := 0; i < b.N; i++ {
28 ioutil.Discard.Write(append(strconv.AppendInt(escOpen, 4, 10), cuuAndEd...))
29 }
30 }
31
32 func BenchmarkWithCopy(b *testing.B) {
33 w := New(ioutil.Discard)
34 w.lineCount = 4
35 for i := 0; i < b.N; i++ {
36 w.ansiCuuAndEd()
37 }
38 }
44 "errors"
55 "io"
66 "os"
7 "strconv"
78
89 "github.com/mattn/go-isatty"
910 )
1112 // NotATTY not a TeleTYpewriter error.
1213 var NotATTY = errors.New("not a terminal")
1314
14 const cuuAndEd = "\x1b[%dA\x1b[J"
15 // http://ascii-table.com/ansi-escape-sequences.php
16 const (
17 escOpen = "\x1b["
18 cuuAndEd = "A\x1b[J"
19 )
1520
1621 // Writer is a buffered the writer that updates the terminal. The
1722 // contents of writer will be flushed when Flush is called.
6873 tw, _, err := GetSize(w.fd)
6974 return tw, err
7075 }
76
77 func (w *Writer) ansiCuuAndEd() {
78 buf := make([]byte, 8)
79 buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(w.lineCount), 10)
80 w.out.Write(append(buf, cuuAndEd...))
81 }
22 package cwriter
33
44 import (
5 "fmt"
6
75 "golang.org/x/sys/unix"
86 )
97
108 func (w *Writer) clearLines() {
11 fmt.Fprintf(w.out, cuuAndEd, w.lineCount)
9 w.ansiCuuAndEd()
1210 }
1311
1412 // GetSize returns the dimensions of the given terminal.
22 package cwriter
33
44 import (
5 "fmt"
65 "syscall"
76 "unsafe"
7
8 "github.com/mattn/go-isatty"
89 )
910
1011 var kernel32 = syscall.NewLazyDLL("kernel32.dll")
3637 }
3738
3839 func (w *Writer) clearLines() {
39 if !w.isTerminal {
40 fmt.Fprintf(w.out, cuuAndEd, w.lineCount)
40 if !w.isTerminal && isatty.IsCygwinTerminal(w.fd) {
41 w.ansiCuuAndEd()
4142 }
4243
4344 info := new(consoleScreenBufferInfo)