survey

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2024 License: Apache-2.0, MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfirmQuestionTemplate = `` /* 571-byte string literal not displayed */

Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format

View Source
var EditorQuestionTemplate = `` /* 656-byte string literal not displayed */

Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format

View Source
var ErrorTemplate = `{{color .Icon.Format }}{{ .Icon.Text }} Sorry, your reply was invalid: {{ .Error.Error }}{{color "reset"}}
`
View Source
var InputQuestionTemplate = `` /* 1153-byte string literal not displayed */

Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format

View Source
var MultiSelectQuestionTemplate = `` /* 1425-byte string literal not displayed */
View Source
var MultilineQuestionTemplate = `` /* 566-byte string literal not displayed */

Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format

View Source
var PasswordQuestionTemplate = `` /* 392-byte string literal not displayed */

PasswordQuestionTemplate is a template with color formatting. See Documentation: https://github.com/mgutz/ansi#style-format

View Source
var SelectQuestionTemplate = `` /* 1033-byte string literal not displayed */

Functions

func Ask

func Ask(qs []*Question, response any, opts ...AskOpt) error

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]any 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

func AskOne(p Prompt, response any, opts ...AskOpt) error

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)

func Required

func Required(val any) error

Required does not allow an empty value

func Title

func Title(ans any) any

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

func ToLower(ans any) any

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

type AskOpt func(options *AskOptions) error

AskOpt allows setting optional ask options.

func WithFilter

func WithFilter(filter func(filter string, value string, index int) (include bool)) AskOpt

WithFilter specifies the default filter to use when asking questions.

func WithHelpInput

func WithHelpInput(r rune) AskOpt

WithHelpInput changes the character that prompts look for to give the user helpful information.

func WithHideCharacter

func WithHideCharacter(char rune) AskOpt

WithHideCharacter sets the default character shown instead of the password for password inputs

func WithIcons

func WithIcons(setIcons func(*IconSet)) AskOpt

WithIcons sets the icons that will be used when prompting the user

func WithKeepFilter

func WithKeepFilter(KeepFilter bool) AskOpt

WithKeepFilter sets the if the filter is kept after selections

func WithPageSize

func WithPageSize(pageSize int) AskOpt

WithPageSize sets the default page size used by prompts

func WithRemoveSelectAll

func WithRemoveSelectAll() AskOpt

WithRemoveSelectAll remove the select all option in Multiselect

func WithRemoveSelectNone

func WithRemoveSelectNone() AskOpt

WithRemoveSelectNone remove the select none/unselect all in Multiselect

func WithShowCursor

func WithShowCursor(ShowCursor bool) AskOpt

WithShowCursor sets the show cursor behavior when prompting the user

func WithStdio

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.

func WithValidator

func WithValidator(v Validator) AskOpt

WithValidator specifies a validator to use while prompting the user

type AskOptions

type AskOptions struct {
	Stdio        terminal.Stdio
	Validators   []Validator
	PromptConfig PromptConfig
}

AskOptions provides additional options on ask.

type Confirm

type Confirm struct {
	Renderer
	Message string
	Default bool
	Help    string
}

Confirm is a regular text input that accept yes/no answers. Response type is a bool.

func (*Confirm) Cleanup

func (c *Confirm) Cleanup(config *PromptConfig, val any) error

Cleanup overwrite the line with the finalized formatted version

func (*Confirm) Prompt

func (c *Confirm) Prompt(config *PromptConfig) (any, error)

Prompt prompts the user with a simple text field and expects a reply followed by a carriage return.

likesPie := false
prompt := &survey.Confirm{ Message: "What is your name?" }
survey.AskOne(prompt, &likesPie)

type ConfirmTemplateData

type ConfirmTemplateData struct {
	Confirm
	Answer   string
	ShowHelp bool
	Config   *PromptConfig
}

data available to the templates when processing

type Editor

type Editor struct {
	Renderer
	Message       string
	Default       string
	Help          string
	Editor        string
	HideDefault   bool
	AppendDefault bool
	FileName      string
}

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)

func (*Editor) Cleanup

func (e *Editor) Cleanup(config *PromptConfig, val any) error

func (*Editor) Prompt

func (e *Editor) Prompt(config *PromptConfig) (any, error)

func (*Editor) PromptAgain

