github.com/kunnos/engine@v1.13.1/cli/command/out.go (about) 1 package command 2 3 import ( 4 "io" 5 "os" 6 7 "github.com/sirupsen/logrus" 8 "github.com/docker/docker/pkg/term" 9 ) 10 11 // OutStream is an output stream used by the DockerCli to write normal program 12 // output. 13 type OutStream struct { 14 out io.Writer 15 fd uintptr 16 isTerminal bool 17 state *term.State 18 } 19 20 func (o *OutStream) Write(p []byte) (int, error) { 21 return o.out.Write(p) 22 } 23 24 // FD returns the file descriptor number for this stream 25 func (o *OutStream) FD() uintptr { 26 return o.fd 27 } 28 29 // IsTerminal returns true if this stream is connected to a terminal 30 func (o *OutStream) IsTerminal() bool { 31 return o.isTerminal 32 } 33 34 // SetRawTerminal sets raw mode on the output terminal 35 func (o *OutStream) SetRawTerminal() (err error) { 36 if os.Getenv("NORAW") != "" || !o.isTerminal { 37 return nil 38 } 39 o.state, err = term.SetRawTerminalOutput(o.fd) 40 return err 41 } 42 43 // RestoreTerminal restores normal mode to the terminal 44 func (o *OutStream) RestoreTerminal() { 45 if o.state != nil { 46 term.RestoreTerminal(o.fd, o.state) 47 } 48 } 49 50 // GetTtySize returns the height and width in characters of the tty 51 func (o *OutStream) GetTtySize() (uint, uint) { 52 if !o.isTerminal { 53 return 0, 0 54 } 55 ws, err := term.GetWinsize(o.fd) 56 if err != nil { 57 logrus.Debugf("Error getting size: %s", err) 58 if ws == nil { 59 return 0, 0 60 } 61 } 62 return uint(ws.Height), uint(ws.Width) 63 } 64 65 // NewOutStream returns a new OutStream object from a Writer 66 func NewOutStream(out io.Writer) *OutStream { 67 fd, isTerminal := term.GetFdInfo(out) 68 return &OutStream{out: out, fd: fd, isTerminal: isTerminal} 69 }