From fdd2413559b810a0eabb46d0a6c3e6039618e630 Mon Sep 17 00:00:00 2001 From: AskAlice Date: Thu, 3 Jun 2021 08:29:24 -0600 Subject: [PATCH 1/3] fix flashing during erase() on windows. --- spinner.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/spinner.go b/spinner.go index d67ad98..2046455 100644 --- a/spinner.go +++ b/spinner.go @@ -268,7 +268,7 @@ func (s *Spinner) Start() { s.mu.Unlock() return } - if s.HideCursor && runtime.GOOS != "windows" { + if s.HideCursor && len(os.Getenv("WT_SESSION")) == 0 { // hides the cursor fmt.Fprint(s.Writer, "\033[?25l") } @@ -287,7 +287,9 @@ func (s *Spinner) Start() { s.mu.Unlock() return } - s.erase() + if len(os.Getenv("WT_SESSION")) == 0 { + s.erase() + } if s.PreUpdate != nil { s.PreUpdate(s) @@ -326,13 +328,17 @@ func (s *Spinner) Stop() { defer s.mu.Unlock() if s.active { s.active = false - if s.HideCursor && runtime.GOOS != "windows" { + if s.HideCursor && len(os.Getenv("WT_SESSION")) == 0 { // makes the cursor visible fmt.Fprint(s.Writer, "\033[?25h") } s.erase() if s.FinalMSG != "" { - fmt.Fprint(s.Writer, s.FinalMSG) + if runtime.GOOS == "windows" && len(os.Getenv("WT_SESSION")) > 0{ + fmt.Fprint(s.Writer, "\r", s.FinalMSG) + }else{ + fmt.Fprint(s.Writer, s.FinalMSG) + } } s.stopChan <- struct{}{} } @@ -390,7 +396,7 @@ 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" && len(os.Getenv("WT_SESSION")) == 0 { clearString := "\r" + strings.Repeat(" ", n) + "\r" fmt.Fprint(s.Writer, clearString) s.lastOutput = "" From 08a40670628846c9fe81ad12fd723e6c50d70d76 Mon Sep 17 00:00:00 2001 From: AskAlice Date: Thu, 3 Jun 2021 08:58:02 -0600 Subject: [PATCH 2/3] clean up booleans --- spinner.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/spinner.go b/spinner.go index 2046455..7ed4187 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 && len(os.Getenv("WT_SESSION")) == 0 { + if s.HideCursor && !isWindowsTerminalOnWindows { // hides the cursor fmt.Fprint(s.Writer, "\033[?25l") } @@ -287,7 +290,7 @@ func (s *Spinner) Start() { s.mu.Unlock() return } - if len(os.Getenv("WT_SESSION")) == 0 { + if !isWindowsTerminalOnWindows { s.erase() } @@ -328,13 +331,13 @@ func (s *Spinner) Stop() { defer s.mu.Unlock() if s.active { s.active = false - if s.HideCursor && len(os.Getenv("WT_SESSION")) == 0 { + if s.HideCursor && !isWindowsTerminalOnWindows { // makes the cursor visible fmt.Fprint(s.Writer, "\033[?25h") } s.erase() if s.FinalMSG != "" { - if runtime.GOOS == "windows" && len(os.Getenv("WT_SESSION")) > 0{ + if isWindowsTerminalOnWindows { fmt.Fprint(s.Writer, "\r", s.FinalMSG) }else{ fmt.Fprint(s.Writer, s.FinalMSG) @@ -396,7 +399,7 @@ 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" && len(os.Getenv("WT_SESSION")) == 0 { + if runtime.GOOS == "windows" && !isWindowsTerminalOnWindows { clearString := "\r" + strings.Repeat(" ", n) + "\r" fmt.Fprint(s.Writer, clearString) s.lastOutput = "" From a975c3cb7b747cb51eac2c47e3baf29e122eb551 Mon Sep 17 00:00:00 2001 From: AskAlice Date: Thu, 3 Jun 2021 09:29:08 -0600 Subject: [PATCH 3/3] fix WSL carriage return --- spinner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spinner.go b/spinner.go index 7ed4187..9e8e697 100644 --- a/spinner.go +++ b/spinner.go @@ -405,7 +405,7 @@ func (s *Spinner) erase() { 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 = "" }