Documentation
¶
Overview ¶
Example (Simple) ¶
// simple table with zero customizations
tw := NewWriter()
// append a header row
tw.AppendHeader(Row{"#", "First Name", "Last Name", "Salary"})
// append some data rows
tw.AppendRows([]Row{
{1, "Arya", "Stark", 3000},
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
{300, "Tyrion", "Lannister", 5000},
})
// append a footer row
tw.AppendFooter(Row{"", "", "Total", 10000})
// render it
fmt.Printf("Table without any customizations:\n%s", tw.Render())
Output: Table without any customizations: +-----+------------+-----------+--------+-----------------------------+ | # | FIRST NAME | LAST NAME | SALARY | | +-----+------------+-----------+--------+-----------------------------+ | 1 | Arya | Stark | 3000 | | | 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! | | 300 | Tyrion | Lannister | 5000 | | +-----+------------+-----------+--------+-----------------------------+ | | | TOTAL | 10000 | | +-----+------------+-----------+--------+-----------------------------+
Example (Styled) ¶
// table with some amount of customization
tw := NewWriter()
// append a header row
tw.AppendHeader(Row{"First Name", "Last Name", "Salary"})
// append some data rows
tw.AppendRows([]Row{
{"Jaime", "Lannister", 5000},
{"Arya", "Stark", 3000, "A girl has no name."},
{"Sansa", "Stark", 4000},
{"Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
{"Tyrion", "Lannister", 5000, "A Lannister always pays his debts."},
})
// append a footer row
tw.AppendFooter(Row{"", "Total", 10000})
// auto-index rows
tw.SetAutoIndex(true)
// sort by last name and then by salary
tw.SortBy([]SortBy{{Name: "Last Name", Mode: Dsc}, {Name: "Salary", Mode: AscNumeric}})
// use a ready-to-use style
tw.SetStyle(StyleLight)
// customize the style and change some stuff
tw.Style().Format.Header = text.FormatLower
tw.Style().Format.Row = text.FormatLower
tw.Style().Format.Footer = text.FormatLower
tw.Style().Options.SeparateColumns = false
// render it
fmt.Printf("Table with customizations:\n%s", tw.Render())
Output: Table with customizations: ┌──────────────────────────────────────────────────────────────────────┐ │ first name last name salary │ ├──────────────────────────────────────────────────────────────────────┤ │ 1 arya stark 3000 a girl has no name. │ │ 2 sansa stark 4000 │ │ 3 jon snow 2000 you know nothing, jon snow! │ │ 4 jaime lannister 5000 │ │ 5 tyrion lannister 5000 a lannister always pays his debts. │ ├──────────────────────────────────────────────────────────────────────┤ │ total 10000 │ └──────────────────────────────────────────────────────────────────────┘
Index ¶
- Constants
- Variables
- func AutoIndexColumnID(colIdx int) string
- type BoxStyle
- type BoxStyleHorizontal
- type ColorOptions
- type ColumnConfig
- type FilterBy
- type FilterOperator
- type FormatOptions
- type HTMLOptions
- type MarkdownOptions
- type Options
- type Pager
- type PagerOption
- type Row
- type RowAttributes
- type RowConfig
- type RowPainter
- type RowPainterWithAttributes
- type SizeOptions
- type SortBy
- type SortMode
- type Style
- type Table
- func (t *Table) AppendFooter(row Row, config ...RowConfig)
- func (t *Table) AppendHeader(row Row, config ...RowConfig)
- func (t *Table) AppendRow(row Row, config ...RowConfig)
- func (t *Table) AppendRows(rows []Row, config ...RowConfig)
- func (t *Table) AppendSeparator()
- func (t *Table) FilterBy(filterBy []FilterBy)
- func (t *Table) ImportGrid(grid interface{}) bool
- func (t *Table) Length() int
- func (t *Table) Pager(opts ...PagerOption) Pager
- func (t *Table) Render() string
- func (t *Table) RenderCSV() string
- func (t *Table) RenderHTML() string
- func (t *Table) RenderMarkdown() string
- func (t *Table) RenderTSV() string
- func (t *Table) ResetFooters()
- func (t *Table) ResetHeaders()
- func (t *Table) ResetRows()
- func (t *Table) SetAllowedRowLength(length int)deprecated
- func (t *Table) SetAutoIndex(autoIndex bool)
- func (t *Table) SetCaption(format string, a ...interface{})
- func (t *Table) SetColumnConfigs(configs []ColumnConfig)
- func (t *Table) SetHTMLCSSClass(cssClass string)deprecated
- func (t *Table) SetIndexColumn(colNum int)
- func (t *Table) SetOutputMirror(mirror io.Writer)
- func (t *Table) SetPageSize(numLines int)
- func (t *Table) SetRowPainter(painter interface{})
- func (t *Table) SetStyle(style Style)
- func (t *Table) SetTitle(format string, a ...interface{})
- func (t *Table) SortBy(sortBy []SortBy)
- func (t *Table) Style() *Style
- func (t *Table) SuppressEmptyColumns()
- func (t *Table) SuppressTrailingSpaces()
- type TitleOptions
- type WidthEnforcer
- type Writer
Examples ¶
Constants ¶
const ( // DefaultHTMLCSSClass stores the css-class to use when none-provided via // SetHTMLCSSClass(cssClass string). DefaultHTMLCSSClass = "go-pretty-table" )
Variables ¶
var ( // StyleDefault renders a Table like below: // +-----+------------+-----------+--------+-----------------------------+ // | # | FIRST NAME | LAST NAME | SALARY | | // +-----+------------+-----------+--------+-----------------------------+ // | 1 | Arya | Stark | 3000 | | // | 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! | // | 300 | Tyrion | Lannister | 5000 | | // +-----+------------+-----------+--------+-----------------------------+ // | | | TOTAL | 10000 | | // +-----+------------+-----------+--------+-----------------------------+ StyleDefault = Style{ Name: "StyleDefault", Box: StyleBoxDefault, Color: ColorOptionsDefault, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsDefault, Size: SizeOptionsDefault, Title: TitleOptionsDefault, } // StyleBold renders a Table like below: // ┏━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ // ┃ # ┃ FIRST NAME ┃ LAST NAME ┃ SALARY ┃ ┃ // ┣━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ // ┃ 1 ┃ Arya ┃ Stark ┃ 3000 ┃ ┃ // ┃ 20 ┃ Jon ┃ Snow ┃ 2000 ┃ You know nothing, Jon Snow! ┃ // ┃ 300 ┃ Tyrion ┃ Lannister ┃ 5000 ┃ ┃ // ┣━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ // ┃ ┃ ┃ TOTAL ┃ 10000 ┃ ┃ // ┗━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ StyleBold = Style{ Name: "StyleBold", Box: StyleBoxBold, Color: ColorOptionsDefault, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsDefault, Size: SizeOptionsDefault, Title: TitleOptionsDefault, } // StyleColoredBright renders a Table without any borders or separators, // and with Black text on Cyan background for Header/Footer and // White background for other rows. StyleColoredBright = Style{ Name: "StyleColoredBright", Box: StyleBoxDefault, Color: ColorOptionsBright, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsDark, } // StyleColoredDark renders a Table without any borders or separators, and // with Header/Footer in Cyan text and other rows with White text, all on // Black background. StyleColoredDark = Style{ Name: "StyleColoredDark", Box: StyleBoxDefault, Color: ColorOptionsDark, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsBright, } // StyleColoredBlackOnBlueWhite renders a Table without any borders or // separators, and with Black text on Blue background for Header/Footer and // White background for other rows. StyleColoredBlackOnBlueWhite = Style{ Name: "StyleColoredBlackOnBlueWhite", Box: StyleBoxDefault, Color: ColorOptionsBlackOnBlueWhite, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsBlueOnBlack, } // StyleColoredBlackOnCyanWhite renders a Table without any borders or // separators, and with Black text on Cyan background for Header/Footer and // White background for other rows. StyleColoredBlackOnCyanWhite = Style{ Name: "StyleColoredBlackOnCyanWhite", Box: StyleBoxDefault, Color: ColorOptionsBlackOnCyanWhite, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsCyanOnBlack, } // StyleColoredBlackOnGreenWhite renders a Table without any borders or // separators, and with Black text on Green background for Header/Footer and // White background for other rows. StyleColoredBlackOnGreenWhite = Style{ Name: "StyleColoredBlackOnGreenWhite", Box: StyleBoxDefault, Color: ColorOptionsBlackOnGreenWhite, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsGreenOnBlack, } // StyleColoredBlackOnMagentaWhite renders a Table without any borders or // separators, and with Black text on Magenta background for Header/Footer and // White background for other rows. StyleColoredBlackOnMagentaWhite = Style{ Name: "StyleColoredBlackOnMagentaWhite", Box: StyleBoxDefault, Color: ColorOptionsBlackOnMagentaWhite, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsMagentaOnBlack, } // StyleColoredBlackOnYellowWhite renders a Table without any borders or // separators, and with Black text on Yellow background for Header/Footer and // White background for other rows. StyleColoredBlackOnYellowWhite = Style{ Name: "StyleColoredBlackOnYellowWhite", Box: StyleBoxDefault, Color: ColorOptionsBlackOnYellowWhite, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsYellowOnBlack, } // StyleColoredBlackOnRedWhite renders a Table without any borders or // separators, and with Black text on Red background for Header/Footer and // White background for other rows. StyleColoredBlackOnRedWhite = Style{ Name: "StyleColoredBlackOnRedWhite", Box: StyleBoxDefault, Color: ColorOptionsBlackOnRedWhite, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsRedOnBlack, } // StyleColoredBlueWhiteOnBlack renders a Table without any borders or // separators, and with Header/Footer in Blue text and other rows with // White text, all on Black background. StyleColoredBlueWhiteOnBlack = Style{ Name: "StyleColoredBlueWhiteOnBlack", Box: StyleBoxDefault, Color: ColorOptionsBlueWhiteOnBlack, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsBlackOnBlue, } // StyleColoredCyanWhiteOnBlack renders a Table without any borders or // separators, and with Header/Footer in Cyan text and other rows with // White text, all on Black background. StyleColoredCyanWhiteOnBlack = Style{ Name: "StyleColoredCyanWhiteOnBlack", Box: StyleBoxDefault, Color: ColorOptionsCyanWhiteOnBlack, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsBlackOnCyan, } // StyleColoredGreenWhiteOnBlack renders a Table without any borders or // separators, and with Header/Footer in Green text and other rows with // White text, all on Black background. StyleColoredGreenWhiteOnBlack = Style{ Name: "StyleColoredGreenWhiteOnBlack", Box: StyleBoxDefault, Color: ColorOptionsGreenWhiteOnBlack, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsBlackOnGreen, } // StyleColoredMagentaWhiteOnBlack renders a Table without any borders or // separators, and with Header/Footer in Magenta text and other rows with // White text, all on Black background. StyleColoredMagentaWhiteOnBlack = Style{ Name: "StyleColoredMagentaWhiteOnBlack", Box: StyleBoxDefault, Color: ColorOptionsMagentaWhiteOnBlack, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsBlackOnMagenta, } // StyleColoredRedWhiteOnBlack renders a Table without any borders or // separators, and with Header/Footer in Red text and other rows with // White text, all on Black background. StyleColoredRedWhiteOnBlack = Style{ Name: "StyleColoredRedWhiteOnBlack", Box: StyleBoxDefault, Color: ColorOptionsRedWhiteOnBlack, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsBlackOnRed, } // StyleColoredYellowWhiteOnBlack renders a Table without any borders or // separators, and with Header/Footer in Yellow text and other rows with // White text, all on Black background. StyleColoredYellowWhiteOnBlack = Style{ Name: "StyleColoredYellowWhiteOnBlack", Box: StyleBoxDefault, Color: ColorOptionsYellowWhiteOnBlack, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsNoBordersAndSeparators, Size: SizeOptionsDefault, Title: TitleOptionsBlackOnYellow, } // StyleDouble renders a Table like below: // ╔═════╦════════════╦═══════════╦════════╦═════════════════════════════╗ // ║ # ║ FIRST NAME ║ LAST NAME ║ SALARY ║ ║ // ╠═════╬════════════╬═══════════╬════════╬═════════════════════════════╣ // ║ 1 ║ Arya ║ Stark ║ 3000 ║ ║ // ║ 20 ║ Jon ║ Snow ║ 2000 ║ You know nothing, Jon Snow! ║ // ║ 300 ║ Tyrion ║ Lannister ║ 5000 ║ ║ // ╠═════╬════════════╬═══════════╬════════╬═════════════════════════════╣ // ║ ║ ║ TOTAL ║ 10000 ║ ║ // ╚═════╩════════════╩═══════════╩════════╩═════════════════════════════╝ StyleDouble = Style{ Name: "StyleDouble", Box: StyleBoxDouble, Color: ColorOptionsDefault, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsDefault, Size: SizeOptionsDefault, Title: TitleOptionsDefault, } // StyleLight renders a Table like below: // ┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐ // │ # │ FIRST NAME │ LAST NAME │ SALARY │ │ // ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤ // │ 1 │ Arya │ Stark │ 3000 │ │ // │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │ // │ 300 │ Tyrion │ Lannister │ 5000 │ │ // ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤ // │ │ │ TOTAL │ 10000 │ │ // └─────┴────────────┴───────────┴────────┴─────────────────────────────┘ StyleLight = Style{ Name: "StyleLight", Box: StyleBoxLight, Color: ColorOptionsDefault, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsDefault, Size: SizeOptionsDefault, Title: TitleOptionsDefault, } // StyleRounded renders a Table like below: // ╭─────┬────────────┬───────────┬────────┬─────────────────────────────╮ // │ # │ FIRST NAME │ LAST NAME │ SALARY │ │ // ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤ // │ 1 │ Arya │ Stark │ 3000 │ │ // │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │ // │ 300 │ Tyrion │ Lannister │ 5000 │ │ // ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤ // │ │ │ TOTAL │ 10000 │ │ // ╰─────┴────────────┴───────────┴────────┴─────────────────────────────╯ StyleRounded = Style{ Name: "StyleRounded", Box: StyleBoxRounded, Color: ColorOptionsDefault, Format: FormatOptionsDefault, HTML: DefaultHTMLOptions, Options: OptionsDefault, Size: SizeOptionsDefault, Title: TitleOptionsDefault, } )
var ( // StyleBoxDefault defines a Boxed-Table like below: // +-----+------------+-----------+--------+-----------------------------+ // | # | FIRST NAME | LAST NAME | SALARY | | // +-----+------------+-----------+--------+-----------------------------+ // | 1 | Arya | Stark | 3000 | | // | 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! | // | 300 | Tyrion | Lannister | 5000 | | // +-----+------------+-----------+--------+-----------------------------+ // | | | TOTAL | 10000 | | // +-----+------------+-----------+--------+-----------------------------+ StyleBoxDefault = BoxStyle{ BottomLeft: "+", BottomRight: "+", BottomSeparator: "+", EmptySeparator: " ", Left: "|", LeftSeparator: "+", MiddleHorizontal: "-", MiddleSeparator: "+", MiddleVertical: "|", PaddingLeft: " ", PaddingRight: " ", PageSeparator: "\n", Right: "|", RightSeparator: "+", TopLeft: "+", TopRight: "+", TopSeparator: "+", UnfinishedRow: " ~", } // StyleBoxBold defines a Boxed-Table like below: // ┏━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ // ┃ # ┃ FIRST NAME ┃ LAST NAME ┃ SALARY ┃ ┃ // ┣━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ // ┃ 1 ┃ Arya ┃ Stark ┃ 3000 ┃ ┃ // ┃ 20 ┃ Jon ┃ Snow ┃ 2000 ┃ You know nothing, Jon Snow! ┃ // ┃ 300 ┃ Tyrion ┃ Lannister ┃ 5000 ┃ ┃ // ┣━━━━━╋━━━━━━━━━━━━╋━━━━━━━━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ // ┃ ┃ ┃ TOTAL ┃ 10000 ┃ ┃ // ┗━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ StyleBoxBold = BoxStyle{ BottomLeft: "┗", BottomRight: "┛", BottomSeparator: "┻", EmptySeparator: " ", Left: "┃", LeftSeparator: "┣", MiddleHorizontal: "━", MiddleSeparator: "╋", MiddleVertical: "┃", PaddingLeft: " ", PaddingRight: " ", PageSeparator: "\n", Right: "┃", RightSeparator: "┫", TopLeft: "┏", TopRight: "┓", TopSeparator: "┳", UnfinishedRow: " ≈", } // StyleBoxDouble defines a Boxed-Table like below: // ╔═════╦════════════╦═══════════╦════════╦═════════════════════════════╗ // ║ # ║ FIRST NAME ║ LAST NAME ║ SALARY ║ ║ // ╠═════╬════════════╬═══════════╬════════╬═════════════════════════════╣ // ║ 1 ║ Arya ║ Stark ║ 3000 ║ ║ // ║ 20 ║ Jon ║ Snow ║ 2000 ║ You know nothing, Jon Snow! ║ // ║ 300 ║ Tyrion ║ Lannister ║ 5000 ║ ║ // ╠═════╬════════════╬═══════════╬════════╬═════════════════════════════╣ // ║ ║ ║ TOTAL ║ 10000 ║ ║ // ╚═════╩════════════╩═══════════╩════════╩═════════════════════════════╝ StyleBoxDouble = BoxStyle{ BottomLeft: "╚", BottomRight: "╝", BottomSeparator: "╩", EmptySeparator: " ", Left: "║", LeftSeparator: "╠", MiddleHorizontal: "═", MiddleSeparator: "╬", MiddleVertical: "║", PaddingLeft: " ", PaddingRight: " ", PageSeparator: "\n", Right: "║", RightSeparator: "╣", TopLeft: "╔", TopRight: "╗", TopSeparator: "╦", UnfinishedRow: " ≈", } // StyleBoxLight defines a Boxed-Table like below: // ┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐ // │ # │ FIRST NAME │ LAST NAME │ SALARY │ │ // ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤ // │ 1 │ Arya │ Stark │ 3000 │ │ // │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │ // │ 300 │ Tyrion │ Lannister │ 5000 │ │ // ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤ // │ │ │ TOTAL │ 10000 │ │ // └─────┴────────────┴───────────┴────────┴─────────────────────────────┘ StyleBoxLight = BoxStyle{ BottomLeft: "└", BottomRight: "┘", BottomSeparator: "┴", EmptySeparator: " ", Left: "│", LeftSeparator: "├", MiddleHorizontal: "─", MiddleSeparator: "┼", MiddleVertical: "│", PaddingLeft: " ", PaddingRight: " ", PageSeparator: "\n", Right: "│", RightSeparator: "┤", TopLeft: "┌", TopRight: "┐", TopSeparator: "┬", UnfinishedRow: " ≈", } // StyleBoxRounded defines a Boxed-Table like below: // ╭─────┬────────────┬───────────┬────────┬─────────────────────────────╮ // │ # │ FIRST NAME │ LAST NAME │ SALARY │ │ // ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤ // │ 1 │ Arya │ Stark │ 3000 │ │ // │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │ // │ 300 │ Tyrion │ Lannister │ 5000 │ │ // ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤ // │ │ │ TOTAL │ 10000 │ │ // ╰─────┴────────────┴───────────┴────────┴─────────────────────────────╯ StyleBoxRounded = BoxStyle{ BottomLeft: "╰", BottomRight: "╯", BottomSeparator: "┴", EmptySeparator: " ", Left: "│", LeftSeparator: "├", MiddleHorizontal: "─", MiddleSeparator: "┼", MiddleVertical: "│", PaddingLeft: " ", PaddingRight: " ", PageSeparator: "\n", Right: "│", RightSeparator: "┤", TopLeft: "╭", TopRight: "╮", TopSeparator: "┬", UnfinishedRow: " ≈", } )
var ( // ColorOptionsDefault defines sensible ANSI color options - basically NONE. ColorOptionsDefault = ColorOptions{} // ColorOptionsBright renders dark text on bright background. ColorOptionsBright = ColorOptionsBlackOnCyanWhite // ColorOptionsDark renders bright text on dark background. ColorOptionsDark = ColorOptionsCyanWhiteOnBlack // ColorOptionsBlackOnBlueWhite renders Black text on Blue/White background. ColorOptionsBlackOnBlueWhite = ColorOptions{ Footer: text.Colors{text.BgBlue, text.FgBlack}, Header: text.Colors{text.BgHiBlue, text.FgBlack}, IndexColumn: text.Colors{text.BgHiBlue, text.FgBlack}, Row: text.Colors{text.BgHiWhite, text.FgBlack}, RowAlternate: text.Colors{text.BgWhite, text.FgBlack}, } // ColorOptionsBlackOnCyanWhite renders Black text on Cyan/White background. ColorOptionsBlackOnCyanWhite = ColorOptions{ Footer: text.Colors{text.BgCyan, text.FgBlack}, Header: text.Colors{text.BgHiCyan, text.FgBlack}, IndexColumn: text.Colors{text.BgHiCyan, text.FgBlack}, Row: text.Colors{text.BgHiWhite, text.FgBlack}, RowAlternate: text.Colors{text.BgWhite, text.FgBlack}, } // ColorOptionsBlackOnGreenWhite renders Black text on Green/White // background. ColorOptionsBlackOnGreenWhite = ColorOptions{ Footer: text.Colors{text.BgGreen, text.FgBlack}, Header: text.Colors{text.BgHiGreen, text.FgBlack}, IndexColumn: text.Colors{text.BgHiGreen, text.FgBlack}, Row: text.Colors{text.BgHiWhite, text.FgBlack}, RowAlternate: text.Colors{text.BgWhite, text.FgBlack}, } // ColorOptionsBlackOnMagentaWhite renders Black text on Magenta/White // background. ColorOptionsBlackOnMagentaWhite = ColorOptions{ Footer: text.Colors{text.BgMagenta, text.FgBlack}, Header: text.Colors{text.BgHiMagenta, text.FgBlack}, IndexColumn: text.Colors{text.BgHiMagenta, text.FgBlack}, Row: text.Colors{text.BgHiWhite, text.FgBlack}, RowAlternate: text.Colors{text.BgWhite, text.FgBlack}, } // ColorOptionsBlackOnRedWhite renders Black text on Red/White background. ColorOptionsBlackOnRedWhite = ColorOptions{ Footer: text.Colors{text.BgRed, text.FgBlack}, Header: text.Colors{text.BgHiRed, text.FgBlack}, IndexColumn: text.Colors{text.BgHiRed, text.FgBlack}, Row: text.Colors{text.BgHiWhite, text.FgBlack}, RowAlternate: text.Colors{text.BgWhite, text.FgBlack}, } // ColorOptionsBlackOnYellowWhite renders Black text on Yellow/White // background. ColorOptionsBlackOnYellowWhite = ColorOptions{ Footer: text.Colors{text.BgYellow, text.FgBlack}, Header: text.Colors{text.BgHiYellow, text.FgBlack}, IndexColumn: text.Colors{text.BgHiYellow, text.FgBlack}, Row: text.Colors{text.BgHiWhite, text.FgBlack}, RowAlternate: text.Colors{text.BgWhite, text.FgBlack}, } // ColorOptionsBlueWhiteOnBlack renders Blue/White text on Black background. ColorOptionsBlueWhiteOnBlack = ColorOptions{ Footer: text.Colors{text.FgBlue, text.BgHiBlack}, Header: text.Colors{text.FgHiBlue, text.BgHiBlack}, IndexColumn: text.Colors{text.FgHiBlue, text.BgHiBlack}, Row: text.Colors{text.FgHiWhite, text.BgBlack}, RowAlternate: text.Colors{text.FgWhite, text.BgBlack}, } // ColorOptionsCyanWhiteOnBlack renders Cyan/White text on Black background. ColorOptionsCyanWhiteOnBlack = ColorOptions{ Footer: text.Colors{text.FgCyan, text.BgHiBlack}, Header: text.Colors{text.FgHiCyan, text.BgHiBlack}, IndexColumn: text.Colors{text.FgHiCyan, text.BgHiBlack}, Row: text.Colors{text.FgHiWhite, text.BgBlack}, RowAlternate: text.Colors{text.FgWhite, text.BgBlack}, } // ColorOptionsGreenWhiteOnBlack renders Green/White text on Black // background. ColorOptionsGreenWhiteOnBlack = ColorOptions{ Footer: text.Colors{text.FgGreen, text.BgHiBlack}, Header: text.Colors{text.FgHiGreen, text.BgHiBlack}, IndexColumn: text.Colors{text.FgHiGreen, text.BgHiBlack}, Row: text.Colors{text.FgHiWhite, text.BgBlack}, RowAlternate: text.Colors{text.FgWhite, text.BgBlack}, } // ColorOptionsMagentaWhiteOnBlack renders Magenta/White text on Black // background. ColorOptionsMagentaWhiteOnBlack = ColorOptions{ Footer: text.Colors{text.FgMagenta, text.BgHiBlack}, Header: text.Colors{text.FgHiMagenta, text.BgHiBlack}, IndexColumn: text.Colors{text.FgHiMagenta, text.BgHiBlack}, Row: text.Colors{text.FgHiWhite, text.BgBlack}, RowAlternate: text.Colors{text.FgWhite, text.BgBlack}, } // ColorOptionsRedWhiteOnBlack renders Red/White text on Black background. ColorOptionsRedWhiteOnBlack = ColorOptions{ Footer: text.Colors{text.FgRed, text.BgHiBlack}, Header: text.Colors{text.FgHiRed, text.BgHiBlack}, IndexColumn: text.Colors{text.FgHiRed, text.BgHiBlack}, Row: text.Colors{text.FgHiWhite, text.BgBlack}, RowAlternate: text.Colors{text.FgWhite, text.BgBlack}, } // ColorOptionsYellowWhiteOnBlack renders Yellow/White text on Black // background. ColorOptionsYellowWhiteOnBlack = ColorOptions{ Footer: text.Colors{text.FgYellow, text.BgHiBlack}, Header: text.Colors{text.FgHiYellow, text.BgHiBlack}, IndexColumn: text.Colors{text.FgHiYellow, text.BgHiBlack}, Row: text.Colors{text.FgHiWhite, text.BgBlack}, RowAlternate: text.Colors{text.FgWhite, text.BgBlack}, } )
var ( // OptionsDefault defines sensible global options. OptionsDefault = Options{ DoNotColorBordersAndSeparators: false, DrawBorder: true, SeparateColumns: true, SeparateFooter: true, SeparateHeader: true, SeparateRows: false, } // OptionsNoBorders sets up a table without any borders. OptionsNoBorders = Options{ DoNotColorBordersAndSeparators: false, DrawBorder: false, SeparateColumns: true, SeparateFooter: true, SeparateHeader: true, SeparateRows: false, } // OptionsNoBordersAndSeparators sets up a table without any borders or // separators. OptionsNoBordersAndSeparators = Options{ DoNotColorBordersAndSeparators: false, DrawBorder: false, SeparateColumns: false, SeparateFooter: false, SeparateHeader: false, SeparateRows: false, } )
var ( // TitleOptionsDefault defines sensible title options - basically NONE. TitleOptionsDefault = TitleOptions{} // TitleOptionsBright renders Bright Bold text on Dark background. TitleOptionsBright = TitleOptionsBlackOnCyan // TitleOptionsDark renders Dark Bold text on Bright background. TitleOptionsDark = TitleOptionsCyanOnBlack // TitleOptionsBlackOnBlue renders Black text on Blue background. TitleOptionsBlackOnBlue = TitleOptions{ Colors: append(ColorOptionsBlackOnBlueWhite.Header, text.Bold), } // TitleOptionsBlackOnCyan renders Black Bold text on Cyan background. TitleOptionsBlackOnCyan = TitleOptions{ Colors: append(ColorOptionsBlackOnCyanWhite.Header, text.Bold), } // TitleOptionsBlackOnGreen renders Black Bold text onGreen background. TitleOptionsBlackOnGreen = TitleOptions{ Colors: append(ColorOptionsBlackOnGreenWhite.Header, text.Bold), } // TitleOptionsBlackOnMagenta renders Black Bold text on Magenta background. TitleOptionsBlackOnMagenta = TitleOptions{ Colors: append(ColorOptionsBlackOnMagentaWhite.Header, text.Bold), } // TitleOptionsBlackOnRed renders Black Bold text on Red background. TitleOptionsBlackOnRed = TitleOptions{ Colors: append(ColorOptionsBlackOnRedWhite.Header, text.Bold), } // TitleOptionsBlackOnYellow renders Black Bold text on Yellow background. TitleOptionsBlackOnYellow = TitleOptions{ Colors: append(ColorOptionsBlackOnYellowWhite.Header, text.Bold), } // TitleOptionsBlueOnBlack renders Blue Bold text on Black background. TitleOptionsBlueOnBlack = TitleOptions{ Colors: append(ColorOptionsBlueWhiteOnBlack.Header, text.Bold), } // TitleOptionsCyanOnBlack renders Cyan Bold text on Black background. TitleOptionsCyanOnBlack = TitleOptions{ Colors: append(ColorOptionsCyanWhiteOnBlack.Header, text.Bold), } // TitleOptionsGreenOnBlack renders Green Bold text on Black background. TitleOptionsGreenOnBlack = TitleOptions{ Colors: append(ColorOptionsGreenWhiteOnBlack.Header, text.Bold), } // TitleOptionsMagentaOnBlack renders Magenta Bold text on Black background. TitleOptionsMagentaOnBlack = TitleOptions{ Colors: append(ColorOptionsMagentaWhiteOnBlack.Header, text.Bold), } // TitleOptionsRedOnBlack renders Red Bold text on Black background. TitleOptionsRedOnBlack = TitleOptions{ Colors: append(ColorOptionsRedWhiteOnBlack.Header, text.Bold), } // TitleOptionsYellowOnBlack renders Yellow Bold text on Black background. TitleOptionsYellowOnBlack = TitleOptions{ Colors: append(ColorOptionsYellowWhiteOnBlack.Header, text.Bold), } )
var ( // DefaultHTMLOptions defines sensible HTML rendering defaults. DefaultHTMLOptions = HTMLOptions{ ConvertColorsToSpans: true, CSSClass: DefaultHTMLCSSClass, EmptyColumn: " ", EscapeText: true, Newline: "<br/>", } )
var ( // DefaultMarkdownOptions defines sensible Markdown rendering defaults. DefaultMarkdownOptions = MarkdownOptions{} )
var ( // FormatOptionsDefault defines sensible formatting options. FormatOptionsDefault = FormatOptions{ Footer: text.FormatUpper, FooterAlign: text.AlignDefault, FooterVAlign: text.VAlignDefault, Header: text.FormatUpper, HeaderAlign: text.AlignDefault, HeaderVAlign: text.VAlignDefault, Row: text.FormatDefault, RowAlign: text.AlignDefault, RowVAlign: text.VAlignDefault, } )
var ( // SizeOptionsDefault defines sensible size options - basically NONE. SizeOptionsDefault = SizeOptions{ WidthMax: 0, WidthMin: 0, } )
Functions ¶
func AutoIndexColumnID ¶
AutoIndexColumnID returns a unique Column ID/Name for the given Column Number. The functionality is similar to what you get in an Excel spreadsheet w.r.t. the Column ID/Name.
Example ¶
fmt.Printf("AutoIndexColumnID( 0): \"%s\"\n", AutoIndexColumnID(0))
fmt.Printf("AutoIndexColumnID( 1): \"%s\"\n", AutoIndexColumnID(1))
fmt.Printf("AutoIndexColumnID( 2): \"%s\"\n", AutoIndexColumnID(2))
fmt.Printf("AutoIndexColumnID( 25): \"%s\"\n", AutoIndexColumnID(25))
fmt.Printf("AutoIndexColumnID( 26): \"%s\"\n", AutoIndexColumnID(26))
fmt.Printf("AutoIndexColumnID( 702): \"%s\"\n", AutoIndexColumnID(702))
fmt.Printf("AutoIndexColumnID(18278): \"%s\"\n", AutoIndexColumnID(18278))
Output: AutoIndexColumnID( 0): "A" AutoIndexColumnID( 1): "B" AutoIndexColumnID( 2): "C" AutoIndexColumnID( 25): "Z" AutoIndexColumnID( 26): "AA" AutoIndexColumnID( 702): "AAA" AutoIndexColumnID(18278): "AAAA"
Types ¶
type BoxStyle ¶
type BoxStyle struct {
BottomLeft string
BottomRight string
BottomSeparator string
EmptySeparator string
Left string
LeftSeparator string
MiddleHorizontal string
MiddleSeparator string
MiddleVertical string
PaddingLeft string
PaddingRight string
PageSeparator string
Right string
RightSeparator string
TopLeft string
TopRight string
TopSeparator string
UnfinishedRow string
// Horizontal lets you customize the horizontal lines for the Table
// in a more granular way than the MiddleHorizontal string. Setting
// this to a non-nil value will override MiddleHorizontal.
Horizontal *BoxStyleHorizontal
}
BoxStyle defines the characters/strings to use to render the borders and separators for the Table.
type BoxStyleHorizontal ¶ added in v6.7.4
type BoxStyleHorizontal struct {
TitleTop string
TitleBottom string // overrides HeaderTop/RowTop
HeaderTop string
HeaderMiddle string
HeaderBottom string // overrides RowTop
RowTop string
RowMiddle string
RowBottom string
}
BoxStyleHorizontal defines the characters/strings to use to render the horizontal lines for the Table.
func NewBoxStyleHorizontal ¶ added in v6.7.4
func NewBoxStyleHorizontal(horizontal string) *BoxStyleHorizontal
NewBoxStyleHorizontal creates a new BoxStyleHorizontal with the given horizontal string.
type ColorOptions ¶
type ColorOptions struct {
Border text.Colors // borders (if nil, uses one of the below)
Header text.Colors // header row(s) colors
IndexColumn text.Colors // index-column colors (row #, etc.)
Row text.Colors // regular row(s) colors
RowAlternate text.Colors // regular row(s) colors for the even-numbered rows
Separator text.Colors // separators (if nil, uses one of the above)
}
ColorOptions defines the ANSI colors to use for parts of the Table.
type ColumnConfig ¶
type ColumnConfig struct {
// Name is the name of the Column as it appears in the first Header row.
// If a Header is not provided, or the name is not found in the header, this
// will not work.
Name string
// Number is the Column # from left. When specified, it overrides the Name
// property. If you know the exact Column number, use this instead of Name.
Number int
// Align defines the horizontal alignment
Align text.Align
AlignFooter text.Align
// AlignHeader defines the horizontal alignment of Header rows
AlignHeader text.Align
// AutoMerge merges cells with similar values and prevents separators from
// being drawn. Caveats:
// * VAlign is applied on the individual cell and not on the merged cell
// * Does not work well with horizontal auto-merge (RowConfig.AutoMerge)
// * Does not work in CSV/Markdown render modes
//
// Works best when:
// * Style().Options.SeparateRows == true
// * Style().Color.Row == Style().Color.RowAlternate (or not set)
AutoMerge bool
// Colors defines the colors to be used on the column
Colors text.Colors
ColorsFooter text.Colors
// ColorsHeader defines the colors to be used on the column in Header rows
ColorsHeader text.Colors
// Hidden when set to true will prevent the column from being rendered.
// This is useful in cases like needing a column for sorting, but not for
// display.
Hidden bool
// Transformer is a custom-function that changes the way the value gets
// rendered to the console. Refer to text/transformer.go for ready-to-use
// Transformer functions.
Transformer text.Transformer
TransformerFooter text.Transformer
// TransformerHeader is like Transformer but for Header rows
TransformerHeader text.Transformer
// VAlign defines the vertical alignment
VAlign text.VAlign
VAlignFooter text.VAlign
// VAlignHeader defines the vertical alignment in Header rows
VAlignHeader text.VAlign
// WidthMax defines the maximum character length of the column
WidthMax int
// WidthEnforcer enforces the WidthMax value on the column contents;
// default: text.WrapText
WidthMaxEnforcer WidthEnforcer
// WidthMin defines the minimum character length of the column
WidthMin int
}
ColumnConfig contains configurations that determine and modify the way the contents of the column get rendered.
type FilterBy ¶ added in v6.7.6
type FilterBy struct {
// Name is the name of the Column as it appears in the first Header row.
// If a Header is not provided, or the name is not found in the header, this
// will not work.
Name string
// Number is the Column # from left. When specified, it overrides the Name
// property. If you know the exact Column number, use this instead of Name.
Number int
// Operator defines how to compare the column value against the Value.
Operator FilterOperator
// Value is the value to compare against. The type should match the expected
// comparison type (string for string operations, numeric for numeric operations).
// For Contains, StartsWith, EndsWith, and RegexMatch, Value should be a string.
// For numeric comparisons (Equal, NotEqual, GreaterThan, etc.), Value can be
// a number (int, float64) or a string representation of a number.
Value interface{}
// IgnoreCase makes string comparisons case-insensitive (only applies to
// string-based operators).
IgnoreCase bool
// CustomFilter is a function that can be used to filter rows in a custom
// manner. Note that:
// * This overrides and ignores the Operator, Value, and IgnoreCase settings
// * This is called after the column contents are converted to string form
// * This function is expected to return:
// * true => include the row
// * false => exclude the row
//
// Use this when the default filtering logic is not sufficient.
CustomFilter func(cellValue string) bool
}
FilterBy defines what to filter (Column Name or Number), how to filter (Operator), and the value to compare against.
type FilterOperator ¶ added in v6.7.6
type FilterOperator int
FilterOperator defines how to filter.
const ( // Equal filters rows where the column value equals the Value. Equal FilterOperator = iota // NotEqual filters rows where the column value does not equal the Value. NotEqual // GreaterThan filters rows where the column value is greater than the Value. GreaterThan // GreaterThanOrEqual filters rows where the column value is greater than or equal to the Value. GreaterThanOrEqual // LessThan filters rows where the column value is less than the Value. LessThan // LessThanOrEqual filters rows where the column value is less than or equal to the Value. LessThanOrEqual // Contains filters rows where the column value contains the Value (string search). Contains // NotContains filters rows where the column value does not contain the Value (string search). NotContains // StartsWith filters rows where the column value starts with the Value. StartsWith // EndsWith filters rows where the column value ends with the Value. EndsWith // RegexMatch filters rows where the column value matches the Value as a regular expression. RegexMatch // RegexNotMatch filters rows where the column value does not match the Value as a regular expression. RegexNotMatch )
type FormatOptions ¶
type FormatOptions struct {
Direction text.Direction // (forced) BiDi direction for each Column
Header text.Format // default text format
HeaderAlign text.Align // default horizontal align
HeaderVAlign text.VAlign // default vertical align
Row text.Format // default text format
RowAlign text.Align // default horizontal align
RowVAlign text.VAlign // default vertical align
}
FormatOptions defines the text-formatting to perform on parts of the Table.
type HTMLOptions ¶ added in v6.0.2
type HTMLOptions struct {
ConvertColorsToSpans bool // convert ANSI escape sequences to HTML <span> tags with CSS classes? EscapeText will be true if this is true.
CSSClass string // CSS class to set on the overall <table> tag
EmptyColumn string // string to replace "" columns with (entire content being "")
EscapeText bool // escape text into HTML-safe content?
Newline string // string to replace "\n" characters with
}
HTMLOptions defines the global options to control HTML rendering.
type MarkdownOptions ¶ added in v6.7.9
type MarkdownOptions struct {
// PadContent pads each column content to match the longest content in
// the column, and extends the separator dashes to match. This makes the
// raw Markdown source more readable without affecting the rendered
// output.
//
// When disabled (default):
// | # | First Name | Last Name | Salary | |
// | ---:| --- | --- | ---:| --- |
// | 1 | Arya | Stark | 3000 | |
//
// When enabled:
// | # | First Name | Last Name | Salary | |
// | ---:| ---------- | --------- | ------:| --------------------------- |
// | 1 | Arya | Stark | 3000 | |
PadContent bool
}
MarkdownOptions defines options to control Markdown rendering.
type Options ¶
type Options struct {
// DoNotColorBordersAndSeparators disables coloring all the borders and row
// or column separators.
DoNotColorBordersAndSeparators bool
// DoNotRenderSeparatorWhenEmpty disables rendering the separator row after
// headers when there are no data rows (for example when only headers and/or
// footers are present).
DoNotRenderSeparatorWhenEmpty bool
// DrawBorder enables or disables drawing the border around the Table.
// Example of a table where it is disabled:
// # │ FIRST NAME │ LAST NAME │ SALARY │
// ─────┼────────────┼───────────┼────────┼─────────────────────────────
// 1 │ Arya │ Stark │ 3000 │
// 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow!
// 300 │ Tyrion │ Lannister │ 5000 │
// ─────┼────────────┼───────────┼────────┼─────────────────────────────
// │ │ TOTAL │ 10000 │
DrawBorder bool
// SeparateColumns enables or disable drawing border between columns.
// Example of a table where it is disabled:
// ┌─────────────────────────────────────────────────────────────────┐
// │ # FIRST NAME LAST NAME SALARY │
// ├─────────────────────────────────────────────────────────────────┤
// │ 1 Arya Stark 3000 │
// │ 20 Jon Snow 2000 You know nothing, Jon Snow! │
// │ 300 Tyrion Lannister 5000 │
// │ TOTAL 10000 │
// └─────────────────────────────────────────────────────────────────┘
SeparateColumns bool
// the rows. Example of a table where it is disabled:
// ┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
// │ # │ FIRST NAME │ LAST NAME │ SALARY │ │
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
// │ 1 │ Arya │ Stark │ 3000 │ │
// │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │
// │ 300 │ Tyrion │ Lannister │ 5000 │ │
// │ │ │ TOTAL │ 10000 │ │
// └─────┴────────────┴───────────┴────────┴─────────────────────────────┘
SeparateFooter bool
// SeparateHeader enables or disable drawing border between the header and
// the rows. Example of a table where it is disabled:
// ┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
// │ # │ FIRST NAME │ LAST NAME │ SALARY │ │
// │ 1 │ Arya │ Stark │ 3000 │ │
// │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │
// │ 300 │ Tyrion │ Lannister │ 5000 │ │
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
// │ │ │ TOTAL │ 10000 │ │
// └─────┴────────────┴───────────┴────────┴─────────────────────────────┘
SeparateHeader bool
// SeparateRows enables or disables drawing separators between each row.
// Example of a table where it is enabled:
// ┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
// │ # │ FIRST NAME │ LAST NAME │ SALARY │ │
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
// │ 1 │ Arya │ Stark │ 3000 │ │
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
// │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
// │ 300 │ Tyrion │ Lannister │ 5000 │ │
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
// │ │ │ TOTAL │ 10000 │ │
// └─────┴────────────┴───────────┴────────┴─────────────────────────────┘
SeparateRows bool
}
Options defines the global options that determine how the Table is rendered.
type Pager ¶ added in v6.6.0
type Pager interface {
// GoTo moves to the given 1-indexed page number.
GoTo(pageNum int) string
// Location returns the current page number in 1-indexed form.
Location() int
// Next moves to the next available page and returns the same.
Next() string
// Prev moves to the previous available page and returns the same.
Prev() string
// Render returns the current page.
Render() string
// SetOutputMirror sets up the writer to which Render() will write the
// output other than returning.
SetOutputMirror(mirror io.Writer)
}
Pager lets you interact with the table rendering in a paged manner.
type PagerOption ¶ added in v6.6.0
type PagerOption func(t *Table)
PagerOption helps control Paging.
func PageSize ¶ added in v6.6.0
func PageSize(pageSize int) PagerOption
PageSize sets the size of each page rendered.
type RowAttributes ¶ added in v6.6.4
type RowAttributes struct {
Number int // Row Number (1-indexed) as appended
NumberSorted int // Row number (1-indexed) after sorting
}
RowAttributes contains properties about the Row during the render.
type RowConfig ¶
type RowConfig struct {
// AutoMerge merges cells with similar values and prevents separators from
// being drawn. Caveats:
// * Align is overridden to text.AlignCenter on the merged cell (unless set
// by AutoMergeAlign value below)
// * Does not work well with vertical auto-merge (ColumnConfig.AutoMerge)
// * Does not work in CSV/Markdown render modes
AutoMerge bool
// Alignment to use on a merge (defaults to text.AlignCenter)
AutoMergeAlign text.Align
}
RowConfig contains configurations that determine and modify the way the contents of a row get rendered.
type RowPainter ¶
RowPainter is a custom function that takes a Row as input and returns the text.Colors{} to use on the entire row
type RowPainterWithAttributes ¶ added in v6.6.4
type RowPainterWithAttributes func(row Row, attr RowAttributes) text.Colors
RowPainterWithAttributes is the same as RowPainter but passes in additional attributes from render time
type SizeOptions ¶ added in v6.6.1
type SizeOptions struct {
// WidthMax is the maximum allotted width for the full row;
// any content beyond this will be truncated using the text
// in Style.Box.UnfinishedRow
WidthMax int
// WidthMin is the minimum allotted width for the full row;
// columns will be auto-expanded until the overall width
// is met
WidthMin int
}
SizeOptions defines the way to control the width of the table output.
type SortBy ¶
type SortBy struct {
// Name is the name of the Column as it appears in the first Header row.
// If a Header is not provided, or the name is not found in the header, this
// will not work.
Name string
// Number is the Column # from left. When specified, it overrides the Name
// property. If you know the exact Column number, use this instead of Name.
Number int
// Mode tells the Writer how to Sort. Asc/Dsc/etc.
Mode SortMode
// IgnoreCase makes sorting case-insensitive
IgnoreCase bool
// CustomLess is a function that can be used to sort the column in a custom
// manner. Note that:
// * This overrides and ignores the Mode and IgnoreCase settings
// * This is called after the column contents are converted to string form
// * This function is expected to return:
// * -1 => when iStr comes before jStr
// * 0 => when iStr and jStr are considered equal
// * 1 => when iStr comes after jStr
//
// Use this when the default sorting logic is not sufficient.
CustomLess func(iStr string, jStr string) int
}
SortBy defines What to sort (Column Name or Number), and How to sort (Mode).
type SortMode ¶
type SortMode int
SortMode defines How to sort.
const ( // Asc sorts the column in Ascending order alphabetically. Asc SortMode = iota // AscAlphaNumeric sorts the column in Ascending order alphabetically and // then numerically. AscAlphaNumeric // AscNumeric sorts the column in Ascending order numerically. AscNumeric // AscNumericAlpha sorts the column in Ascending order numerically and // then alphabetically. AscNumericAlpha // Dsc sorts the column in Descending order alphabetically. Dsc // DscAlphaNumeric sorts the column in Descending order alphabetically and // then numerically. DscAlphaNumeric // DscNumeric sorts the column in Descending order numerically. DscNumeric // DscNumericAlpha sorts the column in Descending order numerically and // then alphabetically. DscNumericAlpha )
type Style ¶
type Style struct {
Name string // name of the Style
Box BoxStyle // characters to use for the boxes
Color ColorOptions // colors to use for the rows and columns
Format FormatOptions // formatting options for the rows and columns
HTML HTMLOptions // rendering options for HTML mode
Markdown MarkdownOptions // rendering options for Markdown mode
Options Options // misc. options for the table
Size SizeOptions // size (width) options for the table
Title TitleOptions // formation options for the title text
}
Style declares how to render the Table and provides very fine-grained control on how the Table gets rendered on the Console.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table helps print a 2-dimensional array in a human-readable pretty-table.
func (*Table) AppendFooter ¶
AppendFooter appends the row to the List of footers to render.
Only the first item in the "config" will be tagged against this row.
func (*Table) AppendHeader ¶
AppendHeader appends the row to the List of headers to render.
Only the first item in the "config" will be tagged against this row.
func (*Table) AppendRow ¶
AppendRow appends the row to the List of rows to render.
Only the first item in the "config" will be tagged against this row.
func (*Table) AppendRows ¶
AppendRows appends the rows to the List of rows to render.
Only the first item in the "config" will be tagged against all the rows.
func (*Table) AppendSeparator ¶
func (t *Table) AppendSeparator()
AppendSeparator helps render a separator row after the current last row. You could call this function over and over, but it will be a no-op unless you call AppendRow or AppendRows in between. Likewise, if the last thing you append is a separator, it will not be rendered in addition to the usual table separator.
****************************************************************************** Please note the following caveats:
- SetPageSize(): this may end up creating consecutive separator rows near the end of a page or at the beginning of a page
- SortBy(): since SortBy could inherently alter the ordering of rows, the separators may not appear after the row it was originally intended to follow
******************************************************************************
func (*Table) FilterBy ¶ added in v6.7.6
FilterBy sets the rules for filtering the Rows. All filters are applied with AND logic (all must match). Filters are applied before sorting.
func (*Table) ImportGrid ¶ added in v6.6.3
ImportGrid helps import 1d or 2d arrays as rows.
func (*Table) Pager ¶ added in v6.6.0
func (t *Table) Pager(opts ...PagerOption) Pager
Pager returns an object that splits the table output into pages and lets you move back and forth through them.
func (*Table) Render ¶
Render renders the Table in a human-readable "pretty" format. Example:
┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐ │ # │ FIRST NAME │ LAST NAME │ SALARY │ │ ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤ │ 1 │ Arya │ Stark │ 3000 │ │ │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │ │ 300 │ Tyrion │ Lannister │ 5000 │ │ ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤ │ │ │ TOTAL │ 10000 │ │ └─────┴────────────┴───────────┴────────┴─────────────────────────────┘
func (*Table) RenderCSV ¶
RenderCSV renders the Table in CSV format. Example:
#,First Name,Last Name,Salary, 1,Arya,Stark,3000, 20,Jon,Snow,2000,"You know nothing\, Jon Snow!" 300,Tyrion,Lannister,5000, ,,Total,10000,
func (*Table) RenderHTML ¶
RenderHTML renders the Table in HTML format. Example:
<table class="go-pretty-table">
<thead>
<tr>
<th align="right">#</th>
<th>First Name</th>
<th>Last Name</th>
<th align="right">Salary</th>
<th> </th>
</tr>
</thead>
<tbody>
<tr>
<td align="right">1</td>
<td>Arya</td>
<td>Stark</td>
<td align="right">3000</td>
<td> </td>
</tr>
<tr>
<td align="right">20</td>
<td>Jon</td>
<td>Snow</td>
<td align="right">2000</td>
<td>You know nothing, Jon Snow!</td>
</tr>
<tr>
<td align="right">300</td>
<td>Tyrion</td>
<td>Lannister</td>
<td align="right">5000</td>
<td> </td>
</tr>
</tbody>
<tfoot>
<tr>
<td align="right"> </td>
<td> </td>
<td>Total</td>
<td align="right">10000</td>
<td> </td>
</tr>
</tfoot>
</table>
func (*Table) RenderMarkdown ¶
RenderMarkdown renders the Table in Markdown format. Example:
| # | First Name | Last Name | Salary | | | ---:| --- | --- | ---:| --- | | 1 | Arya | Stark | 3000 | | | 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! | | 300 | Tyrion | Lannister | 5000 | | | | | Total | 10000 | |
func (*Table) ResetFooters ¶
func (t *Table) ResetFooters()
ResetFooters resets and clears all the Footer rows appended earlier.
func (*Table) ResetHeaders ¶
func (t *Table) ResetHeaders()
ResetHeaders resets and clears all the Header rows appended earlier.
func (*Table) ResetRows ¶
func (t *Table) ResetRows()
ResetRows resets and clears all the rows appended earlier.
func (*Table) SetAllowedRowLength
deprecated
SetAllowedRowLength sets the maximum allowed length or a row (or line of output) when rendered as a table. Rows that are longer than this limit will be "snipped" to the length. Length has to be a positive value to take effect.
Deprecated: in favor if Style().Size.WidthMax
func (*Table) SetAutoIndex ¶
SetAutoIndex adds a generated header with columns such as "A", "B", "C", etc. and a leading column with the row number similar to what you'd see on any spreadsheet application. NOTE: Appending a Header will void this functionality.
func (*Table) SetCaption ¶
SetCaption sets the text to be rendered just below the table. This will not show up when the Table is rendered as a CSV.
func (*Table) SetColumnConfigs ¶
func (t *Table) SetColumnConfigs(configs []ColumnConfig)
SetColumnConfigs sets the configs for each Column.
func (*Table) SetHTMLCSSClass
deprecated
func (*Table) SetIndexColumn ¶
SetIndexColumn sets the given Column # as the column that has the row "Number". Valid values range from 1 to N. Note that this is not 0-indexed.
func (*Table) SetOutputMirror ¶
SetOutputMirror sets an io.Writer for all the Render functions to "Write" to in addition to returning a string.
func (*Table) SetPageSize ¶
SetPageSize sets the maximum number of lines to render before rendering the header rows again. This can be useful when dealing with tables containing a long list of rows that can span pages. Please note that the pagination logic will not consider Header/Footer lines for paging.
func (*Table) SetRowPainter ¶
func (t *Table) SetRowPainter(painter interface{})
SetRowPainter sets up the function which determines the colors to use on a row. Before rendering, this function is invoked on all rows and the color of each row is determined. This color takes precedence over other ways to set color (ColumnConfig.Color*, SetColor*()).
func (*Table) SortBy ¶
SortBy sets the rules for sorting the Rows in the order specified. i.e., the first SortBy instruction takes precedence over the second and so on. Any duplicate instructions on the same column will be discarded while sorting.
func (*Table) SuppressEmptyColumns ¶ added in v6.0.4
func (t *Table) SuppressEmptyColumns()
SuppressEmptyColumns hides columns when the column is empty in ALL the regular rows.
func (*Table) SuppressTrailingSpaces ¶ added in v6.5.0
func (t *Table) SuppressTrailingSpaces()
SuppressTrailingSpaces removes all trailing spaces from the output.
type TitleOptions ¶
TitleOptions defines the way the title text is to be rendered.
type WidthEnforcer ¶
WidthEnforcer is a function that helps enforce a width condition on a string.
type Writer ¶
type Writer interface {
AppendHeader(row Row, configs ...RowConfig)
AppendRow(row Row, configs ...RowConfig)
AppendRows(rows []Row, configs ...RowConfig)
AppendSeparator()
FilterBy(filterBy []FilterBy)
ImportGrid(grid interface{}) bool
Length() int
Pager(opts ...PagerOption) Pager
Render() string
RenderCSV() string
RenderHTML() string
RenderMarkdown() string
RenderTSV() string
ResetHeaders()
ResetRows()
SetAutoIndex(autoIndex bool)
SetCaption(format string, a ...interface{})
SetColumnConfigs(configs []ColumnConfig)
SetIndexColumn(colNum int)
SetOutputMirror(mirror io.Writer)
SetRowPainter(painter interface{})
SetStyle(style Style)
SetTitle(format string, a ...interface{})
SortBy(sortBy []SortBy)
Style() *Style
SuppressEmptyColumns()
SuppressTrailingSpaces()
// deprecated; in favor if Style().Size.WidthMax
SetAllowedRowLength(length int)
// deprecated; in favor of Style().HTML.CSSClass
SetHTMLCSSClass(cssClass string)
// deprecated; in favor of Pager()
SetPageSize(numLines int)
}
Writer declares the interfaces that can be used to set up and render a table.
Source Files
¶
- config.go
- filter.go
- pager.go
- pager_options.go
- render.go
- render_csv.go
- render_hint.go
- render_html.go
- render_init.go
- render_markdown.go
- render_tsv.go
- row.go
- sort.go
- style.go
- style_box.go
- style_color.go
- style_format.go
- style_html.go
- style_markdown.go
- style_options.go
- style_size.go
- styles_title.go
- table.go
- util.go
- util_html.go
- writer.go