2020-04-16 05:02:09 -04:00
|
|
|
package term
|
2014-10-23 19:44:57 -04:00
|
|
|
|
2015-03-05 19:41:48 -05:00
|
|
|
import (
|
|
|
|
"io"
|
2015-03-23 14:38:43 -04:00
|
|
|
"os"
|
2015-04-16 17:01:37 -04:00
|
|
|
"os/signal"
|
2015-03-22 12:55:21 -04:00
|
|
|
|
2020-04-16 05:02:09 -04:00
|
|
|
windowsconsole "github.com/moby/term/windows"
|
2020-05-07 16:21:35 -04:00
|
|
|
"golang.org/x/sys/windows"
|
2015-03-05 19:41:48 -05:00
|
|
|
)
|
|
|
|
|
2015-01-23 20:33:49 -05:00
|
|
|
// State holds the console mode for the terminal.
|
2014-10-23 19:44:57 -04:00
|
|
|
type State struct {
|
2016-06-22 19:34:01 -04:00
|
|
|
mode uint32
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2015-01-23 20:33:49 -05:00
|
|
|
// Winsize is used for window size.
|
2014-10-23 19:44:57 -04:00
|
|
|
type Winsize struct {
|
|
|
|
Height uint16
|
|
|
|
Width uint16
|
|
|
|
}
|
|
|
|
|
2017-10-02 03:49:20 -04:00
|
|
|
// vtInputSupported is true if winterm.ENABLE_VIRTUAL_TERMINAL_INPUT is supported by the console
|
2016-06-22 19:34:01 -04:00
|
|
|
var vtInputSupported bool
|
2015-11-11 18:04:40 -05:00
|
|
|
|
2017-01-16 23:45:27 -05:00
|
|
|
// StdStreams returns the standard streams (stdin, stdout, stderr).
|
2015-04-02 12:35:13 -04:00
|
|
|
func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
|
2016-06-22 19:34:01 -04:00
|
|
|
// Turn on VT handling on all std handles, if possible. This might
|
|
|
|
// fail, in which case we will fall back to terminal emulation.
|
2020-05-07 16:21:35 -04:00
|
|
|
var (
|
|
|
|
emulateStdin, emulateStdout, emulateStderr bool
|
|
|
|
|
|
|
|
mode uint32
|
|
|
|
)
|
|
|
|
|
|
|
|
fd := windows.Handle(os.Stdin.Fd())
|
|
|
|
if err := windows.GetConsoleMode(fd, &mode); err == nil {
|
2017-10-02 03:49:20 -04:00
|
|
|
// Validate that winterm.ENABLE_VIRTUAL_TERMINAL_INPUT is supported, but do not set it.
|
2020-05-07 16:21:35 -04:00
|
|
|
if err = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err != nil {
|
2016-06-22 19:34:01 -04:00
|
|
|
emulateStdin = true
|
|
|
|
} else {
|
|
|
|
vtInputSupported = true
|
2015-10-15 14:40:14 -04:00
|
|
|
}
|
2016-07-22 21:34:14 -04:00
|
|
|
// Unconditionally set the console mode back even on failure because SetConsoleMode
|
|
|
|
// remembers invalid bits on input handles.
|
2020-05-07 16:21:35 -04:00
|
|
|
_ = windows.SetConsoleMode(fd, mode)
|
2015-11-11 18:04:40 -05:00
|
|
|
}
|
|
|
|
|
2020-05-07 16:21:35 -04:00
|
|
|
fd = windows.Handle(os.Stdout.Fd())
|
|
|
|
if err := windows.GetConsoleMode(fd, &mode); err == nil {
|
2017-10-02 03:49:20 -04:00
|
|
|
// Validate winterm.DISABLE_NEWLINE_AUTO_RETURN is supported, but do not set it.
|
2020-05-07 16:21:35 -04:00
|
|
|
if err = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING|windows.DISABLE_NEWLINE_AUTO_RETURN); err != nil {
|
2016-06-22 19:34:01 -04:00
|
|
|
emulateStdout = true
|
|
|
|
} else {
|
2020-05-07 16:21:35 -04:00
|
|
|
_ = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
2015-10-15 14:40:14 -04:00
|
|
|
}
|
2016-05-20 22:19:26 -04:00
|
|
|
}
|
|
|
|
|
2020-05-07 16:21:35 -04:00
|
|
|
fd = windows.Handle(os.Stderr.Fd())
|
|
|
|
if err := windows.GetConsoleMode(fd, &mode); err == nil {
|
2017-10-02 03:49:20 -04:00
|
|
|
// Validate winterm.DISABLE_NEWLINE_AUTO_RETURN is supported, but do not set it.
|
2020-05-07 16:21:35 -04:00
|
|
|
if err = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING|windows.DISABLE_NEWLINE_AUTO_RETURN); err != nil {
|
2016-06-22 19:34:01 -04:00
|
|
|
emulateStderr = true
|
|
|
|
} else {
|
2020-05-07 16:21:35 -04:00
|
|
|
_ = windows.SetConsoleMode(fd, mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
|
2016-06-22 19:34:01 -04:00
|
|
|
}
|
2015-10-15 14:40:14 -04:00
|
|
|
}
|
|
|
|
|
2017-06-08 02:52:21 -04:00
|
|
|
// Temporarily use STD_INPUT_HANDLE, STD_OUTPUT_HANDLE and
|
|
|
|
// STD_ERROR_HANDLE from syscall rather than x/sys/windows as long as
|
|
|
|
// go-ansiterm hasn't switch to x/sys/windows.
|
|
|
|
// TODO: switch back to x/sys/windows once go-ansiterm has switched
|
2016-06-22 19:34:01 -04:00
|
|
|
if emulateStdin {
|
2020-11-18 18:29:13 -05:00
|
|
|
h := uint32(windows.STD_INPUT_HANDLE)
|
|
|
|
stdIn = windowsconsole.NewAnsiReader(int(h))
|
2016-06-22 19:34:01 -04:00
|
|
|
} else {
|
|
|
|
stdIn = os.Stdin
|
2015-11-11 18:04:40 -05:00
|
|
|
}
|
|
|
|
|
2016-06-22 19:34:01 -04:00
|
|
|
if emulateStdout {
|
2020-11-18 18:29:13 -05:00
|
|
|
h := uint32(windows.STD_OUTPUT_HANDLE)
|
|
|
|
stdOut = windowsconsole.NewAnsiWriter(int(h))
|
2016-06-22 19:34:01 -04:00
|
|
|
} else {
|
|
|
|
stdOut = os.Stdout
|
2015-11-11 18:04:40 -05:00
|
|
|
}
|
|
|
|
|
2016-06-22 19:34:01 -04:00
|
|
|
if emulateStderr {
|
2020-11-18 18:29:13 -05:00
|
|
|
h := uint32(windows.STD_ERROR_HANDLE)
|
|
|
|
stdErr = windowsconsole.NewAnsiWriter(int(h))
|
2016-06-22 19:34:01 -04:00
|
|
|
} else {
|
|
|
|
stdErr = os.Stderr
|
2015-11-11 18:04:40 -05:00
|
|
|
}
|
|
|
|
|
2016-06-22 19:34:01 -04:00
|
|
|
return
|
2015-10-15 14:40:14 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// GetFdInfo returns the file descriptor for an os.File and indicates whether the file represents a terminal.
|
2015-04-02 12:35:13 -04:00
|
|
|
func GetFdInfo(in interface{}) (uintptr, bool) {
|
2017-05-04 20:52:19 -04:00
|
|
|
return windowsconsole.GetHandleInfo(in)
|
2015-04-02 12:35:13 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// GetWinsize returns the window size based on the specified file descriptor.
|
2014-10-23 19:44:57 -04:00
|
|
|
func GetWinsize(fd uintptr) (*Winsize, error) {
|
2020-05-07 16:21:35 -04:00
|
|
|
var info windows.ConsoleScreenBufferInfo
|
|
|
|
if err := windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info); err != nil {
|
2014-10-23 19:44:57 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-23 20:33:49 -05:00
|
|
|
|
2015-04-16 17:01:37 -04:00
|
|
|
winsize := &Winsize{
|
2015-04-06 17:31:42 -04:00
|
|
|
Width: uint16(info.Window.Right - info.Window.Left + 1),
|
|
|
|
Height: uint16(info.Window.Bottom - info.Window.Top + 1),
|
2016-03-31 13:53:21 -04:00
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
return winsize, nil
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsTerminal returns true if the given file descriptor is a terminal.
|
|
|
|
func IsTerminal(fd uintptr) bool {
|
2020-05-07 16:21:35 -04:00
|
|
|
var mode uint32
|
|
|
|
err := windows.GetConsoleMode(windows.Handle(fd), &mode)
|
|
|
|
return err == nil
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// RestoreTerminal restores the terminal connected to the given file descriptor
|
|
|
|
// to a previous state.
|
2014-10-23 19:44:57 -04:00
|
|
|
func RestoreTerminal(fd uintptr, state *State) error {
|
2020-05-07 16:21:35 -04:00
|
|
|
return windows.SetConsoleMode(windows.Handle(fd), state.mode)
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2015-04-02 12:35:13 -04:00
|
|
|
// SaveState saves the state of the terminal connected to the given file descriptor.
|
2014-10-23 19:44:57 -04:00
|
|
|
func SaveState(fd uintptr) (*State, error) {
|
2020-05-07 16:21:35 -04:00
|
|
|
var mode uint32
|
|
|
|
|
|
|
|
if err := windows.GetConsoleMode(windows.Handle(fd), &mode); err != nil {
|
|
|
|
return nil, err
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
2015-11-11 18:04:40 -05:00
|
|
|
|
2016-06-22 19:34:01 -04:00
|
|
|
return &State{mode: mode}, nil
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2015-04-02 12:35:13 -04:00
|
|
|
// DisableEcho disables echo for the terminal connected to the given file descriptor.
|
2015-04-16 17:01:37 -04:00
|
|
|
// -- See https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
|
2014-10-23 19:44:57 -04:00
|
|
|
func DisableEcho(fd uintptr, state *State) error {
|
2016-06-22 19:34:01 -04:00
|
|
|
mode := state.mode
|
2020-05-07 16:21:35 -04:00
|
|
|
mode &^= windows.ENABLE_ECHO_INPUT
|
|
|
|
mode |= windows.ENABLE_PROCESSED_INPUT | windows.ENABLE_LINE_INPUT
|
|
|
|
err := windows.SetConsoleMode(windows.Handle(fd), mode)
|
2015-04-16 17:01:37 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register an interrupt handler to catch and restore prior state
|
|
|
|
restoreAtInterrupt(fd, state)
|
|
|
|
return nil
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2016-06-22 19:34:01 -04:00
|
|
|
// SetRawTerminal puts the terminal connected to the given file descriptor into
|
|
|
|
// raw mode and returns the previous state. On UNIX, this puts both the input
|
|
|
|
// and output into raw mode. On Windows, it only puts the input into raw mode.
|
2014-10-23 19:44:57 -04:00
|
|
|
func SetRawTerminal(fd uintptr) (*State, error) {
|
2015-04-02 12:35:13 -04:00
|
|
|
state, err := MakeRaw(fd)
|
2014-10-23 19:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
// Register an interrupt handler to catch and restore prior state
|
|
|
|
restoreAtInterrupt(fd, state)
|
2015-04-02 12:35:13 -04:00
|
|
|
return state, err
|
2014-10-23 19:44:57 -04:00
|
|
|
}
|
|
|
|
|
2016-06-22 19:34:01 -04:00
|
|
|
// SetRawTerminalOutput puts the output of terminal connected to the given file
|
|
|
|
// descriptor into raw mode. On UNIX, this does nothing and returns nil for the
|
|
|
|
// state. On Windows, it disables LF -> CRLF translation.
|
|
|
|
func SetRawTerminalOutput(fd uintptr) (*State, error) {
|
|
|
|
state, err := SaveState(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-02 03:49:20 -04:00
|
|
|
// Ignore failures, since winterm.DISABLE_NEWLINE_AUTO_RETURN might not be supported on this
|
2016-06-22 19:34:01 -04:00
|
|
|
// version of Windows.
|
2020-05-07 16:21:35 -04:00
|
|
|
_ = windows.SetConsoleMode(windows.Handle(fd), state.mode|windows.DISABLE_NEWLINE_AUTO_RETURN)
|
2016-06-22 19:34:01 -04:00
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
|
2015-04-16 17:01:37 -04:00
|
|
|
// MakeRaw puts the terminal (Windows Console) connected to the given file descriptor into raw
|
|
|
|
// mode and returns the previous state of the terminal so that it can be restored.
|
2014-10-23 19:44:57 -04:00
|
|
|
func MakeRaw(fd uintptr) (*State, error) {
|
|
|
|
state, err := SaveState(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-22 19:34:01 -04:00
|
|
|
mode := state.mode
|
2015-11-11 18:04:40 -05:00
|
|
|
|
2015-04-02 12:35:13 -04:00
|
|
|
// See
|
|
|
|
// -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
|
|
|
|
// -- https://msdn.microsoft.com/en-us/library/windows/desktop/ms683462(v=vs.85).aspx
|
|
|
|
|
|
|
|
// Disable these modes
|
2020-05-07 16:21:35 -04:00
|
|
|
mode &^= windows.ENABLE_ECHO_INPUT
|
|
|
|
mode &^= windows.ENABLE_LINE_INPUT
|
|
|
|
mode &^= windows.ENABLE_MOUSE_INPUT
|
|
|
|
mode &^= windows.ENABLE_WINDOW_INPUT
|
|
|
|
mode &^= windows.ENABLE_PROCESSED_INPUT
|
2015-04-02 12:35:13 -04:00
|
|
|
|
|
|
|
// Enable these modes
|
2020-05-07 16:21:35 -04:00
|
|
|
mode |= windows.ENABLE_EXTENDED_FLAGS
|
|
|
|
mode |= windows.ENABLE_INSERT_MODE
|
|
|
|
mode |= windows.ENABLE_QUICK_EDIT_MODE
|
2016-06-22 19:34:01 -04:00
|
|
|
if vtInputSupported {
|
2020-05-07 16:21:35 -04:00
|
|
|
mode |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
|
2016-06-22 19:34:01 -04:00
|
|
|
}
|
2015-04-02 12:35:13 -04:00
|
|
|
|
2020-05-07 16:21:35 -04:00
|
|
|
err = windows.SetConsoleMode(windows.Handle(fd), mode)
|
2014-10-23 19:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
func restoreAtInterrupt(fd uintptr, state *State) {
|
|
|
|
sigchan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigchan, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_ = <-sigchan
|
2020-05-07 16:21:35 -04:00
|
|
|
_ = RestoreTerminal(fd, state)
|
2015-04-16 17:01:37 -04:00
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
}
|