Documentation
¶
Overview ¶
Package table provides simple table formatting functions for command line applications
Example ¶
package main import ( "os" "github.com/jayloop/table" ) func main() { t := table.New("key", "value") t.Precision(2, 1) t.Row("a", 1) t.Row("b", 2.0) t.Row("c", 0.001) t.Sort(1) t.Print(os.Stdout) }
Output: key value c 0.00 a 1 b 2.00
Index ¶
- func DefaultHeaderFormat(f FormatFunc)
- type CodeANSI
- type FormatFunc
- type Table
- func (t *Table) AddStruct(m interface{})
- func (t *Table) FormatCols(fn FormatFunc, cols ...int)
- func (t *Table) FormatHeader(fn FormatFunc)
- func (t *Table) FormatNotZero(fn FormatFunc, cols ...int)
- func (t *Table) FormatRows(fn FormatFunc, rows ...int)
- func (t *Table) Len() int
- func (t *Table) Less(i, j int) bool
- func (t *Table) MaxWidth(chars int, cols ...int)
- func (t *Table) Padding(p int)
- func (t *Table) Precision(digits int, cols ...int)
- func (t *Table) Print(out io.Writer) error
- func (t *Table) Row(values ...interface{})
- func (t *Table) Sort(cols ...int)
- func (t *Table) Swap(i, j int)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultHeaderFormat ¶
func DefaultHeaderFormat(f FormatFunc)
DefaultHeaderFormat sets the default format to use for all new tables.
Types ¶
type CodeANSI ¶
type CodeANSI int
CodeANSI is an ANSI escape code attribute
Bright ANSI colors
const ( Reset CodeANSI = iota Bold Faint Italic Underline BlinkSlow BlinkRapid Reverse Conceal CrossedOut )
ANSI decorations, most not widely supported
func Background ¶
Background converts a foreground color value to it's corresponding background value
type FormatFunc ¶
FormatFunc is a user defined function applying formatting to a header or row value. The typical usecase is setting colors by adding escape characters. A format function should not change the printed length of the value.
func Format ¶
func Format(attr ...CodeANSI) FormatFunc
Format returns a formating function applying ANSI escape codes for the given attributes. Please note that different terminals may support some or none of the colors and decorations.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
A Table record stores all table data and formatting options. It is not safe for concurrent use.
func New ¶
New creates a new table with the given headers. The number of headers decides the number of columns of the table.
func (*Table) AddStruct ¶
func (t *Table) AddStruct(m interface{})
AddStruct adds 2-column rows to a table by iterating over struct fields. The table is created by a previous call to New:
table.New("key", "value")
func (*Table) FormatCols ¶
func (t *Table) FormatCols(fn FormatFunc, cols ...int)
FormatCols adds a format function for the listed column indexes.
func (*Table) FormatHeader ¶
func (t *Table) FormatHeader(fn FormatFunc)
FormatHeader sets to format applied to column headers when printing
func (*Table) FormatNotZero ¶
func (t *Table) FormatNotZero(fn FormatFunc, cols ...int)
FormatNotZero adds a format function applied on values != "0"
func (*Table) FormatRows ¶
func (t *Table) FormatRows(fn FormatFunc, rows ...int)
FormatRows adds a format function for the listed rows indexes. Use row index -1 to denote the last row.
func (*Table) Precision ¶
Precision sets the number of digits to include when printing float values. It must be set before adding the rows.
func (*Table) Print ¶
Print prints the table headers and rows to a io.Writer. Any error returned is from the underlying io.Writer.