Skip to content

Commit

Permalink
fix: return error instead of panicking (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
YannickPferr authored Feb 14, 2024
1 parent 66ffa1d commit 7f51e9a
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion _example/diagnostics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion _example/exec-command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func completer(t prompt.Document) []prompt.Suggest {
}

func main() {
p := prompt.New(
p, _ := prompt.New(
executor,
completer,
)
Expand Down
2 changes: 1 addition & 1 deletion _example/http-prompt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func main() {
client: &http.Client{},
}

p := prompt.New(
p, _ := prompt.New(
executor,
completer,
prompt.OptionPrefix(u.String()+"> "),
Expand Down
2 changes: 1 addition & 1 deletion _example/live-prefix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func changeLivePrefix() (string, bool) {
}

func main() {
p := prompt.New(
p, _ := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
Expand Down
2 changes: 1 addition & 1 deletion _example/simple-echo/multiwidth-completions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func completer(in prompt.Document) []prompt.Suggest {
}

func main() {
p := prompt.New(
p, _ := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
Expand Down
2 changes: 1 addition & 1 deletion _tools/complete_file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func completerFunc(d prompt.Document) []prompt.Suggest {
}

func main() {
p := prompt.New(
p, _ := prompt.New(
executor,
completerFunc,
prompt.OptionPrefix(">>> "),
Expand Down
6 changes: 3 additions & 3 deletions input_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions input_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 8 additions & 4 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
13 changes: 10 additions & 3 deletions shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand Down

0 comments on commit 7f51e9a

Please sign in to comment.