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

fix flashing during erase() on windows terminal #116

Merged
merged 3 commits into from Jun 3, 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
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