interact

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2018 License: MIT Imports: 13 Imported by: 0

README

Interactive methods

console interactive methods

  • ReadInput
  • ReadLine
  • ReadFirst
  • Prompt
  • Confirm
  • Query/Question/Ask
  • Select/Choice
  • MultiSelect/Checkbox
  • ReadPassword

Documentation

Overview

Package interact collect some interactive methods for CLI

Index

Constants

View Source
const (
	// OK success exit code
	OK = 0
	// ERR error exit code
	ERR = 2
)

Variables

This section is empty.

Functions

func AnswerIsYes

func AnswerIsYes(defVal ...bool) bool

AnswerIsYes check user inputted answer is right Usage:

fmt.Print("are you OK?")
ok := AnswerIsYes()
ok := AnswerIsYes(true)

func Ask

func Ask(question, defVal string, fn func(ans string) error, maxTimes ...int) string

Ask a question and return the result of the input. Usage:

answer := Ask("Your name?", "", nil)
answer := Ask("Your name?", "tom", nil)
answer := Ask("Your name?", "", nil, 3)

func Checkbox

func Checkbox(title string, options interface{}, defOpts []string, allowQuit ...bool) []string

Checkbox is alias of method MultiSelect()

func Choice added in v1.1.2

func Choice(title string, options interface{}, defOpt string, allowQuit ...bool) string

Choice is alias of method SingleSelect()

func Confirm

func Confirm(message string, defVal ...bool) bool

Confirm a question, returns bool

func GetHiddenInput added in v1.1.2

func GetHiddenInput(message string, trimmed bool) string

GetHiddenInput interactively prompts for input without echoing to the terminal. usage:

// askPassword
pwd := GetHiddenInput("Enter Password:")

func MultiSelect

func MultiSelect(title string, options interface{}, defOpts []string, allowQuit ...bool) []string

MultiSelect select multi of the options, returns selected option values. like SingleSelect(), but allow select multi option

func Prompt added in v1.1.2

func Prompt(ctx context.Context, query, defaultAnswer string) (string, error)

Prompt query and read user answer.

Usage:

answer,err := Prompt(context.TODO(), "your name?", "")

from package golang.org/x/tools/cmd/getgo

func Query added in v1.1.2

func Query(question, defVal string, fn func(ans string) error, maxTimes ...int) string

Query is alias of method Ask()

func ReadFirst

func ReadFirst(question string) (string, error)

ReadFirst read first char

func ReadInput added in v1.1.2

func ReadInput(question string) (string, error)

ReadInput read user input form Stdin

func ReadLine

func ReadLine(question string) (string, error)

ReadLine read one line from user input. Usage:

in := ReadLine("")
ans := ReadLine("your name?")

func ReadPassword added in v1.1.2

func ReadPassword(message ...string) string

ReadPassword from terminal

func SingleSelect added in v1.1.2

func SingleSelect(title string, options interface{}, defOpt string, allowQuit ...bool) string

SingleSelect select one of the options, returns selected option value map options:

	{
   // option value => option name
   'a' => 'chengdu',
   'b' => 'beijing'
	}

array options:

	{
   // only name, value will use index
   'chengdu',
   'beijing'
	}

func Unconfirmed added in v1.1.2

func Unconfirmed(message string, defVal ...bool) bool

Unconfirmed a question, returns bool

Types

type Interactive

type Interactive struct {
	Name string
}

Interactive

func New

func New(name string) *Interactive

type Option

type Option struct {
	Quit bool
	// default value
	DefVal string
}

Option

type Question

type Question struct {
	// Q the question string
	Q string
	// Func validate user input answer is right.
	// if not set, will only check answer is empty.
	Func func(ans string) error
	// DefVal default value
	DefVal string
	// MaxTimes maximum allowed number of errors, 0 is don't limited
	MaxTimes int
	// contains filtered or unexported fields
}

Question definition

func NewQuestion added in v1.1.2

func NewQuestion(q string, defVal ...string) *Question

NewQuestion instance. usage:

q := NewQuestion("Please input your name?")
val := q.Run().String()

func (*Question) Run added in v1.1.2

func (q *Question) Run() *Value

Run run and returns value

type RunFace added in v1.1.2

type RunFace interface {
	Run() *Value
}

RunFace for interact methods

type Select

type Select struct {
	// Title message for select. e.g "Your city?"
	Title string
	// Options the options data for select. allow: []int,[]string,map[string]string
	Options interface{}
	// DefOpt default option when not input answer
	DefOpt string
	// DefOpts use for `MultiSelect` is true
	DefOpts []string
	// DisableQuit option. if is false, will display "quit" option. default False
	DisableQuit bool
	// MultiSelect allow multi select. default False
	MultiSelect bool
	// contains filtered or unexported fields
}

Select definition

func NewSelect

func NewSelect(title string, options interface{}) *Select

NewSelect instance. usage:

s := NewSelect("Your city?", []string{"chengdu", "beijing"})
val := s.Run().String() // "1"

func (*Select) Run

func (s *Select) Run() *Value

Run select and receive use input answer

type StepHandler added in v1.1.2

type StepHandler func(ctx context.Context) error

StepHandler for steps run

type StepsRun added in v1.1.2

type StepsRun struct {

	// Steps step name and handler define.
	// {
	//	// step 1
	// 	func(ctx context.Context) { do something.}
	//	// step 2
	// 	func(ctx context.Context) { do something.}
	// }
	Steps []StepHandler
	// contains filtered or unexported fields
}

StepsRun follow the steps to run

func (*StepsRun) Err added in v1.1.2

func (s *StepsRun) Err() error

Err get error

func (*StepsRun) Run added in v1.1.2

func (s *StepsRun) Run()

Run all steps

func (*StepsRun) Stop added in v1.1.2

func (s *StepsRun) Stop()

Stop set stop run

type Value

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

Value data store

func (Value) Int

func (v Value) Int() (val int)

Int value

func (Value) IsEmpty

func (v Value) IsEmpty() bool

IsEmpty value

func (Value) Set added in v1.1.2

func (v Value) Set(val interface{})

Set val

func (Value) String

func (v Value) String() string

String value

func (Value) Strings added in v1.1.2

func (v Value) Strings() (ss []string)

Strings value

func (Value) Val

func (v Value) Val() interface{}

Val get

Jump to

Keyboard shortcuts

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