table

package
v0.0.0-...-9ce4fc3 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package table provides a simple API for outputting tabular data to stdout. It is used to implement --format=table.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Table

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

Table represents a set of simple tabular data. Tables have a list of header cells and a list of rows. Each row must be the same length as the list of header cells. Tables can be formatted nicely to stdout. Construct a table with the New or FromStructs functions, and then use the AddRow, SortBy, and Print methods.

func FromStructs

func FromStructs(structs interface{}) Table

FromStructs creates a new table from the given slice of structs. The table headers are generated from the struct field reflection metadata: each struct field must have a reflection metadata key "pretty" whose value is the header to display. The only allowed field types in the struct are string and []string. The strings are used as table cells directly, while the slices are concatenated with commas first.

func New

func New(headers ...string) Table

New creates a new table with the given headers. The table has no rows; add them with AddRow. The headers should all be unique.

func (*Table) AddRow

func (t *Table) AddRow(row ...string)

AddRow adds a row at the end of a table. The length of the row must be the same as the number of headers in the table, or a panic will be generated.

func (*Table) Print

func (t *Table) Print()

Print writes the table to stdout, aligning columns by inserting whitespace. If the table is too wide for the current terminal, and the 'less' utility is installed, Print invokes it with the -S option to truncate long lines and allow horizontal scrolling.

func (*Table) SortBy

func (t *Table) SortBy(header string)

SortBy sorts a table by the column with the given header. The header must exist in the table, or a panic is generated. Since tables cannot have duplicate headers, any column can be specified unambiguously.

Jump to

Keyboard shortcuts

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