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

Commit

Permalink
Icon format (#222)
Browse files Browse the repository at this point in the history
* added icon struct

* added format strings to templates

* documented icon formats

* fixed typo in icon format documentation

* grammar is hard

* so is formatting

* removed duplicative wording in docs

* added default format to docs
  • Loading branch information
AlecAivazis authored Jun 4, 2019
1 parent d9248ab commit 34c84b5
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 130 deletions.
54 changes: 25 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ for the old `v1` version, see [here](https://godoc.org/gopkg.in/AlecAivazis/surv
1. [Help Text](#help-text)
1. [Changing the input rune](#changing-the-input-run)
1. [Custom Types](#custom-types)
1. [Customizing Output](#customizing-output)
1. [Changing the Icons ](#changing-the-icons)
1. [Testing](#testing)

## Examples
Expand Down Expand Up @@ -189,16 +189,13 @@ survey.AskOne(prompt, &color)
The user can also press `esc` to toggle the ability cycle through the options with the j and k keys to do down and up respectively.

By default, the select prompt is limited to showing 7 options at a time
and will paginate lists of options longer than that. To increase, you can either
set the `PageSize` field on the prompt:
and will paginate lists of options longer than that. This can be changed a number of ways:

```golang
prompt := &survey.Select{..., PageSize: 10}
```

Or pass an an `AskOpt` to `survey.Ask` or `survey.AskOne`:
// as a field on a single select
prompt := &survey.MultiSelect{..., PageSize: 10}

```golang
// or as an option to Ask or AskOne
survey.AskOne(prompt, &days, survey.WithPageSize(10))
```

Expand All @@ -218,16 +215,13 @@ survey.AskOne(prompt, &days)
The user can also press `esc` to toggle the ability cycle through the options with the j and k keys to do down and up respectively.

By default, the MultiSelect prompt is limited to showing 7 options at a time
and will paginate lists of options longer than that. To increase, you can either
set the `PageSize` field on the prompt:
and will paginate lists of options longer than that. This can be changed a number of ways:

```golang
// as a field on a single select
prompt := &survey.MultiSelect{..., PageSize: 10}
```

Or pass an an `AskOpt` to `survey.Ask` or `survey.AskOne`:

```golang
// or as an option to Ask or AskOne
survey.AskOne(prompt, &days, survey.WithPageSize(10))
```

Expand Down Expand Up @@ -299,7 +293,7 @@ q := &survey.Question{
}
```

Validators can be passed to `survey.AskOne` by using `survey.WithValidator`:
Validators can be provided with `survey.WithValidator`:

```golang
color := ""
Expand Down Expand Up @@ -336,7 +330,7 @@ All of the prompts have a `Help` field which can be defined to provide more info
### Changing the input rune

In some situations, `?` is a perfectly valid response. To handle this, you can change the rune that survey
looks for by passing an `AskOpt` to `Ask` or `AskOne`:
looks for with `WithHelpInput`:

```golang
import (
Expand Down Expand Up @@ -382,10 +376,10 @@ survey.AskOne(
)
```

## Customizing Output
## Changing the Icons

Customizing the icons and various parts of survey can easily be done by passing the `WithIcons` option
to `Ask` or `AskOne`:
Changing the icons and their color/format can be done by passing the `WithIcons` option. The format
follows the patterns outlined [here](https://github.com/mgutz/ansi#style-format).:

```golang
import (
Expand All @@ -400,20 +394,22 @@ prompt := &survey.Input{

survey.AskOne(prompt, &number, survey.WithIcons(function(icons *survey.IconSet) {
// you can set any icons
icons.Question = ""
icons.Question.Text = ""
// for more information on formatting the icons, see here: https://github.com/mgutz/ansi#style-format
icons.Question.Format = "yellow+hb"
}))
```

The icons available for updating are:
The icons and their default text and format are summarized below:

| 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 |
| name | text | format | description |
| -------------- | ---- | ---------- | ------------------------------------------------------------- |
| Error | X | red | Before an error |
| Help | i | cyan | Before help text |
| Question | ? | green+hb | Before the message of a prompt |
| SelectFocus | > | green | Marks the current focus in `Select` and `MultiSelect` prompts |
| UnmarkedOption | [ ] | default+hb | Marks an unselected option in a `MultiSelect` prompt |
| MarkedOption | [x] | cyan+b | Marks a chosen selection in a `MultiSelect` prompt |

## Testing

Expand Down
4 changes: 2 additions & 2 deletions confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type ConfirmTemplateData struct {

// Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var ConfirmQuestionTemplate = `
{{- if .ShowHelp }}{{- color "cyan"}}{{ .Config.Icons.Help }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color "green+hb"}}{{ .Config.Icons.Question }} {{color "reset"}}
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color .Config.Icons.Question.Format }}{{ .Config.Icons.Question.Text }} {{color "reset"}}
{{- color "default+hb"}}{{ .Message }} {{color "reset"}}
{{- if .Answer}}
{{- color "cyan"}}{{.Answer}}{{color "reset"}}{{"\n"}}
Expand Down
14 changes: 7 additions & 7 deletions confirm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,31 @@ func TestConfirmRender(t *testing.T) {
"Test Confirm question output with default true",
Confirm{Message: "Is pizza your favorite food?", Default: true},
ConfirmTemplateData{},
fmt.Sprintf("%s Is pizza your favorite food? (Y/n) ", defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s Is pizza your favorite food? (Y/n) ", defaultIcons().Question.Text),
},
{
"Test Confirm question output with default false",
Confirm{Message: "Is pizza your favorite food?", Default: false},
ConfirmTemplateData{},
fmt.Sprintf("%s Is pizza your favorite food? (y/N) ", defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s Is pizza your favorite food? (y/N) ", defaultIcons().Question.Text),
},
{
"Test Confirm answer output",
Confirm{Message: "Is pizza your favorite food?"},
ConfirmTemplateData{Answer: "Yes"},
fmt.Sprintf("%s Is pizza your favorite food? Yes\n", defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s Is pizza your favorite food? Yes\n", defaultIcons().Question.Text),
},
{
"Test Confirm with help but help message is hidden",
Confirm{Message: "Is pizza your favorite food?", Help: "This is helpful"},
ConfirmTemplateData{},
fmt.Sprintf("%s Is pizza your favorite food? [%s for help] (y/N) ", defaultAskOptions().PromptConfig.Icons.Question, string(defaultAskOptions().PromptConfig.HelpInput)),
fmt.Sprintf("%s Is pizza your favorite food? [%s for help] (y/N) ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
},
{
"Test Confirm help output with help message shown",
Confirm{Message: "Is pizza your favorite food?", Help: "This is helpful"},
ConfirmTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s Is pizza your favorite food? (y/N) ", defaultAskOptions().PromptConfig.Icons.Help, defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s This is helpful\n%s Is pizza your favorite food? (y/N) ", defaultIcons().Help.Text, defaultIcons().Question.Text),
},
}

Expand All @@ -66,7 +66,7 @@ func TestConfirmRender(t *testing.T) {
test.data.Confirm = test.prompt

// set the runtime config
test.data.Config = &defaultAskOptions().PromptConfig
test.data.Config = defaultPromptConfig()

err = test.prompt.Render(
ConfirmQuestionTemplate,
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestConfirmPrompt(t *testing.T) {
c.ExpectString(
fmt.Sprintf(
"Is pizza your favorite food? [%s for help] (y/N)",
string(defaultAskOptions().PromptConfig.HelpInput),
string(defaultPromptConfig().HelpInput),
),
)
c.SendLine("?")
Expand Down
4 changes: 2 additions & 2 deletions editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type EditorTemplateData struct {

// Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var EditorQuestionTemplate = `
{{- if .ShowHelp }}{{- color "cyan"}}{{ .Config.Icons.Help }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color "green+hb"}}{{ .Config.Icons.Question }} {{color "reset"}}
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color .Config.Icons.Question.Format }}{{ .Config.Icons.Question.Text }} {{color "reset"}}
{{- color "default+hb"}}{{ .Message }} {{color "reset"}}
{{- if .ShowAnswer}}
{{- color "cyan"}}{{.Answer}}{{color "reset"}}{{"\n"}}
Expand Down
20 changes: 10 additions & 10 deletions editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,49 @@ func TestEditorRender(t *testing.T) {
"Test Editor question output without default",
Editor{Message: "What is your favorite month:"},
EditorTemplateData{},
fmt.Sprintf("%s What is your favorite month: [Enter to launch editor] ", defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s What is your favorite month: [Enter to launch editor] ", defaultIcons().Question.Text),
},
{
"Test Editor question output with default",
Editor{Message: "What is your favorite month:", Default: "April"},
EditorTemplateData{},
fmt.Sprintf("%s What is your favorite month: (April) [Enter to launch editor] ", defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s What is your favorite month: (April) [Enter to launch editor] ", defaultIcons().Question.Text),
},
{
"Test Editor question output with HideDefault",
Editor{Message: "What is your favorite month:", Default: "April", HideDefault: true},
EditorTemplateData{},
fmt.Sprintf("%s What is your favorite month: [Enter to launch editor] ", defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s What is your favorite month: [Enter to launch editor] ", defaultIcons().Question.Text),
},
{
"Test Editor answer output",
Editor{Message: "What is your favorite month:"},
EditorTemplateData{Answer: "October", ShowAnswer: true},
fmt.Sprintf("%s What is your favorite month: October\n", defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s What is your favorite month: October\n", defaultIcons().Question.Text),
},
{
"Test Editor question output without default but with help hidden",
Editor{Message: "What is your favorite month:", Help: "This is helpful"},
EditorTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for help] [Enter to launch editor] ", defaultAskOptions().PromptConfig.Icons.Question, string(defaultAskOptions().PromptConfig.HelpInput)),
fmt.Sprintf("%s What is your favorite month: [%s for help] [Enter to launch editor] ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
},
{
"Test Editor question output with default and with help hidden",
Editor{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
EditorTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for help] (April) [Enter to launch editor] ", defaultAskOptions().PromptConfig.Icons.Question, string(defaultAskOptions().PromptConfig.HelpInput)),
fmt.Sprintf("%s What is your favorite month: [%s for help] (April) [Enter to launch editor] ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
},
{
"Test Editor question output without default but with help shown",
Editor{Message: "What is your favorite month:", Help: "This is helpful"},
EditorTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: [Enter to launch editor] ", defaultAskOptions().PromptConfig.Icons.Help, defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: [Enter to launch editor] ", defaultIcons().Help.Text, defaultIcons().Question.Text),
},
{
"Test Editor question output with default and with help shown",
Editor{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
EditorTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: (April) [Enter to launch editor] ", defaultAskOptions().PromptConfig.Icons.Help, defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: (April) [Enter to launch editor] ", defaultIcons().Help.Text, defaultIcons().Question.Text),
},
}

Expand All @@ -85,7 +85,7 @@ func TestEditorRender(t *testing.T) {
test.data.Editor = test.prompt

// set the icon set
test.data.Config = &defaultAskOptions().PromptConfig
test.data.Config = defaultPromptConfig()

err = test.prompt.Render(
EditorQuestionTemplate,
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestEditorPrompt(t *testing.T) {
c.ExpectString(
fmt.Sprintf(
"Edit git commit message [%s for help] [Enter to launch editor]",
string(defaultAskOptions().PromptConfig.HelpInput),
string(defaultPromptConfig().HelpInput),
),
)
c.SendLine("?")
Expand Down
4 changes: 2 additions & 2 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type InputTemplateData struct {

// Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var InputQuestionTemplate = `
{{- if .ShowHelp }}{{- color "cyan"}}{{ .Config.Icons.Help }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color "green+hb"}}{{ .Config.Icons.Question }} {{color "reset"}}
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color .Config.Icons.Question.Format }}{{ .Config.Icons.Question.Text }} {{color "reset"}}
{{- color "default+hb"}}{{ .Message }} {{color "reset"}}
{{- if .ShowAnswer}}
{{- color "cyan"}}{{.Answer}}{{color "reset"}}{{"\n"}}
Expand Down
16 changes: 8 additions & 8 deletions input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,43 @@ func TestInputRender(t *testing.T) {
"Test Input question output without default",
Input{Message: "What is your favorite month:"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: ", defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s What is your favorite month: ", defaultIcons().Question.Text),
},
{
"Test Input question output with default",
Input{Message: "What is your favorite month:", Default: "April"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: (April) ", defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s What is your favorite month: (April) ", defaultIcons().Question.Text),
},
{
"Test Input answer output",
Input{Message: "What is your favorite month:"},
InputTemplateData{Answer: "October", ShowAnswer: true},
fmt.Sprintf("%s What is your favorite month: October\n", defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s What is your favorite month: October\n", defaultIcons().Question.Text),
},
{
"Test Input question output without default but with help hidden",
Input{Message: "What is your favorite month:", Help: "This is helpful"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for help] ", defaultAskOptions().PromptConfig.Icons.Question, string(defaultAskOptions().PromptConfig.HelpInput)),
fmt.Sprintf("%s What is your favorite month: [%s for help] ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
},
{
"Test Input question output with default and with help hidden",
Input{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
InputTemplateData{},
fmt.Sprintf("%s What is your favorite month: [%s for help] (April) ", defaultAskOptions().PromptConfig.Icons.Question, string(defaultAskOptions().PromptConfig.HelpInput)),
fmt.Sprintf("%s What is your favorite month: [%s for help] (April) ", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
},
{
"Test Input question output without default but with help shown",
Input{Message: "What is your favorite month:", Help: "This is helpful"},
InputTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: ", defaultAskOptions().PromptConfig.Icons.Help, defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: ", defaultIcons().Help.Text, defaultIcons().Question.Text),
},
{
"Test Input question output with default and with help shown",
Input{Message: "What is your favorite month:", Default: "April", Help: "This is helpful"},
InputTemplateData{ShowHelp: true},
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: (April) ", defaultAskOptions().PromptConfig.Icons.Help, defaultAskOptions().PromptConfig.Icons.Question),
fmt.Sprintf("%s This is helpful\n%s What is your favorite month: (April) ", defaultIcons().Help.Text, defaultIcons().Question.Text),
},
}

Expand All @@ -78,7 +78,7 @@ func TestInputRender(t *testing.T) {
test.data.Input = test.prompt

// set the runtime config
test.data.Config = &defaultAskOptions().PromptConfig
test.data.Config = defaultPromptConfig()

err = test.prompt.Render(
InputQuestionTemplate,
Expand Down
4 changes: 2 additions & 2 deletions multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type MultilineTemplateData struct {

// Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var MultilineQuestionTemplate = `
{{- if .ShowHelp }}{{- color "cyan"}}{{ .Config.Icons.Help }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color "green+hb"}}{{ .Config.Icons.Question }} {{color "reset"}}
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color .Config.Icons.Question.Format }}{{ .Config.Icons.Question.Text }} {{color "reset"}}
{{- color "default+hb"}}{{ .Message }} {{color "reset"}}
{{- if .ShowAnswer}}
{{- "\n"}}{{color "cyan"}}{{.Answer}}{{color "reset"}}
Expand Down
Loading

0 comments on commit 34c84b5

Please sign in to comment.