output

package
v2.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package output is the single place the CLI writes results. A command hands the writer one Result; the writer decides between a JSON envelope, bare data, an ID list, a count, or a human table based on flags and the terminal.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColorEnabled

func ColorEnabled(format Format, stdoutIsTTY bool, noColorFlag bool, getenv func(string) string) bool

ColorEnabled applies NO_COLOR, TERM=dumb, and the --no-color flag.

func IsTerminal

func IsTerminal(w io.Writer) bool

IsTerminal reports whether w is backed by a character device. Writers that do not expose file metadata, such as buffers and pipes wrapped in an arbitrary io.Writer, are treated as non-terminals.

func Plural added in v2.1.0

func Plural(n int, singular string) string

Plural formats a count with its noun, adding "es" where English needs it.

func Truncate

func Truncate(s string, width int) string

Truncate cuts s to width runes, adding an ellipsis when it was longer.

Types

type Envelope

type Envelope struct {
	OK            bool           `json:"ok"`
	SchemaVersion int            `json:"schemaVersion"`
	Data          any            `json:"data"`
	Summary       string         `json:"summary,omitempty"`
	Notices       []string       `json:"notices,omitempty"`
	Meta          map[string]any `json:"meta,omitempty"`
	Error         string         `json:"error,omitempty"`
	Code          string         `json:"code,omitempty"`
	ExitCode      int            `json:"exitCode,omitempty"`
	Hint          string         `json:"hint,omitempty"`
}

Envelope is the JSON success shape.

type ErrorEnvelope

type ErrorEnvelope struct {
	OK            bool   `json:"ok"`
	SchemaVersion int    `json:"schemaVersion"`
	Error         string `json:"error"`
	Code          string `json:"code"`
	ExitCode      int    `json:"exitCode"`
	Hint          string `json:"hint,omitempty"`
	Command       string `json:"command,omitempty"`
}

ErrorEnvelope is the JSON failure shape, written to stderr.

type Flags

type Flags struct {
	JSON    bool
	Plain   bool
	Quiet   bool
	IDsOnly bool
	Count   bool
	Agent   bool
	NoColor bool
	JQ      string
}

Flags mirrors the root persistent flags that pick a format.

type Format

type Format string

Format is the resolved output mode.

const (
	FormatPlain Format = "plain"
	FormatJSON  Format = "json"
	FormatQuiet Format = "quiet"
	FormatIDs   Format = "ids"
	FormatCount Format = "count"
)

func Resolve

func Resolve(f Flags, configured string, stdoutIsTTY bool) (Format, error)

Resolve picks the format. Precedence: --count, --ids-only, --quiet, --json/--jq/--agent, --plain, the configured default, then auto (plain on a terminal, JSON when piped).

type Printer

type Printer struct {
	Out   io.Writer
	Color bool
	// contains filtered or unexported fields
}

Printer renders the human view. Every method records the first write error so callers can check once.

func (*Printer) Blank

func (p *Printer) Blank()

Blank prints an empty line.

func (*Printer) Bold

func (p *Printer) Bold(s string) string

Bold, Dim, Red, Green, Yellow, Cyan wrap s in ANSI-16 codes when color is on.

func (*Printer) Cyan

func (p *Printer) Cyan(s string) string

func (*Printer) Dim

func (p *Printer) Dim(s string) string

func (*Printer) Green

func (p *Printer) Green(s string) string

func (*Printer) KeyValues

func (p *Printer) KeyValues(pairs [][2]string)

KeyValues prints a two-column block with aligned keys.

func (*Printer) Line

func (p *Printer) Line(format string, args ...any)

Line prints a formatted line.

func (*Printer) Red

func (p *Printer) Red(s string) string

func (*Printer) Table

func (p *Printer) Table(headers []string, rows [][]string)

Table prints aligned columns, measuring width without ANSI codes so styled cells line up. Headers are bold when color is on.

func (*Printer) Yellow

func (p *Printer) Yellow(s string) string

type Result

type Result struct {
	// Data is the structured value. Nil slices are emitted as [].
	Data any
	// Summary is one sentence for humans and the envelope.
	Summary string
	// Notices are things the caller should know that are not errors.
	Notices []string
	// Meta is merged into the envelope's meta object.
	Meta map[string]any
	// Plain renders the human view. When nil, pretty JSON of Data is used.
	Plain func(p *Printer)
	// Err marks the result as a failure that still carries data (a receipt
	// with failed items, an unhealthy doctor). The envelope gets ok:false
	// and the error fields; the caller exits with the error's code.
	Err *clierr.Error
}

Result is what a command produces.

type Writer

type Writer struct {
	Format        Format
	Stdout        io.Writer
	Stderr        io.Writer
	Color         bool
	JQ            string
	Command       string
	SchemaVersion int
	// contains filtered or unexported fields
}

Writer renders results and errors.

func New

func New(format Format, stdout, stderr io.Writer, color bool, jq, command string, schemaVersion int) (*Writer, error)

New builds a writer. command is the space-joined command path used in meta. The jq expression is compiled here so a bad one fails before the command runs.

func (*Writer) AddNotice

func (w *Writer) AddNotice(message string)

AddNotice queues a warning for the next Write. In plain mode it is printed to stderr right away.

func (*Writer) Error

func (w *Writer) Error(err *clierr.Error)

Error renders a failure. JSON-style formats get an envelope on stderr; plain mode gets a message and hint.

func (*Writer) Write

func (w *Writer) Write(r Result) error

Write renders one result in the writer's format. When r.Err is set the error is marked Reported and returned after the data is written.

Jump to

Keyboard shortcuts

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