2014-10-23 19:44:57 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Package term provides provides structures and helper functions to work with
|
|
|
|
// terminal (state, sizes).
|
2013-03-13 03:29:40 -04:00
|
|
|
package term
|
2013-02-13 20:10:00 -05:00
|
|
|
|
|
|
|
import (
|
2013-11-29 12:52:44 -05:00
|
|
|
"errors"
|
2015-01-23 20:33:49 -05:00
|
|
|
"io"
|
2013-05-14 18:37:35 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2013-02-13 20:10:00 -05:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2013-11-29 12:52:44 -05:00
|
|
|
var (
|
2015-07-25 04:35:07 -04:00
|
|
|
// ErrInvalidState is returned if the state of the terminal is invalid.
|
2013-11-29 14:08:37 -05:00
|
|
|
ErrInvalidState = errors.New("Invalid terminal state")
|
2013-11-29 12:52:44 -05:00
|
|
|
)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// State represents the state of the terminal.
|
2013-02-13 20:10:00 -05:00
|
|
|
type State struct {
|
2013-02-26 20:26:46 -05:00
|
|
|
termios Termios
|
2013-02-13 20:10:00 -05:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// Winsize represents the size of the terminal window.
|
2013-05-24 17:44:16 -04:00
|
|
|
type Winsize struct {
|
|
|
|
Height uint16
|
2013-07-08 13:20:13 -04:00
|
|
|
Width uint16
|
2013-05-24 17:44:16 -04:00
|
|
|
x uint16
|
|
|
|
y uint16
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// StdStreams returns the standard streams (stdin, stdout, stedrr).
|
2015-03-22 12:55:21 -04:00
|
|
|
func StdStreams() (stdIn io.ReadCloser, stdOut, stdErr io.Writer) {
|
|
|
|
return os.Stdin, os.Stdout, os.Stderr
|
2015-01-23 20:33:49 -05: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-03-06 20:04:35 -05:00
|
|
|
func GetFdInfo(in interface{}) (uintptr, bool) {
|
2015-01-23 20:33:49 -05:00
|
|
|
var inFd uintptr
|
|
|
|
var isTerminalIn bool
|
|
|
|
if file, ok := in.(*os.File); ok {
|
|
|
|
inFd = file.Fd()
|
|
|
|
isTerminalIn = IsTerminal(inFd)
|
|
|
|
}
|
|
|
|
return inFd, isTerminalIn
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// GetWinsize returns the window size based on the specified file descriptor.
|
2013-05-24 17:44:16 -04:00
|
|
|
func GetWinsize(fd uintptr) (*Winsize, error) {
|
|
|
|
ws := &Winsize{}
|
|
|
|
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(ws)))
|
2015-12-13 11:00:39 -05:00
|
|
|
// Skip errno = 0
|
2013-10-16 19:05:50 -04:00
|
|
|
if err == 0 {
|
|
|
|
return ws, nil
|
|
|
|
}
|
2013-05-24 17:44:16 -04:00
|
|
|
return ws, err
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// SetWinsize tries to set the specified window size for the specified file descriptor.
|
2013-05-24 17:44:16 -04:00
|
|
|
func SetWinsize(fd uintptr, ws *Winsize) error {
|
|
|
|
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TIOCSWINSZ), uintptr(unsafe.Pointer(ws)))
|
2015-12-13 11:00:39 -05:00
|
|
|
// Skip errno = 0
|
2013-10-16 19:05:50 -04:00
|
|
|
if err == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2013-05-24 17:44:16 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-02-13 20:10:00 -05:00
|
|
|
// IsTerminal returns true if the given file descriptor is a terminal.
|
2013-06-01 18:55:05 -04:00
|
|
|
func IsTerminal(fd uintptr) bool {
|
2013-02-26 20:26:46 -05:00
|
|
|
var termios Termios
|
2014-11-21 08:12:03 -05:00
|
|
|
return tcget(fd, &termios) == 0
|
2013-02-13 20:10:00 -05:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// RestoreTerminal restores the terminal connected to the given file descriptor
|
|
|
|
// to a previous state.
|
2013-06-24 17:52:02 -04:00
|
|
|
func RestoreTerminal(fd uintptr, state *State) error {
|
2013-11-29 12:52:44 -05:00
|
|
|
if state == nil {
|
|
|
|
return ErrInvalidState
|
|
|
|
}
|
2014-11-21 08:12:03 -05:00
|
|
|
if err := tcset(fd, &state.termios); err != 0 {
|
2013-11-29 12:52:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2013-02-13 20:10:00 -05:00
|
|
|
}
|
2013-05-14 18:37:35 -04:00
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// SaveState saves the state of the terminal connected to the given file descriptor.
|
2013-08-03 19:43:20 -04:00
|
|
|
func SaveState(fd uintptr) (*State, error) {
|
|
|
|
var oldState State
|
2014-11-21 08:12:03 -05:00
|
|
|
if err := tcget(fd, &oldState.termios); err != 0 {
|
2013-05-14 18:37:35 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-08-03 19:43:20 -04:00
|
|
|
|
|
|
|
return &oldState, nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// DisableEcho applies the specified state to the terminal connected to the file
|
|
|
|
// descriptor, with echo disabled.
|
2013-08-18 00:29:37 -04:00
|
|
|
func DisableEcho(fd uintptr, state *State) error {
|
2013-08-03 19:43:20 -04:00
|
|
|
newState := state.termios
|
|
|
|
newState.Lflag &^= syscall.ECHO
|
|
|
|
|
2014-11-21 08:12:03 -05:00
|
|
|
if err := tcset(fd, &newState); err != 0 {
|
2013-08-03 19:43:20 -04:00
|
|
|
return err
|
|
|
|
}
|
2013-08-18 00:29:37 -04:00
|
|
|
handleInterrupt(fd, state)
|
2013-08-03 19:43:20 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// SetRawTerminal puts the terminal connected to the given file descriptor into
|
|
|
|
// raw mode and returns the previous state.
|
2013-08-18 00:29:37 -04:00
|
|
|
func SetRawTerminal(fd uintptr) (*State, error) {
|
|
|
|
oldState, err := MakeRaw(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
handleInterrupt(fd, oldState)
|
|
|
|
return oldState, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleInterrupt(fd uintptr, state *State) {
|
2013-08-03 19:43:20 -04:00
|
|
|
sigchan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigchan, os.Interrupt)
|
|
|
|
|
2013-05-14 18:37:35 -04:00
|
|
|
go func() {
|
2013-08-03 19:43:20 -04:00
|
|
|
_ = <-sigchan
|
|
|
|
RestoreTerminal(fd, state)
|
2013-05-14 18:37:35 -04:00
|
|
|
os.Exit(0)
|
|
|
|
}()
|
2013-08-03 19:43:20 -04:00
|
|
|
}
|