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

Commit

Permalink
added WithIconSet AskOpt
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Jun 1, 2019
1 parent fe2c1bd commit 47f6e11
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,36 @@ survey.AskOne(

## Customizing Output

Customizing the icons and various parts of survey can easily be done by setting the following variables
in `survey/core`:

| name | default | description |
| ------------------ | ------- | ------------------------------------------------------------- |
| ErrorIcon | X | Before an error |
| HelpIcon | i | Before help text |
| QuestionIcon | ? | Before the message of a prompt |
| SelectFocusIcon | > | Marks the current focus in `Select` and `MultiSelect` prompts |
| UnmarkedOptionIcon | [ ] | Marks an unselected option in a `MultiSelect` prompt |
| MarkedOptionIcon | [x] | Marks a chosen selection in a `MultiSelect` prompt |
Customizing the icons and various parts of survey can easily be done by passing the `WithIcons` option
to `Ask` or `AskOne`:

```golang
import (
"github.com/AlecAivazis/survey/v2"
)

number := ""
prompt := &survey.Input{
Message: "If you have this need, please give me a reasonable message.",
Help: "I couldn't come up with one.",
}

survey.AskOne(prompt, &number, survey.WithIconSet(function(icons *survey.IconSet) {
// you can set any icons
icons.Question = ""
}))
```

The icons available for updating are:

| name | default | description |
| -------------- | ------- | ------------------------------------------------------------- |
| Error | X | Before an error |
| Help | i | Before help text |
| Question | ? | Before the message of a prompt |
| SelectFocus | > | Marks the current focus in `Select` and `MultiSelect` prompts |
| UnmarkedOption | [ ] | Marks an unselected option in a `MultiSelect` prompt |
| MarkedOption | [x] | Marks a chosen selection in a `MultiSelect` prompt |

## Testing

Expand Down
15 changes: 13 additions & 2 deletions survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,21 @@ func WithPageSize(pageSize int) AskOpt {
}

// WithHelpInputRune changes the character that prompts look for to give the user helpful information.
func WithHelpInputRune(r string) AskOpt {
func WithHelpInputRune(r rune) AskOpt {
return func(options *AskOptions) error {
// set the input character
options.PromptConfig.IconSet.HelpInput = r
options.PromptConfig.IconSet.HelpInput = string(r)

// nothing went wrong
return nil
}
}

// WithIconSet sets the icons that will be used when prompting the user
func WithIconSet(setIcons func(*IconSet)) AskOpt {
return func(options *AskOptions) error {
// update the default icons with whatever the user says
setIcons(&options.PromptConfig.IconSet)

// nothing went wrong
return nil
Expand Down

0 comments on commit 47f6e11

Please sign in to comment.