ui

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnvColorDisabled added in v0.7.0

func EnvColorDisabled() bool

func EnvColorForced added in v0.7.0

func EnvColorForced() bool

func IsTerminal added in v0.7.0

func IsTerminal(stream IOStream) bool

IsTerminal returns true if the stream is a terminal.

func MatchAny added in v0.7.0

func MatchAny(p Prompt) bool

MatchAny is a matcher that matches any prompt.

Types

type Formatter added in v0.7.0

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

Formatter wraps strings in ANSI escape sequences.

func NewFormatter added in v0.7.0

func NewFormatter(enabled bool) *Formatter

func (*Formatter) Blue added in v0.7.0

func (f *Formatter) Blue(t string) string

func (*Formatter) Bluef added in v0.7.0

func (f *Formatter) Bluef(t string, args ...interface{}) string

func (*Formatter) Bold added in v0.7.0

func (f *Formatter) Bold(t string) string

func (*Formatter) Boldf added in v0.7.0

func (f *Formatter) Boldf(t string, args ...interface{}) string

func (*Formatter) Color added in v0.7.0

func (f *Formatter) Color(s string, style string) string

func (*Formatter) Cyan added in v0.7.0

func (f *Formatter) Cyan(t string) string

func (*Formatter) Cyanf added in v0.7.0

func (f *Formatter) Cyanf(t string, args ...interface{}) string

func (*Formatter) Failure added in v0.7.0

func (f *Formatter) Failure(t string) string

func (*Formatter) FailureIcon added in v0.7.0

func (f *Formatter) FailureIcon() string

func (*Formatter) Gray added in v0.7.0

func (f *Formatter) Gray(t string) string

func (*Formatter) Grayf added in v0.7.0

func (f *Formatter) Grayf(t string, args ...interface{}) string

func (*Formatter) Green added in v0.7.0

func (f *Formatter) Green(t string) string

func (*Formatter) Greenf added in v0.7.0

func (f *Formatter) Greenf(t string, args ...interface{}) string

func (*Formatter) Info added in v0.7.0

func (f *Formatter) Info(t string) string

func (*Formatter) InfoIcon added in v0.7.0

func (f *Formatter) InfoIcon() string

func (*Formatter) Magenta added in v0.7.0

func (f *Formatter) Magenta(t string) string

func (*Formatter) Magentaf added in v0.7.0

func (f *Formatter) Magentaf(t string, args ...interface{}) string

func (*Formatter) Red added in v0.7.0

func (f *Formatter) Red(t string) string

func (*Formatter) Redf added in v0.7.0

func (f *Formatter) Redf(t string, args ...interface{}) string

func (*Formatter) Success added in v0.7.0

func (f *Formatter) Success(t string) string

func (*Formatter) SuccessIcon added in v0.7.0

func (f *Formatter) SuccessIcon() string

func (*Formatter) Underline added in v0.7.0

func (f *Formatter) Underline(t string) string

func (*Formatter) Underlinef added in v0.7.0

func (f *Formatter) Underlinef(t string, args ...interface{}) string

func (*Formatter) Warning added in v0.7.0

func (f *Formatter) Warning(t string) string

func (*Formatter) WarningIcon added in v0.7.0

func (f *Formatter) WarningIcon() string

func (*Formatter) Yellow added in v0.7.0

func (f *Formatter) Yellow(t string) string

func (*Formatter) Yellowf added in v0.7.0

func (f *Formatter) Yellowf(t string, args ...interface{}) string

type IOStream added in v0.7.0

type IOStream interface {
	io.Reader
	io.Writer
	Fd() uintptr
	String() string
	Lines() []string
}

IOStream represents an input or output stream.

type IOStreams added in v0.7.0

type IOStreams struct {
	// os.Stdin (or mock when unit testing)
	In IOStream
	// os.Stdout (or mock when unit testing)
	Out IOStream
	// os.Stderr (or mock when unit testing)
	Err IOStream
	// contains filtered or unexported fields
}

Container for the three main CLI I/O streams.

