2016-09-08 17:11:39 +00:00
|
|
|
package command
|
2016-08-29 15:11:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"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 {
|
|
|
|
out io.Writer
|
|
|
|
fd uintptr
|
|
|
|
isTerminal bool
|
|
|
|
state *term.State
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *OutStream) Write(p []byte) (int, error) {
|
|
|
|
return o.out.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FD returns the file descriptor number for this stream
|
|
|
|
func (o *OutStream) FD() uintptr {
|
|
|
|
return o.fd
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTerminal returns true if this stream is connected to a terminal
|
|
|
|
func (o *OutStream) IsTerminal() bool {
|
|
|
|
return o.isTerminal
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:36:29 +00:00
|
|
|
// SetRawTerminal sets raw mode on the output terminal
|
|
|
|
func (o *OutStream) SetRawTerminal() (err error) {
|
2016-08-29 15:11:29 +00:00
|
|
|
if os.Getenv("NORAW") != "" || !o.isTerminal {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
o.state, err = term.SetRawTerminalOutput(o.fd)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:36:29 +00:00
|
|
|
// RestoreTerminal restores normal mode to the terminal
|
|
|
|
func (o *OutStream) RestoreTerminal() {
|
2016-08-29 15:11:29 +00:00
|
|
|
if o.state != nil {
|
|
|
|
term.RestoreTerminal(o.fd, o.state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTtySize returns the height and width in characters of the tty
|
2016-09-14 18:55:07 +00:00
|
|
|
func (o *OutStream) GetTtySize() (uint, uint) {
|
2016-08-29 15:11:29 +00: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 18:55:07 +00:00
|
|
|
return uint(ws.Height), uint(ws.Width)
|
2016-08-29 15:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOutStream returns a new OutStream object from a Writer
|
|
|
|
func NewOutStream(out io.Writer) *OutStream {
|
|
|
|
fd, isTerminal := term.GetFdInfo(out)
|
|
|
|
return &OutStream{out: out, fd: fd, isTerminal: isTerminal}
|
|
|
|
}
|