Skip to content

Commit

Permalink
Merge pull request #354 from howardjohn/dots/hide-cursor
Browse files Browse the repository at this point in the history
dots-v2: hide cursor during output
  • Loading branch information
dnephin committed Aug 12, 2023
2 parents 0e788d2 + 77cd725 commit b637c82
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() {}
}

0 comments on commit b637c82

Please sign in to comment.