Documentation
¶
Overview ¶
Package interact collect some interactive methods for CLI
Index ¶
- Constants
- Variables
- func AnswerIsYes(defVal ...bool) bool
- func Ask(question, defVal string, fn func(ans string) error, maxTimes ...int) string
- func Checkbox(title string, options any, defOpts []string, allowQuit ...bool) []string
- func Choice(title string, options any, defOpt string, allowQuit ...bool) string
- func Confirm(message string, defVal ...bool) bool
- func GetHiddenInput(message string, trimmed bool) (string, error)
- func MultiSelect(title string, options any, defOpts []string, allowQuit ...bool) []string
- func NewUIConfirm(prompt string, def bool) *ui.Confirm
- func NewUIFakeBackend(events ...backend.Event) backend.Backend
- func NewUIInput(prompt string) *ui.Input
- func NewUIMultiSelect(prompt string, items []ui.Item) *ui.MultiSelect
- func NewUIPlainBackend() backend.Backend
- func NewUIReadlineBackend() backend.Backend
- func NewUISelect(prompt string, items []ui.Item) *ui.Select
- func NewUIStrictReadlineBackend() backend.Backend
- func Prompt(ctx context.Context, query, defaultAnswer string) (string, error)
- func Query(question, defVal string, fn func(ans string) error, maxTimes ...int) string
- func ReadFirst(question string) (string, error)
- func ReadInput(question string) (string, error)
- func ReadLine(question string) (string, error)
- func ReadPassword(question ...string) string
- func SelectOne(title string, options any, defOpt string, allowQuit ...bool) string
- func SingleSelect(title string, options any, defOpt string, allowQuit ...bool) string
- func Unconfirmed(message string, defVal ...bool) bool
- type Collector
- func (c *Collector) AddParam(p InputParameter) error
- func (c *Collector) AddParams(ps ...InputParameter) error
- func (c *Collector) MustParam(name string) InputParameter
- func (c *Collector) Param(name string) (InputParameter, bool)
- func (c *Collector) Results() maputil.Data
- func (c *Collector) Run() error
- type ComOptions
- type InputParameter
- type Interactive
- type ItemFace
- type Options
- type Question
- type RunFace
- type Select
- type SelectResult
- type StepHandler
- type StepsRun
- type UIBackend
- type UIConfirm
- type UIInput
- type UIItem
- type UIMultiSelect
- type UIResult
- type UISelect
- type Value
Constants ¶
const ( // OK success exit code OK = 0 // ERR error exit code ERR = 2 )
Variables ¶
var ( // ErrUIAborted reports that the current interaction was aborted. ErrUIAborted = ui.ErrAborted // ErrUINoTTY reports that a strict terminal backend requires a TTY. ErrUINoTTY = ui.ErrNoTTY // ErrUIInvalidState reports invalid ui component state. ErrUIInvalidState = ui.ErrInvalidState // ErrUINotImplemented reports a missing backend implementation. ErrUINotImplemented = ui.ErrNotImplemented )
Functions ¶
func AnswerIsYes ¶
AnswerIsYes check user inputted answer is right
Usage:
fmt.Print("are you OK?")
ok := AnswerIsYes()
ok := AnswerIsYes(true)
func Ask ¶
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 GetHiddenInput ¶
GetHiddenInput interactively prompts for input without echoing to the terminal.
Usage:
// askPassword
pwd := GetHiddenInput("Enter Password:")
func MultiSelect ¶
MultiSelect select multi of the options, returns selected option values. like SingleSelect(), but allow select multi option
func NewUIConfirm ¶
NewUIConfirm creates a ui.Confirm component.
func NewUIFakeBackend ¶
NewUIFakeBackend creates an in-memory backend for tests or scripted event flows.
func NewUIMultiSelect ¶
func NewUIMultiSelect(prompt string, items []ui.Item) *ui.MultiSelect
NewUIMultiSelect creates a ui.MultiSelect component.
func NewUIPlainBackend ¶
NewUIPlainBackend creates a line-based backend for ui components.
func NewUIReadlineBackend ¶
NewUIReadlineBackend creates a raw-terminal backend that falls back to plain mode on non-TTY input.
func NewUISelect ¶
NewUISelect creates a ui.Select component.
func NewUIStrictReadlineBackend ¶
NewUIStrictReadlineBackend creates a strict raw-terminal backend that requires a TTY.
func Prompt ¶
Prompt query and read user answer.
Usage:
answer, err := Prompt(context.Background(), "your name?", "")
from package golang.org/x/tools/cmd/getgo
func ReadLine ¶
ReadLine read one line from user input.
Usage:
in := ReadLine("")
ans, _ := ReadLine("your name?")
func SelectOne ¶
SelectOne 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 SingleSelect ¶
SingleSelect is alias of method SelectOne()
func Unconfirmed ¶
Unconfirmed a question, returns bool
Types ¶
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector information collector 信息收集者 cli input values collector
func (*Collector) AddParam ¶
func (c *Collector) AddParam(p InputParameter) error
AddParam to collector
func (*Collector) AddParams ¶
func (c *Collector) AddParams(ps ...InputParameter) error
AddParams definitions at once.
func (*Collector) MustParam ¶
func (c *Collector) MustParam(name string) InputParameter
MustParam get from collector
type ComOptions ¶
ComOptions struct
type InputParameter ¶
type InputParameter interface {
Type() string
Name() string
Desc() string
Value() structs.Value
Set(v string) error
Run() error
}
InputParameter interface
type Question ¶
type Question struct {
// Out output writer. default is cutypes.Output
Out io.Writer
// Q the question message
Q string
// Func validate user input answer is right.
// if not set, will only check the 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 ¶
NewQuestion instance.
Usage:
q := NewQuestion("Please input your name?")
ans := q.Run().String()
type Select ¶
type Select struct {
// Out output writer. default is cutypes.Output
Out io.Writer
// Title message for select. e.g "Your city?"
Title string
// Options the items data for select. allow: []int, []string, map[string]string
Options any
// 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
type SelectResult ¶
SelectResult data store
type StepsRun ¶
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
type UIMultiSelect ¶
type UIMultiSelect = ui.MultiSelect
UIMultiSelect aliases the new multi select component.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package backend defines the runtime abstraction used by interact/ui.
|
Package backend defines the runtime abstraction used by interact/ui. |
|
fake
Package fake provides an in-memory backend for interaction tests.
|
Package fake provides an in-memory backend for interaction tests. |
|
plain
Package plain provides a line-based backend for interact/ui.
|
Package plain provides a line-based backend for interact/ui. |
|
readline
Package readline provides a minimal raw-terminal event backend for interact/ui.
|
Package readline provides a minimal raw-terminal event backend for interact/ui. |
|
Package ui defines interactive component models built on top of a backend.
|
Package ui defines interactive component models built on top of a backend. |