func NewIOStreams added in v0.7.0

func NewIOStreams() *IOStreams

NewIOStreams returns the default IOStreams containing os.Stdin, os.Stdout, and os.Stderr.

func NewTestIOStreams added in v0.7.0

func NewTestIOStreams() *IOStreams

NewTestIOStreams returns an IOStreams with mock in/out/err values for testing.

func (*IOStreams) Formatter added in v0.7.0

func (s *IOStreams) Formatter() *Formatter

Formatter returns a ANSI string formatter.

func (*IOStreams) IsColorEnabled added in v0.7.0

func (s *IOStreams) IsColorEnabled() bool

IsColorEnabled returns true if color output is enabled.

func (*IOStreams) IsInteractive added in v0.7.0

func (s *IOStreams) IsInteractive() bool

IsInteractive returns true if running interactively. Will be false if either (a) std in/out is not a TTY, or (b) the user has explicitly requested not to be prompted.

func (*IOStreams) IsStderrTTY added in v0.7.0

func (s *IOStreams) IsStderrTTY() bool

IsStderrTTY returns true if [IOStreams.Err] is a TTY.

func (*IOStreams) IsStdinTTY added in v0.7.0

func (s *IOStreams) IsStdinTTY() bool

IsStdinTTY returns true if [IOStreams.In] is a TTY.

func (*IOStreams) IsStdoutTTY added in v0.7.0

func (s *IOStreams) IsStdoutTTY() bool

IsStdoutTTY returns true if [IOStreams.Out] is a TTY.

func (*IOStreams) ProgressIndicator added in v0.7.0

func (s *IOStreams) ProgressIndicator() *ProgressIndicator

ProgressIndicator returns a new progress indicator.

func (*IOStreams) Prompter added in v0.7.0

func (s *IOStreams) Prompter() *SurveyPrompter

Prompter returns the default, survey based prompter.

func (*IOStreams) SetColorEnabled added in v0.7.0

func (s *IOStreams) SetColorEnabled(v bool)

SetColorEnabled sets whether color is enabled.

func (*IOStreams) SetInteractive added in v0.7.0

func (s *IOStreams) SetInteractive(v bool)

SetInteractive explicitly sets whether this is an interactive session.

func (*IOStreams) SetStderrTTY added in v0.7.0

func (s *IOStreams) SetStderrTTY(isTTY bool)

SetStderrTTY explicitly flags [IOStreams.Err] as a TTY.

func (*IOStreams) SetStdinTTY added in v0.7.0

func (s *IOStreams) SetStdinTTY(isTTY bool)

SetStdinTTY explicitly flags [IOStreams.In] as a TTY.

func (*IOStreams) SetStdoutTTY added in v0.7.0

func (s *IOStreams) SetStdoutTTY(isTTY bool)

SetStdoutTTY explicitly flags [IOStreams.Out] as a TTY.

type Matcher added in v0.7.0

type Matcher func(p Prompt) bool

Matcher is function that matches prompts.

func MatchConfirm added in v0.7.0

func MatchConfirm(msg string) Matcher

MatchConfirm returns a matcher that matches confirm prompts.

func MatchInput added in v0.7.0

func MatchInput(msg string) Matcher

MatchInput returns a matcher that matches input prompts.

func MatchMultiSelect added in v0.7.0

func MatchMultiSelect(msg string) Matcher

MatchMultiSelect returns a matcher that matches multi-select prompts.

func MatchSelect added in v0.7.0

func MatchSelect(msg string) Matcher

MatchSelect returns a matcher that matches select prompts.

type ProgressIndicator added in v0.7.0

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

func NewProgressIndicator added in v0.7.0

func NewProgressIndicator(ios *IOStreams) *ProgressIndicator

func (*ProgressIndicator) IsEnabled added in v0.7.0

func (pi *ProgressIndicator) IsEnabled() bool

func (*ProgressIndicator) Start added in v0.7.0

func (pi *ProgressIndicator) Start()

func (*ProgressIndicator) StartWithLabel added in v0.7.0

func (pi *ProgressIndicator) StartWithLabel(label string)

func (*ProgressIndicator) Stop added in v0.7.0

func (pi *ProgressIndicator) Stop()

type Prompt added in v0.7.0

type Prompt struct {
	Type    PromptType
	Message string
	Help    string
	Value   any
	Options any
}

func (Prompt) String added in v0.7.0

func (p Prompt) String() string

type PromptOpt added in v0.8.0

type PromptOpt func(params *PromptParams)

PromptOpt allows setting optional prompt params.

func WithHelp added in v0.8.0

func WithHelp(text string) PromptOpt

WithHelp sets the help text for a prompt.

func WithValidation added in v0.8.0

func WithValidation(rules string) PromptOpt

WithValidation sets the validation rules for a prompt.

type PromptParams added in v0.8.0

type PromptParams struct {
	// Help text to show when the user presses "?".
	Help string
	// One or more validation rules to be passed
	// to [validate.KeyVal].
	ValidationRules string
}

PromptParams holds optional params for Prompter methods.

func GetPromptParams added in v0.8.0

func GetPromptParams(opts ...PromptOpt) *PromptParams

GetPromptParams returns the params for the given prompt ops.

type PromptType added in v0.7.0

type PromptType int
var (
	PromptTypeUnknown     PromptType = 0
	PromptTypeConfirm     PromptType = 1
	PromptTypeInput       PromptType = 2
	PromptTypeMultiSelect PromptType = 3
	PromptTypeSelect      PromptType = 4
)

func (PromptType) String added in v0.7.0

func (pt PromptType) String() string

type Prompter

type Prompter interface {
	// Confirm prompts for a boolean yes/no value.
	Confirm(msg string, value bool, opts ...PromptOpt) (bool, error)
	// Input prompts for single string value.
	Input(msg string, value string, opts ...PromptOpt) (string, error)
	// MultiSelect prompts for a slice of string values w/ a fixed set of options.
	MultiSelect(msg string, options []string, values []string, opts ...PromptOpt) ([]string, error)
	// Select prompts for single string value w/ a fixed set of options.
	Select(msg string, options []string, value string, opts ...PromptOpt) (string, error)
}

Prompter is an interface for types that prompt for user input.

type Responder added in v0.7.0

type Responder func(prompt Prompt) (response any, err error)

Responder is a function that returns stubbed prompt responses.

func RespondBool added in v0.7.0

func RespondBool(response bool) Responder

RespondBool creates a responder that returns the given bool.

func RespondDefault added in v0.9.0

func RespondDefault() Responder

RespondDefault creates a responder that returns the prompt's default value.

func RespondError added in v0.7.0

func RespondError(err error) Responder

RespondError creates a responder that returns the given error.

func RespondString added in v0.7.0

func RespondString(response string) Responder

RespondString creates a responder that returns the given string.

func RespondStringSlice added in v0.7.0

func RespondStringSlice(response []string) Responder

RespondStringSlice creates a responder that returns the given slice of strings.

type Stub added in v0.7.0

type Stub struct {
	Matched   bool
	Matcher   Matcher
	Responder Responder
}

Stub is a stubbed Prompter invocation.

type StubPrompter added in v0.7.0

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

StubPrompter is an implementation of Prompter that invokes stubbed prompts.

func NewStubPrompter added in v0.7.0

func NewStubPrompter(ios *IOStreams) *StubPrompter

func (*StubPrompter) Confirm added in v0.7.0

func (sp *StubPrompter) Confirm(msg string, value bool, opts ...PromptOpt) (bool, error)

Confirm prompts for a boolean yes/no value.

func (*StubPrompter) Input added in v0.7.0

func (sp *StubPrompter) Input(msg string, value string, opts ...PromptOpt) (string, error)

Input prompts for single string value.

func (*StubPrompter) MultiSelect added in v0.7.0

func (sp *StubPrompter) MultiSelect(msg string, options []string, values []string, opts ...PromptOpt) ([]string, error)

MultiSelect prompts for a slice of string values w/ a fixed set of options.

func (*StubPrompter) RegisterStub added in v0.7.0

func (sp *StubPrompter) RegisterStub(matcher Matcher, responder Responder) *StubPrompter

RegisterStub registers a new stub for the given matcher/responder pair.

func (*StubPrompter) Select added in v0.7.0

func (sp *StubPrompter) Select(msg string, options []string, value string, opts ...PromptOpt) (string, error)

Select prompts for single string value w/ a fixed set of options.

func (*StubPrompter) VerifyStubs added in v0.7.0

func (sp *StubPrompter) VerifyStubs(test testable)

VerifyStubs fails the test if there are unmatched stubs.

type SurveyPrompter

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

SurveyPrompter is a light wrapper around the survey(https://github.com/go-survey/survey) library.

func NewSurveyPrompter

func NewSurveyPrompter(ios *IOStreams) *SurveyPrompter

func (*SurveyPrompter) Confirm

func (p *SurveyPrompter) Confirm(msg string, value bool, opts ...PromptOpt) (bool, error)

Confirm prompts for a boolean yes/no value.

func (*SurveyPrompter) Input

func (p *SurveyPrompter) Input(msg string, value string, opts ...PromptOpt) (string, error)

Input prompts for single string value.

func (*SurveyPrompter) MultiSelect

func (p *SurveyPrompter) MultiSelect(
	msg string, options []string, values []string, opts ...PromptOpt,
) ([]string, error)

MultiSelect prompts for a slice of string values w/ a fixed set of options.

func (*SurveyPrompter) Select

func (p *SurveyPrompter) Select(
	msg string, options []string, value string, opts ...PromptOpt,
) (string, error)

Select prompts for single string value w/ a fixed set of options.

type UserInterface added in v0.7.0

type UserInterface struct {
	*Formatter

	ProgressIndicator *ProgressIndicator
	Prompter          Prompter
	// contains filtered or unexported fields
}

UserInterface is a high level abstraction for rendering terminal UIs. It supports stubbing interactive prompts and rendering formatted text to os.Stdout.

func NewUserInterface added in v0.7.0

func NewUserInterface(ios *IOStreams) *UserInterface

NewUserInterface returns a new UserInterface.

func (*UserInterface) Confirm added in v0.7.0

func (ui *UserInterface) Confirm(msg string, value bool, opts ...PromptOpt) (bool, error)

Confirm prompts for a boolean yes/no value.

func (*UserInterface) Err added in v0.7.0

func (ui *UserInterface) Err(s string, args ...any)

func (*UserInterface) Input added in v0.7.0

func (ui *UserInterface) Input(msg string, value string, opts ...PromptOpt) (string, error)

Input prompts for single string value.

func (*UserInterface) IsStubbed added in v0.7.0

func (ui *UserInterface) IsStubbed() bool

IsStubbed returns true if the executor is configured for stubbing.

func (*UserInterface) MultiSelect added in v0.7.0

func (ui *UserInterface) MultiSelect(msg string, options []string, values []string, opts ...PromptOpt) ([]string, error)

MultiSelect prompts for a slice of string values w/ a fixed set of options.

func (*UserInterface) Out added in v0.7.0

func (ui *UserInterface) Out(s string, args ...any)

func (*UserInterface) RegisterStub added in v0.7.0

func (ui *UserInterface) RegisterStub(matcher Matcher, responder Responder) *UserInterface

RegisterStub registers a new stub for the given matcher/responder pair.

func (*UserInterface) Select added in v0.7.0

func (ui *UserInterface) Select(msg string, options []string, value string, opts ...PromptOpt) (string, error)

Select prompts for single string value w/ a fixed set of options.

func (*UserInterface) VerifyStubs added in v0.7.0

func (ui *UserInterface) VerifyStubs(t testable)

VerifyStubs fails the test if there are unmatched stubs.

func (*UserInterface) WithStubbing added in v0.7.0

func (ui *UserInterface) WithStubbing() *UserInterface

WithStubbing configures stubbing and returns the receiver.

Jump to

Keyboard shortcuts

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