Skip to content

Commit

Permalink
start spinner only when fd is terminal (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwertent committed Feb 7, 2022
1 parent 55bc95e commit 16b2c08
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
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
)
8 changes: 7 additions & 1 deletion spinner.go
Expand Up @@ -28,6 +28,7 @@ import (
"unicode/utf8"

"github.com/fatih/color"
"github.com/mattn/go-isatty"
)

// errInvalidColor is returned when attempting to set an invalid color
Expand Down Expand Up @@ -271,7 +272,7 @@ func (s *Spinner) Active() bool {
// Start will start the indicator.
func (s *Spinner) Start() {
s.mu.Lock()
if s.active {
if s.active || !isRunningInTerminal() {
s.mu.Unlock()
return
}
Expand Down Expand Up @@ -439,3 +440,8 @@ func GenerateNumberSequence(length int) []string {
}
return numSeq
}

// isRunningInTerminal check if stdout file descriptor is terminal
func isRunningInTerminal() bool {
return isatty.IsTerminal(os.Stdout.Fd())
}
1 change: 1 addition & 0 deletions vendor/modules.txt
Expand Up @@ -5,6 +5,7 @@ github.com/fatih/color
## explicit
github.com/mattn/go-colorable
# github.com/mattn/go-isatty v0.0.8
## explicit
github.com/mattn/go-isatty
# golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223
golang.org/x/sys/unix

0 comments on commit 16b2c08

Please sign in to comment.