2020-09-17 19:53:01 -04:00
|
|
|
// +build !windows
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
// Package term provides structures and helper functions to work with
|
|
|
|
// terminal (state, sizes).
|
2020-04-16 05:23:37 -04:00
|
|
|
package term
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2017-05-10 18:41:46 -04:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidState is returned if the state of the terminal is invalid.
|
|
|
|
ErrInvalidState = errors.New("Invalid terminal state")
|
|
|
|
)
|
|
|
|
|
|
|
|
// State represents the state of the terminal.
|
|
|
|
type State struct {
|
|
|
|
termios Termios
|
|
|
|
}
|
|
|
|
|
|
|
|
// Winsize represents the size of the terminal window.
|
|
|
|
type Winsize struct {
|
|
|
|
Height uint16
|
|
|
|
Width uint16
|
|
|
|
x uint16
|
|
|
|
y uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
// StdStreams returns the standard streams (stdin, stdout, stderr).
|
|
|
|
func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
|
|
|
|
return os.Stdin, os.Stdout, os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFdInfo returns the file descriptor for an os.File and indicates whether the file represents a terminal.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTerminal returns true if the given file descriptor is a terminal.
|
|
|
|
func IsTerminal(fd uintptr) bool {
|
2020-09-17 19:53:01 -04:00
|
|
|
_, err := tcget(fd)
|
|
|
|
return err == nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreTerminal restores the terminal connected to the given file descriptor
|
|
|
|
// to a previous state.
|
|
|
|
func RestoreTerminal(fd uintptr, state *State) error {
|
|
|
|
if state == nil {
|
|
|
|
return ErrInvalidState
|
|
|
|
}
|
2020-09-17 19:53:01 -04:00
|
|
|
return tcset(fd, &state.termios)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SaveState saves the state of the terminal connected to the given file descriptor.
|
|
|
|
func SaveState(fd uintptr) (*State, error) {
|
2020-09-17 19:53:01 -04:00
|
|
|
termios, err := tcget(fd)
|
|
|
|
if err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-17 19:53:01 -04:00
|
|
|
return &State{termios: *termios}, nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DisableEcho applies the specified state to the terminal connected to the file
|
|
|
|
// descriptor, with echo disabled.
|
|
|
|
func DisableEcho(fd uintptr, state *State) error {
|
|
|
|
newState := state.termios
|
2017-05-10 18:41:46 -04:00
|
|
|
newState.Lflag &^= unix.ECHO
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2020-09-17 19:53:01 -04:00
|
|
|
if err := tcset(fd, &newState); err != nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
handleInterrupt(fd, state)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func SetRawTerminal(fd uintptr) (*State, error) {
|
|
|
|
oldState, err := MakeRaw(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
handleInterrupt(fd, oldState)
|
|
|
|
return oldState, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleInterrupt(fd uintptr, state *State) {
|
|
|
|
sigchan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigchan, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
for range sigchan {
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|