2016-09-08 13:11:39 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-04-25 12:25:05 -04:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/pkg/term"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OutStream is an output stream used by the DockerCli to write normal program
|
|
|
|
// output.
|
|
|
|
type OutStream struct {
|
2017-03-30 20:21:14 -04:00
|
|
|
CommonStream
|
|
|
|
out io.Writer
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OutStream) Write(p []byte) (int, error) {
|
|
|
|
return o.out.Write(p)
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:25:05 -04:00
|
|
|
// SetRawTerminal sets raw mode on the input terminal
|
|
|
|
func (o *OutStream) SetRawTerminal() (err error) {
|
|
|
|
if os.Getenv("NORAW") != "" || !o.CommonStream.isTerminal {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
o.CommonStream.state, err = term.SetRawTerminalOutput(o.CommonStream.fd)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
// GetTtySize returns the height and width in characters of the tty
|
2016-09-14 14:55:07 -04:00
|
|
|
func (o *OutStream) GetTtySize() (uint, uint) {
|
2016-09-08 13:11:39 -04:00
|
|
|
if !o.isTerminal {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
ws, err := term.GetWinsize(o.fd)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("Error getting size: %s", err)
|
|
|
|
if ws == nil {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
}
|
2016-09-14 14:55:07 -04:00
|
|
|
return uint(ws.Height), uint(ws.Width)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOutStream returns a new OutStream object from a Writer
|
|
|
|
func NewOutStream(out io.Writer) *OutStream {
|
|
|
|
fd, isTerminal := term.GetFdInfo(out)
|
2017-03-30 20:21:14 -04:00
|
|
|
return &OutStream{CommonStream: CommonStream{fd: fd, isTerminal: isTerminal}, out: out}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|