output

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package output renders command results in the three formats every weg command supports: human-readable text, JSON and YAML.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknownFormat = errors.New("unknown output format")

ErrUnknownFormat is returned when a format string is not one of the supported values.

Functions

func ColorEnabled

func ColorEnabled(w io.Writer) bool

ColorEnabled reports whether colour output is appropriate for w.

func Formats

func Formats() []string

Formats lists the supported formats, for flag help and shell completion.

func IsTerminal

func IsTerminal(f any) bool

IsTerminal reports whether f is backed by a character device.

Types

type Color

type Color string

Color is a terminal colour, as the SGR parameter that selects it.

TODO: Lipgloss is the eventual home for this, once there is a table to draw with it. Until then a handful of escape sequences is less than a dependency, and having them here rather than in a command is what keeps the NO_COLOR and TTY rules in one place.

const (
	ColorNone   Color = ""
	ColorRed    Color = "31"
	ColorGreen  Color = "32"
	ColorYellow Color = "33"
	ColorDim    Color = "2"
)

The colours anything this CLI prints may use. One accent, and the three states an operator scans for.

type Format

type Format string

Format selects how a command renders its result.

const (
	// FormatText is the default, human-readable rendering.
	FormatText Format = "text"
	// FormatJSON is indented JSON, suitable for piping into jq.
	FormatJSON Format = "json"
	// FormatYAML is YAML produced from the same field names as [FormatJSON].
	FormatYAML Format = "yaml"
)

func ParseFormat

func ParseFormat(s string) (Format, error)

ParseFormat converts a flag value into a Format. It is case-insensitive and accepts "yml" as an alias for YAML.

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer renders command results to a writer in a chosen format.

func New

func New(out, errOut io.Writer, format Format) *Printer

New returns a Printer writing to out, with errors and diagnostics going to errOut. Colour is enabled only when the format is text and out is a terminal that has not opted out; see ColorEnabled.

func (*Printer) Color

func (p *Printer) Color() bool

Color reports whether colour escape sequences should be emitted.

Commands must consult this rather than deciding for themselves, so that NO_COLOR and non-terminal output are honoured consistently.

func (*Printer) ErrOut

func (p *Printer) ErrOut() io.Writer

ErrOut returns the writer diagnostics are printed to.

func (*Printer) Format

func (p *Printer) Format() Format

Format reports which format the Printer renders.

func (*Printer) Out

func (p *Printer) Out() io.Writer

Out returns the writer results are printed to.

func (*Printer) Paint

func (p *Printer) Paint(c Color, s string) string

Paint wraps s in c, or returns it unchanged when colour is not appropriate.

func (*Printer) Print

func (p *Printer) Print(v any, text func(w io.Writer) error) error

Print renders v.

For JSON and YAML, v is marshalled directly. For text, the supplied renderer is called with the output writer. A nil renderer falls back to fmt.Fprintln, which is adequate for scalars and types with a String method.

Jump to

Keyboard shortcuts

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