@@ -2,6 +2,7 @@ package iostreams
22
33import (
44 "bytes"
5+ "errors"
56 "fmt"
67 "io"
78 "io/ioutil"
@@ -41,6 +42,7 @@ type IOStreams struct {
4142 stderrTTYOverride bool
4243 stderrIsTTY bool
4344 termWidthOverride int
45+ ttySize func () (int , int , error )
4446
4547 pagerCommand string
4648 pagerProcess * os.Process
@@ -273,7 +275,7 @@ func (s *IOStreams) ForceTerminal(spec string) {
273275 return
274276 }
275277
276- ttyWidth , _ , err := ttySize ()
278+ ttyWidth , _ , err := s . ttySize ()
277279 if err != nil {
278280 return
279281 }
@@ -284,7 +286,6 @@ func (s *IOStreams) ForceTerminal(spec string) {
284286 s .termWidthOverride = int (float64 (s .termWidthOverride ) * (float64 (p ) / 100 ))
285287 }
286288 }
287-
288289}
289290
290291func (s * IOStreams ) ColorScheme () * ColorScheme {
@@ -325,6 +326,7 @@ func System() *IOStreams {
325326 colorEnabled : EnvColorForced () || (! EnvColorDisabled () && stdoutIsTTY ),
326327 is256enabled : Is256ColorSupported (),
327328 pagerCommand : os .Getenv ("PAGER" ),
329+ ttySize : ttySize ,
328330 }
329331
330332 if stdoutIsTTY && stderrIsTTY {
@@ -345,6 +347,9 @@ func Test() (*IOStreams, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer) {
345347 In : ioutil .NopCloser (in ),
346348 Out : out ,
347349 ErrOut : errOut ,
350+ ttySize : func () (int , int , error ) {
351+ return - 1 , - 1 , errors .New ("ttySize not implemented in tests" )
352+ },
348353 }, in , out , errOut
349354}
350355
@@ -359,6 +364,7 @@ func isCygwinTerminal(w io.Writer) bool {
359364 return false
360365}
361366
367+ // terminalSize measures the viewport of the terminal that the output stream is connected to
362368func terminalSize (w io.Writer ) (int , int , error ) {
363369 if f , isFile := w .(* os.File ); isFile {
364370 return term .GetSize (int (f .Fd ()))
0 commit comments