Documentation
¶
Overview ¶
Package msg is a simple, easy to use, opinionated console printing toolkit for Go CLIs rendering pretty formatted output with colours specific to the particular message type:
- Info: For general user information and progress updates
- Title: Separation between sections of output
- Warn: User warnings
- Success: Report success
- Error: Report failure
All message types default to stdout other than the `Error` type which prints to stderr by default.
There are also "F-style" print methods that allow you to specify an io.Writer to print the messages to.
Index ¶
- func ColorEnabled(v bool)
- func Err(err error)
- func Error(format string, a ...any)
- func Ferr(w io.Writer, err error)
- func Ferror(w io.Writer, format string, a ...any)
- func Finfo(w io.Writer, format string, a ...any)
- func Fsuccess(w io.Writer, format string, a ...any)
- func Ftitle(w io.Writer, format string, a ...any)
- func Fwarn(w io.Writer, format string, a ...any)
- func Info(format string, a ...any)
- func Success(format string, a ...any)
- func Title(format string, a ...any)
- func Warn(format string, a ...any)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ColorEnabled ¶ added in v1.7.0
func ColorEnabled(v bool)
ColorEnabled sets whether the output from this package is colourised.
msg defaults to automatic detection based on a number of attributes:
- The value of $NO_COLOR and/or $FORCE_COLOR
- The value of $TERM (xterm enables colour)
- Whether os.Stdout is pointing to a terminal
This means that msg should do a reasonable job of auto-detecting when to colourise output and should not write escape sequences when piping between processes or when writing to files etc.
This function may be called to bypass the above detection and explicitly set the value, useful in CLI applications where a --no-color flag might be expected.
ColorEnabled may be called safely from concurrently executing goroutines.
func Err ¶ added in v1.3.0
func Err(err error)
Err prints a nicely formatted error message from an actual error to os.Stderr, bypassing the need for the caller to construct the format string.
err := errors.New("Uh oh!") msg.Err(err) // Equivalent to msg.Error("%v", err)
In the case of wrapped errors with fmt.Errorf, Err recursively unwraps the error, showing each of the errors in the causal chain in a tree-like structure.
root := errors.New("some deep error") wrapped := fmt.Errorf("could not process file: %w", root) again := fmt.Errorf("failed to do thing: %w", wrapped) msg.Err(again) // Unwraps the above and shows each cause as a new indented line
The intended use case for Err is at the top level of a CLI application where all errors are eventually bubbled up to with the appropriate context, which Err can then show to end users in a very clear, concise way.
func Error ¶ added in v0.6.0
Error prints an error message with optional format args to stderr.
msg.Error("Invalid config") msg.Error("Could not find file: %s", "missing.txt")
func Ferr ¶ added in v1.3.0
Ferr prints a nicely formatted error message from an actual error to w, bypassing the need for the caller to construct the format string.
err := errors.New("Uh oh!") msg.Ferr(os.Stderr, err) // Equivalent to msg.Err(err)
In the case of wrapped errors with fmt.Errorf, Ferr recursively unwraps the error, showing each of the errors in the causal chain in a tree-like structure.
root := errors.New("some deep error") wrapped := fmt.Errorf("could not process file: %w", root) again := fmt.Errorf("failed to do thing: %w", wrapped) msg.Ferr(os.Stderr, again) // Unwraps the above and shows each cause as a new indented line
The intended use case for Ferr and Err is at the top level of a CLI application where all errors are eventually bubbled up to with the appropriate context, which can then be shown to end users in a very clear, concise way.
func Ferror ¶ added in v0.6.0
Ferror prints an error message with optional format args to w.
msg.Ferror(os.Stderr, "Uh oh! %s", "something wrong")
func Finfo ¶ added in v0.6.0
Finfo prints an info message with optional format args to w.
msg.Finfo(os.Stdout, "The meaning of life is %v", 42)
func Fsuccess ¶ added in v0.6.0
Fsuccess prints a success message with optional format args to w.
msg.Fsuccess(os.Stdout, "Compiled project: %s", "msg")
func Ftitle ¶ added in v0.6.0
Ftitle prints a title message to w.
A title message differs from every other message type in msg as it has 1 leading newline and 2 trailing newlines to create separation between the sections it is differentiating in your CLI.
msg.Ftitle(os.Stdout, "Some section")
func Fwarn ¶ added in v0.6.0
Fwarn prints a warning message with optional format args to w.
msg.Fwarn(os.Stderr, "hmmmm: %v", true)
func Info ¶
Info prints an info message with optional format args to stdout.
msg.Info("You have %d repos on GitHub", 42)
func Success ¶ added in v0.6.0
Success prints a success message with optional format args to stdout.
msg.Success("Compiled project: %s", "msg")
Types ¶
This section is empty.