From 16b2c08520470d8126885d3833d66bcf76ac5cea Mon Sep 17 00:00:00 2001 From: David Wertenteil Date: Mon, 7 Feb 2022 19:03:21 +0200 Subject: [PATCH] start spinner only when fd is terminal (#131) --- go.mod | 1 + spinner.go | 8 +++++++- vendor/modules.txt | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index df62180..2da0084 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/spinner.go b/spinner.go index e9bbfe9..f6bb029 100644 --- a/spinner.go +++ b/spinner.go @@ -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 @@ -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 } @@ -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()) +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 0a845d8..2df272c 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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