out.go 1.1 KB

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