Skip to content
Snippets Groups Projects
Commit ab182e27 authored by Tw's avatar Tw Committed by Junegunn Choi
Browse files

Use read syscall directly to get character (#931)

Due to go std lib uses poller for os.File introducing in this commit:
https://github.com/golang/go/commit/c05b06a12d005f50e4776095a60d6bd9c2c91fac


There are two changes to watch out:
1. os.File.Fd will always return a blocking fd except on bsd.
2. os.File.Read won't return EAGAIN error for nonblocking fd.

So
For 1, we just get tty's fd in advance and then set its block mode.
For 2, we use read syscall directly to get what we wanted error(EAGAIN).

Fix issue #910.

Signed-off-by: default avatarTw <tw19881113@gmail.com>
parent 96a32501
No related branches found
No related tags found
No related merge requests found
......@@ -251,8 +251,9 @@ func (r *LightRenderer) updateTerminalSize() {
func (r *LightRenderer) getch(nonblock bool) (int, bool) {
b := make([]byte, 1)
fd := r.fd()
util.SetNonblock(r.ttyin, nonblock)
_, err := r.ttyin.Read(b)
_, err := util.Read(fd, b)
if err != nil {
return 0, false
}
......
......@@ -26,3 +26,8 @@ func IsWindows() bool {
func SetNonblock(file *os.File, nonblock bool) {
syscall.SetNonblock(int(file.Fd()), nonblock)
}
// Read executes syscall.Read on file descriptor
func Read(fd int, b []byte) (int, error) {
return syscall.Read(int(fd), b)
}
......@@ -32,3 +32,8 @@ func IsWindows() bool {
func SetNonblock(file *os.File, nonblock bool) {
syscall.SetNonblock(syscall.Handle(file.Fd()), nonblock)
}
// Read executes syscall.Read on file descriptor
func Read(fd int, b []byte) (int, error) {
return syscall.Read(syscall.Handle(fd), b)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment