Documentation ¶
Overview ¶
Package tablewriter exposes a utility to render tabular data as text.
Index ¶
- Constants
- type Border
- type CellWrapper
- type CellWrapperFactory
- type Formatter
- type HAlignment
- type Option
- func WithAllBorders(enabled bool) Option
- func WithBorders(border Border) Option
- func WithCaption(caption string) Option
- func WithCaptionFormatter(formatter Formatter) Option
- func WithCellAlignment(align HAlignment) Option
- func WithCellWrapper(factory func(*Table) CellWrapper) Option
- func WithCenterSeparator(sep string) Option
- func WithColAlignment(align map[int]HAlignment) Option
- func WithColFormatters(formatters map[int]Formatter) Option
- func WithColMaxWidth(column int, width int) Option
- func WithColMaxWidths(maxWidths map[int]int) Option
- func WithColMinWidth(column int, width int) Option
- func WithColWidth(width int) Option
- func WithColumnSeparator(sep string) Option
- func WithFooter(footer []string) Option
- func WithFooterAlignment(footerAlign HAlignment) Option
- func WithFooterFormatters(formatters map[int]Formatter) Option
- func WithFooterLine(enabled bool) Option
- func WithHeader(header []string) Option
- func WithHeaderAlignment(align HAlignment) Option
- func WithHeaderFormatters(formatters map[int]Formatter) Option
- func WithHeaderLine(enabled bool) Option
- func WithMarkdown(enabled bool) Option
- func WithMaxTableWidth(width int, opts ...wrap.Option) Option
- func WithMergeCells(enabled bool) Option
- func WithNewLine(nl string) Option
- func WithNoWhiteSpace(enabled bool) Option
- func WithPadding(padding string) Option
- func WithRowLine(enabled bool) Option
- func WithRowSeparator(sep string) Option
- func WithRows(rows [][]string) Option
- func WithTitledHeader(enabled bool) Option
- func WithTitler(titler Titler) Option
- func WithWrap(enabled bool) Option
- func WithWriter(writer io.Writer) Option
- type Table
- type Titler
- Bugs
Examples ¶
Constants ¶
const ( CENTER = "+" ROW = "-" COLUMN = "|" SPACE = " " NEWLINE = "\n" NOPADDING = "" )
Default separator characters.
const (
// MaxColWidth is the default maximum width of a column.
MaxColWidth = 30
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CellWrapper ¶
CellWrapper knows how to wrap the content of a table cell into multiple lines.
The wrapper knows about the display constraints.
A few useful wrappers are provided by the package tablewrappers.
type CellWrapperFactory ¶
type CellWrapperFactory func(*Table) CellWrapper
CellWrapperFactory produces a cell wrapper with the knowledge of the table to be rendered.
type Formatter ¶
type Formatter = func(interface{}) aurora.Value
Formatter is a formatting function from the github.com/logrusorgru/aurora/v4 package, to be used for nice terminal formatting such as colors, bold face, etc.
It wraps some argument with an appropriate ANSI terminal escape sequence.
Example ¶
data := sampleData() table := tablewriter.New( tablewriter.WithHeader([]string{"Name", "Sign", "Rating"}), tablewriter.WithRows(data), tablewriter.WithHeaderFormatters(map[int]tablewriter.Formatter{ 0: aurora.Red, 1: aurora.Blue, 2: aurora.Bold, }), ) table.Render()
Output: +------+-----------------------+--------+ | �[31mNAME�[0m | �[34m SIGN �[0m | �[1mRATING�[0m | +------+-----------------------+--------+ | A | The Good | 500 | | B | The Very very Bad Man | 288 | | C | The Ugly | 120 | | D | The Gopher | 800 | +------+-----------------------+--------+
type HAlignment ¶
type HAlignment uint8
HAlignment describes how to horizontally align an element in a cell.
const ( AlignDefault HAlignment = iota AlignCenter AlignRight AlignLeft )
Horizontal alignment
type Option ¶
type Option func(*options)
Option to render a table
Example ¶
package main import ( "os" "github.com/fredbi/tablewriter" ) func main() { data := [][]string{ {"Learn East has computers with adapted keyboards with enlarged print etc", "Some Data ", "Another Data "}, {"Instead of lining up the letters all ", "the way across, he splits the keyboard in two", "Like most ergonomic keyboards", "See Data"}, } table := tablewriter.New( tablewriter.WithWriter(os.Stdout), // default is os.Stdout tablewriter.WithHeader([]string{"Name", "Sign", "Rating"}), tablewriter.WithCenterSeparator("*"), // default is '+' tablewriter.WithRowSeparator("="), // default is '-' ) for _, v := range data { table.Append(v) // an alternative to WithRows(data) } table.Render() }
Output: *=============================*===============================*===============================*==========* | NAME | SIGN | RATING | | *=============================*===============================*===============================*==========* | Learn East has computers | Some Data | Another Data | | | with adapted keyboards with | | | | | enlarged print etc | | | | | Instead of lining up the | the way across, he splits the | Like most ergonomic keyboards | See Data | | letters all | keyboard in two | | | *=============================*===============================*===============================*==========*
func WithAllBorders ¶
WithAllBorders enables (resp. disables) all table borders.
Borders are enabled by default.
func WithBorders ¶
WithBorders allows for a detailed specification of which borders are rendered.
func WithCaption ¶
WithCaption displays a caption under the table.
func WithCaptionFormatter ¶
WithCaptionFormatter allows to specify ANSI terminal control sequences to format the table caption.
func WithCellAlignment ¶
func WithCellAlignment(align HAlignment) Option
WithCellAlignment defines the default alignment for row cells.
The default is CENTER for strings, RIGHT for numbers (and %).
func WithCellWrapper ¶
func WithCellWrapper(factory func(*Table) CellWrapper) Option
WithCellWrapper allows to inject a customized cell content CellWrapper.
Specifying a cell wrapper overrides any StringWrapper setting.
func WithCenterSeparator ¶
WithCenterSeparator defines the string used to represent intersections of the table grid.
The default is '+'.
func WithColAlignment ¶
func WithColAlignment(align map[int]HAlignment) Option
WithColAlignment defines the aligment for a set of columns.
func WithColFormatters ¶
WithColFormatters allows to specify ANSI terminal control sequences to format cells by columns.
func WithColMaxWidth ¶
WithColMaxWidth defines the maximum width for a specific column.
This overrides the setting defined by WithColWidth.
func WithColMaxWidths ¶
WithColMaxWidths defines the maximum width for a set of columns.
func WithColMinWidth ¶
WithColMinWidth specifies the minimum width of columns.
func WithColWidth ¶
WithColWidth defines the maximum width for all columns (in characters).
The default is 30.
func WithColumnSeparator ¶
WithColumnSeparator defines the character to separate columns.
The default is '|'.
func WithFooter ¶
WithFooter specifies the footer fields for this table.
func WithFooterAlignment ¶
func WithFooterAlignment(footerAlign HAlignment) Option
WithFooterAlignment defines the alignment for all footer fields.
The default is CENTER.
func WithFooterFormatters ¶
WithFooterFormatters allows to specify ANSI terminal control sequences to format the footer.
In particular this may be used to colorize the footer.
func WithFooterLine ¶
WithFooterLine prints a separation line under the footer.
This is enabled by default.
func WithHeader ¶
WithHeader specifies the header fields for this table.
func WithHeaderAlignment ¶
func WithHeaderAlignment(align HAlignment) Option
WithHeaderAlignment defines the alignment for all headings.
The default is CENTER.
func WithHeaderFormatters ¶
WithHeaderFormatters allows to specify ANSI terminal control sequences to format the header.
In particular this may be used to colorize the header.
func WithHeaderLine ¶
WithHeaderLine prints a separation line under the header.
This is enabled by default.
func WithMarkdown ¶
WithMarkdown reproduces classifical markdown tables.
This option is a shortcut to:
WithCenterSeparator("|") WithBorders(Border{Left: true, Top: false, Right: true, Bottom: false})
func WithMaxTableWidth ¶
WithMaxTableWidth defines a maximum display width for the table.
This options injects a CellWrapper that automatically determine width constraints on columns. This option overrides max widths per column that could have been specified otherwise.
Options determine how aggressive the wrapper can be: e.g. if individual words may be split.
Example ¶
package main import ( "github.com/fredbi/tablewriter" ) func sampleData() [][]string { return [][]string{ {"A", "The Good", "500"}, {"B", "The Very very Bad Man", "288"}, {"C", "The Ugly", "120"}, {"D", "The Gopher", "800"}, } } func main() { data := sampleData() // adapt the width of the table to the maximum display size table := tablewriter.New( tablewriter.WithHeader([]string{"Name", "Sign", "Rating"}), tablewriter.WithRows(data), tablewriter.WithMaxTableWidth(30), ) table.Render() }
Output: +------+------------+--------+ | NAME | SIGN | RATING | +------+------------+--------+ | A | The Good | 500 | | B | The Very | 288 | | | very Bad | | | | Man | | | C | The Ugly | 120 | | D | The Gopher | 800 | +------+------------+--------+
func WithMergeCells ¶
WithMergeCells enables the merging of adjacent cells with the same value.
func WithNoWhiteSpace ¶
WithNoWhiteSpace packs the table by removing some padding.
This is disabled by default.
Example ¶
package main import ( "fmt" "os" "github.com/fredbi/tablewriter" ) func main() { data := [][]string{ {"Learn East has computers with adapted keyboards with enlarged print etc", "Some Data ", "Another Data "}, {"Instead of lining up the letters all ", "the way across, he splits the keyboard in two", "Like most ergonomic keyboards", "See Data"}, } options := []tablewriter.Option{ tablewriter.WithWriter(os.Stdout), // default is os.Stdout tablewriter.WithHeader([]string{"Name", "Sign", "Rating"}), tablewriter.WithCenterSeparator("*"), // default is '+' tablewriter.WithRowSeparator("="), // default is '-' tablewriter.WithRows(data), } // packed without extraneous blank space table := tablewriter.New(append(options, tablewriter.WithNoWhiteSpace(true), )...) table.Render() fmt.Println() // default layout table = tablewriter.New(append(options, tablewriter.WithNoWhiteSpace(false), )...) table.Render() }
Output:
func WithPadding ¶
WithPadding defines the padding character inside the table.
The default is a blank space.
func WithRowLine ¶
WithRowLine indicates that each row is followed by a separation line.
By default, rows are packed without line separator.
func WithRowSeparator ¶
WithRowSeparator defines the string used to separate rows.
The default is '-'.
Example ¶
data := sampleData() red := func(in string) string { return aurora.Sprintf(aurora.Red(in)) } // prints a colorized grid table := tablewriter.New( tablewriter.WithHeader([]string{"Name", "Sign", "Rating"}), tablewriter.WithRows(data), tablewriter.WithRowSeparator(red(tablewriter.ROW)), tablewriter.WithColumnSeparator(red(tablewriter.COLUMN)), tablewriter.WithCenterSeparator(red(tablewriter.CENTER)), ) table.Render()
Output: �[31m+�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m+�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m+�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m+�[0m �[31m|�[0m NAME �[31m|�[0m SIGN �[31m|�[0m RATING �[31m|�[0m �[31m+�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m+�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m+�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m+�[0m �[31m|�[0m A �[31m|�[0m The Good �[31m|�[0m 500 �[31m|�[0m �[31m|�[0m B �[31m|�[0m The Very very Bad Man �[31m|�[0m 288 �[31m|�[0m �[31m|�[0m C �[31m|�[0m The Ugly �[31m|�[0m 120 �[31m|�[0m �[31m|�[0m D �[31m|�[0m The Gopher �[31m|�[0m 800 �[31m|�[0m �[31m+�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m+�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m+�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m-�[0m�[31m+�[0m
func WithRows ¶
WithRows specifies the rows of the table, each being a record of columns.
The input is not required to contain the same number of columns for each row.
func WithTitledHeader ¶
WithTitledHeader autoformats headers and footer as titles. This is enabled by default.
Whenever enabled, the default titler is being used. The title string is trimmed, uppercased. Underscores are replaced by blank spaces.
func WithTitler ¶
WithTitler injects a Titler to apply to header and footer values.
This overrides the WithTitledHeader() option.
func WithWrap ¶
WithWrap enables content wrapping inside columns to abide by column width constraints.
Wrapping is enabled by default (the default maximum column width is 30 characters).
Whenever enabled, the default wrapper is used. The default wrapper wraps cells into multiline content, based on their maximum column width, wrapping only on word boundaries.
Example ¶
package main import ( "fmt" "os" "github.com/fredbi/tablewriter" ) func main() { const multiline = `A multiline string with some lines being really long.` type testMode uint8 const ( // test mode testRow testMode = iota testHeader testFooter testFooter2 ) for mode := testRow; mode <= testFooter2; mode++ { for _, titled := range []bool{false, true} { if mode == testRow && titled { // Nothing special to test, skip continue } for _, wrapped := range []bool{false, true} { fmt.Println("mode:", mode, "titled:", titled, "wrapped:", wrapped) options := []tablewriter.Option{ tablewriter.WithWriter(os.Stdout), tablewriter.WithTitledHeader(titled), tablewriter.WithWrap(wrapped), } switch mode { case testHeader: options = append(options, tablewriter.WithHeader([]string{"woo", multiline})) options = append(options, tablewriter.WithFooter([]string{"woo", "waa"})) options = append(options, tablewriter.WithRows([][]string{{"woo", "waa"}})) case testRow: options = append(options, tablewriter.WithHeader([]string{"woo", "waa"})) options = append(options, tablewriter.WithFooter([]string{"woo", "waa"})) options = append(options, tablewriter.WithRows([][]string{{"woo", multiline}})) case testFooter: options = append(options, tablewriter.WithHeader([]string{"woo", "waa"})) options = append(options, tablewriter.WithFooter([]string{"woo", multiline})) options = append(options, tablewriter.WithRows([][]string{{"woo", "waa"}})) case testFooter2: options = append(options, tablewriter.WithHeader([]string{"woo", "waa"})) options = append(options, tablewriter.WithFooter([]string{"", multiline})) options = append(options, tablewriter.WithRows([][]string{{"woo", "waa"}})) } t := tablewriter.New(options...) t.Render() fmt.Println() } } } }
Output: mode: 0 titled: false wrapped: false +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | woo | A multiline | | | string with some lines being really long. | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ mode: 0 titled: false wrapped: true +-----+------------------------------+ | woo | waa | +-----+------------------------------+ | woo | A multiline string with some | | | lines being really long. | +-----+------------------------------+ | woo | waa | +-----+------------------------------+ mode: 1 titled: false wrapped: false +-----+-------------------------------------------+ | woo | A multiline | | | string with some lines being really long. | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ mode: 1 titled: false wrapped: true +-----+------------------------------+ | woo | A multiline string with some | | | lines being really long. | +-----+------------------------------+ | woo | waa | +-----+------------------------------+ | woo | waa | +-----+------------------------------+ mode: 1 titled: true wrapped: false +-----+-------------------------------------------+ | WOO | A MULTILINE | | | STRING WITH SOME LINES BEING REALLY LONG | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | WOO | WAA | +-----+-------------------------------------------+ mode: 1 titled: true wrapped: true +-----+------------------------------+ | WOO | A MULTILINE STRING WITH SOME | | | LINES BEING REALLY LONG | +-----+------------------------------+ | woo | waa | +-----+------------------------------+ | WOO | WAA | +-----+------------------------------+ mode: 2 titled: false wrapped: false +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | woo | A multiline | | | string with some lines being really long. | +-----+-------------------------------------------+ mode: 2 titled: false wrapped: true +-----+------------------------------+ | woo | waa | +-----+------------------------------+ | woo | waa | +-----+------------------------------+ | woo | A multiline string with some | | | lines being really long. | +-----+------------------------------+ mode: 2 titled: true wrapped: false +-----+-------------------------------------------+ | WOO | WAA | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | WOO | A MULTILINE | | | STRING WITH SOME LINES BEING REALLY LONG | +-----+-------------------------------------------+ mode: 2 titled: true wrapped: true +-----+------------------------------+ | WOO | WAA | +-----+------------------------------+ | woo | waa | +-----+------------------------------+ | WOO | A MULTILINE STRING WITH SOME | | | LINES BEING REALLY LONG | +-----+------------------------------+ mode: 3 titled: false wrapped: false +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | A multiline | | string with some lines being really long. | +-----+-------------------------------------------+ mode: 3 titled: false wrapped: true +-----+------------------------------+ | woo | waa | +-----+------------------------------+ | woo | waa | +-----+------------------------------+ | A multiline string with some | | lines being really long. | +-----+------------------------------+ mode: 3 titled: true wrapped: false +-----+-------------------------------------------+ | WOO | WAA | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | A MULTILINE | | STRING WITH SOME LINES BEING REALLY LONG | +-----+-------------------------------------------+ mode: 3 titled: true wrapped: true +-----+------------------------------+ | WOO | WAA | +-----+------------------------------+ | woo | waa | +-----+------------------------------+ | A MULTILINE STRING WITH SOME | | LINES BEING REALLY LONG | +-----+------------------------------+
func WithWriter ¶
WithWriter specifies the output writer to render this table.
The default is os.Stdout.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table renders a text table.
Example ¶
package main import ( "github.com/fredbi/tablewriter" ) func sampleData() [][]string { return [][]string{ {"A", "The Good", "500"}, {"B", "The Very very Bad Man", "288"}, {"C", "The Ugly", "120"}, {"D", "The Gopher", "800"}, } } func main() { data := sampleData() table := tablewriter.New( tablewriter.WithHeader([]string{"Name", "Sign", "Rating"}), tablewriter.WithRows(data), ) table.Render() }
Output: +------+-----------------------+--------+ | NAME | SIGN | RATING | +------+-----------------------+--------+ | A | The Good | 500 | | B | The Very very Bad Man | 288 | | C | The Ugly | 120 | | D | The Gopher | 800 | +------+-----------------------+--------+
func NewBuffered ¶
NewBuffered builds a new empty table writer that writes in a new bytes.Buffer.
Example ¶
package main import ( "fmt" "github.com/fredbi/tablewriter" ) func main() { data := [][]string{ {"Learn East has computers with adapted keyboards with enlarged print etc", "Some Data ", "Another Data "}, {"Instead of lining up the letters all ", "the way across, he splits the keyboard in two", "Like most ergonomic keyboards", "See Data"}, } table, buf := tablewriter.NewBuffered( tablewriter.WithHeader([]string{"Name", "Sign", "Rating"}), tablewriter.WithCenterSeparator("*"), // default is '+' tablewriter.WithRowSeparator("="), // default is '-' ) for _, v := range data { table.Append(v) } table.Render() // writes to buffer fmt.Println(buf) }
Output: *=============================*===============================*===============================*==========* | NAME | SIGN | RATING | | *=============================*===============================*===============================*==========* | Learn East has computers | Some Data | Another Data | | | with adapted keyboards with | | | | | enlarged print etc | | | | | Instead of lining up the | the way across, he splits the | Like most ergonomic keyboards | See Data | | letters all | keyboard in two | | | *=============================*===============================*===============================*==========*
func NewCSV ¶
NewCSV builds a Table writer that reads its rows from a csv.Reader.
Example ¶
package main import ( "encoding/csv" "log" "os" "github.com/fredbi/tablewriter" ) func main() { file, err := os.Open("testdata/test.csv") if err != nil { log.Fatal(err) } reader := csv.NewReader(file) table, err := tablewriter.NewCSV(reader, true, tablewriter.WithCenterSeparator("*"), tablewriter.WithRowSeparator("="), ) if err != nil { log.Fatal(err) } table.Render() }
Output: *============*===========*=========* | FIRST NAME | LAST NAME | SSN | *============*===========*=========* | John | Barry | 123456 | | Kathy | Smith | 687987 | | Bob | McCornick | 3979870 | *============*===========*=========*
func (Table) Overhead ¶
Overhead yields the amount extra padding and separators needed to display the table.
func (*Table) SetStructs ¶
SetStructs sets header and rows from slice of struct.
If something that is not a slice is passed, an error will be returned.
The tag specified by "tablewriter" for the struct becomes the header. If not specified or empty, the field name will be used.
The field of the first element of the slice is used as the header. If the element implements fmt.Stringer, the result will be used. And the slice contains nil, it will be skipped without rendering.
Notes ¶
Bugs ¶
this doesn't work well with noWhiteSpace
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package tablewrappers exposes different ways to wrap text under display width constraint.
|
Package tablewrappers exposes different ways to wrap text under display width constraint. |
Package titlers expose utilities to "title-ize" headings.
|
Package titlers expose utilities to "title-ize" headings. |