Skip to content

Commit

Permalink
dots-v2: hide cursor during output
Browse files Browse the repository at this point in the history
Contributes to gotestyourself#352

Currently, as the new screen is written the cursor jumps around the
terminal. This hides it, writes everything else, then returns it.

Prior art:
https://github.com/docker/compose/blob/80856eacafcf96d6f27e74c07e42386fe662f8ef/pkg/progress/tty.go#L160-L162
  • Loading branch information
howardjohn committed Aug 10, 2023
1 parent 0e788d2 commit 77cd725
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 149 deletions.
1 change: 1 addition & 0 deletions internal/dotwriter/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func (w *Writer) Flush() error {
if w.buf.Len() == 0 {
return nil
}
defer w.hideCursor()()
w.clearLines(w.lineCount)
w.lineCount = bytes.Count(w.buf.Bytes(), []byte{'\n'})
_, err := w.out.Write(w.buf.Bytes())
Expand Down
10 changes: 10 additions & 0 deletions internal/dotwriter/writer_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ import (

// clear the line and move the cursor up
var clear = fmt.Sprintf("%c[%dA%c[2K", ESC, 1, ESC)
var hide = fmt.Sprintf("%c[?25l", ESC)
var show = fmt.Sprintf("%c[?25h", ESC)

func (w *Writer) clearLines(count int) {
_, _ = fmt.Fprint(w.out, strings.Repeat(clear, count))
}

// hideCursor hides the cursor and returns a function to restore the cursor back.
func (w *Writer) hideCursor() func() {
_, _ = fmt.Fprint(w.out, hide)
return func() {
_, _ = fmt.Fprint(w.out, show)
}
}
6 changes: 6 additions & 0 deletions internal/dotwriter/writer_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package dotwriter
Expand Down Expand Up @@ -70,3 +71,8 @@ func isConsole(fd uintptr) bool {
err := windows.GetConsoleMode(windows.Handle(fd), &mode)
return err == nil
}

// This may work on Windows but I am not sure how to do it and its optional. For now, just do nothing.
func (w *Writer) hideCursor() func() {
return func() {}
}
Loading

0 comments on commit 77cd725

Please sign in to comment.