Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to avoid panicking in PosixParser if there's no /dev/tty #185

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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