go-prompt/input_windows.go

84 lines
1.6 KiB
Go
Raw Normal View History

2017-08-16 03:22:38 +00:00
// +build windows
package prompt
import (
"errors"
"syscall"
2018-02-12 10:12:31 +00:00
"unicode/utf8"
"unsafe"
2017-08-16 03:22:38 +00:00
tty "github.com/mattn/go-tty"
2017-08-16 03:22:38 +00:00
)
2018-02-12 10:12:31 +00:00
const maxReadBytes = 1024
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
var procGetNumberOfConsoleInputEvents = kernel32.NewProc("GetNumberOfConsoleInputEvents")
// WindowsParser is a ConsoleParser implementation for Win32 console.
2018-02-12 10:12:31 +00:00
type WindowsParser struct {
2017-08-16 03:22:38 +00:00
tty *tty.TTY
}
2018-02-13 16:14:22 +00:00
// Setup should be called before starting input
2018-02-12 10:12:31 +00:00
func (p *WindowsParser) Setup() error {
t, err := tty.Open()
2017-08-16 03:22:38 +00:00
if err != nil {
return err
}
2018-02-12 10:12:31 +00:00
p.tty = t
2017-08-16 03:22:38 +00:00
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 (p *WindowsParser) TearDown() error {
return p.tty.Close()
2017-08-16 03:22:38 +00:00
}
2018-02-13 16:14:22 +00:00
// Read returns byte array.
2018-02-12 10:12:31 +00:00
func (p *WindowsParser) Read() ([]byte, error) {
var ev uint32
r0, _, err := procGetNumberOfConsoleInputEvents.Call(p.tty.Input().Fd(), uintptr(unsafe.Pointer(&ev)))
if r0 == 0 {
return nil, err
}
if ev == 0 {
return nil, errors.New("EAGAIN")
}
2018-02-12 10:12:31 +00:00
r, err := p.tty.ReadRune()
if err != nil {
return nil, err
2018-02-12 10:12:31 +00:00
}
buf := make([]byte, maxReadBytes)
2018-02-12 10:12:31 +00:00
n := utf8.EncodeRune(buf[:], r)
2018-02-14 05:55:48 +00:00
for p.tty.Buffered() && n < maxReadBytes {
2018-02-14 05:17:13 +00:00
r, err := p.tty.ReadRune()
if err != nil {
break
}
n += utf8.EncodeRune(buf[n:], r)
}
2018-02-12 10:12:31 +00:00
return buf[:n], nil
}
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 (p *WindowsParser) GetWinSize() *WinSize {
w, h, err := p.tty.Size()
2017-08-16 03:22:38 +00:00
if err != nil {
panic(err)
}
return &WinSize{
Row: uint16(h),
Col: uint16(w),
}
}
// NewStandardInputParser returns ConsoleParser object to read from stdin.
2018-02-12 10:12:31 +00:00
func NewStandardInputParser() *WindowsParser {
return &WindowsParser{}
2017-08-16 03:22:38 +00:00
}