printer

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package printer renders a Printable as a table, JSON, YAML or a Go template.

Index

Constants

This section is empty.

Variables

View Source
var ErrColumnsOfRecords = errors.New("columns are picked for a table: json and yaml show whole records")

ErrColumnsOfRecords is the error for columns picked from JSON or YAML, which show whole records.

Functions

func IsStructured

func IsStructured(s string) bool

IsStructured reports whether the format s names is machine-readable -- JSON or YAML -- rather than something for a person to read.

func IsTable added in v0.4.0

func IsTable(s string) bool

IsTable reports whether the format s names is a table.

func Print

func Print(item Printable, out io.Writer, opts Options) error

Print writes item to out as opts say: a table, JSON or YAML of its records, or a Go template such as '{{.Name}}' applied to each row.

func PrintStructured added in v0.4.0

func PrintStructured(v any, out io.Writer, s string) error

PrintStructured writes v to out as JSON or YAML, as the format s names, with v's fields named as their JSON tags name them in both. A nil list is written as an empty one. Any other format is an error.

Types

type Options

type Options struct {
	// Format is table, json, yaml, or a Go template.
	Format string

	// Columns are those a table shows. Empty means the default ones. JSON
	// and YAML show whole records, so they take none.
	Columns []string

	// AllColumns shows every column in a table, not just the default ones.
	AllColumns bool
}

Options are how Print renders.

type Printable

type Printable interface {
	Columns() []string
	Rows() []map[string]any
	Records() any
}

Printable is implemented by anything a command can print. Columns names the columns in display order; Rows returns one map per row, keyed by column, for people: a table and a template show them. Records returns the list of records the rows are made from, for programs: JSON and YAML show them, with their raw values, as their JSON tags name them.

A Printable with more columns than a table shows by default also has a method DefaultColumns() []string. Asked for no columns in particular, a table shows those; a template still has every column.

Jump to

Keyboard shortcuts

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