Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
can set error icon
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Jun 1, 2019
1 parent 47f6e11 commit bf49040
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *Confirm) getBool(showHelp bool, config *PromptConfig) (bool, error) {
continue
default:
// we didnt get a valid answer, so print error and prompt again
if err := c.Error(fmt.Errorf("%q is not a valid answer, please try again.", val)); err != nil {
if err := c.Error(fmt.Errorf("%q is not a valid answer, please try again.", val), config.IconSet.Error); err != nil {
return c.Default, err
}
err := c.Render(
Expand Down
14 changes: 11 additions & 3 deletions core/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ type Renderer struct {
errorLineCount int
}

var ErrorTemplate = `{{color "red"}}X Sorry, your reply was invalid: {{.Error}}{{color "reset"}}
type ErrorTemplateData struct {
Error error
Icon string
}

var ErrorTemplate = `{{color "red"}}{{ .Icon }} Sorry, your reply was invalid: {{ .Error.Error }}{{color "reset"}}
`

func (r *Renderer) WithStdio(stdio terminal.Stdio) {
Expand All @@ -35,14 +40,17 @@ func (r *Renderer) NewCursor() *terminal.Cursor {
}
}

func (r *Renderer) Error(invalid error) error {
func (r *Renderer) Error(invalid error, icon string) error {
// since errors are printed on top we need to reset the prompt
// as well as any previous error print
r.resetPrompt(r.lineCount + r.errorLineCount)

// we just cleared the prompt lines
r.lineCount = 0
out, err := RunTemplate(ErrorTemplate, invalid)
out, err := RunTemplate(ErrorTemplate, &ErrorTemplateData{
Error: invalid,
Icon: icon,
})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type PromptConfig struct {
type Prompt interface {
Prompt(config *PromptConfig) (interface{}, error)
Cleanup(interface{}, *PromptConfig) error
Error(error) error
Error(error, string) error
}

// PromptAgainer Interface for Prompts that support prompting again after invalid input
Expand Down Expand Up @@ -198,7 +198,7 @@ func Ask(qs []*Question, response interface{}, opts ...AskOpt) error {
if q.Validate != nil {
// wait for a valid response
for invalid := q.Validate(ans); invalid != nil; invalid = q.Validate(ans) {
err := q.Prompt.Error(invalid)
err := q.Prompt.Error(invalid, options.PromptConfig.IconSet.Error)
// if there was a problem
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion survey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ func TestValidationError(t *testing.T) {

actual, err := core.RunTemplate(
core.ErrorTemplate,
err,
&core.ErrorTemplateData{
Error: err,
Icon: "X",
},
)
if err != nil {
t.Errorf("Failed to run template to format error: %s", err)
Expand Down

0 comments on commit bf49040

Please sign in to comment.