diff --git a/spinner.go b/spinner.go index d67ad98..9e8e697 100644 --- a/spinner.go +++ b/spinner.go @@ -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 @@ -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") } @@ -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) @@ -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{}{} } @@ -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 = "" }