output¶
Structured, themeable output formatting for CLI tools.
output gives a command-line tool one configured Renderer that speaks both
languages a CLI needs at once — human-readable text for a terminal and
machine-readable JSON/YAML for a pipeline — plus tables, spinners, progress bars
and live status lines. It degrades gracefully to plain output when it is not
attached to a terminal.
r := output.New(output.WithWriter(os.Stdout), output.WithFormat(format))
_ = r.Write(result, func(w io.Writer) {
fmt.Fprintf(w, "%s is up to date (%s)\n", result.Name, result.Version)
})
Highlights¶
- Configure once, render anywhere. The writer, format, theme and interactivity
are fixed on the
Renderer; every method reads them. No globals — trivially testable with abytes.Buffer. - One façade, six formats.
Write,Table,Render,Emit/EmitError,Status,ProgressandSpinshare the same configured destination and format. - Unicode-correct. Widths and truncation use display width, so CJK, emoji and combining characters never corrupt a table or a status line.
- Framework-free core. Importing
outputnever pulls cobra or pflag; the cobra wiring is the opt-inoutput/cobrasubpackage.
Where next¶
- Tutorial — getting started, then build a CLI that speaks text and JSON end to end.
- How-to guides — emit JSON, render tables, progress & spinners, theming, cobra wiring.
- Reference — formats & options on-site, and the full API on pkg.go.dev.
- Explanation — the Renderer façade, interactive vs plain rendering, framework-free core.
Part of the phpboyscout Go toolkit — small, framework-free Go modules extracted from go-tool-base.