Documentation ¶
Index ¶
- Variables
- func Ask(qs []*Question, response interface{}, opts ...AskOpt) error
- func AskOne(p Prompt, response interface{}, v Validator, opts ...AskOpt) error
- func Required(val interface{}) error
- func Title(ans interface{}) interface{}
- func ToLower(ans interface{}) interface{}
- type AskOpt
- type AskOptions
- type Confirm
- type ConfirmTemplateData
- type Editor
- type EditorTemplateData
- type Input
- type InputTemplateData
- type MultiSelect
- type MultiSelectTemplateData
- type Multiline
- type MultilineTemplateData
- type Password
- type PasswordTemplateData
- type Prompt
- type PromptAgainer
- type Question
- type Select
- type SelectTemplateData
- type Transformer
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ConfirmQuestionTemplate = `` /* 497-byte string literal not displayed */
Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var DefaultAskOptions = AskOptions{ Stdio: terminal.Stdio{ In: os.Stdin, Out: os.Stdout, Err: os.Stderr, }, }
DefaultAskOptions is the default options on ask, using the OS stdio.
var DefaultFilterFn = func(filter string, options []string) (answer []string) { filter = strings.ToLower(filter) for _, o := range options { if strings.Contains(strings.ToLower(o), filter) { answer = append(answer, o) } } return answer }
var EditorQuestionTemplate = `` /* 582-byte string literal not displayed */
Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var InputQuestionTemplate = `` /* 496-byte string literal not displayed */
Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var MultiSelectQuestionTemplate = `` /* 858-byte string literal not displayed */
var MultilineQuestionTemplate = `` /* 465-byte string literal not displayed */
Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var PageSize = 7
PageSize is the default maximum number of items to show in select/multiselect prompts
var PasswordQuestionTemplate = `` /* 318-byte string literal not displayed */
Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var SelectQuestionTemplate = `` /* 719-byte string literal not displayed */
Functions ¶
func Ask ¶
Ask performs the prompt loop, asking for validation when appropriate. The response type can be one of two options. If a struct is passed, the answer will be written to the field whose name matches the Name field on the corresponding question. Field types should be something that can be casted from the response type designated in the documentation. Note, a survey tag can also be used to identify a Otherwise, a map[string]interface{} can be passed, responses will be written to the key with the matching name. For example:
qs := []*survey.Question{ { Name: "name", Prompt: &survey.Input{Message: "What is your name?"}, Validate: survey.Required, Transform: survey.Title, }, } answers := struct{ Name string }{} err := survey.Ask(qs, &answers)
func AskOne ¶
AskOne performs the prompt for a single prompt and asks for validation if required. Response types should be something that can be casted from the response type designated in the documentation. For example:
name := "" prompt := &survey.Input{ Message: "name", } survey.AskOne(prompt, &name, nil)
func Title ¶ added in v1.4.0
func Title(ans interface{}) interface{}
Title is a `Transformer`. It receives an answer value and returns a copy of the "ans" with all Unicode letters that begin words mapped to their title case.
Note that if "ans" is not a string then it will return a nil value, meaning that the above answer will not be affected by this call at all.
func ToLower ¶ added in v1.4.0
func ToLower(ans interface{}) interface{}
ToLower is a `Transformer`. It receives an answer value and returns a copy of the "ans" with all Unicode letters mapped to their lower case.
Note that if "ans" is not a string then it will return a nil value, meaning that the above answer will not be affected by this call at all.
Types ¶
type AskOpt ¶ added in v1.6.0
type AskOpt func(options *AskOptions) error
AskOpt allows setting optional ask options.
func WithStdio ¶ added in v1.6.0
func WithStdio(in terminal.FileReader, out terminal.FileWriter, err io.Writer) AskOpt
WithStdio specifies the standard input, output and error files survey interacts with. By default, these are os.Stdin, os.Stdout, and os.Stderr.
type AskOptions ¶ added in v1.6.0
AskOptions provides additional options on ask.
type Confirm ¶
Confirm is a regular text input that accept yes/no answers. Response type is a bool.
type ConfirmTemplateData ¶
data available to the templates when processing
type Editor ¶ added in v1.3.0
type Editor struct { core.Renderer Message string Default string Help string Editor string HideDefault bool AppendDefault bool }
Editor launches an instance of the users preferred editor on a temporary file. The editor to use is determined by reading the $VISUAL or $EDITOR environment variables. If neither of those are present, notepad (on Windows) or vim (others) is used. The launch of the editor is triggered by the enter key. Since the response may be long, it will not be echoed as Input does, instead, it print <Received>. Response type is a string.
message := "" prompt := &survey.Editor{ Message: "What is your commit message?" } survey.AskOne(prompt, &message, nil)
func (*Editor) PromptAgain ¶ added in v1.7.1
type EditorTemplateData ¶ added in v1.3.0
data available to the templates when processing
type Input ¶
Input is a regular text input that prints each character the user types on the screen and accepts the input with the enter key. Response type is a string.
name := "" prompt := &survey.Input{ Message: "What is your name?" } survey.AskOne(prompt, &name, nil)
type InputTemplateData ¶
data available to the templates when processing
type MultiSelect ¶ added in v1.0.1
type MultiSelect struct { core.Renderer Message string Options []string Default []string Help string PageSize int VimMode bool FilterMessage string FilterFn func(string, []string) []string // contains filtered or unexported fields }
MultiSelect is a prompt that presents a list of various options to the user for them to select using the arrow keys and enter. Response type is a slice of strings.
days := []string{} prompt := &survey.MultiSelect{ Message: "What days do you prefer:", Options: []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}, } survey.AskOne(prompt, &days, nil)
func (*MultiSelect) Cleanup ¶ added in v1.0.1
func (m *MultiSelect) Cleanup(val interface{}) error
Cleanup removes the options section, and renders the ask like a normal question.
func (*MultiSelect) OnChange ¶ added in v1.0.1
func (m *MultiSelect) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)
OnChange is called on every keypress.
func (*MultiSelect) Prompt ¶ added in v1.0.1
func (m *MultiSelect) Prompt() (interface{}, error)
type MultiSelectTemplateData ¶ added in v1.0.1
type MultiSelectTemplateData struct { MultiSelect Answer string ShowAnswer bool Checked map[string]bool SelectedIndex int ShowHelp bool PageEntries []string }
data available to the templates when processing
type MultilineTemplateData ¶ added in v1.7.0
data available to the templates when processing
type Password ¶
Password is like a normal Input but the text shows up as *'s and there is no default. Response type is a string.
password := "" prompt := &survey.Password{ Message: "Please type your password" } survey.AskOne(prompt, &password, nil)
type PasswordTemplateData ¶ added in v1.1.1
type Prompt ¶
type Prompt interface { Prompt() (interface{}, error) Cleanup(interface{}) error Error(error) error }
Prompt is the primary interface for the objects that can take user input and return a response.
type PromptAgainer ¶ added in v1.7.1
PromptAgainer Interface for Prompts that support prompting again after invalid input
type Question ¶
type Question struct { Name string Prompt Prompt Validate Validator Transform Transformer }
Question is the core data structure for a survey questionnaire.
type Select ¶ added in v1.0.1
type Select struct { core.Renderer Message string Options []string Default string Help string PageSize int VimMode bool FilterMessage string FilterFn func(string, []string) []string // contains filtered or unexported fields }
Select is a prompt that presents a list of various options to the user for them to select using the arrow keys and enter. Response type is a string.
color := "" prompt := &survey.Select{ Message: "Choose a color:", Options: []string{"red", "blue", "green"}, } survey.AskOne(prompt, &color, nil)
type SelectTemplateData ¶
type SelectTemplateData struct { Select PageEntries []string SelectedIndex int Answer string ShowAnswer bool ShowHelp bool }
the data available to the templates when processing
type Transformer ¶ added in v1.4.0
type Transformer func(ans interface{}) (newAns interface{})
Transformer is a function passed to a Question after a user has provided a response. The function can be used to implement a custom logic that will result to return a different representation of the given answer.
Look `TransformString`, `ToLower` `Title` and `ComposeTransformers` for more.
func ComposeTransformers ¶ added in v1.4.0
func ComposeTransformers(transformers ...Transformer) Transformer
ComposeTransformers is a variadic function used to create one transformer from many.
func TransformString ¶ added in v1.4.0
func TransformString(f func(s string) string) Transformer
TransformString returns a `Transformer` based on the "f" function which accepts a string representation of the answer and returns a new one, transformed, answer. Take for example the functions inside the std `strings` package, they can be converted to a compatible `Transformer` by using this function, i.e: `TransformString(strings.Title)`, `TransformString(strings.ToUpper)`.
Note that `TransformString` is just a helper, `Transformer` can be used to transform any type of answer.
type Validator ¶
type Validator func(ans interface{}) error
Validator is a function passed to a Question after a user has provided a response. If the function returns an error, then the user will be prompted again for another response.
func ComposeValidators ¶
ComposeValidators is a variadic function used to create one validator from many.