interact

package
v2.3.4 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2020 License: MIT Imports: 15 Imported by: 3

README

Interactive Util

command-line interactive util methods

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

Select & Choice

Usage:

package main

import (
	"fmt"
	"os/exec"

	"github.com/gookit/color"
	"github.com/gookit/gcli/v2/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:

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

func Choice(title string, options interface{}, 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 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

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

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 added in v2.0.10

func SelectOne(title string, options interface{}, 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 interface{}, 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 Interactive

type Interactive struct {
	Name string
}

Interactive definition

func New

func New(name string) *Interactive

New Interactive instance

type Option

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

Option definition

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

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 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 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"})
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 added in v2.3.0

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

SelectResult data store

func (*SelectResult) Key added in v2.3.0

func (sv *SelectResult) Key() interface{}

Key value get

func (*SelectResult) KeyString added in v2.3.0

func (sv *SelectResult) KeyString() string

KeyString get

func (*SelectResult) KeyStrings added in v2.3.0

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

KeyStrings get

func (*SelectResult) WithKey added in v2.3.0

func (sv *SelectResult) WithKey(key interface{}) *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 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

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

Set val

func (Value) String

func (v Value) String() string

String value

func (Value) Strings

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