Documentation
¶
Index ¶
- Variables
- func IsNo(s string) bool
- func IsYes(s string) bool
- func SStyled(content string, s ...Styler) string
- type Cell
- type Col
- type Color
- type InteractiveSession
- func (i *InteractiveSession) Ask(prompt string, validators ...ValidationFunc) string
- func (i *InteractiveSession) AskFromTable(prompt string, choices map[string]string, def string) string
- func (i *InteractiveSession) AskPassword(validators ...ValidationFunc) string
- func (i *InteractiveSession) AskPasswordPrompt(prompt string, validators ...ValidationFunc) string
- func (i *InteractiveSession) AskWithDefault(prompt string, defaultChoice string, validators ...ValidationFunc) string
- func (i *InteractiveSession) AskWithHint(prompt string, hint string, validators ...ValidationFunc) string
- func (i *InteractiveSession) AskYesNo(prompt string, defaultChoice string) string
- func (i *InteractiveSession) Error(format string, args ...interface{})
- func (i *InteractiveSession) Pause()
- func (i *InteractiveSession) PauseWithPrompt(format string, args ...interface{})
- func (i *InteractiveSession) Reset()
- func (i *InteractiveSession) Say(format string, args ...interface{}) *InteractiveSession
- func (i *InteractiveSession) Warn(format string, args ...interface{}) *InteractiveSession
- type Justification
- type Progress
- func NewIncrementalProgressBar(steps int, format string, args ...interface{}) *Progress
- func NewLoadingMessage(message string, spinner Spinner, delay time.Duration) *Progress
- func NewProgressBar(format string, args ...interface{}) *Progress
- func NewProgressSpinner(format string, args ...interface{}) *Progress
- type Row
- type SessionOption
- type Spinner
- type Style
- type Styler
- type Table
- func (t *Table) AddRow(rowStrings ...string) *Table
- func (t *Table) AddStyledRow(cells ...Cell) *Table
- func (t *Table) AsString() string
- func (t *Table) ColumnHeaderStyles(styles ...*Style) *Table
- func (t *Table) ColumnHeaders(headers ...string) *Table
- func (t *Table) ColumnStyles(styles ...*Style) *Table
- func (t *Table) Justification(cellJustifications ...Justification) *Table
- func (t *Table) SetWriter(w io.Writer)
- func (t *Table) Show()
- func (t *Table) ShowPage(n int)
- func (t *Table) Title(s string, styles ...Styler) *Table
- type TableOption
- type Textstyle
- type Title
- type ValidationFunc
Constants ¶
This section is empty.
Variables ¶
var ( // Colors Black = Color{30, 39} Red = Color{31, 39} Green = Color{32, 39} Yellow = Color{33, 39} Blue = Color{34, 39} Magenta = Color{35, 39} Cyan = Color{36, 39} White = Color{37, 39} Default = Color{39, 39} // Shortcut Colors K = Color{30, 39} R = Color{31, 39} G = Color{32, 39} Y = Color{33, 39} B = Color{34, 39} M = Color{35, 39} C = Color{36, 39} W = Color{37, 39} Def = Color{39, 39} // Textstyles Bold = Textstyle{1, 22} Italic = Textstyle{3, 23} Underline = Textstyle{4, 24} )
Functions ¶
Types ¶
type Cell ¶
type Cell struct {
// contains filtered or unexported fields
}
Cell represents a cell in the table. Most often you'll create a cell using StyledCell in conjuction with AddStyledRow
func StyledCell ¶
StyledCell returns a new cell with a custom style for use with AddStyledRow
type Col ¶
type Col struct {
// contains filtered or unexported fields
}
Col is a column of a table. Use ColumnHeaders, ColumnStyles, etc. to adjust default styling and header properties. You can always override a particular cell in a column by passing in a different Cell style when you AddStyledRow.
type Color ¶
type Color struct {
// contains filtered or unexported fields
}
Color represents a ANSI-coded color style for text
func Background ¶
Background returns a style that sets the background to the appropriate color
type InteractiveSession ¶
type InteractiveSession struct { Prompt string Default string ValHint string // contains filtered or unexported fields }
InteractiveSession creates a system for collecting user input in response to questions and choices
func NewInteractiveSession ¶
func NewInteractiveSession(opts ...SessionOption) *InteractiveSession
NewInteractiveSession returns a new InteractiveSession outputting to Stdout and reading from Stdin by default, but other inputs and outputs may be specified with SessionOptions
func (*InteractiveSession) Ask ¶
func (i *InteractiveSession) Ask(prompt string, validators ...ValidationFunc) string
Ask is a terminator for an interactive session that results in returning the user's input. Validators can optionally be applied to ensure that acceptable input is returned or the question will be asked again.
func (*InteractiveSession) AskFromTable ¶
func (i *InteractiveSession) AskFromTable(prompt string, choices map[string]string, def string) string
AskFromTable creates a table to select choices from. It has a built-in validation function that will ensure that only the options listed are valid choices.
func (*InteractiveSession) AskPassword ¶
func (i *InteractiveSession) AskPassword(validators ...ValidationFunc) string
AskPassword is a terminator that asks for a password and does not echo input to the terminal.
func (*InteractiveSession) AskPasswordPrompt ¶ added in v1.1.0
func (i *InteractiveSession) AskPasswordPrompt(prompt string, validators ...ValidationFunc) string
AskPasswordPrompt is a terminator that asks for a password with a custom prompt
func (*InteractiveSession) AskWithDefault ¶
func (i *InteractiveSession) AskWithDefault(prompt string, defaultChoice string, validators ...ValidationFunc) string
AskWithDefault is like ask, but sets a default choice that the user can select by pressing enter.
func (*InteractiveSession) AskWithHint ¶
func (i *InteractiveSession) AskWithHint(prompt string, hint string, validators ...ValidationFunc) string
AskWithHint is like ask, but gives a hint about the proper format of the response. This is useful combined with a validation function to get input in the right format.
func (*InteractiveSession) AskYesNo ¶
func (i *InteractiveSession) AskYesNo(prompt string, defaultChoice string) string
AskYesNo asks the user a yes or no question with a default value. Defaults of `y` or `yes` will set the default to yes. Anything else will default to no. You can use IsYes or IsNo to act on the response without worrying about what version of y, Y, YES, yes, etc. that the user entered.
func (*InteractiveSession) Error ¶
func (i *InteractiveSession) Error(format string, args ...interface{})
Error is a terminator that gives an informational error message to the user in format Error: <user defined string>. Exits the program returning status code 1
func (*InteractiveSession) Pause ¶
func (i *InteractiveSession) Pause()
Pause is a terminator that will render long-form text added via the another method that returns *InteractiveSession and will wait for the user to press enter to continue. It is useful for long-form content or paging.
func (*InteractiveSession) PauseWithPrompt ¶
func (i *InteractiveSession) PauseWithPrompt(format string, args ...interface{})
PauseWithPrompt is a terminator that will render long-form text added via the another method that returns *InteractiveSession and will wait for the user to press enter to continue. This will use the custom prompt specified by format and args.
func (*InteractiveSession) Reset ¶ added in v1.2.0
func (i *InteractiveSession) Reset()
Reset allows reuse of the same interactive session by reseting its state and keeping its current input and output
func (*InteractiveSession) Say ¶
func (i *InteractiveSession) Say(format string, args ...interface{}) *InteractiveSession
Say is a short form of fmt.Fprintf but allows you to chain additional terminators to the interactive session to collect user input
func (*InteractiveSession) Warn ¶
func (i *InteractiveSession) Warn(format string, args ...interface{}) *InteractiveSession
Warn adds an informational warning message to the user in format Warning: <user defined string>
type Justification ¶
type Justification int
Justification sets the default placement of text inside each cell of a column
const ( Left Justification = iota Center Right )
Column justification flags
type Progress ¶
type Progress struct { // Prompt to display before spinner or bar Prompt string // Approximate length of the total progress display, including // the prompt and the ..., does not include status indicator // at the end (e.g, the spinner, FAIL, OK, or XX%) DisplayLength int // contains filtered or unexported fields }
Progress structure used to render progress and loading indicators
func NewIncrementalProgressBar ¶ added in v1.3.0
NewIncrementalProgressBar returns a new progress bar with a fixed number of steps. This can be useful when you want independent go routines to update total progress as they finish work without knowing the total state of the system. You can call `defer mybar.Increment()` in your go routine to update the value of the bar by one increment.
func NewLoadingMessage ¶
NewLoadingMessage creates a spinning loading indicator followed by a message. The loading indicator does not indicate sucess or failure and disappears when you call either Success() or Failure(). This is useful to show action when making remote calls that are expected to be short. The delay parameter is to prevent flickering when the remote call finishes quickly. If you finish your call and call Success() or Failure() within the delay period, the loading indicator will never be shown.
func NewProgressBar ¶
NewProgressBar returns a new progress bar with prompt <message> display length defaults to 20
func NewProgressSpinner ¶
NewProgressSpinner returns a new spinner with prompt <message> display length defaults to 30.
func (*Progress) Fail ¶
func (p *Progress) Fail()
Fail should be called on a progress bar or spinner if a failure occurs
func (*Progress) Increment ¶ added in v1.3.0
func (p *Progress) Increment()
Increment updates a stepped progress bar
func (*Progress) Start ¶
func (p *Progress) Start()
Start launches a Goroutine to render the progress bar or spinner and returns control to the caller for further processing. Spinner will update automatically every 250ms until Success() or Fail() is called. Bars will update by calling Update(<pct_complete>). You must always finally call either Success() or Fail() to terminate the go routine.
type Row ¶
type Row struct {
// contains filtered or unexported fields
}
Row is a row of cells in a table. You want to use AddRow or AddStyledRow to create one.
type SessionOption ¶ added in v1.1.0
type SessionOption func(i *InteractiveSession)
SessionOption optionally configures aspects of the interactive session
func WithInput ¶ added in v1.1.0
func WithInput(r io.Reader) SessionOption
WithInput uses an input other than os.Stdin
func WithOutput ¶ added in v1.1.0
func WithOutput(w io.Writer) SessionOption
WithOutput uses an output other than os.Stdout
type Spinner ¶
type Spinner []string
Spinner is a set of unicode strings that show a moving progress indication in the terminal
var ( // Wheel created with pipes and slashes Wheel Spinner = []string{"|", "/", "-", "\\"} // Bouncing dots Bouncing Spinner = []string{"⠁", "⠂", "⠄", "⠂"} // Clock that spins two hours per step Clock Spinner = []string{"🕐 ", "🕑 ", "🕒 ", "🕓 ", "🕔 ", "🕕 ", "🕖 ", "🕗 ", "🕘 ", "🕙 ", "🕚 "} // Dots that spin around a rectangle Dots Spinner = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} )
type Style ¶
type Style struct {
// contains filtered or unexported fields
}
Style represents a computed style from one or more colors or textstyles as the ANSI code suitable for terminal output
type Styler ¶
Styler is an interface that is fulfilled by either a Color or Textstyle to be applied to a string
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is a table output to the console. Use NewTable to construct the table with sensible defaults. Tables detect the terminal width and step through a number of rendering strategies to intelligently wrap column information to fit within the available space.
func NewTable ¶
func NewTable(numColumns int, options ...TableOption) *Table
NewTable creates a new table with a given number of columns, setting the default justfication to left, and attempting to detect the existing terminal size to set size defaults.
func (*Table) AddRow ¶
AddRow adds a new row to the table given an array of strings for each column's content. You can set styles on a row by using AddStyledRow instead. If you add more cells than available columns, the cells will be silently truncated. If there are fewer values than columns, the remaining columns will be empty.
func (*Table) AddStyledRow ¶
AddStyledRow adds a new row to the table with custom styles for each Cell. If you add more cells than available columns, the cells will be silently truncated. If there are fewer values than columns, the remaining columns will be empty.
func (*Table) AsString ¶
AsString returns the rendered table as a string instead of immediately writing to the configured writer
func (*Table) ColumnHeaderStyles ¶
ColumnHeaderStyles sets the column header styles.
func (*Table) ColumnHeaders ¶
ColumnHeaders sets the column headers with an array of strings The default style is Underline and Bold. This can be changed through a call to ColumnHeaderStyles.
func (*Table) ColumnStyles ¶
ColumnStyles sets the default styles for each column in the row except the column headers.
func (*Table) Justification ¶
func (t *Table) Justification(cellJustifications ...Justification) *Table
Justification sets the justification of each column. If you pass more justifications than the number of columns they will be silently dropped.
func (*Table) Show ¶
func (t *Table) Show()
Show will render the table using the headers, title, and styles previously set.
type TableOption ¶
TableOption is a function that sets an option on a table
func MaxHeight ¶
func MaxHeight(h int) TableOption
MaxHeight sets the table maximum height that can be used for pagination of long tables. Use this only when you want to reduce the maximum height of the table to something less than the detected height of the terminal. Normally you don't need to use this and should prefer the auto detection.
func MaxWidth ¶
func MaxWidth(w int) TableOption
MaxWidth sets the max width of the table. The actual max width will be set to the smaller of this number of the detected width of the terminal. Very small max widths can be a problem because the layout engine will not be able to find a strategy to render the table.
func Spacing ¶
func Spacing(n int) TableOption
Spacing adjusts the spacing of the table. For n=2, there will be one whitespace line between each row
type Textstyle ¶
type Textstyle struct {
// contains filtered or unexported fields
}
Textstyle represents a ANSI-coded text style
type Title ¶
type Title struct {
// contains filtered or unexported fields
}
Title is a special cell that is rendered at the center top of the table that can contain its own styling.
type ValidationFunc ¶
ValidationFunc is a type alias for a validator that takes a string and returns true if it passes validation. Error provides a helpful error message to the user that is shown before re-asking the question.
func AllowedOptions ¶
func AllowedOptions(options []string) ValidationFunc
AllowedOptions builds a new validator from a []string of options
func Required ¶
func Required() ValidationFunc
Required validates that the length of the input is greater than 0
func ValidateYesNo ¶
func ValidateYesNo() ValidationFunc
ValidateYesNo is a validation function that ensures that a yes/no question receives a valid response. Use IsYes or IsNo to test for a particular yes or no response.