output

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatJSON  = "json"
	FormatTable = "table"
)

Variables

View Source
var ValidFormats = []string{FormatJSON, FormatTable}

ValidFormats lists the output formats accepted by the --format flag.

Functions

func Truncate

func Truncate(s string, max int) string

Truncate shortens s to at most max runes, appending "..." when it must cut. It slices on rune boundaries, so the result is always valid UTF-8 even when s contains multibyte characters.

func ValidateFormat

func ValidateFormat(format string) error

ValidateFormat reports whether format is an accepted --format value. The empty string is allowed: it represents the unset flag, which callers resolve to a concrete format based on TTY detection and command type.

Types

type Column

type Column struct {
	// Header is the column title, written in Title Case (e.g., "Key Name").
	// It is automatically uppercased when rendered in a table.
	Header string
	Value  func(any) string
}

func Col

func Col[T any](header string, value func(T) string) Column

Col builds a Column from a typed accessor, removing the per-call-site v.(T) assertion. Value is still stored as func(any) string, so Col-built columns mix freely with TableFromTags-derived ones.

type DynamicTableDef

type DynamicTableDef struct {
	Headers []string
	Rows    [][]string
}

type Field

type Field struct {
	Label string
	Value string
}

func FieldsFromTags

func FieldsFromTags(v any) []Field

FieldsFromTags derives detail Fields from the `detail:"Label"` struct tags on v, in field order. Fields without a `detail` tag are skipped, so computed or conditional fields can be appended by the caller.

type Formatter

type Formatter interface {
	FormatField() string
}

Formatter renders a detail field value as a single string. A field whose value implements Formatter is rendered via FormatField instead of the primitive switch in formatField, letting non-primitive types carry their own table representation while keeping JSON output governed by their json tags.

type TableDef

type TableDef struct {
	Columns []Column
}

func TableFromTags

func TableFromTags[T any]() TableDef

TableFromTags derives a TableDef from the `col:"Header"` struct tags on T, in field order. Each column reads its field by reflection and formats it the same way the hand-written closures did (strings as-is, bools via FormatBool, integers and floats via strconv). Fields without a `col` tag are skipped, so computed columns can be appended to the returned TableDef explicitly.

type Writer

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

func New

func New(out io.Writer, format string) *Writer

func (*Writer) Write

func (w *Writer) Write(data any, td TableDef) error

func (*Writer) WriteDeleted

func (w *Writer) WriteDeleted(id, msg string) error

func (*Writer) WriteDynamic

func (w *Writer) WriteDynamic(data any, td DynamicTableDef) error

func (*Writer) WriteFields

func (w *Writer) WriteFields(data any, fields []Field) error

func (*Writer) WriteList

func (w *Writer) WriteList(data any, td TableDef, emptyMessage string) error

WriteList renders a slice the same as Write, except that an empty slice in table mode prints emptyMessage on its own line instead of a header-only table. JSON mode is unchanged and always emits the slice (e.g. []).

func (*Writer) WriteMessage

func (w *Writer) WriteMessage(data any, line string) error

WriteMessage emits data as JSON, or a single human-readable line in table mode. An empty line writes nothing in table mode. It covers the "JSON object, or one status line" shape that previously required callers to pass a table-building closure.

Jump to

Keyboard shortcuts

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