output

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllTypes = []Type{
	JSONType,
	JQType,
	TableType,
	PrettyType,
}

AllTypes lists the output types that every command supports.

Functions

func Filter

func Filter(objects map[any]any, filterList []string) map[any]any

func JSONStringToInterface added in v0.26.0

func JSONStringToInterface(jsonString string) interface{}

func PrintJSONChecked added in v0.35.0

func PrintJSONChecked(objects interface{}, format string, rawStrings bool, writer io.Writer) error

PrintJSONChecked preserves JSON versus jq raw-string output semantics while returning filter/encoding/write failures to the command. Results are buffered so a runtime filter error does not leave a misleading partial JSON stream. Existing Print callers retain their historical logging behavior.

func PrintJSONEvent added in v0.33.2

func PrintJSONEvent(writer io.Writer, data []byte) error

PrintJSONEvent writes one event immediately: indented JSON on a terminal, or a single compact JSON line when redirected. It preserves JSON numbers.

func StyledText added in v0.35.0

func StyledText(writer io.Writer, text string, attributes ...color.Attribute) string

StyledText uses the same terminal and colour preferences as table cells.

func ValidateJSONFormat added in v0.35.0

func ValidateJSONFormat(format string) error

ValidateJSONFormat checks filters before callers perform requests or edits.

Types

type AddFlagsOption added in v0.29.0

type AddFlagsOption func(*addFlagsConfig)

AddFlagsOption is a functional option for AddFlagsWithOpts.

func WithAdditionalTypes added in v0.29.0

func WithAdditionalTypes(types ...Type) AddFlagsOption

WithAdditionalTypes adds extra output types to the flag help text.

func WithDefaultOutput added in v0.29.0

func WithDefaultOutput(t Type) AddFlagsOption

WithDefaultOutput sets the fixed default output type.

func WithDefaultPipe added in v0.31.0

func WithDefaultPipe(t Type) AddFlagsOption

WithDefaultPipe sets the output type used when stdout is not a TTY and the user has not specified --output.

func WithDefaultTTY added in v0.31.0

func WithDefaultTTY(t Type) AddFlagsOption

WithDefaultTTY sets the output type used when stdout is a TTY and the user has not specified --output. Implies WithDefaultPipe must also be set; together they cause the flag default to become "auto".

func WithFilterFlagDisabled added in v0.29.0

func WithFilterFlagDisabled() AddFlagsOption

WithFilterFlagDisabled disables the -f / --filter flag.

func WithPrettyBinding added in v0.31.0

func WithPrettyBinding(pb PrettyBinding) AddFlagsOption

WithPrettyBinding enables pretty output and sets its binding.

func WithTableBinding added in v0.29.0

func WithTableBinding(tb *TableBinding) AddFlagsOption

WithTableBinding enables table output and sets its binding.

type Graph added in v0.33.2

type Graph struct {
	Title  string
	Points []GraphPoint
	Bars   bool
	// Width is measured in terminal columns; zero detects the terminal size.
	Width int
}

Graph renders a time series or horizontal ranking without changing the data.

func (Graph) Print added in v0.33.2

func (g Graph) Print(writer io.Writer) error

Print writes a labelled graph. Empty datasets produce a message, not fake zeros.

type GraphPoint added in v0.33.2

type GraphPoint struct {
	Label string
	URL   string
	Value float64
	Time  time.Time
}

GraphPoint is an observation or a ranked value. Time is required for line charts.

type Hyperlink struct {
	Text string
	URL  string
}

Hyperlink keeps a cell's label separate from its destination so truncation and alignment happen before terminal escape sequences are added.

func (Hyperlink) String added in v0.33.2

func (h Hyperlink) String() string

type JSON

type JSON struct {
	Objects interface{}
	Format  string
}

func NewJSON

func NewJSON(objects interface{}, format string) *JSON

func NewJSONFromString added in v0.26.0

func NewJSONFromString(objects string, format string) *JSON

func (*JSON) Print

func (j *JSON) Print(writer io.Writer)

type Output

type Output struct {
	Type          string
	Filter        []string
	Format        string
	TableBinding  *TableBinding
	PrettyBinding PrettyBinding
	// contains filtered or unexported fields
}

Output holds the output configuration for a command.

func (*Output) AddFlags deprecated

func (o *Output) AddFlags(cmd *cobra.Command, defaultOutput Type, additionalTypes ...Type)

AddFlags adds output-related flags to cmd using a positional default and optional extra types.

