diff --git a/writer_posix.go b/writer_posix.go index bec6a1d..1f1516a 100644 --- a/writer_posix.go +++ b/writer_posix.go @@ -4,11 +4,12 @@ package uilive import ( "fmt" + "strings" ) +// clear the line and move the cursor up +var clear = fmt.Sprintf("%c[%dA%c[2K", ESC, 1, ESC) + func (w *Writer) clearLines() { - for i := 0; i < w.lineCount; i++ { - fmt.Fprintf(w.Out, "%c[2K", ESC) // clear the line - fmt.Fprintf(w.Out, "%c[%dA", ESC, 1) // move the cursor up - } + fmt.Fprint(w.Out, strings.Repeat(clear, w.lineCount)) } diff --git a/writer_windows.go b/writer_windows.go index 4de3b36..f44a487 100644 --- a/writer_windows.go +++ b/writer_windows.go @@ -4,9 +4,11 @@ package uilive import ( "fmt" - "github.com/mattn/go-isatty" + "strings" "syscall" "unsafe" + + "github.com/mattn/go-isatty" ) var kernel32 = syscall.NewLazyDLL("kernel32.dll") @@ -18,6 +20,9 @@ var ( procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute") ) +// clear the line and move the cursor up +var clear = fmt.Sprintf("%c[%dA%c[2K", ESC, 1, ESC) + type short int16 type dword uint32 type word uint16 @@ -48,10 +53,7 @@ func (w *Writer) clearLines() { ok = false } if !ok { - for i := 0; i < w.lineCount; i++ { - fmt.Fprintf(w.Out, "%c[%dA", ESC, 0) // move the cursor up - fmt.Fprintf(w.Out, "%c[2K\r", ESC) // clear the line - } + fmt.Fprint(w.Out, strings.Repeat(clear, w.lineCount)) return } fd := f.Fd()