Documentation
¶
Index ¶
- Constants
- Variables
- func Truncate(s string, max int) string
- func ValidateFormat(format string) error
- type Column
- type DynamicTableDef
- type Field
- type Formatter
- type TableDef
- type Writer
- func (w *Writer) Write(data any, td TableDef) error
- func (w *Writer) WriteDeleted(id, msg string) error
- func (w *Writer) WriteDynamic(data any, td DynamicTableDef) error
- func (w *Writer) WriteFields(data any, fields []Field) error
- func (w *Writer) WriteList(data any, td TableDef, emptyMessage string) error
- func (w *Writer) WriteMessage(data any, line string) error
Constants ¶
const ( FormatJSON = "json" FormatTable = "table" )
Variables ¶
var ValidFormats = []string{FormatJSON, FormatTable}
ValidFormats lists the output formats accepted by the --format flag.
Functions ¶
func Truncate ¶
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 ¶
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 DynamicTableDef ¶
type Field ¶
func FieldsFromTags ¶
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 ¶
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 (*Writer) WriteDeleted ¶
func (*Writer) WriteDynamic ¶
func (w *Writer) WriteDynamic(data any, td DynamicTableDef) error
func (*Writer) WriteList ¶
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 ¶
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.