Skip to content

Commit

Permalink
Try to avoid panicking in PosixParser if there's no /dev/tty.
Browse files Browse the repository at this point in the history
When using go-prompt before /dev/ is populated, it will panic() from
NewStandardInputParser() because /dev/ isn't populated yet.

This will use stdin (fd 0) as a fallback if /dev/tty doesn't exist.

Furthermore GetWinSize() will return the default console size if the
IOCTL call fails. This can happen if the default stdin is not a
terminal, but a pipe as fixed in commit 846777c and described in
issue c-bata#88.
  • Loading branch information
abrander committed Aug 14, 2020
1 parent 8717360 commit aba8e36
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions input_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package prompt

import (
"os"
"syscall"

"github.com/c-bata/go-prompt/internal/term"
Expand Down Expand Up @@ -54,7 +55,12 @@ func (t *PosixParser) Read() ([]byte, error) {
func (t *PosixParser) GetWinSize() *WinSize {
ws, err := unix.IoctlGetWinsize(t.fd, unix.TIOCGWINSZ)
if err != nil {
panic(err)
// If this errors, we simply return the default window size as
// it's our best guess.
return &WinSize{
Row: 25,
Col: 80,
}
}
return &WinSize{
Row: ws.Row,
Expand All @@ -67,7 +73,9 @@ var _ ConsoleParser = &PosixParser{}
// NewStandardInputParser returns ConsoleParser object to read from stdin.
func NewStandardInputParser() *PosixParser {
in, err := syscall.Open("/dev/tty", syscall.O_RDONLY, 0)
if err != nil {
if os.IsNotExist(err) {
in = syscall.Stdin
} else if err != nil {
panic(err)
}

Expand Down

0 comments on commit aba8e36

Please sign in to comment.