vendor: github.com/moby/term v0.5.0
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>pull/1806/head
parent
4429ccbcc2
commit
75b5c6560f
@ -0,0 +1,3 @@
|
|||||||
|
// Package term provides structures and helper functions to work with
|
||||||
|
// terminal (state, sizes).
|
||||||
|
package term
|
@ -1,20 +0,0 @@
|
|||||||
//go:build !windows
|
|
||||||
// +build !windows
|
|
||||||
|
|
||||||
package term
|
|
||||||
|
|
||||||
import (
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
func tcget(fd uintptr) (*Termios, error) {
|
|
||||||
p, err := unix.IoctlGetTermios(int(fd), getTermios)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return p, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func tcset(fd uintptr, p *Termios) error {
|
|
||||||
return unix.IoctlSetTermios(int(fd), setTermios, p)
|
|
||||||
}
|
|
@ -1,119 +1,85 @@
|
|||||||
//go:build !windows
|
|
||||||
// +build !windows
|
|
||||||
|
|
||||||
// Package term provides structures and helper functions to work with
|
|
||||||
// terminal (state, sizes).
|
|
||||||
package term
|
package term
|
||||||
|
|
||||||
import (
|
import "io"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ErrInvalidState is returned if the state of the terminal is invalid.
|
// State holds the platform-specific state / console mode for the terminal.
|
||||||
var ErrInvalidState = errors.New("Invalid terminal state")
|
type State terminalState
|
||||||
|
|
||||||
// State represents the state of the terminal.
|
|
||||||
type State struct {
|
|
||||||
termios Termios
|
|
||||||
}
|
|
||||||
|
|
||||||
// Winsize represents the size of the terminal window.
|
// Winsize represents the size of the terminal window.
|
||||||
type Winsize struct {
|
type Winsize struct {
|
||||||
Height uint16
|
Height uint16
|
||||||
Width uint16
|
Width uint16
|
||||||
|
|
||||||
|
// Only used on Unix
|
||||||
x uint16
|
x uint16
|
||||||
y uint16
|
y uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// StdStreams returns the standard streams (stdin, stdout, stderr).
|
// StdStreams returns the standard streams (stdin, stdout, stderr).
|
||||||
|
//
|
||||||
|
// On Windows, it attempts to turn on VT handling on all std handles if
|
||||||
|
// supported, or falls back to terminal emulation. On Unix, this returns
|
||||||
|
// the standard [os.Stdin], [os.Stdout] and [os.Stderr].
|
||||||
func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
|
func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
|
||||||
return os.Stdin, os.Stdout, os.Stderr
|
return stdStreams()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFdInfo returns the file descriptor for an os.File and indicates whether the file represents a terminal.
|
// GetFdInfo returns the file descriptor for an os.File and indicates whether the file represents a terminal.
|
||||||
func GetFdInfo(in interface{}) (uintptr, bool) {
|
func GetFdInfo(in interface{}) (fd uintptr, isTerminal bool) {
|
||||||
var inFd uintptr
|
return getFdInfo(in)
|
||||||
var isTerminalIn bool
|
}
|
||||||
if file, ok := in.(*os.File); ok {
|
|
||||||
inFd = file.Fd()
|
// GetWinsize returns the window size based on the specified file descriptor.
|
||||||
isTerminalIn = IsTerminal(inFd)
|
func GetWinsize(fd uintptr) (*Winsize, error) {
|
||||||
|
return getWinsize(fd)
|
||||||
}
|
}
|
||||||
return inFd, isTerminalIn
|
|
||||||
|
// SetWinsize tries to set the specified window size for the specified file
|
||||||
|
// descriptor. It is only implemented on Unix, and returns an error on Windows.
|
||||||
|
func SetWinsize(fd uintptr, ws *Winsize) error {
|
||||||
|
return setWinsize(fd, ws)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsTerminal returns true if the given file descriptor is a terminal.
|
// IsTerminal returns true if the given file descriptor is a terminal.
|
||||||
func IsTerminal(fd uintptr) bool {
|
func IsTerminal(fd uintptr) bool {
|
||||||
_, err := tcget(fd)
|
return isTerminal(fd)
|
||||||
return err == nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RestoreTerminal restores the terminal connected to the given file descriptor
|
// RestoreTerminal restores the terminal connected to the given file descriptor
|
||||||
// to a previous state.
|
// to a previous state.
|
||||||
func RestoreTerminal(fd uintptr, state *State) error {
|
func RestoreTerminal(fd uintptr, state *State) error {
|
||||||
if state == nil {
|
return restoreTerminal(fd, state)
|
||||||
return ErrInvalidState
|
|
||||||
}
|
|
||||||
return tcset(fd, &state.termios)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveState saves the state of the terminal connected to the given file descriptor.
|
// SaveState saves the state of the terminal connected to the given file descriptor.
|
||||||
func SaveState(fd uintptr) (*State, error) {
|
func SaveState(fd uintptr) (*State, error) {
|
||||||
termios, err := tcget(fd)
|
return saveState(fd)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &State{termios: *termios}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableEcho applies the specified state to the terminal connected to the file
|
// DisableEcho applies the specified state to the terminal connected to the file
|
||||||
// descriptor, with echo disabled.
|
// descriptor, with echo disabled.
|
||||||
func DisableEcho(fd uintptr, state *State) error {
|
func DisableEcho(fd uintptr, state *State) error {
|
||||||
newState := state.termios
|
return disableEcho(fd, state)
|
||||||
newState.Lflag &^= unix.ECHO
|
|
||||||
|
|
||||||
if err := tcset(fd, &newState); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
handleInterrupt(fd, state)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetRawTerminal puts the terminal connected to the given file descriptor into
|
// 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
|
// raw mode and returns the previous state. On UNIX, this is the equivalent of
|
||||||
// and output into raw mode. On Windows, it only puts the input into raw mode.
|
// [MakeRaw], and puts both the input and output into raw mode. On Windows, it
|
||||||
func SetRawTerminal(fd uintptr) (*State, error) {
|
// only puts the input into raw mode.
|
||||||
oldState, err := MakeRaw(fd)
|
func SetRawTerminal(fd uintptr) (previousState *State, err error) {
|
||||||
if err != nil {
|
return setRawTerminal(fd)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
handleInterrupt(fd, oldState)
|
|
||||||
return oldState, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetRawTerminalOutput puts the output of terminal connected to the given file
|
// 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
|
// descriptor into raw mode. On UNIX, this does nothing and returns nil for the
|
||||||
// state. On Windows, it disables LF -> CRLF translation.
|
// state. On Windows, it disables LF -> CRLF translation.
|
||||||
func SetRawTerminalOutput(fd uintptr) (*State, error) {
|
func SetRawTerminalOutput(fd uintptr) (previousState *State, err error) {
|
||||||
return nil, nil
|
return setRawTerminalOutput(fd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleInterrupt(fd uintptr, state *State) {
|
// MakeRaw puts the terminal (Windows Console) connected to the
|
||||||
sigchan := make(chan os.Signal, 1)
|
// given file descriptor into raw mode and returns the previous state of
|
||||||
signal.Notify(sigchan, os.Interrupt)
|
// the terminal so that it can be restored.
|
||||||
go func() {
|
func MakeRaw(fd uintptr) (previousState *State, err error) {
|
||||||
for range sigchan {
|
return makeRaw(fd)
|
||||||
// quit cleanly and the new terminal item is on a new line
|
|
||||||
fmt.Println()
|
|
||||||
signal.Stop(sigchan)
|
|
||||||
close(sigchan)
|
|
||||||
RestoreTerminal(fd, state)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,98 @@
|
|||||||
|
//go:build !windows
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package term
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrInvalidState is returned if the state of the terminal is invalid.
|
||||||
|
//
|
||||||
|
// Deprecated: ErrInvalidState is no longer used.
|
||||||
|
var ErrInvalidState = errors.New("Invalid terminal state")
|
||||||
|
|
||||||
|
// terminalState holds the platform-specific state / console mode for the terminal.
|
||||||
|
type terminalState struct {
|
||||||
|
termios unix.Termios
|
||||||
|
}
|
||||||
|
|
||||||
|
func stdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
|
||||||
|
return os.Stdin, os.Stdout, os.Stderr
|
||||||
|
}
|
||||||
|
|
||||||
|
func getFdInfo(in interface{}) (uintptr, bool) {
|
||||||
|
var inFd uintptr
|
||||||
|
var isTerminalIn bool
|
||||||
|
if file, ok := in.(*os.File); ok {
|
||||||
|
inFd = file.Fd()
|
||||||
|
isTerminalIn = isTerminal(inFd)
|
||||||
|
}
|
||||||
|
return inFd, isTerminalIn
|
||||||
|
}
|
||||||
|
|
||||||
|
func getWinsize(fd uintptr) (*Winsize, error) {
|
||||||
|
uws, err := unix.IoctlGetWinsize(int(fd), unix.TIOCGWINSZ)
|
||||||
|
ws := &Winsize{Height: uws.Row, Width: uws.Col, x: uws.Xpixel, y: uws.Ypixel}
|
||||||
|
return ws, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func setWinsize(fd uintptr, ws *Winsize) error {
|
||||||
|
return unix.IoctlSetWinsize(int(fd), unix.TIOCSWINSZ, &unix.Winsize{
|
||||||
|
Row: ws.Height,
|
||||||
|
Col: ws.Width,
|
||||||
|
Xpixel: ws.x,
|
||||||
|
Ypixel: ws.y,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func isTerminal(fd uintptr) bool {
|
||||||
|
_, err := tcget(fd)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func restoreTerminal(fd uintptr, state *State) error {
|
||||||
|
if state == nil {
|
||||||
|
return errors.New("invalid terminal state")
|
||||||
|
}
|
||||||
|
return tcset(fd, &state.termios)
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveState(fd uintptr) (*State, error) {
|
||||||
|
termios, err := tcget(fd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &State{termios: *termios}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func disableEcho(fd uintptr, state *State) error {
|
||||||
|
newState := state.termios
|
||||||
|
newState.Lflag &^= unix.ECHO
|
||||||
|
|
||||||
|
return tcset(fd, &newState)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setRawTerminal(fd uintptr) (*State, error) {
|
||||||
|
return makeRaw(fd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setRawTerminalOutput(fd uintptr) (*State, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func tcget(fd uintptr) (*unix.Termios, error) {
|
||||||
|
p, err := unix.IoctlGetTermios(int(fd), getTermios)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func tcset(fd uintptr, p *unix.Termios) error {
|
||||||
|
return unix.IoctlSetTermios(int(fd), setTermios, p)
|
||||||
|
}
|
13
vendor/github.com/moby/term/termios.go → vendor/github.com/moby/term/termios_unix.go
generated
vendored
13
vendor/github.com/moby/term/termios.go → vendor/github.com/moby/term/termios_unix.go
generated
vendored
@ -0,0 +1,37 @@
|
|||||||
|
package term
|
||||||
|
|
||||||
|
import "golang.org/x/sys/windows"
|
||||||
|
|
||||||
|
func makeRaw(fd uintptr) (*State, error) {
|
||||||
|
state, err := SaveState(fd)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mode := state.mode
|
||||||
|
|
||||||
|
// 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
|
||||||
|
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
|
||||||
|
|
||||||
|
// Enable these modes
|
||||||
|
mode |= windows.ENABLE_EXTENDED_FLAGS
|
||||||
|
mode |= windows.ENABLE_INSERT_MODE
|
||||||
|
mode |= windows.ENABLE_QUICK_EDIT_MODE
|
||||||
|
if vtInputSupported {
|
||||||
|
mode |= windows.ENABLE_VIRTUAL_TERMINAL_INPUT
|
||||||
|
}
|
||||||
|
|
||||||
|
err = windows.SetConsoleMode(windows.Handle(fd), mode)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return state, nil
|
||||||
|
}
|
@ -1,21 +0,0 @@
|
|||||||
//go:build !windows
|
|
||||||
// +build !windows
|
|
||||||
|
|
||||||
package term
|
|
||||||
|
|
||||||
import (
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetWinsize returns the window size based on the specified file descriptor.
|
|
||||||
func GetWinsize(fd uintptr) (*Winsize, error) {
|
|
||||||
uws, err := unix.IoctlGetWinsize(int(fd), unix.TIOCGWINSZ)
|
|
||||||
ws := &Winsize{Height: uws.Row, Width: uws.Col, x: uws.Xpixel, y: uws.Ypixel}
|
|
||||||
return ws, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetWinsize tries to set the specified window size for the specified file descriptor.
|
|
||||||
func SetWinsize(fd uintptr, ws *Winsize) error {
|
|
||||||
uws := &unix.Winsize{Row: ws.Height, Col: ws.Width, Xpixel: ws.x, Ypixel: ws.y}
|
|
||||||
return unix.IoctlSetWinsize(int(fd), unix.TIOCSWINSZ, uws)
|
|
||||||
}
|
|
Loading…
Reference in New Issue