// IsTerminal returns true if w writes to a terminal.
979314
func IsTerminal(w io.Writer) bool {
	fw, ok := w.(interface {
		Fd() uintptr
	})
	if !ok {
		return false
	}
	var st uint32
	r, _, e := syscall.Syscall(procGetConsoleMode.Addr(), 2, fw.Fd(), uintptr(unsafe.Pointer(&st)), 0)
	return r != 0 && e == 0
}
1250824
func RestoreTerminal(fd uintptr, state *State) error {
	return winterm.SetConsoleMode(fd, state.mode)
}
1100526
func IsTemporary(name string) bool {
	name = filepath.Base(name)
	if strings.HasPrefix(name, WindowsTempPrefix) ||
		strings.HasPrefix(name, UnixTempPrefix) {
		return true
	}
	return false
}
688141
func WithTerminal(ctx context.Context, isTerm bool) context.Context {
	return context.WithValue(ctx, ctxKeyTerminal, isTerm)
}
213171
func IsMSYSTerminal(w io.Writer) bool {
	var handle syscall.Handle

	if fw, ok := w.(fder); ok {
		handle = syscall.Handle(fw.Fd())
	} else {
		// The writer has no file-descriptor and so can't be a terminal.
		return false
	}

	// MSYS(2) terminal reports as a pipe for STDIN/STDOUT/STDERR. If it isn't
	// a pipe, it can't be a MSYS(2) terminal.
	filetype, err := syscall.GetFileType(handle)

	if filetype != syscall.FILE_TYPE_PIPE || err != nil {
		return false
	}

	// MSYS2/Cygwin terminal's name looks like: \msys-dd50a72ab4668b33-pty2-to-master
	data := make([]byte, 256, 256)

	r, _, e := syscall.Syscall6(
		procGetFileInformationByHandleEx.Addr(),
		4,
		uintptr(handle),
		uintptr(fileNameInfo),
		uintptr(unsafe.Pointer(&data[0])),
		uintptr(len(data)),
		0,
		0,
	)

	if r != 0 && e == 0 {
		// The first 4 bytes of the buffer are the size of the UTF16 name, in bytes.
		unameLen := binary.LittleEndian.Uint32(data[:4]) / 2
		uname := make([]uint16, unameLen, unameLen)

		for i := uint32(0); i < unameLen; i++ {
			uname[i] = binary.LittleEndian.Uint16(data[i*2+4 : i*2+2+4])
		}

		name := syscall.UTF16ToString(uname)

		return msysPipeNameRegex.MatchString(name)
	}

	return false
}
356183
func IsWorker() bool {
	masterPid := os.Getenv("EINHORN_MASTER_PID")
	if masterPid == "" {
		return false
	}

	pid, err := strconv.Atoi(masterPid)
	if err != nil {
		return false
	}

	return pid == os.Getppid()
}
1320242
func Stderr() io.Writer {
	stderr := colorable.NewColorableStderr()
	if stderr == os.Stderr {
		return os.Stderr
	}

	return &WinColorTerminal{output: stderr}
}
484462
func isTerminalFunc(fd int) bool {
	var st uint32
	r, _, errno := syscall.Syscall(procMode.Addr(), 2, uintptr(fd), uintptr(unsafe.Pointer(&st)), 0)
	if errno != 0 {
		return false
	}

	return r != 0
}
1161032
func (s *commonStream) RestoreTerminal() {
	if s.state != nil {
		term.RestoreTerminal(s.fd, s.state)
	}
}
1327688
func (c *client) ResizeTerminal(_ context.Context, containerID, processID string, width, height int) error {
	_, p, err := c.getProcess(containerID, processID)
	if err != nil {
		return err
	}

	c.logger.WithFields(logrus.Fields{
		"container": containerID,
		"process":   processID,
		"height":    height,
		"width":     width,
		"pid":       p.pid,
	}).Debug("resizing")
	return p.hcsProcess.ResizeConsole(uint16(width), uint16(height))
}
852533
func (pbp *ProgressBarPrinter) isTerminal(w io.Writer) bool {
	if pbp.printToTTYAlways {
		return true
	}
	if f, ok := w.(*os.File); ok {
		return terminal.IsTerminal(int(f.Fd()))
	}
	return false
}
1067953
func (s Status) Terminal() bool {
	switch s {
	case Success, Failure, InfraFailure, Warning, Expired, Exception, Cancelled:
		return true
	default:
		return false
	}
}
974423
func NewTerminalParamsFromUint32(w uint32, h uint32) (*TerminalParams, error) {
	if w > maxSize || w < minSize {
		return nil, trace.BadParameter("bad width")
	}
	if h > maxSize || h < minSize {
		return nil, trace.BadParameter("bad height")
	}
	return &TerminalParams{W: int(w), H: int(h)}, nil
}
1076469
func IsTerminal() bool {
	ws := &winsize{}
	isTerm := true
	defer func() {
		if r := recover(); r != nil {
			isTerm = false
		}
	}()
	// This blows up on windows
	retCode, _, _ := syscall.Syscall(syscall.SYS_IOCTL,
		uintptr(syscall.Stdin),
		uintptr(_TIOCGWINSZ),
		uintptr(unsafe.Pointer(ws)))

	if int(retCode) == -1 {
		return false
	}
	return isTerm
}
879637
func IsTerminal(fd uintptr) bool {
	_, err := unix.IoctlGetTermios(int(fd), unix.TCGETS)
	return err == nil
}
708144
func IsStdin(ctx context.Context) bool {
	bv, ok := ctx.Value(ctxKeyStdin).(bool)
	if !ok {
		return false
	}
	return bv
}
590191
func (e *Evaluation) TerminalStatus() bool {
	switch e.Status {
	case EvalStatusComplete, EvalStatusFailed, EvalStatusCancelled:
		return true
	default:
		return false
	}
}