table

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package table implements a row-based data set that can the printed with a header and space aligned columns. The text output looks like a spreadsheet.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotStruct = errors.New("not a struct")

ErrNotStruct is returned from a Table's Write method if the argument is not a struct.

Functions

func WithColor

func WithColor(c *Colors) func(*Table)

WithColor is an option setting function for New. It replaces the default set of Colors with c.

func WithLabelFunction

func WithLabelFunction(fn func(string) string) func(*Table)

WithLabelFunction is an option setting function for New. This function convert struct field names into text header labels. The default behavior is to convert the CamelCase field names into UPPER_CASE labels. The "table" struct tags can be used to override this.

func WithWriter

func WithWriter(w io.Writer) func(*Table)

WithWriter is an option setting function for New. It replaces the default io.Writer with w. The io.Writer is used for all Table output.

Types

type Colors

type Colors struct {
	Header     []sgr.Param
	EvenRow    []sgr.Param
	OddRow     []sgr.Param
	Empty      []sgr.Param
	Repeat     []sgr.Param
	Annotation []sgr.Param
}

Colors is the set styles/colors to be applied to Table elements.

type Table

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

Table holds a slice of structs that can be Flush()ed as a Text table, or encoded as JSON or YAML.

func New

func New(opts ...func(*Table)) *Table

New returns a new Table. The default settings can be overridden using the With* options setting functions. For example: WithColors() can be used to replace the default coloring scheme.

func NewJSON

func NewJSON(opts ...func(*Table)) *Table

NewJSON returns a Table with JSON as the default for `Flush`.

func NewYAML

func NewYAML(opts ...func(*Table)) *Table

NewYAML returns a Table with YAML as the default for `Flush`.

func (*Table) Annotate added in v0.4.0

func (t *Table) Annotate(s string)

Annotate inserts a string into the table as a row. This is useful for inserting comments or other information that is not a struct. The string will be printed as-is, without any formatting or coloring.

Annotations are only used in text output, and are ignored in JSON or YAML formats.

func (*Table) Flush

func (t *Table) Flush() error

Flush writes the table to its writer in its default style.

func (*Table) FlushJSON

func (t *Table) FlushJSON() error

FlushJSON flushes the Table data to its io.Writer as JSON.

func (*Table) FlushText

func (t *Table) FlushText()

FlushText flushes the Table data to its io.Writer as column aligned ANSI styled text. If the io.Writer is not a terminal no ANSI styles will be applied.

func (*Table) FlushYAML

func (t *Table) FlushYAML() error

FlushYAML flushes the Table data to its io.Writer as YAML.

func (*Table) Write

func (t *Table) Write(a any)

Write appends the struct a to the table as a row. The current table will be flushed if a new struct type is written. If a is not a struct, an error table will be added to the output.

Directories

Path Synopsis
Package main shows some features of the table package.
Package main shows some features of the table package.
sgr
Package sgr is a minimalist package for setting ANSI color terminal escape sequences.
Package sgr is a minimalist package for setting ANSI color terminal escape sequences.
color
Package color provides default foreground and background color convenience variables.
Package color provides default foreground and background color convenience variables.

Jump to

Keyboard shortcuts

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