go-prompt/input_posix.go

134 lines
3.0 KiB
Go
Raw Normal View History

2017-08-16 03:22:38 +00:00
// +build !windows
2017-07-05 15:59:22 +00:00
package prompt
import (
"bytes"
2017-08-09 07:20:31 +00:00
"log"
2017-07-05 15:59:22 +00:00
"syscall"
"unsafe"
"github.com/pkg/term/termios"
)
2018-02-12 10:12:31 +00:00
const maxReadBytes = 1024
// PosixParser is a ConsoleParser implementation for POSIX environment.
2018-02-12 10:12:31 +00:00
type PosixParser struct {
2017-07-05 15:59:22 +00:00
fd int
origTermios syscall.Termios
}
2018-02-13 16:14:22 +00:00
// Setup should be called before starting input
2018-02-12 10:12:31 +00:00
func (t *PosixParser) Setup() error {
2017-08-09 07:20:31 +00:00
// Set NonBlocking mode because if syscall.Read block this goroutine, it cannot receive data from stopCh.
if err := syscall.SetNonblock(t.fd, true); err != nil {
log.Println("[ERROR] Cannot set non blocking mode.")
return err
}
if err := t.setRawMode(); err != nil {
log.Println("[ERROR] Cannot set raw mode.")
return err
}
return nil
}
2018-02-13 16:14:22 +00:00
// TearDown should be called after stopping input
2018-02-12 10:12:31 +00:00
func (t *PosixParser) TearDown() error {
2017-08-09 07:20:31 +00:00
if err := syscall.SetNonblock(t.fd, false); err != nil {
log.Println("[ERROR] Cannot set blocking mode.")
return err
}
2017-08-09 12:28:34 +00:00
if err := t.resetRawMode(); err != nil {
2017-08-09 07:20:31 +00:00
log.Println("[ERROR] Cannot reset from raw mode.")
return err
}
return nil
2017-07-05 15:59:22 +00:00
}
2018-02-13 16:14:22 +00:00
// Read returns byte array.
2018-02-12 10:12:31 +00:00
func (t *PosixParser) Read() ([]byte, error) {
buf := make([]byte, maxReadBytes)
n, err := syscall.Read(t.fd, buf)
2018-02-12 10:12:31 +00:00
if err != nil {
return []byte{}, err
}
return buf[:n], nil
}
func (t *PosixParser) setRawMode() error {
2017-08-09 12:28:34 +00:00
x := t.origTermios.Lflag
2017-08-09 12:33:47 +00:00
if x &^= syscall.ICANON; x != 0 && x == t.origTermios.Lflag {
2017-08-09 11:52:37 +00:00
// fd is already raw mode
return nil
}
2017-07-05 15:59:22 +00:00
var n syscall.Termios
if err := termios.Tcgetattr(uintptr(t.fd), &t.origTermios); err != nil {
return err
}
n = t.origTermios
// "&^=" used like: https://play.golang.org/p/8eJw3JxS4O
n.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.IEXTEN | syscall.ISIG
n.Cc[syscall.VMIN] = 1
n.Cc[syscall.VTIME] = 0
2017-08-16 04:39:59 +00:00
termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, &n)
2017-07-05 15:59:22 +00:00
return nil
}
2018-02-12 10:12:31 +00:00
func (t *PosixParser) resetRawMode() error {
2017-08-09 12:28:34 +00:00
if t.origTermios.Lflag == 0 {
return nil
}
return termios.Tcsetattr(uintptr(t.fd), termios.TCSANOW, &t.origTermios)
}
2018-02-13 16:14:22 +00:00
// GetKey returns Key correspond to input byte codes.
2018-02-12 10:12:31 +00:00
func (t *PosixParser) GetKey(b []byte) Key {
2017-07-05 15:59:22 +00:00
for _, k := range asciiSequences {
2017-08-16 04:39:59 +00:00
if bytes.Equal(k.ASCIICode, b) {
2017-08-03 06:53:38 +00:00
return k.Key
2017-07-05 15:59:22 +00:00
}
}
2017-08-03 06:53:38 +00:00
return NotDefined
2017-07-05 15:59:22 +00:00
}
2017-07-16 08:39:26 +00:00
// winsize is winsize struct got from the ioctl(2) system call.
type ioctlWinsize struct {
Row uint16
Col uint16
X uint16 // pixel value
Y uint16 // pixel value
}
2018-02-13 16:14:22 +00:00
// GetWinSize returns WinSize object to represent width and height of terminal.
2018-02-12 10:12:31 +00:00
func (t *PosixParser) GetWinSize() *WinSize {
2017-07-05 15:59:22 +00:00
ws := &ioctlWinsize{}
retCode, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(t.fd),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
panic(errno)
}
2017-07-14 01:51:19 +00:00
return &WinSize{
Row: ws.Row,
Col: ws.Col,
}
2017-07-05 15:59:22 +00:00
}
2018-02-12 10:12:31 +00:00
var _ ConsoleParser = &PosixParser{}
2017-07-16 18:53:23 +00:00
// NewStandardInputParser returns ConsoleParser object to read from stdin.
2018-02-12 10:12:31 +00:00
func NewStandardInputParser() *PosixParser {
in, err := syscall.Open("/dev/tty", syscall.O_RDONLY, 0)
if err != nil {
panic(err)
}
2018-02-12 10:12:31 +00:00
return &PosixParser{
fd: in,
2017-07-16 18:53:23 +00:00
}
}