tableprinter

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package tableprinter renders a command's result for a reader or for a machine. It holds the two layout primitives melange commands use:

  • TablePrinter (this file) for lists: a padded, width-fitted table with a ruled header for humans, or stable tab-separated lines for machines.
  • Fields (fields.go) for one record: aligned "Label value" blocks. A single object laid out as a one-row table pushes every value off the right edge, so detail views need their own shape. It is human-only — callers keep their own tab-separated branch.

Which layout a caller gets follows IOStreams.HumanOutput (a terminal, or --format table), not TTY-ness directly. Human output may evolve; the tab-separated form is a contract (no headers, no rule, no truncation, no color, no caption).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldOption

type FieldOption func(*field)

FieldOption customizes a single field.

func WithColor

func WithColor(fn func(string) string) FieldOption

WithColor colors the field in TTY mode when color is enabled. Padding is computed from the plain text, so ANSI sequences never skew alignment.

func WithTruncate

func WithTruncate(v bool) FieldOption

WithTruncate(false) opts the field out of terminal-width truncation.

type Fields added in v0.2.1

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

Fields renders one record as a title followed by aligned "Label value" lines and free-form paragraphs. It is the detail-view counterpart to TablePrinter: a single object is not a list, and laying one out as a one-row table pushes every value off the right edge.

Fields is TTY-only by construction — callers keep their own tab-separated branch for the machine contract, which must stay byte-stable. Label widths are computed rather than hardcoded, so adding a longer label cannot silently misalign the block.

func NewFields added in v0.2.1

func NewFields(ios *iostreams.IOStreams) *Fields

NewFields builds a Fields bound to ios.Out.

func (*Fields) Add added in v0.2.1

func (p *Fields) Add(label, value string)

Add appends a label/value row, skipping it when the value is empty: a detail view reads better without a column of blanks for data the object never had.

The value is sanitized to a single line here rather than at render time, because the rendered block also carries our own color escapes — sanitizing the assembled output would strip those too. A field value that arrived with an embedded newline would otherwise break the alignment of everything after it.

func (*Fields) Paragraph added in v0.2.1

func (p *Fields) Paragraph(s string)

Paragraph appends a free-form block, separated from its neighbors by a blank line. Newlines are preserved; other terminal controls are removed.

func (*Fields) Render added in v0.2.1

func (p *Fields) Render() error

Render writes the record to the output stream.

func (*Fields) Title added in v0.2.1

func (p *Fields) Title(s string)

Title sets the heading printed above the fields.

type TablePrinter

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

TablePrinter accumulates rows and renders them on Render.

func New

func New(ios *iostreams.IOStreams) *TablePrinter

New builds a TablePrinter bound to ios.Out, keyed off whether the caller wants human output (a TTY, or --format table).

func (*TablePrinter) AddField

func (t *TablePrinter) AddField(s string, opts ...FieldOption)

AddField appends a field to the current row.

func (*TablePrinter) Caption added in v0.2.1

func (t *TablePrinter) Caption(s string)

Caption sets a one-line summary printed under the table, dimmed, in TTY mode only — it tells a reader how much they are looking at without disturbing the tab-separated machine contract.

func (*TablePrinter) EndRow

func (t *TablePrinter) EndRow()

EndRow finishes the current row.

func (*TablePrinter) HeaderRow

func (t *TablePrinter) HeaderRow(cols ...string)

HeaderRow sets the column headers. In TTY mode they render uppercased (and dimmed when color is enabled) above a rule that separates them from the data; in non-TTY mode headers are omitted entirely. The header row is prepended to the accumulated rows, so it may be called at any point before Render, regardless of AddField/EndRow ordering.

func (*TablePrinter) Heading added in v0.2.1

func (t *TablePrinter) Heading(s string)

Heading sets a label printed above the table, preceded by a blank line, in human mode only. Reports stack several tables in one response and each needs naming; without this the callers hand-print the label and the table primitive owns only half of its own layout.

func (*TablePrinter) Render

func (t *TablePrinter) Render() error

Render writes all rows to the output stream.

Jump to

Keyboard shortcuts

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