diff --git a/_example/diagnostics/main.go b/_example/diagnostics/main.go index 98265638..4c53af0c 100644 --- a/_example/diagnostics/main.go +++ b/_example/diagnostics/main.go @@ -77,7 +77,7 @@ func Lexer(line string) []prompt.LexerElement { } func main() { - p := prompt.New(nil, completer, + p, _ := prompt.New(nil, completer, prompt.OptionTitle("sql-prompt"), prompt.OptionHistory([]string{"SELECT * FROM users;"}), prompt.OptionPrefixTextColor(prompt.Yellow), diff --git a/_example/exec-command/main.go b/_example/exec-command/main.go index 65cfe6ef..11fb6d43 100644 --- a/_example/exec-command/main.go +++ b/_example/exec-command/main.go @@ -25,7 +25,7 @@ func completer(t prompt.Document) []prompt.Suggest { } func main() { - p := prompt.New( + p, _ := prompt.New( executor, completer, ) diff --git a/_example/http-prompt/main.go b/_example/http-prompt/main.go index f576deb0..7dc30f91 100644 --- a/_example/http-prompt/main.go +++ b/_example/http-prompt/main.go @@ -181,7 +181,7 @@ func main() { client: &http.Client{}, } - p := prompt.New( + p, _ := prompt.New( executor, completer, prompt.OptionPrefix(u.String()+"> "), diff --git a/_example/live-prefix/main.go b/_example/live-prefix/main.go index 9288f1b9..0cb9a448 100644 --- a/_example/live-prefix/main.go +++ b/_example/live-prefix/main.go @@ -37,7 +37,7 @@ func changeLivePrefix() (string, bool) { } func main() { - p := prompt.New( + p, _ := prompt.New( executor, completer, prompt.OptionPrefix(">>> "), diff --git a/_example/simple-echo/multiwidth-completions/main.go b/_example/simple-echo/multiwidth-completions/main.go index 739773fc..2ebac1b0 100644 --- a/_example/simple-echo/multiwidth-completions/main.go +++ b/_example/simple-echo/multiwidth-completions/main.go @@ -21,7 +21,7 @@ func completer(in prompt.Document) []prompt.Suggest { } func main() { - p := prompt.New( + p, _ := prompt.New( executor, completer, prompt.OptionPrefix(">>> "), diff --git a/_tools/complete_file/main.go b/_tools/complete_file/main.go index 81b9069b..4c4c6cad 100644 --- a/_tools/complete_file/main.go +++ b/_tools/complete_file/main.go @@ -33,7 +33,7 @@ func completerFunc(d prompt.Document) []prompt.Suggest { } func main() { - p := prompt.New( + p, _ := prompt.New( executor, completerFunc, prompt.OptionPrefix(">>> "), diff --git a/input_posix.go b/input_posix.go index 4c0a5e6d..6b2a2413 100644 --- a/input_posix.go +++ b/input_posix.go @@ -64,13 +64,13 @@ func (t *PosixParser) GetWinSize() *WinSize { var _ ConsoleParser = &PosixParser{} // NewStandardInputParser returns ConsoleParser object to read from stdin. -func NewStandardInputParser() *PosixParser { +func NewStandardInputParser() (*PosixParser, error) { in, err := syscall.Open("/dev/tty", syscall.O_RDONLY, 0) if err != nil { - panic(err) + return nil, err } return &PosixParser{ fd: in, - } + }, nil } diff --git a/input_windows.go b/input_windows.go index 5b805bcc..0bd0652c 100644 --- a/input_windows.go +++ b/input_windows.go @@ -78,6 +78,6 @@ func (p *WindowsParser) GetWinSize() *WinSize { } // NewStandardInputParser returns ConsoleParser object to read from stdin. -func NewStandardInputParser() *WindowsParser { - return &WindowsParser{} +func NewStandardInputParser() (*WindowsParser, error) { + return &WindowsParser{}, nil } diff --git a/option.go b/option.go index 6b92bce3..4ade56d5 100644 --- a/option.go +++ b/option.go @@ -316,12 +316,16 @@ func OptionSetStatementTerminator(fn StatementTerminatorCb) Option { } // New returns a Prompt with powerful auto-completion. -func New(executor Executor, completer Completer, opts ...Option) IPrompt { +func New(executor Executor, completer Completer, opts ...Option) (IPrompt, error) { defaultWriter := NewStdoutWriter() registerConsoleWriter(defaultWriter) + standardInputParser, err := NewStandardInputParser() + if err != nil { + return nil, err + } pt := &Prompt{ - in: NewStandardInputParser(), + in: standardInputParser, renderer: &Render{ prefix: "> ", out: defaultWriter, @@ -363,8 +367,8 @@ func New(executor Executor, completer Completer, opts ...Option) IPrompt { for _, opt := range opts { if err := opt(pt); err != nil { - panic(err) + return nil, err } } - return pt + return pt, nil } diff --git a/shortcut.go b/shortcut.go index 1964a364..a84db259 100644 --- a/shortcut.go +++ b/shortcut.go @@ -4,13 +4,17 @@ func dummyExecutor(in string) {} // Input get the input data from the user and return it. func Input(prefix string, completer Completer, opts ...Option) string { - pt := New(dummyExecutor, completer) + pt, err := New(dummyExecutor, completer) + if err != nil { + return "" + } + pt.Renderer().prefixTextColor = DefaultColor pt.Renderer().prefix = prefix for _, opt := range opts { if err := opt(pt); err != nil { - panic(err) + return "" } } return pt.Input() @@ -20,7 +24,10 @@ func Input(prefix string, completer Completer, opts ...Option) string { // Deprecated: Maybe anyone want to use this. func Choose(prefix string, choices []string, opts ...Option) string { completer := newChoiceCompleter(choices, FilterHasPrefix) - pt := New(dummyExecutor, completer) + pt, err := New(dummyExecutor, completer) + if err != nil { + return "" + } pt.Renderer().prefixTextColor = DefaultColor pt.Renderer().prefix = prefix