output

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package output renders command results as human-readable text, JSON or YAML. Commands pass a view model (with json tags) plus a text renderer; machine formats are derived from the view model only, so the JSON schema is what the view model declares and never an internal Go structure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderTree

func RenderTree(w io.Writer, n *Node) error

RenderTree writes n with box-drawing connectors.

func VisibleWidth

func VisibleWidth(s string) int

VisibleWidth returns the printed width of s, ignoring ANSI sequences.

func WriteJSON

func WriteJSON(w io.Writer, v any) error

WriteJSON writes indented JSON with a trailing newline.

func WriteYAML

func WriteYAML(w io.Writer, v any) error

WriteYAML writes v as YAML using its JSON field names and order. The value is first encoded as JSON, then decoded into a yaml.Node (which keeps key order) and re-encoded in block style.

Types

type Format

type Format string

Format selects the output encoding.

const (
	Text Format = "text"
	JSON Format = "json"
	YAML Format = "yaml"
	Raw  Format = "raw"
)

func ParseFormat

func ParseFormat(s string) (Format, error)

ParseFormat validates a --format value. "table" is an alias of text.

type KV

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

KV renders aligned "Label: value" blocks, optionally under headings. Empty values are skipped so renderers can add optional fields freely.

func NewKV

func NewKV(style Style) *KV

NewKV creates a key/value block.

func (*KV) Add

func (k *KV) Add(key, value string) *KV

Add adds a field; skipped when value is empty.

func (*KV) Addf

func (k *KV) Addf(key, format string, args ...any) *KV

Addf adds a formatted field.

func (*KV) Heading

func (k *KV) Heading(title string) *KV

Heading starts a new titled section.

func (*KV) List

func (k *KV) List(key string, values []string) *KV

List adds a multi-line field; skipped when values is empty.

func (*KV) Render

func (k *KV) Render(w io.Writer) error

Render writes all sections. Keys are aligned per section.

type Node

type Node struct {
	Label    string
	Children []*Node
}

Node is an element of a printable tree, e.g. a certificate chain.

func (*Node) Add

func (n *Node) Add(label string) *Node

Add appends a child and returns it.

type Printer

type Printer struct {
	Out    io.Writer
	Err    io.Writer
	Format Format
	Style  Style
	Quiet  bool
}

Printer writes results to Out and diagnostics to Err.

func New

func New(out, errw io.Writer, format Format, noColor, quiet bool) *Printer

New creates a printer. Colour is enabled only for terminals, and never when NO_COLOR is set or noColor is true.

func (*Printer) Emit

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

Emit writes v in the selected machine format, or calls text for text/raw output. text may be nil when a command has no human rendering.

func (*Printer) Infof

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

Infof prints a progress or status message to stderr unless quiet.

func (*Printer) Machine

func (p *Printer) Machine() bool

Machine reports whether output is JSON or YAML.

func (*Printer) Warnf

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

Warnf prints a warning to stderr; warnings are shown even in quiet mode because they usually concern security.

type Style

type Style struct{ Enabled bool }

Style applies ANSI styling when enabled.

func (Style) Blue

func (s Style) Blue(v string) string

func (Style) Bold

func (s Style) Bold(v string) string

func (Style) Cyan

func (s Style) Cyan(v string) string

func (Style) Dim

func (s Style) Dim(v string) string

func (Style) Green

func (s Style) Green(v string) string

func (Style) Level

func (s Style) Level(level string) string

Level colours a severity or status word consistently across commands.

func (Style) Red

func (s Style) Red(v string) string

func (Style) Yellow

func (s Style) Yellow(v string) string

type Table

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

Table renders aligned columns. Cells may contain ANSI colour codes.

func NewTable

func NewTable(headers ...string) *Table

NewTable creates a table with the given column headers.

func (*Table) Len

func (t *Table) Len() int

Len returns the number of data rows.

func (*Table) Render

func (t *Table) Render(w io.Writer, style Style) error

Render writes the table. Headers are styled bold/dim when style is enabled.

func (*Table) Row

func (t *Table) Row(cells ...string)

Row appends a row; missing cells are rendered empty.

Jump to

Keyboard shortcuts

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