Skip to content

Commit

Permalink
feat(terminal): add terminal detection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Apr 7, 2020
1 parent 54cf96b commit 28794e5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
21 changes: 21 additions & 0 deletions streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions streams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 28794e5

Please sign in to comment.