out.go 895 B

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