Skip to content

Commit

Permalink
Merge pull request #116 from AskAlice/master
Browse files Browse the repository at this point in the history
fix flashing during erase() on windows terminal
  • Loading branch information
briandowns committed Jun 3, 2021
2 parents f42c2c3 + a975c3c commit 2f0aa90
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions spinner.go
Expand Up @@ -96,6 +96,9 @@ var validColors = map[string]bool{
"bgHiWhite": true,
}

// returns true if the OS is windows and the WT_SESSION env variable is set.
var isWindowsTerminalOnWindows = len(os.Getenv("WT_SESSION")) > 0 && runtime.GOOS == "windows"

// returns a valid color's foreground text color attribute
var colorAttributeMap = map[string]color.Attribute{
// default colors for backwards compatibility
Expand Down Expand Up @@ -268,7 +271,7 @@ func (s *Spinner) Start() {
s.mu.Unlock()
return
}
if s.HideCursor && runtime.GOOS != "windows" {
if s.HideCursor && !isWindowsTerminalOnWindows {
// hides the cursor
fmt.Fprint(s.Writer, "\033[?25l")
}
Expand All @@ -287,7 +290,9 @@ func (s *Spinner) Start() {
s.mu.Unlock()
return
}
s.erase()
if !isWindowsTerminalOnWindows {
s.erase()
}

if s.PreUpdate != nil {
s.PreUpdate(s)
Expand Down Expand Up @@ -326,13 +331,17 @@ func (s *Spinner) Stop() {
defer s.mu.Unlock()
if s.active {
s.active = false
if s.HideCursor && runtime.GOOS != "windows" {
if s.HideCursor && !isWindowsTerminalOnWindows {
// makes the cursor visible
fmt.Fprint(s.Writer, "\033[?25h")
}
s.erase()
if s.FinalMSG != "" {
fmt.Fprint(s.Writer, s.FinalMSG)
if isWindowsTerminalOnWindows {
fmt.Fprint(s.Writer, "\r", s.FinalMSG)
}else{
fmt.Fprint(s.Writer, s.FinalMSG)
}
}
s.stopChan <- struct{}{}
}
Expand Down Expand Up @@ -390,13 +399,13 @@ func (s *Spinner) UpdateCharSet(cs []string) {
// Caller must already hold s.lock.
func (s *Spinner) erase() {
n := utf8.RuneCountInString(s.lastOutput)
if runtime.GOOS == "windows" {
if runtime.GOOS == "windows" && !isWindowsTerminalOnWindows {
clearString := "\r" + strings.Repeat(" ", n) + "\r"
fmt.Fprint(s.Writer, clearString)
s.lastOutput = ""
return
}
fmt.Fprintf(s.Writer, "\r\033[K") // erases to end of line
fmt.Fprintf(s.Writer, "\033[K") // erases to end of line
s.lastOutput = ""
}

Expand Down

0 comments on commit 2f0aa90

Please sign in to comment.