interact

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2022 License: MIT Imports: 18 Imported by: 2

README

Interactive

command-line interactive util methods

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

GoDoc

Please see https://pkg.go.dev/github.com/gookit/gcli/v3/interact

Install

go get github.com/gookit/gcli/v3/interact

Select & Choice

Usage:

package main

import (
	"fmt"
	"os/exec"

	"github.com/gookit/color"
	"github.com/gookit/gcli/v3/interact"
)

func main() {
	color.Green.Println("This's An Select Demo")
	fmt.Println("----------------------------------------------------------")

	ans := interact.SelectOne(
		"Your city name(use string slice/array)?",
		[]string{"chengdu", "beijing", "shanghai"},
		"",
	)
	color.Info.Println("your select is:", ans)
	fmt.Println("----------------------------------------------------------")

	ans1 := interact.Choice(
		"Your age(use int slice/array)?",
		[]int{23, 34, 45},
		"",
	)
	color.Info.Println("your select is:", ans1)

	fmt.Println("----------------------------------------------------------")

	ans2 := interact.SingleSelect(
		"Your city name(use map)?",
		map[string]string{"a": "chengdu", "b": "beijing", "c": "shanghai"},
		"a",
	)
	color.Info.Println("your select is:", ans2)

	s := interact.NewSelect("Your city", []string{"chengdu", "beijing", "shanghai"})
	s.DefOpt = "2"
	r := s.Run()
	color.Info.Println("your select key:", r.K.String())
	color.Info.Println("your select val:", r.String())
}

Preview:

Refers

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 (
	Input  io.Reader = os.Stdin
	Output io.Writer = os.Stdout
)

the global input output stream

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

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 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 ResetIO added in v3.1.0

func ResetIO()

ResetIO stream

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 SetInput added in v3.1.0

func SetInput(in io.Reader)

SetInput stream

func SetOutput added in v3.1.0

func SetOutput(out io.Writer)

SetOutput stream

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 added in v3.1.0

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

Collector information collector 信息收集者

func NewCollector added in v3.1.0

func NewCollector() *Collector

func (*Collector) Run added in v3.1.0

func (c *Collector) Run() error

type ComOptions added in v3.1.0

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

ComOptions struct

type Interactive

type Interactive struct {
	Name string
}

Interactive definition

func New

func New(name string) *Interactive

New Interactive instance

type Options added in v3.1.0

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

Options definition

type Question

type Question struct {
	// Q the question message
	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

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

Usage:

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

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 Value

type Value = structs.Value

Value alias of structs.Value

Jump to

Keyboard shortcuts

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