std

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Source: https://github.com/ukautz/clif/blob/v1/input.go

Source: https://github.com/ukautz/clif/blob/v1/output.go

Index

Constants

This section is empty.

Variables

View Source
var (
	RenderAskQuestion = func(question string) string {
		return color.Question.Sprint(strings.TrimRight(question, " "))
	}
	RenderInputRequiredError = fmt.Errorf("Input required")
)
View Source
var ConfirmNoRegex = regexp.MustCompile(`^(?i)no?$`)

ConfirmNoRegex is the regular expression used to check if the user replied negative

View Source
var ConfirmRejection = fmt.Sprintf("%s\n\n", color.Warn.Sprint(`Please respond with "yes" or "no"`))

ConfirmRejection is the message replied to the user if she does not answer with "yes", "y", "no" or "n" (case insensitive)

View Source
var ConfirmYesRegex = regexp.MustCompile(`^(?i)y(es)?$`)

ConfirmYesRegex is the regular expression used to check if the user replied positive

View Source
var RenderChooseOption = func(key, value string, size int) string {
	return fmt.Sprintf("  %-"+fmt.Sprintf("%d", size+1)+"s %s\n", color.Notice.Sprint(key+")"), color.Secondary.Sprint(value))
}

RenderChooseOption is the method used by default input `Choose()` method to to render a singular choice into a string. Can be overwritten at users discretion.

View Source
var RenderChooseQuery = func() string {
	return "Choose: "
}

RenderChooseQuery is the method used by default input `Choose()` method to to render the query prompt choice (after the choices) into a string. Can be overwritten at users discretion.

View Source
var RenderChooseQuestion = func(question string) string {
	return question + "\n"
}

RenderChooseQuestion is the method used by default input `Choose()` method to to render the question (displayed before listing the choices) into a string. Can be overwritten at users discretion.

Functions

func InputAny

func InputAny(s string) error

func InputEmptyOk

func InputEmptyOk(s string) error

Types

type DefaultInput

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

DefaultInput is the default used input implementation

func NewDefaultInput

func NewDefaultInput(in io.Reader, out Output) *DefaultInput

NewDefaultInput constructs a new default input implementation on given io reader (if nil, fall back to `os.Stdin`). Requires Output for issuing questions to user.

func (*DefaultInput) Ask

func (i *DefaultInput) Ask(question string, check func(string) error, _default ...string) string

func (*DefaultInput) Choose

func (i *DefaultInput) Choose(question string, choices map[string]string, _default ...string) string

func (*DefaultInput) Confirm

func (i *DefaultInput) Confirm(question string, _default ...bool) bool

type DefaultOutput

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

DefaultOutput is the default used output type

func NewDefaultOutput

func NewDefaultOutput(io io.Writer) *DefaultOutput

NewOutput generates a new (default) output with provided io writer (if nil then `os.Stdout` is used) and a formatter

func (*DefaultOutput) Printf

func (o *DefaultOutput) Printf(msg string, args ...any)

func (*DefaultOutput) Sprintf

func (o *DefaultOutput) Sprintf(msg string, args ...any) string

func (*DefaultOutput) Writer

func (o *DefaultOutput) Writer() io.Writer

type Input

type Input interface {

	// Ask prints question to user and then reads user input and returns as soon
	// as it's non empty or queries again until it is
	Ask(question string, check func(string) error) string

	// AskRegex prints question to user and then reads user input, compares it
	// against regex and return if matching or queries again until it does
	AskRegex(question string, rx *regexp.Regexp) string

	// Choose renders choices for user and returns what was choosen
	Choose(question string, choices map[string]string) string

	// Confirm prints question to user until she replies with "yes", "y", "no" or "n"
	Confirm(question string) bool
}

Input is an interface for input helping. It provides shorthand methods for often used CLI interactions.

type Output

type Output interface {

	// Printf applies format (renders styles) and writes to output
	Printf(msg string, args ...any)

	// Sprintf applies format (renders styles) and returns as string
	Sprintf(msg string, args ...any) string

	// Writer returns the `io.Writer` used by this output
	Writer() io.Writer
}

Output is interface for

Jump to

Keyboard shortcuts

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