diff --git a/go.mod b/go.mod index e779741..771d5b2 100644 --- a/go.mod +++ b/go.mod @@ -6,5 +6,5 @@ require ( github.com/briandowns/spinner v0.0.0-20190319032542-ac46072a5a91 github.com/fatih/color v1.7.0 // indirect github.com/mattn/go-colorable v0.1.1 // indirect - github.com/mattn/go-isatty v0.0.8 // indirect + github.com/mattn/go-isatty v0.0.12 ) diff --git a/go.sum b/go.sum index 6f3b553..afce676 100644 --- a/go.sum +++ b/go.sum @@ -7,4 +7,8 @@ github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcncea github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/streams.go b/streams.go index 70a6f1c..faef312 100644 --- a/streams.go +++ b/streams.go @@ -10,6 +10,7 @@ import ( "time" "github.com/briandowns/spinner" + "github.com/mattn/go-isatty" ) // IOStreams provides standard names for iostreams and common methods for managing @@ -28,6 +29,26 @@ type IOStreams struct { sp *spinner.Spinner } +// IsTerminal returns true when IOStreams Out is a terminal file descriptor +func (s IOStreams) IsTerminal() bool { + if osOutFile, ok := s.Out.(*os.File); ok { + if osOutFile == os.Stdout { + return isatty.IsTerminal(osOutFile.Fd()) + } + } + return false +} + +// IsCygwinTerminal returns true when IOStreams Out is a Cygwin file descriptor +func (s IOStreams) IsCygwinTerminal() bool { + if osOutFile, ok := s.Out.(*os.File); ok { + if osOutFile == os.Stdout { + return isatty.IsCygwinTerminal(osOutFile.Fd()) + } + } + return false +} + // StartSpinner begins the progress spinner func (s IOStreams) StartSpinner() { s.sp.Start() diff --git a/streams_test.go b/streams_test.go index 61c7dd5..a7486df 100644 --- a/streams_test.go +++ b/streams_test.go @@ -28,3 +28,12 @@ func TestSpinnerPrint(t *testing.T) { s.Print("hallo!") s.StopSpinner() } + +func TestIsNotATerminal(t *testing.T) { + str, _, _, _ := NewTestIOStreams() + if str.IsTerminal() { + t.Fatal("expected buffer streams to not be a terminal") + } else if str.IsCygwinTerminal() { + t.Fatal("expected buffer streams to not be a cygwin terminal") + } +}