Documentation
¶
Overview ¶
Package table provides a simple table builder with a fluent API.
Index ¶
Examples ¶
Constants ¶
const ( // Ignore html tags and treat entities (starting with '&' // and ending in ';') as single characters (width = 1). FilterHTML uint = 1 << iota // Strip Escape characters bracketing escaped text segments // instead of passing them through unchanged with the text. StripEscape // Force right-alignment of cell content. // Default is left-alignment. AlignRight // Handle empty columns as if they were not present in // the input in the first place. DiscardEmptyColumns // Always use tabs for indentation columns (i.e., padding of // leading empty cells on the left) independent of padchar. TabIndent // Print a vertical bar ('|') between columns (after formatting). // Discarded columns appear as zero-width columns ("||"). Debug )
const (
// this is the default header name used when a simple go value is passed to the table
UnknownHeaderName = "Unknown"
)
Variables ¶
This section is empty.
Functions ¶
func NewBuilder ¶
NewBuilder creates a new table builder choice to create a table
Types ¶
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is a simple abstraction for tex/tabwriter golang package
Example (Builder) ¶
ExampleTable_builder shows how tou use the builder pattern and regular methods
buf := new(bytes.Buffer)
t := NewBuilder(buf).
WithMinWidth(10).
WithTabWidth(10).
WithPadding(10).
WithPadChar('.').
WithFlags(AlignRight | Debug).
WithSep("\t").
WithHeader([]string{"Name", "Age", "City"}).
WithRow([]any{"John", "30", "New York"}...).
WithRow([]any{"Jane", "20", "London"}...).
Build()
t.AddRow([]any{"Jack", "40", "Paris"}...)
t.Render()
fmt.Println(buf.String())
Output: ..........Name|..........Age|City ..........John|...........30|New York ..........Jane|...........20|London ..........Jack|...........40|Paris
Example (Builder2) ¶
ExampleTable_builder2 shows how tou use the builder pattern and regular methods in different order and code style
out := new(bytes.Buffer)
t := NewBuilder(out).
WithFlags(Debug)
// add header and rows before building
t.WithHeader([]string{"Name", "Age", "City"})
t.WithRow([]any{"John", "30", "New York"}...)
t.WithRow([]any{"Jane", "20", "London"}...)
t.WithRow([]any{"Jack", "40", "Paris"}...)
t.WithRow([]any{"Christian", "47", "Barcelona"}...)
// build the table
table := t.Build()
// add a new row after building
table.AddRow([]any{"Ely", "50", "Barcelona"}...)
// render the table
table.Render()
fmt.Println(out.String())
Output: Name |Age |City John |30 |New York Jane |20 |London Jack |40 |Paris Christian |47 |Barcelona Ely |50 |Barcelona
func New ¶
func New(output io.Writer, opts ...TableOption) *Table
New creates a new table
Example ¶
ExampleNew shows how to create a new table
buf := new(bytes.Buffer)
t := New(buf)
t.SetHeader([]string{"Name", "Age", "City"})
t.AddRow([]any{"John", "30", "New York"}...)
t.AddRow([]any{"Jane", "20", "London"}...)
t.AddRow([]any{"Jack", "40", "Paris"}...)
t.Render()
fmt.Println(buf.String())
Output: Name Age City John 30 New York Jane 20 London Jack 40 Paris
Example (WithOptions) ¶
ExampleNew shows how to create a new table with options
buf := new(bytes.Buffer)
t := New(
buf,
WithMinWidth(10),
WithTabWidth(10),
WithPadding(10),
WithPadChar('.'),
WithFlags(AlignRight|Debug),
WithSep("\t"),
)
t.SetHeader([]string{"Name", "Age", "City"})
t.AddRow([]any{"John", "30", "New York"}...)
t.AddRow([]any{"Jane", "20", "London"}...)
t.AddRow([]any{"Jack", "40", "Paris"}...)
t.Render()
fmt.Println(buf.String())
Output: ..........Name|..........Age|City ..........John|...........30|New York ..........Jane|...........20|London ..........Jack|...........40|Paris
Example (WithOptions_csv) ¶
ExampleNew_withOptions_csv shows how to output a csv
buf := new(bytes.Buffer)
t := New(buf, WithSep(","))
t.SetHeader([]string{"Name", "Age", "City"})
t.AddRow([]any{"John", "30", "New York"}...)
t.AddRow([]any{"Jane", "20", "London"}...)
t.AddRow([]any{"Jack", "40", "Paris"}...)
t.Render()
fmt.Println(buf.String())
Output: Name,Age,City John,30,New York Jane,20,London Jack,40,Paris
func (*Table) AddRowsf ¶
AddRowsf adds multiple rows to the table, matrix style, using a format string
func (*Table) SetHeader ¶
SetHeader sets the header of the table
Example (NoCall) ¶
ExampleTable_SetHeader_noCall show the default behavior when no header is set
buf := new(bytes.Buffer)
t := New(buf)
// t.SetHeader([]string{"Name", "Age", "City"}) // with no headers, show Unknown[1,2,3...]
t.AddRow([]any{"John", "30", "New York"}...)
t.AddRow([]any{"Jane", "20", "London"}...)
t.AddRow([]any{"Jack", "40", "Paris"}...)
t.Render()
fmt.Println(buf.String())
Output: Unknown1 Unknown2 Unknown3 John 30 New York Jane 20 London Jack 40 Paris
type TableOption ¶
type TableOption func(*tableOptions)
TableOption is a function that configures a table
func WithMinWidth ¶
func WithMinWidth(minWidth int) TableOption
WithMinWidth sets the minimum width of the table
func WithPadChar ¶
func WithPadChar(padChar byte) TableOption
WithPadChar sets the pad char of the table
func WithPadding ¶
func WithPadding(padding int) TableOption
WithPadding sets the padding of the table
func WithTabWidth ¶
func WithTabWidth(tabWidth int) TableOption
WithTabWidth sets the tab width of the table