func (e *Editor) PromptAgain(config *PromptConfig, invalid any, err error) (any, error)

type EditorTemplateData

type EditorTemplateData struct {
	Editor
	Answer     string
	ShowAnswer bool
	ShowHelp   bool
	Config     *PromptConfig
}

data available to the templates when processing

type ErrorTemplateData

type ErrorTemplateData struct {
	Error error
	Icon  Icon
}

type Icon

type Icon struct {
	Text   string
	Format string
}

Icon holds the text and format to show for a particular icon

type IconSet

type IconSet struct {
	HelpInput      Icon
	Error          Icon
	Help           Icon
	Question       Icon
	MarkedOption   Icon
	UnmarkedOption Icon
	SelectFocus    Icon
}

IconSet holds the icons to use for various prompts

type Input

type Input struct {
	Renderer
	Message string
	Default string
	Help    string
	Suggest func(toComplete string) []string
	// contains filtered or unexported fields
}

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)

func (*Input) Cleanup

func (i *Input) Cleanup(config *PromptConfig, val any) error

func (*Input) Prompt

func (i *Input) Prompt(config *PromptConfig) (any, error)

type InputTemplateData

type InputTemplateData struct {
	Input
	ShowAnswer    bool
	ShowHelp      bool
	Answer        string
	PageEntries   []core.OptionAnswer
	SelectedIndex int
	Config        *PromptConfig
}

data available to the templates when processing

type IterableOpts

type IterableOpts interface {
	IterateOption(int, core.OptionAnswer) any
}

type MultiSelect

type MultiSelect struct {
	Renderer
	Message       string
	Options       []string
	Default       any
	Help          string
	PageSize      int
	VimMode       bool
	FilterMessage string
	Filter        func(filter string, value string, index int) bool
	Description   func(value string, index int) 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)

func (*MultiSelect) Cleanup

func (m *MultiSelect) Cleanup(config *PromptConfig, val any) error

Cleanup removes the options section, and renders the ask like a normal question.

func (*MultiSelect) OnChange

func (m *MultiSelect) OnChange(key rune, config *PromptConfig)

OnChange is called on every keypress.

func (*MultiSelect) Prompt

func (m *MultiSelect) Prompt(config *PromptConfig) (any, error)

type MultiSelectTemplateData

type MultiSelectTemplateData struct {
	MultiSelect
	Answer        string
	ShowAnswer    bool
	Checked       map[int]bool
	SelectedIndex int
	ShowHelp      bool
	Description   func(value string, index int) string
	PageEntries   []core.OptionAnswer
	Config        *PromptConfig

	// These fields are used when rendering an individual option
	CurrentOpt   core.OptionAnswer
	CurrentIndex int
}

data available to the templates when processing

func (MultiSelectTemplateData) GetDescription

func (m MultiSelectTemplateData) GetDescription(opt core.OptionAnswer) string

func (MultiSelectTemplateData) IterateOption

func (m MultiSelectTemplateData) IterateOption(ix int, opt core.OptionAnswer) any

IterateOption sets CurrentOpt and CurrentIndex appropriately so a multiselect option can be rendered individually

type Multiline

type Multiline struct {
	Renderer
	Message string
	Default string
	Help    string
}

func (*Multiline) Cleanup

func (i *Multiline) Cleanup(config *PromptConfig, val any) error

func (*Multiline) Prompt

func (i *Multiline) Prompt(config *PromptConfig) (any, error)

type MultilineTemplateData

type MultilineTemplateData struct {
	Multiline
	Answer     string
	ShowAnswer bool
	ShowHelp   bool
	Config     *PromptConfig
}

data available to the templates when processing

type OptionAnswer

type OptionAnswer = core.OptionAnswer

OptionAnswer is an ergonomic alias for core.OptionAnswer

type Password

type Password struct {
	Renderer
	Message string
	Help    string
}

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)

func (*Password) Cleanup

func (prompt *Password) Cleanup(config *PromptConfig, val any) error

Cleanup hides the string with a fixed number of characters.

func (*Password) Prompt

func (p *Password) Prompt(config *PromptConfig) (any, error)

type PasswordTemplateData

type PasswordTemplateData struct {
	Password
	ShowHelp bool
	Config   *PromptConfig
}

type Prompt

