table

package
v0.0.24 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMaxRowWidth is the default maximum width for table row cells.
	// Messages longer than this will be wrapped at word boundaries.
	DefaultMaxRowWidth = 100
)

Variables

View Source
var DefaultTableOptions = []tablewriter.Option{
	tablewriter.WithHeaderAlignment(tw.AlignLeft),
	tablewriter.WithRowAutoWrap(tw.WrapNormal),
	tablewriter.WithRowMaxWidth(DefaultMaxRowWidth),
	tablewriter.WithRendition(tw.Rendition{
		Settings: tw.Settings{
			Separators: tw.Separators{
				BetweenColumns: tw.Off,
				BetweenRows:    tw.Off,
			},
			Lines: tw.Lines{
				ShowTop:        tw.On,
				ShowBottom:     tw.On,
				ShowHeaderLine: tw.On,
			},
		},
	}),
}

DefaultTableOptions provides a clean, minimal table style with left-aligned headers and no borders or separators.

Functions

This section is empty.

Types

type Column

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

Column represents a table column with its header name and optional formatters. Multiple formatters can be chained and will be applied in sequence.

func NewColumn

func NewColumn(name string) Column

NewColumn creates a new column with the specified header name.

func (Column) Fn

func (c Column) Fn(formatter ColumnFormatter) Column

Fn appends a custom Go function formatter to this column's formatter chain. Can be chained with other formatters: Column().JQ(...).Fn(...)

func (Column) JQ

func (c Column) JQ(query string) Column

JQ appends a JQ query formatter to this column's formatter chain. The query will be executed against each row value to extract the column value. Can be chained with other formatters: Column().JQ(...).Fn(...)

type ColumnFormatter

type ColumnFormatter func(value any) any

ColumnFormatter is a function that transforms a value for display in a specific column.

func ChainFormatters

func ChainFormatters(formatters ...ColumnFormatter) ColumnFormatter

ChainFormatters composes multiple formatters into a single formatter pipeline. The output of each formatter is passed as input to the next formatter. This enables building transformation pipelines like: JQ extraction → colorization → truncation.

func JQFormatter

func JQFormatter(query string) ColumnFormatter

JQFormatter creates a ColumnFormatter that executes a jq query on the input value. Uses the jq.Query utility which properly handles unstructured types.

type Option

type Option[T any] = util.Option[Renderer[T]]

Option is a functional option for configuring a Renderer.

func WithFormatter

func WithFormatter[T any](columnName string, formatter ColumnFormatter) Option[T]

WithFormatter adds a column-specific formatter function.

func WithHeaders

func WithHeaders[T any](headers ...string) Option[T]

WithHeaders sets the column headers for the table.

func WithTableOptions

func WithTableOptions[T any](values ...tablewriter.Option) Option[T]

WithTableOptions sets the underlying tablewriter options.

func WithWriter

func WithWriter[T any](w io.Writer) Option[T]

WithWriter sets the output writer for the table renderer.

type Renderer

type Renderer[T any] struct {
	// contains filtered or unexported fields
}

Renderer provides a flexible interface for creating and rendering tables. T is the type of objects that will be appended to the table.

func NewRenderer

func NewRenderer[T any](opts ...Option[T]) *Renderer[T]

NewRenderer creates a new table renderer with the given tableOptions.

func NewWithColumns

func NewWithColumns[T any](writer io.Writer, columns ...Column) *Renderer[T]

NewWithColumns creates a new table renderer with columns defined using the fluent API. This provides a more declarative way to define tables with JQ queries or custom formatters. Supports chaining formatters: Column().JQ(...).Fn(...)

Example:

renderer := table.NewWithColumns(os.Stdout,
    table.NewColumn("NAME").JQ(".metadata.name").Fn(strings.ToUpper),
    table.NewColumn("TYPE").JQ(".kind"),
    table.NewColumn("READY").JQ(`.status.conditions[] | select(.type=="Ready") | .status // "Unknown"`),
)

func (*Renderer[T]) Append

func (r *Renderer[T]) Append(value T) error

Append adds a single row to the table. Accepts either []any (legacy) or a struct (auto-extracted via mapstructure).

func (*Renderer[T]) AppendAll

func (r *Renderer[T]) AppendAll(rows []T) error

AppendAll adds multiple rows to the table in a single operation. Each item in the slice can be either []any or a struct.

func (*Renderer[T]) GetHeaders

func (r *Renderer[T]) GetHeaders() []string

GetHeaders returns the currently configured table headers.

func (*Renderer[T]) Render

func (r *Renderer[T]) Render() error

Render outputs the table to the configured writer.

func (*Renderer[T]) SetHeaders

func (r *Renderer[T]) SetHeaders(headers ...string)

SetHeaders configures table headers dynamically after renderer creation.

Jump to

Keyboard shortcuts

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