Deprecated: prefer AddFlagsWithOpts which uses the functional-option pattern and supports smart TTY/pipe defaults.

func (*Output) AddFlagsWithOpts added in v0.29.0

func (o *Output) AddFlagsWithOpts(cmd *cobra.Command, opts ...AddFlagsOption)

AddFlagsWithOpts adds output-related flags to cmd using the functional-option pattern. When both WithDefaultTTY and WithDefaultPipe are provided the flag default becomes "auto", which resolves at print-time based on whether stdout is a terminal.

func (*Output) ConfiguredOutputTypes added in v0.17.0

func (o *Output) ConfiguredOutputTypes() []string

func (*Output) ConfiguredOutputTypesString added in v0.17.0

func (o *Output) ConfiguredOutputTypesString() string

func (*Output) Print

func (o *Output) Print(cmd *cobra.Command, objects any)

Print outputs objects using the configured output type.

type Pretty added in v0.31.0

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

Pretty holds the state for a grouped pretty-print run.

func (*Pretty) AddItem added in v0.31.0

func (p *Pretty) AddItem(section, item string)

AddItem adds an item string to a named section, creating the section if needed.

func (*Pretty) InitSection added in v0.31.0

func (p *Pretty) InitSection(name string)

InitSection registers a section without any items.

func (*Pretty) Print added in v0.31.0

func (p *Pretty) Print(writer io.Writer)

Print renders the grouped output to writer. When stdout is a TTY the section headers are rendered in bold green and line-number prefixes (digits followed by ':') are rendered in yellow.

type PrettyBinding added in v0.31.0

type PrettyBinding func(interface{}, *Pretty)

PrettyBinding populates a Pretty from objects.

type StyledCell added in v0.35.0

type StyledCell struct {
	Text  string
	Color color.Attribute
}

StyledCell adds a semantic colour to a table cell while retaining its plain string representation for width calculations and redirected output.

func (StyledCell) String added in v0.35.0

func (c StyledCell) String() string

type Table

type Table struct {
	Headings []interface{}
	Rows     [][]interface{}
	// TrimSpace trims leading and trailing whitespace from every cell before rendering.
	TrimSpace bool
	// ColumnMaxWidths holds the maximum character width for each column (0 = unlimited).
	ColumnMaxWidths []int
}

func NewTable

func NewTable(headings []interface{}, rows [][]interface{}) *Table

func TableFromObjects

func TableFromObjects(objects interface{}, headings []string, processObjects func(interface{}, *Table)) *Table

func (*Table) AddHeadings

func (t *Table) AddHeadings(headings ...interface{})

func (*Table) AddHeadingsS

func (t *Table) AddHeadingsS(headings ...string)

func (*Table) AddRow

func (t *Table) AddRow(rowValues ...interface{})

func (*Table) AddRowS

func (t *Table) AddRowS(rowValues ...string)

func (*Table) Print

func (t *Table) Print(writer io.Writer)

type TableBinding

type TableBinding struct {
	Headings []string
	// ProcessObjects receives the full object collection and populates the table.
	// Use this when per-row extraction needs custom logic (e.g. nested maps).
	ProcessObjects func(interface{}, *Table)
	// RowExtractor is a simpler alternative to ProcessObjects.
	// When set (and ProcessObjects is nil) the library iterates over the
	// object collection and calls RowExtractor for each element.
	// Return nil to skip a row.
	RowExtractor func(interface{}) []string
	// ColumnMaxWidths holds the maximum character width for each column (0 = unlimited).
	ColumnMaxWidths []int
	// TrimSpace trims leading/trailing whitespace from every cell before rendering.
	TrimSpace bool
}

TableBinding configures how objects are rendered as a table.

type Type added in v0.17.0

type Type string

Type is the output format identifier.

const (
	JSONType   Type = "json"
	JQType     Type = "jq"
	TableType  Type = "table"
	PrettyType Type = "pretty"
	// AutoType selects ttyDefault or pipeDefault based on whether stdout is a TTY.
	// It is used as the flag default when WithDefaultTTY / WithDefaultPipe are set.
	AutoType Type = "auto"

	// WebType is a special type for opening a browser.
	// It is not included in AllTypes and must be passed as an additional type.
	WebType Type = "web"
)

These are the different output types.

Source Files

  • checked_json.go
  • color.go
  • filter.go
  • graph.go
  • hyperlink.go
  • jq.go
  • json.go
  • json_event.go
  • pretty.go
  • root.go
  • table.go
  • width.go

Jump to

Keyboard shortcuts

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