interact

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: May 4, 2026 License: MIT Imports: 26 Imported by: 0

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

View Source
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

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 any, defOpts []string, allowQuit ...bool) []string

Checkbox is alias of method MultiSelect()

func Choice

func Choice(title string, options any, defOpt string, allowQuit ...bool) string

Choice is alias of method SelectOne()

func Confirm

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

Confirm a question, returns bool

func GetHiddenInput

func GetHiddenInput(message string, trimmed bool) (string, error)

GetHiddenInput interactively prompts for input without echoing to the terminal.

Usage:

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

func MultiSelect

func MultiSelect(title string, options any, defOpts []string, allowQuit ...bool) []string

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

func NewUIConfirm

func NewUIConfirm(prompt string, def bool) *ui.Confirm

NewUIConfirm creates a ui.Confirm component.

func NewUIFakeBackend

func NewUIFakeBackend(events ...backend.Event) backend.Backend

NewUIFakeBackend creates an in-memory backend for tests or scripted event flows.

func NewUIInput

func NewUIInput(prompt string) *ui.Input

NewUIInput creates a ui.Input component.

func NewUIMultiSelect

func NewUIMultiSelect(prompt string, items []ui.Item) *ui.MultiSelect

NewUIMultiSelect creates a ui.MultiSelect component.

func NewUIPlainBackend

func NewUIPlainBackend() backend.Backend

NewUIPlainBackend creates a line-based backend for ui components.

func NewUIReadlineBackend

func NewUIReadlineBackend() backend.Backend

NewUIReadlineBackend creates a raw-terminal backend that falls back to plain mode on non-TTY input.

func NewUISelect

func NewUISelect(prompt string, items []ui.Item) *ui.Select

NewUISelect creates a ui.Select component.

func NewUIStrictReadlineBackend

func NewUIStrictReadlineBackend() backend.Backend

NewUIStrictReadlineBackend creates a strict raw-terminal backend that requires a TTY.

func Prompt

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

Prompt query and read user answer.

Usage:

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

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

func Query

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

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

func ReadPassword(question ...string) string

ReadPassword from terminal

func SelectOne

func SelectOne(title string, options any, defOpt string, allowQuit ...bool) string

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

func SingleSelect(title string, options any, defOpt string, allowQuit ...bool) string

SingleSelect is alias of method SelectOne()

func Unconfirmed

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

Unconfirmed a question, returns bool

Types

type Collector

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

Collector information collector 信息收集者 cli input values collector

func NewCollector

func NewCollector() *Collector

NewCollector instance

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

func (*Collector) Param

func (c *Collector) Param(name string) (InputParameter, bool)

Param get from collector

func (*Collector) Results

func (c *Collector) Results() maputil.Data

Results for collector

func (*Collector) Run

func (c *Collector) Run() error

Run collector

type ComOptions

type ComOptions struct {
	// ValidFn check input value
	ValidFn func(val any) (any, error)
}

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 Interactive

type Interactive struct {
	Name string
}

Interactive definition

func New

func New(name string) *Interactive

New Interactive instance

type ItemFace

type ItemFace interface {
	Name() string
	Value() string
}

ItemFace for interact methods

type Options

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

Options definition

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

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

NewQuestion instance.

Usage:

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

func (*Question) Run

func (q *Question) Run() *Value

Run and returns value

type RunFace

type RunFace interface {
	Run() *Value
}

RunFace for interact methods

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

func NewSelect

func NewSelect(title string, items any) *Select

NewSelect instance.

- items allow: []int, []string, map[string]string

Usage:

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

func (*Select) EnableMulti

func (s *Select) EnableMulti() *Select

EnableMulti select

func (*Select) Run

func (s *Select) Run() *SelectResult

Run select and receive use input answer

type SelectResult

type SelectResult struct {
	Value // V the select value(s)
	// K the select key(s)
	K Value
}

SelectResult data store

func (*SelectResult) Key

func (sv *SelectResult) Key() any

Key value get

func (*SelectResult) KeyString

func (sv *SelectResult) KeyString() string

KeyString get

func (*SelectResult) KeyStrings

func (sv *SelectResult) KeyStrings() []string

KeyStrings get

func (*SelectResult) WithKey

func (sv *SelectResult) WithKey(key any) *SelectResult

WithKey value

type StepHandler

type StepHandler func(ctx context.Context) error

StepHandler for steps run

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

func (*StepsRun) Err

func (s *StepsRun) Err() error

Err get error

func (*StepsRun) Run

func (s *StepsRun) Run()

Run all steps

func (*StepsRun) Stop

func (s *StepsRun) Stop()

Stop set stop run

type UIBackend

type UIBackend = backend.Backend

UIBackend aliases the new backend abstraction.

type UIConfirm

type UIConfirm = ui.Confirm

UIConfirm aliases the new confirm component.

type UIInput

type UIInput = ui.Input

UIInput aliases the new input component.

type UIItem

type UIItem = ui.Item

UIItem aliases the new ui item type.

type UIMultiSelect

type UIMultiSelect = ui.MultiSelect

UIMultiSelect aliases the new multi select component.

type UIResult

type UIResult = ui.Result

UIResult aliases the new ui result type.

type UISelect

type UISelect = ui.Select

UISelect aliases the new select component.

type Value

type Value = structs.Value

Value alias of structs.Value

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.

Jump to

Keyboard shortcuts

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