Documentation
¶
Overview ¶
Package prettytable is a library for Golang to build a simple text table with a multibyte, doublewidth character support.
Example ¶
package main
import (
"github.com/tatsushid/go-prettytable"
)
func main() {
tbl, err := prettytable.NewTable([]prettytable.Column{
{Header: "COL1"},
{Header: "COL2", MinWidth: 6},
{Header: "COL3", AlignRight: true},
}...)
if err != nil {
panic(err)
}
tbl.Separator = " | "
tbl.AddRow("foo", "bar", "baz")
tbl.AddRow(1, 2.3, 4)
tbl.Print()
}
Output: COL1 | COL2 | COL3 foo | bar | baz 1 | 2.3 | 4
Example (DoublewidthChars) ¶
package main
import (
"github.com/tatsushid/go-prettytable"
)
func main() {
tbl, err := prettytable.NewTable([]prettytable.Column{
{Header: "名前"},
{Header: "個数", AlignRight: true},
}...)
if err != nil {
panic(err)
}
tbl.Separator = " | "
tbl.AddRow("りんご", 5)
tbl.AddRow("みかん", 3)
tbl.AddRow("柿", 2)
tbl.Print()
}
Output: 名前 | 個数 りんご | 5 みかん | 3 柿 | 2
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Separator = " "
Package wide column separator, used as default when NewTable is called.
Functions ¶
This section is empty.
Types ¶
type Column ¶
type Column struct {
// Column name used as its header
Header string
// If this value is true, the column is aligned to the right.
AlignRight bool
// Minimal width of column. If Header or column value's length is
// larger than it, the column width is extended to the length.
MinWidth int
// Maximal width of column. If Header or column value's length is
// larger than it, the header or value is truncated
MaxWidth int
// contains filtered or unexported fields
}
Column is used for column definitions of a table
type Table ¶
type Table struct {
// If this value is true, a header line isn't generated
NoHeader bool
// Column separator characters. Separator package value is default
Separator string
// contains filtered or unexported fields
}
Table represents a table itself and has its option values
func NewTable ¶
NewTable defines a table with columns and returns *Table. It returns an error if no columns are passed or passed invalid columns, for example, MinWidth is larger than MaxWidth
func (*Table) AddRow ¶
AddRow adds a row with given values. It returns an error if no values are passed or a number of values is larger than a number of columns.
It converts values into strings by following rules
- If a value fulfills fmt.Stringer interface, it is converted into its String() function result
- If a value is an integer or a float, it is converted into a decimal number string.
- If a value is a bool, it is converted into "true" or "false" string
- If a value is a string, it is used as is.
- If a value is a []byte or []rune, it is converted int string
- Otherwise, an error is returned