Help us shape the future of Go! Take the 2026 Go Developer Survey

table

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package table renders a result table as an ansi box-drawing table on a terminal and as markdown everywhere else. The style constants are shared so every command that prints a table looks like the same program.

Index

Constants

View Source
const (
	BoxTopLeft     = "╭"
	BoxTopRight    = "╮"
	BoxBottomLeft  = "╰"
	BoxBottomRight = "╯"
	BoxHorizontal  = "─"
	BoxVertical    = "│"
	BoxTeeDown     = "┬"
	BoxTeeUp       = "┴"
	BoxTeeRight    = "├"
	BoxTeeLeft     = "┤"
	BoxCross       = "┼"
)

Box drawing characters of the ansi table.

View Source
const (
	ColorReset     = "\033[0m"
	ColorSeparator = "\033[38;5;238m"
	ColorHeader    = "\033[38;5;146m"
	ColorAmber     = "\033[38;5;214m"
	ColorGreen     = "\033[38;5;114m"
	ColorWhite     = "\033[38;5;255m"
	ColorRed       = "\033[38;5;167m"
	ColorDim       = "\033[38;5;244m"
)

Colors of the ansi table. They are 256-color codes rather than the basic eight, so a row keeps the same shade whatever palette the terminal ships.

Variables

This section is empty.

Functions

func IsTerminal

func IsTerminal(w io.Writer) bool

IsTerminal reports whether w is a tty, which is what decides between the box-drawing table and markdown when the caller has not said which it wants.

Types

type Align

type Align int

Align is how a column pads its cells.

const (
	// Left pads a cell on the right, which is what a label wants.
	Left Align = iota
	// Right pads a cell on the left, which is what a number wants.
	Right
)

type Cell

type Cell struct {
	Text  string
	Color string
}

Cell is one value and the color it is printed in. The color is dropped in markdown, so a caller sets it once rather than branching per output format.

func Colored

func Colored(color, value string) Cell

Colored returns a cell printed in color on a terminal.

func Text

func Text(value string) Cell

Text returns an uncolored cell.

type Column

type Column struct {
	Title string
	Align Align
}

Column is one column of a Table.

type Table

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

Table buffers rows and renders them once the widest cell of every column is known. A check that collects its findings before reporting them gains nothing from streaming, and a buffered table sizes its columns from the data rather than guessing at them up front.

func New

func New(w io.Writer, markdown bool, columns ...Column) *Table

New returns a table writing to w. Markdown is what a redirected stdout and a report file get; a terminal gets the box-drawing table.

func (*Table) Flush

func (t *Table) Flush()

Flush writes the table. It is called once: the rows are kept so the columns can be sized, not so the table can be reprinted.

func (*Table) Row

func (t *Table) Row(cells ...Cell)

Row appends a row. Cells past the last column are dropped and missing ones are left empty, so a caller never has to pad a short row itself.

func (*Table) Summary

func (t *Table) Summary(format string, args ...any)

Summary writes a line below the table, in the header color on a terminal. The blank line before it is not decoration: markdown ends a table at the first line that is not a row, so a summary butted against one is read as part of it.

Jump to

Keyboard shortcuts

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