Codebase list golang-github-vbauerster-mpb / 2a8b5e0
write esc cuuAndEd to buf rather than to out drectly Vladimir Bauer 3 years ago
3 changed file(s) with 13 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
2121 type Writer struct {
2222 out io.Writer
2323 buf bytes.Buffer
24 lines int // how much lines to clear before flushing new ones
2524 fd int
2625 terminal bool
2726 termSize func(int) (int, int, error)
4847 }
4948
5049 // Flush flushes the underlying buffer.
51 func (w *Writer) Flush(lines int) (err error) {
50 func (w *Writer) Flush(lines int) error {
51 _, err := w.buf.WriteTo(w.out)
5252 // some terminals interpret 'cursor up 0' as 'cursor up 1'
53 if w.lines > 0 {
54 err = w.clearLines()
55 if err != nil {
56 return
57 }
53 if err == nil && lines > 0 {
54 err = w.clearLines(lines)
5855 }
59 w.lines = lines
60 _, err = w.buf.WriteTo(w.out)
61 return
56 return err
6257 }
6358
6459 // Write appends the contents of p to the underlying buffer.
8277 return w.termSize(w.fd)
8378 }
8479
85 func (w *Writer) ansiCuuAndEd() error {
80 func (w *Writer) ansiCuuAndEd(n int) error {
8681 buf := make([]byte, 8)
87 buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(w.lines), 10)
88 _, err := w.out.Write(append(buf, cuuAndEd...))
82 buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(n), 10)
83 _, err := w.buf.Write(append(buf, cuuAndEd...))
8984 return err
9085 }
55 "golang.org/x/sys/unix"
66 )
77
8 func (w *Writer) clearLines() error {
9 return w.ansiCuuAndEd()
8 func (w *Writer) clearLines(n int) error {
9 return w.ansiCuuAndEd(n)
1010 }
1111
1212 // GetSize returns the dimensions of the given terminal.
1414 procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
1515 )
1616
17 func (w *Writer) clearLines() error {
17 func (w *Writer) clearLines(n int) error {
1818 if !w.terminal {
1919 // hope it's cygwin or similar
2020 return w.ansiCuuAndEd()
2525 return err
2626 }
2727
28 info.CursorPosition.Y -= int16(w.lines)
28 info.CursorPosition.Y -= int16(n)
2929 if info.CursorPosition.Y < 0 {
3030 info.CursorPosition.Y = 0
3131 }
3939 X: info.Window.Left,
4040 Y: info.CursorPosition.Y,
4141 }
42 count := uint32(info.Size.X) * uint32(w.lines)
42 count := uint32(info.Size.X) * uint32(n)
4343 _, _, _ = procFillConsoleOutputCharacter.Call(
4444 uintptr(w.fd),
4545 uintptr(' '),