type Prompt interface {
	Prompt(config *PromptConfig) (any, error)
	Cleanup(*PromptConfig, any) error
	Error(*PromptConfig, error) error
}

Prompt is the primary interface for the objects that can take user input and return a response.

type PromptAgainer

type PromptAgainer interface {
	PromptAgain(config *PromptConfig, invalid any, err error) (any, error)
}

PromptAgainer Interface for Prompts that support prompting again after invalid input

type PromptConfig

type PromptConfig struct {
	PageSize         int
	Icons            IconSet
	HelpInput        string
	SuggestInput     string
	Filter           func(filter string, option string, index int) bool
	KeepFilter       bool
	ShowCursor       bool
	RemoveSelectAll  bool
	RemoveSelectNone bool
	HideCharacter    rune
}

PromptConfig holds the global configuration for a prompt

type Question

type Question struct {
	Name      string
	Prompt    Prompt
	Validate  Validator
	Transform Transformer
}

Question is the core data structure for a survey questionnaire.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

func (*Renderer) AppendRenderedText

func (r *Renderer) AppendRenderedText(text string)

AppendRenderedText appends text to the renderer's text buffer which is used to track of what has been printed. The buffer is used to calculate how many lines to erase before updating the prompt.

func (*Renderer) Error

func (r *Renderer) Error(config *PromptConfig, invalid error) error

func (*Renderer) NewCursor

func (r *Renderer) NewCursor() *terminal.Cursor

func (*Renderer) NewRuneReader

func (r *Renderer) NewRuneReader() *terminal.RuneReader

func (*Renderer) OffsetCursor

func (r *Renderer) OffsetCursor(offset int)

func (*Renderer) Render

func (r *Renderer) Render(tmpl string, data any) error

func (*Renderer) RenderWithCursorOffset

func (r *Renderer) RenderWithCursorOffset(tmpl string, data IterableOpts, opts []core.OptionAnswer, idx int) error

func (*Renderer) Stdio

func (r *Renderer) Stdio() terminal.Stdio

func (*Renderer) WithStdio

func (r *Renderer) WithStdio(stdio terminal.Stdio)

type Select

type Select struct {
	Renderer
	Message       string
	Options       []string
	Default       any
	Help          string
	PageSize      int
	VimMode       bool
	FilterMessage string
	Filter        func(filter string, value string, index int) bool
	Description   func(value string, index int) 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)

func (*Select) Cleanup

func (s *Select) Cleanup(config *PromptConfig, val any) error

func (*Select) OnChange

func (s *Select) OnChange(key rune, config *PromptConfig) bool

OnChange is called on every keypress.

func (*Select) Prompt

func (s *Select) Prompt(config *PromptConfig) (any, error)

type SelectTemplateData

type SelectTemplateData struct {
	Select
	PageEntries   []core.OptionAnswer
	SelectedIndex int
	Answer        string
	ShowAnswer    bool
	ShowHelp      bool
	Description   func(value string, index int) string
	Config        *PromptConfig

	// These fields are used when rendering an individual option
	CurrentOpt   core.OptionAnswer
	CurrentIndex int
}

SelectTemplateData is the data available to the templates when processing

func (SelectTemplateData) GetDescription

func (s SelectTemplateData) GetDescription(opt core.OptionAnswer) string

func (SelectTemplateData) IterateOption

func (s SelectTemplateData) IterateOption(ix int, opt core.OptionAnswer) any

IterateOption sets CurrentOpt and CurrentIndex appropriately so a select option can be rendered individually

type Transformer

type Transformer func(ans any) (newAns any)

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

func ComposeTransformers(transformers ...Transformer) Transformer

ComposeTransformers is a variadic function used to create one transformer from many.

func TransformString

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 any) 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

func ComposeValidators(validators ...Validator) Validator

ComposeValidators is a variadic function used to create one validator from many.

func MaxItems

func MaxItems(numberItems int) Validator

MaxItems requires that the list is no longer than the specified value

func MaxLength

func MaxLength(length int) Validator

MaxLength requires that the string is no longer than the specified value

func MinItems

func MinItems(numberItems int) Validator

MinItems requires that the list is longer or equal in length to the specified value

func MinLength

func MinLength(length int) Validator

MinLength requires that the string is longer or equal in length to the specified value

Directories

Path Synopsis
tests

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL