ui

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

package ui contains functionality for prompting for input, colorizing output, and creating animations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// AbortedCause represents types of errors where the user aborted the operation
	AbortedCause = errors.NewCause(errors.BadRequestCategory, "user_aborted")

	// NotAttachedCause represents types of errors that are caused when a ui
	// element is attempted to be used that are not supported by the terminal
	NotAttachedCause = errors.NewCause(errors.BadRequestCategory, "not_attached")

	// CannotDisplayCause represents types of errors that are caused when a
	// provided value cannot be displayed by a ui component
	CannotDisplayCause = errors.NewCause(errors.BadRequestCategory, "cannot_display")

	// ErrAborted is an error when a user aborts while filling out a prompt
	ErrAborted = errors.New(AbortedCause, "Aborted")

	// ErrCantDisplay happens when a value is given to a UI Component that
	// cannot be displayed (e.g. isn't a string or doesn't have a String()
	// method)
	ErrCantDisplay = errors.New(CannotDisplayCause, "Cannot display value, expected a string or fmt.Stringer")
)
View Source
var (
	// Warn represents a warning notification to the user
	Warn = NotificationType{
		Symbol: "⚠",
		Text:   "Warning",
		Color:  ansiterm.Yellow,
	}

	// Error represents an error notification to the user
	Error = NotificationType{
		Symbol: "✗",
		Text:   "Error",
		Color:  ansiterm.Red,
	}

	// Remember represents a reminder notification to the user
	Remember = NotificationType{
		Symbol: "‼",
		Text:   "Remember",
		Color:  ansiterm.Green,
	}
)

Functions

func Attached

func Attached() bool

Attached return a boolean representing whether or not the current session is attached to a terminal or not.

Types

type Call

type Call struct {
	Name string
	Args []interface{}
}

Call is a helper struct to record types of calls

type Details

type Details map[string]interface{}

Details is used to colorize a series of key and value pairs when listing colors in the Stdout

type Mock

type Mock struct {
	Calls []*Call
}

Mock is a fake UI that will track what it was called with

func (*Mock) Confirm

func (m *Mock) Confirm(question string) error

Confirm from the UI interface

func (*Mock) Details

func (m *Mock) Details(details Details) error

Details from the UI interface

func (*Mock) Notify

func (m *Mock) Notify(t NotificationType, msg string, args ...interface{}) error

Notify from the UI interface

func (*Mock) Question

func (m *Mock) Question(question string, validator promptui.ValidateFunc) (string, error)

Question from the UI interface

func (*Mock) Secret

func (m *Mock) Secret(question string, validator promptui.ValidateFunc) (string, error)

Secret from the UI interface

func (*Mock) Table

func (m *Mock) Table(header TableHeader, body TableBody) error

Table from the UI interface

func (*Mock) Template

func (m *Mock) Template(t string, args interface{}) error

Template from the UI interface

type NotificationType

type NotificationType struct {
	Symbol string
	Text   string
	Color  ansiterm.Color
}

NotificationType represents a type of notification such as a warning or error

type Stdout

type Stdout struct {
	Attached   bool
	Animations bool
	Colors     bool
}

Stdout is an implementation of UI that prints to stdout while taking into account the state of a users terminal.

func NewStdout

func NewStdout(colors bool, animations bool) *Stdout

NewStdout returns a configured Stdout struct

func (*Stdout) CanColorize

func (u *Stdout) CanColorize() bool

CanColorized returns whether or not colorization can be supported

func (*Stdout) Confirm

func (u *Stdout) Confirm(question string) error

Confirm prompts the user with a confirmation dialog

Confirmation dialogs are usually used to ask the user if they really want to perform an action. If stdout is not attached to a terminal then an error is returned.

func (*Stdout) Details

func (u *Stdout) Details(details Details) error

Details prints the provided details to the Stdout. Details is a Stdout Component with labelled key and value pairs.

func (*Stdout) Notify

func (u *Stdout) Notify(t NotificationType, msg string, args ...interface{}) error

Notify is used to notify the user about some piece of information including error messages or warning.

func (*Stdout) Question

func (u *Stdout) Question(question string, validator promptui.ValidateFunc) (string, error)

Question promps the user to answer a terminal question

func (*Stdout) Secret

func (u *Stdout) Secret(question string, validator promptui.ValidateFunc) (string, error)

Secret prompts the user to answer a terminal question that will be masked

func (*Stdout) Table

func (u *Stdout) Table(header TableHeader, body TableBody) error

Table prints the provided header and body to the Stdout.

func (*Stdout) Template

func (u *Stdout) Template(t string, args interface{}) error

Template takes in a text/template style template and renders it

type TableBody

type TableBody [][]string

TableBody is used with the `Table` function. It is a slice of string slices that contain the body of your table. E.g. [

[ "Ben", "100", "jdkslajdklas" ],
[ "Ian", "100", "jdklsajdklsa" ],
[ "Justin", "100", "kljdaslkdaskjl" ],

]

type TableHeader

type TableHeader []string

TableHeader is used with the `Table` function. It will print the header of your table in bold. E.g. [ "Name", "Number", "Gibberish" ]

type UI

type UI interface {
	Notify(t NotificationType, msg string, args ...interface{}) error
	Secret(question string, validator promptui.ValidateFunc) (string, error)
	Question(question string, validator promptui.ValidateFunc) (string, error)
	Confirm(question string) error
	Table(header TableHeader, body TableBody) error
	Template(t string, args interface{}) error
	Details(details Details) error
}

UI makes it easy to present prompts, animation, and other ui enhancements

Jump to

Keyboard shortcuts

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