diff --git a/cwriter/writer.go b/cwriter/writer.go index 19fd90e..c25cf43 100644 --- a/cwriter/writer.go +++ b/cwriter/writer.go @@ -22,7 +22,6 @@ type Writer struct { out io.Writer buf bytes.Buffer - lines int // how much lines to clear before flushing new ones fd int terminal bool termSize func(int) (int, int, error) @@ -49,17 +48,13 @@ } // Flush flushes the underlying buffer. -func (w *Writer) Flush(lines int) (err error) { +func (w *Writer) Flush(lines int) error { + _, err := w.buf.WriteTo(w.out) // some terminals interpret 'cursor up 0' as 'cursor up 1' - if w.lines > 0 { - err = w.clearLines() - if err != nil { - return - } + if err == nil && lines > 0 { + err = w.clearLines(lines) } - w.lines = lines - _, err = w.buf.WriteTo(w.out) - return + return err } // Write appends the contents of p to the underlying buffer. @@ -83,9 +78,9 @@ return w.termSize(w.fd) } -func (w *Writer) ansiCuuAndEd() error { +func (w *Writer) ansiCuuAndEd(n int) error { buf := make([]byte, 8) - buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(w.lines), 10) - _, err := w.out.Write(append(buf, cuuAndEd...)) + buf = strconv.AppendInt(buf[:copy(buf, escOpen)], int64(n), 10) + _, err := w.buf.Write(append(buf, cuuAndEd...)) return err } diff --git a/cwriter/writer_posix.go b/cwriter/writer_posix.go index 9d03197..d3e1acd 100644 --- a/cwriter/writer_posix.go +++ b/cwriter/writer_posix.go @@ -6,8 +6,8 @@ "golang.org/x/sys/unix" ) -func (w *Writer) clearLines() error { - return w.ansiCuuAndEd() +func (w *Writer) clearLines(n int) error { + return w.ansiCuuAndEd(n) } // GetSize returns the dimensions of the given terminal. diff --git a/cwriter/writer_windows.go b/cwriter/writer_windows.go index 91fdfb3..cc8d4d9 100644 --- a/cwriter/writer_windows.go +++ b/cwriter/writer_windows.go @@ -15,7 +15,7 @@ procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW") ) -func (w *Writer) clearLines() error { +func (w *Writer) clearLines(n int) error { if !w.terminal { // hope it's cygwin or similar return w.ansiCuuAndEd() @@ -26,7 +26,7 @@ return err } - info.CursorPosition.Y -= int16(w.lines) + info.CursorPosition.Y -= int16(n) if info.CursorPosition.Y < 0 { info.CursorPosition.Y = 0 } @@ -40,7 +40,7 @@ X: info.Window.Left, Y: info.CursorPosition.Y, } - count := uint32(info.Size.X) * uint32(w.lines) + count := uint32(info.Size.X) * uint32(n) _, _, _ = procFillConsoleOutputCharacter.Call( uintptr(w.fd), uintptr(' '),