cout

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package cout provides verbosity-levelled, coloured console output for command-line tools.

Tools separate what they *log* (clog, stderr, for diagnosing the tool) from what they *print* (this package, for the person running it). Every print call carries a minimum verbosity so a --quiet or --verbose flag is honoured in one place, and colour tags such as <red>...</> are rendered by gookit/color, which strips them when stdout is not a colour terminal.

Tags are rendered anywhere in the final string, arguments included, so helpers may build coloured fragments and pass them through %s.

Index

Constants

This section is empty.

Variables

Err is where Errorf writes. It defaults to stderr and is separate from Out so errors stay visible when Out is redirected or discarded.

Level controls the output verbosity. Tools set it once from their flags before any output call; it is a plain variable rather than a setter so the cobra flag-handling block in every tool stays a one-line assignment.

Out is where normal output goes. It defaults to stdout; tools whose stdout is a data channel (a JSON emitter, an MCP server on stdio) point it at stderr so progress messages never corrupt the stream.

Functions

func Errorf

func Errorf(format string, args ...any)

Errorf prints an error to Err in every mode except silent, so failures stay visible even when Out is machine-readable (quiet) or suppressed.

func Printf

func Printf(format string, args ...any)

Printf prints normal output; suppressed in quiet and silent modes. Console write failures are not actionable, so they are dropped.

func Println

func Println(args ...any)

Println prints normal output followed by a newline, rendering colour tags in its arguments; suppressed in quiet and silent modes.

func QuietOnlyf

func QuietOnlyf(format string, args ...any)

QuietOnlyf prints only in quiet mode. Use it when quiet mode has its own terse format for a line that normal mode prints differently via Printf, so the two never appear together.

func Quietf

func Quietf(format string, args ...any)

Quietf prints in quiet mode and above. Use it for the one line a script would parse, which should also appear in normal output alongside any decoration Printf adds around it.

func SetLevelFromFlags added in v0.2.0

func SetLevelFromFlags(silent, quiet, verbose bool)

SetLevelFromFlags sets the package Level from individual flag values; it is the one-liner that replaces the switch every tool used to carry.

func Sprintf

func Sprintf(format string, args ...any) string

Sprintf formats like fmt.Sprintf and renders colour tags in the result. Use it to build coloured fragments that are later passed to Printf and friends, or to colour text destined for somewhere other than Out.

func Verbosef

func Verbosef(format string, args ...any)

Verbosef prints detail that only matters when someone asked for it with -v; suppressed at Normal and below.

func Writer

func Writer() io.Writer

Writer returns Out when Level is Normal or above and io.Discard below, for code that streams output through something else (a tabwriter, an encoder) and cannot go through Printf.

Types

type Flags added in v0.2.0

type Flags struct {
	Silent  bool `mapstructure:"silent"`
	JSON    bool `mapstructure:"json"`
	Quiet   bool `mapstructure:"quiet"`
	Verbose bool `mapstructure:"verbose"`
}

Flags is the set of verbosity flags katbyte tools expose. Embed it in a tool's flag struct with `mapstructure:",squash"` and viper fills it from the "silent", "json", "quiet" and "verbose" keys; a tool registers only the flags it offers and keeps ownership of their names, shorthands and help text.

func (Flags) Apply added in v0.2.0

func (f Flags) Apply()

Apply sets the package Level from the flags. Call it once, after the flags are parsed and before any output.

func (Flags) Level added in v0.2.0

func (f Flags) Level() Verbosity

Level returns the verbosity the flags select. Silent wins over JSON, which wins over quiet, which wins over verbose, so contradictory flags err on the side of less output; none set means Normal.

type Verbosity

type Verbosity int

Verbosity is how much a tool prints. The levels are ordered, so "print at Normal and above" is a plain comparison.

const (
	// VerbositySilent prints nothing at all, not even errors. For callers that
	// only want the exit code.
	VerbositySilent Verbosity = iota
	// VerbosityJSON is for tools that emit a JSON document on stdout at the
	// end of a run: nothing else is printed there, errors still go to Err. The
	// document itself is the tool's to write; this level only keeps the
	// channel clean.
	VerbosityJSON
	// VerbosityQuiet prints only the minimal machine-readable lines (Quietf,
	// QuietOnlyf) and errors.
	VerbosityQuiet
	// VerbosityNormal is the default: everything a person wants to see.
	VerbosityNormal
	// VerbosityVerbose adds the detail behind Verbosef, typically -v.
	VerbosityVerbose
)

The verbosity levels, from least to most output.

func LevelFromFlags added in v0.2.0

func LevelFromFlags(silent, quiet, verbose bool) Verbosity

LevelFromFlags is Flags.Level for tools that read their flags individually, for example straight from viper in a cobra pre-run hook. Tools with a JSON mode use Flags, which is the only place that level is selected.

func (Verbosity) String

func (v Verbosity) String() string

String returns the level's name in lower case, matching the flag that usually selects it.

Jump to

Keyboard shortcuts

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