github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/cli/command/out.go (about)

     1  package command
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  
     7  	"github.com/docker/docker/pkg/term"
     8  	"github.com/sirupsen/logrus"
     9  )
    10  
    11  // OutStream is an output stream used by the DockerCli to write normal program
    12  // output.
    13  type OutStream struct {
    14  	CommonStream
    15  	out io.Writer
    16  }
    17  
    18  func (o *OutStream) Write(p []byte) (int, error) {
    19  	return o.out.Write(p)
    20  }
    21  
    22  // SetRawTerminal sets raw mode on the input terminal
    23  func (o *OutStream) SetRawTerminal() (err error) {
    24  	if os.Getenv("NORAW") != "" || !o.CommonStream.isTerminal {
    25  		return nil
    26  	}
    27  	o.CommonStream.state, err = term.SetRawTerminalOutput(o.CommonStream.fd)
    28  	return err
    29  }
    30  
    31  // GetTtySize returns the height and width in characters of the tty
    32  func (o *OutStream) GetTtySize() (uint, uint) {
    33  	if !o.isTerminal {
    34  		return 0, 0
    35  	}
    36  	ws, err := term.GetWinsize(o.fd)
    37  	if err != nil {
    38  		logrus.Debugf("Error getting size: %s", err)
    39  		if ws == nil {
    40  			return 0, 0
    41  		}
    42  	}
    43  	return uint(ws.Height), uint(ws.Width)
    44  }
    45  
    46  // NewOutStream returns a new OutStream object from a Writer
    47  func NewOutStream(out io.Writer) *OutStream {
    48  	fd, isTerminal := term.GetFdInfo(out)
    49  	return &OutStream{CommonStream: CommonStream{fd: fd, isTerminal: isTerminal}, out: out}
    50  }