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

Remove line cleaning with \r #108

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.14
require (
github.com/fatih/color v1.7.0
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.8
)
35 changes: 21 additions & 14 deletions spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package spinner
import (
"errors"
"fmt"
"github.com/mattn/go-isatty"
"io"
"os"
"runtime"
Expand Down Expand Up @@ -264,20 +265,32 @@ func (s *Spinner) Active() bool {
return s.active
}

func isTerminal() bool {
if isatty.IsTerminal(os.Stdout.Fd()){
return true
} else if isatty.IsCygwinTerminal(os.Stdout.Fd()){
return true
}
return false
}

// Start will start the indicator.
func (s *Spinner) Start() {
if !isTerminal() {
fmt.Printf("\n")
return
}
s.mu.Lock()
if s.active {
s.mu.Unlock()
return
}
if s.HideCursor && runtime.GOOS != "windows" {
// hides the cursor
fmt.Print("\033[?25l")
fmt.Fprint(s.Writer, "\033[?25l")
}
s.active = true
s.mu.Unlock()

go func() {
for {
for i := 0; i < len(s.chars); i++ {
Expand All @@ -299,14 +312,14 @@ func (s *Spinner) Start() {
var outColor string
if runtime.GOOS == "windows" {
if s.Writer == os.Stderr {
outColor = fmt.Sprintf("\r%s%s%s ", s.Prefix, s.chars[i], s.Suffix)
outColor = fmt.Sprintf("%s%s ", s.chars[i], s.Suffix)
} else {
outColor = fmt.Sprintf("\r%s%s%s ", s.Prefix, s.color(s.chars[i]), s.Suffix)
outColor = fmt.Sprintf("%s%s ", s.chars[i], s.Suffix)
}
} else {
outColor = fmt.Sprintf("\r%s%s%s ", s.Prefix, s.color(s.chars[i]), s.Suffix)
outColor = fmt.Sprintf("%s%s ", s.chars[i], s.Suffix)
}
outPlain := fmt.Sprintf("\r%s%s%s ", s.Prefix, s.chars[i], s.Suffix)
outPlain := fmt.Sprintf("%s%s ", s.chars[i], s.Suffix)
fmt.Fprint(s.Writer, outColor)
s.lastOutput = outPlain
delay := s.Delay
Expand All @@ -326,12 +339,13 @@ func (s *Spinner) Start() {
// Stop stops the indicator.
func (s *Spinner) Stop() {
s.mu.Lock()
defer fmt.Print("\n")
defer s.mu.Unlock()
if s.active {
s.active = false
if s.HideCursor && runtime.GOOS != "windows" {
// makes the cursor visible
fmt.Print("\033[?25h")
fmt.Fprint(s.Writer, "\033[?25h")
}
s.erase()
if s.FinalMSG != "" {
Expand Down Expand Up @@ -393,16 +407,9 @@ 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" {
clearString := "\r" + strings.Repeat(" ", n) + "\r"
fmt.Fprint(s.Writer, clearString)
s.lastOutput = ""
return
}
for _, c := range []string{"\b", "\127", "\b", "\033[K"} { // "\033[K" for macOS Terminal
fmt.Fprint(s.Writer, strings.Repeat(c, n))
}
fmt.Fprintf(s.Writer, "\r\033[K") // erases to end of line
s.lastOutput = ""
}

Expand Down