Documentation
¶
Overview ¶
Package disgo is a console output and prompting library for modern command line interfaces.
It does not provide structured logging and is not built with performance in mind, since it is aimed at building user-friendly command line interfaces, and not applications.
The packages that compose disgo can be used independently.
The console package (github.com/ullaakut/disgo/console) is a simplified console output library which only handles two basic output levels (standard and debug), and can also manage outputs for step-by-step processes as well as formatting outputs.
The prompter package (github.com/ullaakut/disgo/prompter) is a simple user prompter that asks users for input data or confirmations.
The symbol package (github.com/ullaakut/disgo/symbol) provides access to cherry-picked UTF-8 symbols that are useful for making user-friendly command line interfaces.
Index ¶
- Constants
- Variables
- func Confirm(config Confirmation) (bool, error)
- func Debug(a ...interface{})
- func Debugf(format string, a ...interface{})
- func Debugln(a ...interface{})
- func DefaultConfirmation(input string) (bool, error)
- func EndStep()
- func Error(a ...interface{})
- func Errorf(format string, a ...interface{})
- func Errorln(a ...interface{})
- func FailStep(err error) error
- func FailStepf(format string, a ...interface{}) error
- func Info(a ...interface{})
- func Infof(format string, a ...interface{})
- func Infoln(a ...interface{})
- func SetupGlobalConsole(options ...func(*Console))
- func SetupGlobalPrompter(options ...func(*Prompter))
- func StartStep(label string)
- func StartStepf(format string, a ...interface{})
- func WithColors(enabled bool) func(*Console)
- func WithDebug(enabled bool) func(*Console)
- func WithDefaultOutput(writer io.Writer) func(*Console)
- func WithErrorOutput(writer io.Writer) func(*Console)
- func WithInteractive(enabled bool) func(*Prompter)
- func WithReader(reader io.Reader) func(*Prompter)
- func WithWriter(writer io.Writer) func(*Prompter)
- type Confirmation
- type ConfirmationParser
- type Console
- func (c Console) Debug(a ...interface{})
- func (c Console) Debugf(format string, a ...interface{})
- func (c Console) Debugln(a ...interface{})
- func (c *Console) EndStep()
- func (c Console) Error(a ...interface{})
- func (c Console) Errorf(format string, a ...interface{})
- func (c Console) Errorln(a ...interface{})
- func (c *Console) FailStep(err error) error
- func (c *Console) FailStepf(format string, a ...interface{}) error
- func (c Console) Info(a ...interface{})
- func (c Console) Infof(format string, a ...interface{})
- func (c Console) Infoln(a ...interface{})
- func (c *Console) StartStep(label string)
- func (c *Console) StartStepf(format string, a ...interface{})
- type Prompter
Constants ¶
const ( // Check displays ✔ SymbolCheck = "\xe2\x9c\x94" // Cross displays ✖ SymbolCross = "\xe2\x9c\x96" // LeftArrow displays ❮ SymbolLeftArrow = "\xe2\x9d\xae" // RightArrow displays ❯ SymbolRightArrow = "\xe2\x9d\xaf" // LeftTriangle displays ◀ SymbolLeftTriangle = "\xe2\x97\x80" // RightTriangle displays ▶ SymbolRightTriangle = "\xe2\x96\xb6" )
This file contains a few cherry-picked UTF-8 symbols to be used to build user-friendly command-line interfaces. They are all colorless so that they can be used along with disgo's formatting helpers.
Variables ¶
var ( // Success colors a message in bold green to represent success. Success = color.New(color.FgGreen, color.Bold).SprintFunc() // Failure colors a message in bold red to represent failure. Failure = color.New(color.FgRed, color.Bold).SprintFunc() // Trace colors a message in faint white (usually rendered in gray) // to represent an output of low importance for the user. Trace = color.New(color.FgHiWhite, color.Faint).SprintFunc() // Important colors a message in bold to represent an important // information. Important = color.New(color.Bold).SprintFunc() // Link colors a message in underlined blue to represent a clickable link. Link = color.New(color.FgBlue, color.Underline).SprintFunc() )
var ( // DefaultConfirmationChoices is the default value // for the choices that are given to // the users in a confirmation prompt. DefaultConfirmationChoices = []string{"y", "n"} )
Functions ¶
func Confirm ¶
func Confirm(config Confirmation) (bool, error)
Confirm prompts the user to confirm something using the global prompt.
func Debug ¶
func Debug(a ...interface{})
Debug writes a debug output on the global console's default writer if the debug outputs are enabled.
func Debugf ¶
func Debugf(format string, a ...interface{})
Debugf formats according to a format specifier and writes to the global console's default writer if the debug outputs are enabled.
func Debugln ¶
func Debugln(a ...interface{})
Debugln writes a debug output on the global console's default writer if the debug outputs are enabled and appends a newline to its input.
func DefaultConfirmation ¶
DefaultConfirmation is a confirmation parser that covers most cases for confirmation. It converts y/Y/yes/YES/t/T/true/True/1 to true. It converts n/N/no/NO/f/F/false/FALSE/0 to false.
func EndStep ¶
func EndStep()
EndStep ends a step with a success state on the global. console. It then prints all of the outputs that were queued while the step was in progress. Warning: This is not thread-safe.
func Error ¶
func Error(a ...interface{})
Error writes an error output on the global console's error writer.
func Errorf ¶
func Errorf(format string, a ...interface{})
Errorf formats according to a format specifier and writes to the global console's error writer.
func Errorln ¶
func Errorln(a ...interface{})
Errorln writes an error output on the global console's error writer. It appends a newline to its input.
func FailStep ¶
FailStep ends a step with a failure state. It then prints all of the outputs that were queued while the step was in progress, and returns the given error for error handling. Warning: This is not thread-safe.
func FailStepf ¶
FailStepf ends a step with a failure state on the global. console. It then prints all of the outputs that were queued while the step was in progress, and returns an error created from the given format and arguments. Warning: This is not thread-safe.
func Info ¶
func Info(a ...interface{})
Info writes an info output on the global console's default writer.
func Infof ¶
func Infof(format string, a ...interface{})
Infof formats according to a format specifier and writes to the global console's default writer.
func Infoln ¶
func Infoln(a ...interface{})
Infoln writes an info output on the global console's default writer and appends a newline to its input.
func SetupGlobalConsole ¶
func SetupGlobalConsole(options ...func(*Console))
SetupGlobalConsole applies options to the global console.
func SetupGlobalPrompter ¶
func SetupGlobalPrompter(options ...func(*Prompter))
SetupGlobalPrompter applies options to the global prompter.
func StartStep ¶
func StartStep(label string)
StartStep sets a step in the global console, which prints the step's label and makes the console queue outputs until the step is ended or failed. If a step was already in progress, it is considered to have been ended successfully. Warning: This is not thread-safe.
func StartStepf ¶
func StartStepf(format string, a ...interface{})
StartStepf sets a step in the console, which prints the step's label and makes the console queue outputs until the step is ended or failed. If a step was already in progress, it is considered to have been ended successfully. Warning: This is not thread-safe.
func WithColors ¶
WithColors sets the use of colors in the console. By default, whether or not colors are enabled depends on the user's TTY, but this option can be used to force colors to be enabled or disabled.
func WithDefaultOutput ¶
WithDefaultOutput sets the default writer on the console.
func WithErrorOutput ¶
WithErrorOutput sets the error writer on the console.
func WithInteractive ¶
WithInteractive enables or disables the prompter interactive mode.
func WithReader ¶
WithReader sets the reader on the prompter. By default, if this option is not used, the default reader will be os.Stdin.
func WithWriter ¶
WithWriter sets the writer on the prompter. By default, if this option is not used, the default writer will be os.Stdout.
Types ¶
type Confirmation ¶
type Confirmation struct {
// The label that will be prompted to the user.
// Example: `Are you sure?`
Label string
// The choices that will be presented to the user.
// Example: `Y/n`. (A good practice is to uppercase
// the default value, if there is one).
Choices []string
// EnableDefaultValue tells the prompter whether or not
// there is a default value that will be used when the
// user doesn't input any data.
EnableDefaultValue bool
// DefaultValue is the default value that will be used when
// the user doesn't input any data, if EnableDefaultValue
// is set to true OR that the prompter is set to not
// interactive.
DefaultValue bool
// The parser that will be used to convert the user's input
// into a true/false value.
Parser ConfirmationParser
}
Confirmation represents a confirmation prompt's configuration.
type ConfirmationParser ¶
ConfirmationParser is a function that parses an input and returns a confirmation value as well as an error, if the input can't be parsed.
type Console ¶
type Console struct {
// contains filtered or unexported fields
}
Console represents a disgo Console. It writes the output on a given io.Writer and can toggle debug outputs and have an error writer.
func NewConsole ¶
NewConsole creates a new Console and binds the given writer to its outputs.
func (Console) Debug ¶
func (c Console) Debug(a ...interface{})
Debug writes a debug output on the console's default writer if the debug outputs are enabled.
func (Console) Debugf ¶
Debugf formats according to a format specifier and writes to the console's default writer if the debug outputs are enabled.
func (Console) Debugln ¶
func (c Console) Debugln(a ...interface{})
Debugln writes a debug output on the console's default writer if the debug outputs are enabled and appends a newline to its input.
func (*Console) EndStep ¶
func (c *Console) EndStep()
EndStep ends a step with a success state. It then prints all of the outputs that were queued while the step was in progress.
func (Console) Error ¶
func (c Console) Error(a ...interface{})
Error writes an error output on the console's error writer.
func (Console) Errorf ¶
Errorf formats according to a format specifier and writes to the console's error writer.
func (Console) Errorln ¶
func (c Console) Errorln(a ...interface{})
Errorln writes an error output on the console's error writer. It appends a newline to its input.
func (*Console) FailStep ¶
FailStep ends a step with a failure state. It then prints all of the outputs that were queued while the step was in progress, and returns the given error for error handling.
func (*Console) FailStepf ¶
FailStepf ends a step with a failure state. It then prints all of the outputs that were queued while the step was in progress, and returns an error created from the given format and arguments.
func (Console) Info ¶
func (c Console) Info(a ...interface{})
Info writes an info output on the console's default writer.
func (Console) Infof ¶
Infof formats according to a format specifier and writes to the console's default writer.
func (Console) Infoln ¶
func (c Console) Infoln(a ...interface{})
Infoln writes an info output on the console's default writer and appends a newline to its input.
func (*Console) StartStep ¶
StartStep sets a step in the console, which prints the step's label and makes the console queue outputs until the step is ended or failed. If a step was already in progress, it is considered to have been ended successfully.
func (*Console) StartStepf ¶
StartStepf sets a step in the console, which prints the step's label and makes the console queue outputs until the step is ended or failed. If a step was already in progress, it is considered to have been ended successfully.
type Prompter ¶
type Prompter struct {
// contains filtered or unexported fields
}
Prompter prompts users to let them input data, and parses it.
func NewPrompter ¶
NewPrompter instantiates a new prompter which will prompt users on the writer and read their output from the reader. The interactive boolean makes all prompts return a default value if set to false, and won't prompt them. This should be used if your users are not in a TTY and can't write to answer to the prompt.

