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

Move cursor to the beginning of the line before erasing #126

Merged
merged 1 commit into from Dec 17, 2021
Merged
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
11 changes: 9 additions & 2 deletions spinner.go
Expand Up @@ -395,7 +395,7 @@ func (s *Spinner) UpdateCharSet(cs []string) {
s.mu.Unlock()
}

// erase deletes written characters.
// erase deletes written characters on the current line.
// Caller must already hold s.lock.
func (s *Spinner) erase() {
n := utf8.RuneCountInString(s.lastOutput)
Expand All @@ -405,7 +405,14 @@ func (s *Spinner) erase() {
s.lastOutput = ""
return
}
fmt.Fprintf(s.Writer, "\033[K") // erases to end of line

// Taken from https://en.wikipedia.org/wiki/ANSI_escape_code:
// \r - Carriage return - Moves the cursor to column zero
// \033[K - Erases part of the line. If n is 0 (or missing), clear from
// cursor to the end of the line. If n is 1, clear from cursor to beginning
// of the line. If n is 2, clear entire line. Cursor position does not
// change.
fmt.Fprintf(s.Writer, "\r\033[K")
s.lastOutput = ""
}

Expand Down