survey

package module
v0.0.0-...-a79cd8b Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2016 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"
    "github.com/icyxp/survey"
)

// 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

This section is empty.

Variables

This section is empty.

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 CursorLocation

func CursorLocation() (*cursorCoordinate, error)

CursorLocation returns the location (col, row) of the cursor in the current terminal session.

func Required

func Required(str string) error

Required does not allow an empty value

Types

type Choice

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

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 (prompt *Choice) Cleanup(val string) error

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

func (*Choice) Prompt

func (prompt *Choice) Prompt() (string, error)

Prompt shows the list, and listens for input from the user using /dev/tty.

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 (input *Input) Cleanup(val string) error

Cleanup overwrite the line with the finalized formatted version

func (*Input) Prompt

func (input *Input) Prompt() (string, error)

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

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(val string) error

Cleanup hides the string with a fixed number of characters.

func (*Password) Prompt

func (prompt *Password) Prompt() (string, error)

Prompt behaves like a normal int but hides the input.

type Prompt

type Prompt interface {
	Prompt() (string, error)
	Cleanup(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 TerminalKey

type TerminalKey int

TerminalKey is a type used to refer to keys of interest

const (
	KeyArrowUp TerminalKey = iota
	KeyArrowDown
	KeyArrowLeft
	KeyArrowRight
	KeyEsc
	KeyEnter
	KeyNull
)

key codes for the common keys

func GetInput

func GetInput(format func(input string) string) (val string, keyCode TerminalKey, err error)

GetChar listens for input from the keyboard and returns the key value as a string or one of the Key* enum values.

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