Skip to content

Commit 12b31a6

Browse files
authored
Move cursor to the beginning of the line before erasing (#126)
The \033[K escape sequence signifies 'clear from cursor position to the end of the line'. Without moving the cursor to the beggining of the line first it results in no text being deleted. The behaviour of Carriage Return (\r) in terminal emulators is not formally standardized but it is generally interpreted as a move to the beginning of the line. Fixes #123
1 parent 8cd3c74 commit 12b31a6

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

spinner.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func (s *Spinner) UpdateCharSet(cs []string) {
395395
s.mu.Unlock()
396396
}
397397

398-
// erase deletes written characters.
398+
// erase deletes written characters on the current line.
399399
// Caller must already hold s.lock.
400400
func (s *Spinner) erase() {
401401
n := utf8.RuneCountInString(s.lastOutput)
@@ -405,7 +405,14 @@ func (s *Spinner) erase() {
405405
s.lastOutput = ""
406406
return
407407
}
408-
fmt.Fprintf(s.Writer, "\033[K") // erases to end of line
408+
409+
// Taken from https://en.wikipedia.org/wiki/ANSI_escape_code:
410+
// \r - Carriage return - Moves the cursor to column zero
411+
// \033[K - Erases part of the line. If n is 0 (or missing), clear from
412+
// cursor to the end of the line. If n is 1, clear from cursor to beginning
413+
// of the line. If n is 2, clear entire line. Cursor position does not
414+
// change.
415+
fmt.Fprintf(s.Writer, "\r\033[K")
409416
s.lastOutput = ""
410417
}
411418

0 commit comments

Comments
 (0)