survey

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2017 License: MIT Imports: 12 Imported by: 0

README

Survey

Build Status GoDoc

A library for building interactive prompts. Heavily inspired by the great inquirer.js.

package main

import (
    "fmt"
    "gopkg.in/alecaivazis/survey.v0"
)

// the questions to ask
var qs = []*survey.Question{
    {
        Name:     "name",
        Prompt:   &survey.Input{"What is your name?", ""},
        Validate: survey.Required,
    },
    {
        Name: "color",
        Prompt: &survey.Choice{
            Message: "Choose a color:",
            Choices: []string{"red", "blue", "green"},
            Default: "red",
        },
    },
}

func main() {
    answers, err := survey.Ask(qs)

    if err != nil {
        fmt.Println("\n", err.Error())
        return
    }

    fmt.Printf("%s chose %s.", answers["name"], answers["color"])
}

Documentation

Index

Constants

View Source
const (
	SelectQuestionTemplate = `` /* 175-byte string literal not displayed */

	// the template used to show the list of Selects
	SelectChoicesTemplate = `` /* 191-byte string literal not displayed */

)

Variables

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

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

View Source
var DisableColor = false
View Source
var ErrorTemplate = `{{color "red"}}✘ Sorry, your reply was invalid: {{.Error}}{{color "reset"}}
`
View Source
var InputQuestionTemplate = `` /* 263-byte string literal not displayed */

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

View Source
var MultiChoiceOptionsTemplate = `` /* 271-byte string literal not displayed */
View Source
var MultiChoiceQuestionTemplate = `` /* 181-byte string literal not displayed */
View Source
var PasswordQuestionTemplate = `
{{- color "green+hb"}}? {{color "reset"}}
{{- color "default+hb"}}{{ .Message }} {{color "reset"}}`

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

View Source
var TemplateFuncs = map[string]interface{}{

	"color": func(color string) string {
		if DisableColor {
			return ""
		}
		return ansi.ColorCode(color)
	},
}

Functions

func Ask

func Ask(qs []*Question) (map[string]string, error)

Ask performs the prompt loop

func AskOne

func AskOne(p Prompt) (string, error)

AskOne asks a single question without performing validation on the answer.

func AskOneValidate

func AskOneValidate(p Prompt, v Validator) (string, error)

AskOneValidate asks a single question and validates the answer with v.

func Required

func Required(str string) error

Required does not allow an empty value

func RunTemplate

func RunTemplate(tmpl string, data interface{}) (string, error)

Types

type Choice

type Choice struct {
	Message       string
	Choices       []string
	Default       string
	SelectedIndex int
}

Choice is a prompt that presents a list of various options to the user for them to select using the arrow keys and enter.

func (*Choice) Cleanup

func (s *Choice) Cleanup(rl *readline.Instance, val string) error

func (*Choice) OnChange

func (s *Choice) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)

OnChange is called on every keypress.

func (*Choice) Prompt

func (s *Choice) Prompt(rl *readline.Instance) (string, error)

type Confirm

type Confirm struct {
	Message string
	Default bool
	Answer  *bool
}

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

func (*Confirm) Cleanup

func (c *Confirm) Cleanup(rl *readline.Instance, val string) error

Cleanup overwrite the line with the finalized formatted version

func (*Confirm) Prompt

func (c *Confirm) Prompt(rl *readline.Instance) (string, error)

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

type ConfirmTemplateData

type ConfirmTemplateData struct {
	Confirm
	Answer string
}

data available to the templates when processing

type Input

type Input struct {
	Message string
	Default string
}

Input is a regular text input that prints each character the user types on the screen and accepts the input with the enter key.

func (*Input) Cleanup

func (i *Input) Cleanup(rl *readline.Instance, val string) error

func (*Input) Prompt

func (i *Input) Prompt(rl *readline.Instance) (line string, err error)

type InputTemplateData

type InputTemplateData struct {
	Input
	Answer string
}

data available to the templates when processing

type MultiChoice

type MultiChoice struct {
	Message  string
	Options  []string
	Defaults []string
	Answer   *[]string
	// contains filtered or unexported fields
}

MultiChoice is a prompt that presents a list of various options to the user for them to select using the arrow keys and enter.

func (*MultiChoice) Cleanup

func (m *MultiChoice) Cleanup(rl *readline.Instance, val string) error

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

func (*MultiChoice) OnChange

func (m *MultiChoice) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)

OnChange is called on every keypress.

func (*MultiChoice) Prompt

func (m *MultiChoice) Prompt(rl *readline.Instance) (string, error)

type MultiChoiceTemplateData

type MultiChoiceTemplateData struct {
	MultiChoice
	Answer        []string
	Checked       map[int]bool
	SelectedIndex int
}

data available to the templates when processing

type Password

type Password struct {
	Message string
}

Password is like a normal Input but the text shows up as *'s and there is no default.

func (*Password) Cleanup

func (prompt *Password) Cleanup(rl *readline.Instance, val string) error

Cleanup hides the string with a fixed number of characters.

func (*Password) Prompt

func (p *Password) Prompt(rl *readline.Instance) (line string, err error)

type Prompt

type Prompt interface {
	Prompt(*readline.Instance) (string, error)
	Cleanup(*readline.Instance, string) error
}

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

type Question

type Question struct {
	Name     string
	Prompt   Prompt
	Validate Validator
}

Question is the core data structure for a survey questionnaire.

type SelectTemplateData

type SelectTemplateData struct {
	Select Choice
	Answer string
}

the data available to the templates when processing

type Validator

type Validator func(string) error

Validator is a function passed to a Question in order to redefine

func ComposeValidators

func ComposeValidators(validators ...Validator) Validator

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

func MaxLength

func MaxLength(length int) Validator

MaxLength requires that the string is no longer than 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

Jump to

Keyboard shortcuts

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