pulumi/pkg/backend/local/stream.go

83 lines
1.9 KiB
Go

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
package local
import (
"io"
"os"
"github.com/docker/docker/pkg/term"
"github.com/pulumi/pulumi/pkg/util/contract"
)
// copied from: https://github.com/docker/cli/blob/master/cli/command/out.go
// replace with usage of that library when we can figure out the right version story
type commonStream struct {
fd uintptr
isTerminal bool
state *term.State
}
// FD returns the file descriptor number for this stream
func (s *commonStream) FD() uintptr {
return s.fd
}
// IsTerminal returns true if this stream is connected to a terminal
func (s *commonStream) IsTerminal() bool {
return s.isTerminal
}
// RestoreTerminal restores normal mode to the terminal
func (s *commonStream) RestoreTerminal() {
if s.state != nil {
err := term.RestoreTerminal(s.fd, s.state)
contract.IgnoreError(err)
}
}
// SetIsTerminal sets the boolean used for isTerminal
func (s *commonStream) SetIsTerminal(isTerminal bool) {
s.isTerminal = isTerminal
}
type outStream struct {
commonStream
out io.Writer
}
func (o *outStream) Write(p []byte) (int, error) {
return o.out.Write(p)
}
// 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
}
// GetTtySize returns the height and width in characters of the tty
func (o *outStream) GetTtySize() (uint, uint) {
if !o.isTerminal {
return 0, 0
}
ws, err := term.GetWinsize(o.fd)
if err != nil {
if ws == nil {
return 0, 0
}
}
return uint(ws.Height), uint(ws.Width)
}
// NewOutStream returns a new OutStream object from a Writer
func newOutStream(out io.Writer) *outStream {
fd, isTerminal := term.GetFdInfo(out)
return &outStream{commonStream: commonStream{fd: fd, isTerminal: isTerminal}, out: out}
}