remove duplication of clear codes for win and posix, write all at once
Kirill Merkushev authored 8 years ago
Vladimir Bauer committed 8 years ago
| 2 | 2 |
import (
|
| 3 | 3 |
"bytes"
|
| 4 | 4 |
"io"
|
|
5 |
"fmt"
|
| 5 | 6 |
)
|
| 6 | 7 |
|
| 7 | 8 |
// ESC is the ASCII code for escape character
|
| 8 | 9 |
const ESC = 27
|
|
10 |
|
|
11 |
var (
|
|
12 |
cursorUp = fmt.Sprintf("%c[%dA", ESC, 1)
|
|
13 |
clearLine = fmt.Sprintf("%c[2K\r", ESC)
|
|
14 |
clearCursorAndLine = cursorUp + clearLine
|
|
15 |
)
|
| 9 | 16 |
|
| 10 | 17 |
// Writer is a buffered the writer that updates the terminal.
|
| 11 | 18 |
// The contents of writer will be flushed when Flush is called.
|
| 5 | 5 |
"fmt"
|
| 6 | 6 |
"syscall"
|
| 7 | 7 |
"unsafe"
|
| 8 | |
)
|
| 9 | |
|
| 10 | |
var (
|
| 11 | |
cursorUp = fmt.Sprintf("%c[%dA", ESC, 1)
|
| 12 | |
clearLine = fmt.Sprintf("%c[2K\r", ESC)
|
| 13 | |
clearCursorAndLine = cursorUp + clearLine
|
|
8 |
"strings"
|
| 14 | 9 |
)
|
| 15 | 10 |
|
| 16 | 11 |
func (w *Writer) clearLines() {
|
| 17 | |
for i := 0; i < w.lineCount; i++ {
|
| 18 | |
fmt.Fprint(w.out, clearCursorAndLine)
|
| 19 | |
}
|
|
12 |
fmt.Fprint(w.out, strings.Repeat(clearCursorAndLine, w.lineCount))
|
| 20 | 13 |
}
|
| 21 | 14 |
|
| 22 | 15 |
// GetTermSize returns the dimensions of the given terminal.
|
| 17 | 17 |
out := new(bytes.Buffer)
|
| 18 | 18 |
w := cwriter.New(out)
|
| 19 | 19 |
|
| 20 | |
testCases := []struct {
|
|
20 |
for _, tcase := range []struct {
|
| 21 | 21 |
input, expectedOutput string
|
| 22 | 22 |
}{
|
| 23 | 23 |
{input: "foo\n", expectedOutput: "foo\n"},
|
| 24 | 24 |
{input: "bar\n", expectedOutput: "foo\n" + clearSequence + "bar\n"},
|
| 25 | 25 |
{input: "fizz\n", expectedOutput: "foo\n" + clearSequence + "bar\n" + clearSequence + "fizz\n"},
|
| 26 | |
}
|
| 27 | |
for _, testCase := range testCases {
|
| 28 | |
w.Write([]byte(testCase.input))
|
| 29 | |
w.Flush()
|
| 30 | |
output := out.String()
|
| 31 | |
if output != testCase.expectedOutput {
|
| 32 | |
t.Fatalf("want %q, got %q", testCase.expectedOutput, output)
|
| 33 | |
}
|
|
26 |
} {
|
|
27 |
t.Run(tcase.input, func(t *testing.T) {
|
|
28 |
w.Write([]byte(tcase.input))
|
|
29 |
w.Flush()
|
|
30 |
output := out.String()
|
|
31 |
if output != tcase.expectedOutput {
|
|
32 |
t.Fatalf("want %q, got %q", tcase.expectedOutput, output)
|
|
33 |
}
|
|
34 |
})
|
| 34 | 35 |
}
|
| 35 | 36 |
}
|
| 6 | 6 |
"io"
|
| 7 | 7 |
"syscall"
|
| 8 | 8 |
"unsafe"
|
| 9 | |
|
|
9 |
"strings"
|
| 10 | 10 |
"github.com/mattn/go-isatty"
|
| 11 | 11 |
)
|
| 12 | 12 |
|
|
| 52 | 52 |
func (w *Writer) clearLines() {
|
| 53 | 53 |
f, ok := w.out.(FdWriter)
|
| 54 | 54 |
if ok && !isatty.IsTerminal(f.Fd()) {
|
| 55 | |
for i := 0; i < w.lineCount; i++ {
|
| 56 | |
fmt.Fprintf(w.out, "%c[%dA", ESC, 1) // move the cursor up
|
| 57 | |
fmt.Fprintf(w.out, "%c[2K\r", ESC) // clear the line
|
| 58 | |
}
|
|
55 |
fmt.Fprint(w.out, strings.Repeat(clearCursorAndLine, w.lineCount))
|
| 59 | 56 |
return
|
| 60 | 57 |
}
|
| 61 | 58 |
fd := f.Fd()
|