2019-01-28 08:30:31 -05:00
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2020-04-16 05:23:37 -04:00
|
|
|
"github.com/moby/term"
|
2019-01-28 08:30:31 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-04-01 14:11:42 -04:00
|
|
|
// Out is an output stream to write normal program output. It implements
|
|
|
|
// an [io.Writer], with additional utilities for detecting whether a terminal
|
|
|
|
// is connected, getting the TTY size, and putting the terminal in raw mode.
|
2019-01-28 08:30:31 -05:00
|
|
|
type Out struct {
|
|
|
|
commonStream
|
|
|
|
out io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Out) Write(p []byte) (int, error) {
|
|
|
|
return o.out.Write(p)
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:42 -04:00
|
|
|
// SetRawTerminal puts the output of the terminal connected to the stream
|
|
|
|
// into raw mode.
|
|
|
|
//
|
|
|
|
// On UNIX, this does nothing. On Windows, it disables LF -> CRLF/ translation.
|
|
|
|
// It is a no-op if Out is not a TTY, or if the "NORAW" environment variable is
|
|
|
|
// set to a non-empty value.
|
2019-01-28 08:30:31 -05:00
|
|
|
func (o *Out) SetRawTerminal() (err error) {
|
2023-04-01 14:11:42 -04:00
|
|
|
if !o.isTerminal || os.Getenv("NORAW") != "" {
|
2019-01-28 08:30:31 -05:00
|
|
|
return nil
|
|
|
|
}
|
2023-04-01 14:11:42 -04:00
|
|
|
o.state, err = term.SetRawTerminalOutput(o.fd)
|
2019-01-28 08:30:31 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:42 -04:00
|
|
|
// GetTtySize returns the height and width in characters of the TTY, or
|
|
|
|
// zero for both if no TTY is connected.
|
|
|
|
func (o *Out) GetTtySize() (height uint, width uint) {
|
2019-01-28 08:30:31 -05:00
|
|
|
if !o.isTerminal {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
ws, err := term.GetWinsize(o.fd)
|
|
|
|
if err != nil {
|
2023-04-01 14:11:42 -04:00
|
|
|
logrus.WithError(err).Debug("Error getting TTY size")
|
2019-01-28 08:30:31 -05:00
|
|
|
if ws == nil {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uint(ws.Height), uint(ws.Width)
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:42 -04:00
|
|
|
// NewOut returns a new [Out] from an [io.Writer].
|
2019-01-28 08:30:31 -05:00
|
|
|
func NewOut(out io.Writer) *Out {
|
2023-04-01 14:11:42 -04:00
|
|
|
o := &Out{out: out}
|
|
|
|
o.fd, o.isTerminal = term.GetFdInfo(out)
|
|
|
|
return o
|
2019-01-28 08:30:31 -05:00
|
|
|
}
|