output

package
v0.15.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyFilterFlag

func ApplyFilterFlag(cmd *cobra.Command) error

ApplyFilterFlag reads the --filter persistent flag from the root command and sets the corresponding flags on cmd. The filter string is a comma-separated list of key=value pairs where keys are matched to flag names (converted to kebab-case). If a flag is not found, the plural form (appending "s") is also tried.

Example: --filter "status=ACTIVE,currency=DKK" sets --status ACTIVE --currency DKK (or --statuses ACTIVE if --status doesn't exist).

func ExtractFields

func ExtractFields(data any, columns []Column) [][]string

ExtractFields pulls column values out of data for each row. data may be a slice/array (one row per element) or a single map[string]any / struct (treated as one row).

func FilterFields

func FilterFields(data any, fields []string) any

FilterFields filters data to include only the specified field paths. It accepts map[string]any, []any, or []map[string]any data structures. Field paths use dot notation for nested fields (e.g., "worker.name"). If fields is empty, data is returned unchanged.

func IsTTYFile

func IsTTYFile(f *os.File) bool

IsTTYFile is a convenience wrapper: returns true when f is a terminal.

Types

type Column

type Column struct {
	Header string
	Field  string
	Width  int
}

Column describes a single table column: header text, the JSON field path used to extract values from data, and an optional fixed width.

func FilterColumns

func FilterColumns(columns []Column, selected string) []Column

FilterColumns returns only the columns whose Field matches one of the comma-separated names in selected. If selected is empty, all columns are returned unchanged. The result preserves the order from selected.

type Format

type Format string

Format controls how output is rendered.

const (
	FormatJSON  Format = "json"
	FormatTable Format = "table"
)

type Formatter

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

Formatter writes structured data as either JSON or an ASCII table.

func Auto

func Auto(w io.Writer, noColor bool) *Formatter

Auto creates a Formatter that picks the format automatically: table when the writer is a terminal, JSON otherwise.

func New

func New(w io.Writer, format Format, noColor bool) *Formatter

New creates a Formatter with an explicit format choice.

func (*Formatter) Format

func (f *Formatter) Format() Format

Format returns the configured output format.

func (*Formatter) Print

func (f *Formatter) Print(data any, columns []Column) error

Print dispatches to PrintJSON or PrintTable based on the configured format.

func (*Formatter) PrintJSON

func (f *Formatter) PrintJSON(data any) error

PrintJSON marshals data as indented JSON and writes it to the output writer.

func (*Formatter) PrintTable

func (f *Formatter) PrintTable(data any, columns []Column) error

PrintTable renders data as an ASCII table using the provided column definitions. data must be a slice/array or a single object.

Jump to

Keyboard shortcuts

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