github.1git.de/docker/cli@v26.1.3+incompatible/cli/streams/out.go (about) 1 package streams 2 3 import ( 4 "io" 5 "os" 6 7 "github.com/moby/term" 8 "github.com/sirupsen/logrus" 9 ) 10 11 // Out is an output stream to write normal program output. It implements 12 // an [io.Writer], with additional utilities for detecting whether a terminal 13 // is connected, getting the TTY size, and putting the terminal in raw mode. 14 type Out struct { 15 commonStream 16 out io.Writer 17 } 18 19 func (o *Out) Write(p []byte) (int, error) { 20 return o.out.Write(p) 21 } 22 23 // SetRawTerminal puts the output of the terminal connected to the stream 24 // into raw mode. 25 // 26 // On UNIX, this does nothing. On Windows, it disables LF -> CRLF/ translation. 27 // It is a no-op if Out is not a TTY, or if the "NORAW" environment variable is 28 // set to a non-empty value. 29 func (o *Out) SetRawTerminal() (err error) { 30 if !o.isTerminal || os.Getenv("NORAW") != "" { 31 return nil 32 } 33 o.state, err = term.SetRawTerminalOutput(o.fd) 34 return err 35 } 36 37 // GetTtySize returns the height and width in characters of the TTY, or 38 // zero for both if no TTY is connected. 39 func (o *Out) GetTtySize() (height uint, width uint) { 40 if !o.isTerminal { 41 return 0, 0 42 } 43 ws, err := term.GetWinsize(o.fd) 44 if err != nil { 45 logrus.WithError(err).Debug("Error getting TTY size") 46 if ws == nil { 47 return 0, 0 48 } 49 } 50 return uint(ws.Height), uint(ws.Width) 51 } 52 53 // NewOut returns a new [Out] from an [io.Writer]. 54 func NewOut(out io.Writer) *Out { 55 o := &Out{out: out} 56 o.fd, o.isTerminal = term.GetFdInfo(out) 57 return o 58 }