Documentation ¶
Overview ¶
Create & Generate text based table
Example (Autowrap) ¶
var multiline = `A multiline string with some lines being really long.` const ( testRow = iota testHeader testFooter testFooter2 ) for mode := testRow; mode <= testFooter2; mode++ { for _, autoFmt := range []bool{false, true} { if mode == testRow && autoFmt { // Nothing special to test, skip continue } for _, autoWrap := range []bool{false, true} { for _, reflow := range []bool{false, true} { if !autoWrap && reflow { // Invalid configuration, skip continue } fmt.Println("mode", mode, "autoFmt", autoFmt, "autoWrap", autoWrap, "reflow", reflow) t := NewWriter(os.Stdout) t.SetAutoFormatHeaders(autoFmt) t.SetAutoWrapText(autoWrap) t.SetReflowDuringAutoWrap(reflow) if mode == testHeader { t.SetHeader([]string{"woo", multiline}) } else { t.SetHeader([]string{"woo", "waa"}) } if mode == testRow { t.Append([]string{"woo", multiline}) } else { t.Append([]string{"woo", "waa"}) } if mode == testFooter { t.SetFooter([]string{"woo", multiline}) } else if mode == testFooter2 { t.SetFooter([]string{"", multiline}) } else { t.SetFooter([]string{"woo", "waa"}) } t.Render() } } } fmt.Println() }
Output: mode 0 autoFmt false autoWrap false reflow false +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | woo | A multiline | | | string with some lines being really long. | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ mode 0 autoFmt false autoWrap true reflow false +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | woo | A multiline | | | | | | string with some lines being | | | really long. | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ mode 0 autoFmt false autoWrap true reflow true +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | woo | A multiline string with some | | | lines being really long. | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ mode 1 autoFmt false autoWrap false reflow false +-----+-------------------------------------------+ | woo | A multiline | | | string with some lines being really long. | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ mode 1 autoFmt false autoWrap true reflow false +-----+--------------------------------+ | woo | A multiline | | | | | | string with some lines being | | | really long. | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ mode 1 autoFmt false autoWrap true reflow true +-----+--------------------------------+ | woo | A multiline string with some | | | lines being really long. | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ mode 1 autoFmt true autoWrap false reflow false +-----+-------------------------------------------+ | WOO | A MULTILINE | | | STRING WITH SOME LINES BEING REALLY LONG | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | WOO | WAA | +-----+-------------------------------------------+ mode 1 autoFmt true autoWrap true reflow false +-----+--------------------------------+ | WOO | A MULTILINE | | | | | | STRING WITH SOME LINES BEING | | | REALLY LONG | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | WOO | WAA | +-----+--------------------------------+ mode 1 autoFmt true autoWrap true reflow true +-----+--------------------------------+ | WOO | A MULTILINE STRING WITH SOME | | | LINES BEING REALLY LONG | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | WOO | WAA | +-----+--------------------------------+ mode 2 autoFmt false autoWrap false reflow false +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | woo | A multiline | | | string with some lines being really long. | +-----+-------------------------------------------+ mode 2 autoFmt false autoWrap true reflow false +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | woo | A multiline | | | | | | string with some lines being | | | really long. | +-----+--------------------------------+ mode 2 autoFmt false autoWrap true reflow true +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | woo | A multiline string with some | | | lines being really long. | +-----+--------------------------------+ mode 2 autoFmt true autoWrap false reflow false +-----+-------------------------------------------+ | WOO | WAA | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | WOO | A MULTILINE | | | STRING WITH SOME LINES BEING REALLY LONG | +-----+-------------------------------------------+ mode 2 autoFmt true autoWrap true reflow false +-----+--------------------------------+ | WOO | WAA | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | WOO | A MULTILINE | | | | | | STRING WITH SOME LINES BEING | | | REALLY LONG | +-----+--------------------------------+ mode 2 autoFmt true autoWrap true reflow true +-----+--------------------------------+ | WOO | WAA | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | WOO | A MULTILINE STRING WITH SOME | | | LINES BEING REALLY LONG | +-----+--------------------------------+ mode 3 autoFmt false autoWrap false reflow false +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | A multiline | | string with some lines being really long. | +-----+-------------------------------------------+ mode 3 autoFmt false autoWrap true reflow false +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | A multiline | | | | string with some lines being | | really long. | +-----+--------------------------------+ mode 3 autoFmt false autoWrap true reflow true +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | A multiline string with some | | lines being really long. | +-----+--------------------------------+ mode 3 autoFmt true autoWrap false reflow false +-----+-------------------------------------------+ | WOO | WAA | +-----+-------------------------------------------+ | woo | waa | +-----+-------------------------------------------+ | A MULTILINE | | STRING WITH SOME LINES BEING REALLY LONG | +-----+-------------------------------------------+ mode 3 autoFmt true autoWrap true reflow false +-----+--------------------------------+ | WOO | WAA | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | A MULTILINE | | | | STRING WITH SOME LINES BEING | | REALLY LONG | +-----+--------------------------------+ mode 3 autoFmt true autoWrap true reflow true +-----+--------------------------------+ | WOO | WAA | +-----+--------------------------------+ | woo | waa | +-----+--------------------------------+ | A MULTILINE STRING WITH SOME | | LINES BEING REALLY LONG | +-----+--------------------------------+
Index ¶
- Constants
- func Color(colors ...int) []int
- func ConditionString(cond bool, valid, inValid string) string
- func DisplayWidth(str string) int
- func Pad(s, pad string, width int) string
- func PadLeft(s, pad string, width int) string
- func PadRight(s, pad string, width int) string
- func Title(name string) string
- func WrapString(s string, lim int) ([]string, int)
- func WrapWords(words []string, spc, lim, pen int) [][]string
- type Border
- type Colors
- type Table
- func (t *Table) Append(row []string)
- func (t *Table) AppendBulk(rows [][]string)
- func (t *Table) ClearFooter()
- func (t *Table) ClearHeaders()
- func (t *Table) ClearRows()
- func (t *Table) DisableHeaders(val bool)
- func (t *Table) NumLines() int
- func (t *Table) Render()
- func (t *Table) RenderFooter()
- func (t *Table) RenderHeader()
- func (t *Table) RenderRows()
- func (t *Table) Rich(row []string, colors []Colors)
- func (t *Table) SetAlignment(align int)
- func (t *Table) SetAutoFormatHeaders(auto bool)
- func (t *Table) SetAutoMergeCells(auto bool)
- func (t *Table) SetAutoMergeCellsByColumnIndex(cols []int)
- func (t *Table) SetAutoWrapText(auto bool)
- func (t *Table) SetBorder(border bool)
- func (t *Table) SetBorders(border Border)
- func (t *Table) SetCaption(caption bool, captionText ...string)
- func (t *Table) SetCenterSeparator(sep string)
- func (t *Table) SetColMinWidth(column int, width int)
- func (t *Table) SetColWidth(width int)
- func (t *Table) SetColumnAlignment(keys []int)
- func (t *Table) SetColumnColor(colors ...Colors)
- func (t *Table) SetColumnSeparator(sep string)
- func (t *Table) SetFooter(keys []string)
- func (t *Table) SetFooterAlignment(fAlign int)
- func (t *Table) SetFooterColor(colors ...Colors)
- func (t *Table) SetHeader(keys []string)
- func (t *Table) SetHeaderAlignment(hAlign int)
- func (t *Table) SetHeaderColor(colors ...Colors)
- func (t *Table) SetHeaderLine(line bool)
- func (t *Table) SetNewLine(nl string)
- func (t *Table) SetNoWhiteSpace(allow bool)
- func (t *Table) SetReflowDuringAutoWrap(auto bool)
- func (t *Table) SetRowLine(line bool)
- func (t *Table) SetRowSeparator(sep string)
- func (t *Table) SetStructs(v interface{}) error
- func (t *Table) SetTablePadding(padding string)
Examples ¶
Constants ¶
const ( CENTER = "+" ROW = "-" COLUMN = "|" SPACE = " " NEWLINE = "\n" )
const ( ALIGN_DEFAULT = iota ALIGN_CENTER ALIGN_RIGHT ALIGN_LEFT )
const ( BgBlackColor int = iota + 40 BgRedColor BgGreenColor BgYellowColor BgBlueColor BgMagentaColor BgCyanColor BgWhiteColor )
const ( FgBlackColor int = iota + 30 FgRedColor FgGreenColor FgYellowColor FgBlueColor FgMagentaColor FgCyanColor FgWhiteColor )
const ( BgHiBlackColor int = iota + 100 BgHiRedColor BgHiGreenColor BgHiYellowColor BgHiBlueColor BgHiMagentaColor BgHiCyanColor BgHiWhiteColor )
const ( FgHiBlackColor int = iota + 90 FgHiRedColor FgHiGreenColor FgHiYellowColor FgHiBlueColor FgHiMagentaColor FgHiCyanColor FgHiWhiteColor )
const ( Normal = 0 Bold = 1 UnderlineSingle = 4 Italic )
const ESC = "\033"
const (
MAX_ROW_WIDTH = 30
)
const SEP = ";"
Variables ¶
This section is empty.
Functions ¶
func ConditionString ¶
Simple Condition for string Returns value based on condition
func DisplayWidth ¶
func WrapString ¶
Wrap wraps s into a paragraph of lines of length lim, with minimal raggedness.
func WrapWords ¶
WrapWords is the low-level line-breaking algorithm, useful if you need more control over the details of the text wrapping process. For most uses, WrapString will be sufficient and more convenient.
WrapWords splits a list of words into lines with minimal "raggedness", treating each rune as one unit, accounting for spc units between adjacent words on each line, and attempting to limit lines to lim units. Raggedness is the total error over all lines, where error is the square of the difference of the length of the line and lim. Too-long lines (which only happen when a single word is longer than lim units) have pen penalty units added to the error.
Types ¶
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Example ¶
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 := NewWriter(os.Stdout) table.SetHeader([]string{"Name", "Sign", "Rating"}) table.SetCenterSeparator("*") table.SetRowSeparator("=") for _, v := range data { table.Append(v) } 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 NewCSV ¶
Start A new table by importing from a CSV file Takes io.Writer and csv File name
Example ¶
table, _ := NewCSV(os.Stdout, "testdata/test.csv", true) table.SetCenterSeparator("*") table.SetRowSeparator("=") table.Render()
Output: *============*===========*=========* | FIRST NAME | LAST NAME | SSN | *============*===========*=========* | John | Barry | 123456 | | Kathy | Smith | 687987 | | Bob | McCornick | 3979870 | *============*===========*=========*
func NewCSVReader ¶
Start a New Table Writer with csv.Reader
This enables customisation such as reader.Comma = ';' See http://golang.org/src/pkg/encoding/csv/reader.go?s=3213:3671#L94
func (*Table) AppendBulk ¶
Allow Support for Bulk Append Eliminates repeated for loops
func (*Table) ClearHeaders ¶ added in v0.0.7
func (t *Table) ClearHeaders()
ClearHeaders - clears stored headers
func (*Table) DisableHeaders ¶ added in v0.0.7
DisableHeaders - set a flag, which disable/enable headers rendering on Render method
func (*Table) RenderFooter ¶ added in v0.0.7
func (t *Table) RenderFooter()
RenderFooter - renders footer only
func (*Table) RenderHeader ¶ added in v0.0.7
func (t *Table) RenderHeader()
RenderHeader - renders headers only
func (*Table) RenderRows ¶ added in v0.0.7
func (t *Table) RenderRows()
RenderRows - renders rows only
func (*Table) SetAutoFormatHeaders ¶
Turn header autoformatting on/off. Default is on (true).
func (*Table) SetAutoMergeCells ¶
Set Auto Merge Cells This would enable / disable the merge of cells with identical values
func (*Table) SetAutoMergeCellsByColumnIndex ¶ added in v0.0.7
Set Auto Merge Cells By Column Index This would enable / disable the merge of cells with identical values for specific columns If cols is empty, it is the same as `SetAutoMergeCells(true)`.
func (*Table) SetAutoWrapText ¶
Turn automatic multiline text adjustment on/off. Default is on (true).
func (*Table) SetBorders ¶
func (*Table) SetCaption ¶
Set table Caption
func (*Table) SetCenterSeparator ¶
Set the center Separator
func (*Table) SetColMinWidth ¶
Set the minimal width for a column
func (*Table) SetColumnAlignment ¶
func (*Table) SetColumnColor ¶
Adding column colors (ANSI codes)
func (*Table) SetColumnSeparator ¶
Set the Column Separator
func (*Table) SetFooterAlignment ¶
Set Footer Alignment
func (*Table) SetFooterColor ¶
Adding column colors (ANSI codes)
func (*Table) SetHeaderAlignment ¶
Set Header Alignment
func (*Table) SetHeaderColor ¶
Adding header colors (ANSI codes)
func (*Table) SetHeaderLine ¶
Set Header Line This would enable / disable a line after the header
func (*Table) SetNoWhiteSpace ¶ added in v0.0.7
Set No White Space
func (*Table) SetReflowDuringAutoWrap ¶
Turn automatic reflowing of multiline text when rewrapping. Default is on (true).
func (*Table) SetRowLine ¶
Set Row Line This would enable / disable a line on each row of the table
func (*Table) SetStructs ¶ added in v0.0.7
SetStructs sets header and rows from slice of struct. If something that is not a slice is passed, 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.
func (*Table) SetTablePadding ¶ added in v0.0.7
Set Table Padding