Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dots-v2: hide cursor during output #354

Merged
merged 1 commit into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() {}
}