From 21acaf90fd2b3ab3655bc2072a4e3042181b83d7 Mon Sep 17 00:00:00 2001 From: Cheney Date: Wed, 17 Feb 2016 14:08:55 +0800 Subject: [PATCH] fixed crash if stdin is not a tty (pipe) --- operation.go | 7 +++++++ terminal.go | 8 +++++++- utils.go | 1 + utils_unix.go | 2 +- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/operation.go b/operation.go index 503c624..83d48a1 100644 --- a/operation.go +++ b/operation.go @@ -84,6 +84,13 @@ func (o *Operation) ioloop() { keepInSearchMode := false keepInCompleteMode := false r := o.t.ReadRune() + if r == 0 { // io.EOF + o.buf.Clean() + select { + case o.errchan <- io.EOF: + } + break + } isUpdateHistory := true if o.IsInCompleteSelectMode() { diff --git a/terminal.go b/terminal.go index 4e17227..71a72f6 100644 --- a/terminal.go +++ b/terminal.go @@ -67,8 +67,13 @@ func (t *Terminal) Readline() *Operation { return NewOperation(t, t.cfg) } +// return rune(0) if meet EOF func (t *Terminal) ReadRune() rune { - return <-t.outchan + ch, ok := <-t.outchan + if !ok { + return rune(0) + } + return ch } func (t *Terminal) IsReading() bool { @@ -136,6 +141,7 @@ func (t *Terminal) ioloop() { t.outchan <- r } } + close(t.outchan) } func (t *Terminal) Bell() { diff --git a/utils.go b/utils.go index 18e659a..386bcba 100644 --- a/utils.go +++ b/utils.go @@ -10,6 +10,7 @@ import ( var ( StdinFd = int(uintptr(syscall.Stdin)) + StdoutFd = int(uintptr(syscall.Stdout)) isWindows = false ) diff --git a/utils_unix.go b/utils_unix.go index 0937b28..7a40cff 100644 --- a/utils_unix.go +++ b/utils_unix.go @@ -18,7 +18,7 @@ type winsize struct { func getWidth() int { ws := &winsize{} retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL, - uintptr(StdinFd), + uintptr(StdoutFd), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(ws)))