package
4.3.0+incompatible
Repository: https://github.com/static-flow/go-pretty.git
Documentation: pkg.go.dev

# README

Table

GoDoc

Pretty-print tables into ASCII/Unicode strings.

  • Add Rows one-by-one or as a group
  • Add Header(s) and Footer(s)
  • Auto Index Rows (1, 2, 3 ...) and Columns (A, B, C, ...)
  • Limit the length of the Rows; limit the length of individual Columns
  • Page results by a specified number of Lines
  • Alignment - Horizontal & Vertical
    • Auto (horizontal) Align (numeric columns are aligned Right)
    • Custom (horizontal) Align per column
    • Custom (vertical) VAlign per column (and multi-line column support)
  • Mirror output to an io.Writer object (like os.StdOut)
  • Sort by any of the Columns (by Column Name or Number)
  • Transformers to customize individual cell rendering
  • Completely customizable styles
    • Many ready-to-use styles: style.go
    • Colorize Headers/Body/Footers using ../text/color.go
    • Custom text-case for Headers/Body/Footers
    • Enable separators between each row
    • Render table without a Border
  • Render as:
    • (ASCII/Unicode) Table
    • CSV
    • HTML Table (with custom CSS Class)
    • Markdown Table
+---------------------------------------------------------------------+
| Game of Thrones                                                     +
+-----+------------+-----------+--------+-----------------------------+
|   # | FIRST NAME | LAST NAME | SALARY |                             |
+-----+------------+-----------+--------+-----------------------------+
|   1 | Arya       | Stark     |   3000 |                             |
|  20 | Jon        | Snow      |   2000 | You know nothing, Jon Snow! |
| 300 | Tyrion     | Lannister |   5000 |                             |
+-----+------------+-----------+--------+-----------------------------+
|     |            | TOTAL     |  10000 |                             |
+-----+------------+-----------+--------+-----------------------------+

A demonstration of all the capabilities can be found here: ../cmd/demo-table

If you want very specific examples, read ahead.

Examples

All the examples below are going to start with the following block, although nothing except a single Row is mandatory for the Render() function to render something:

package main

import (
    "os"

    "github.com/jedib0t/go-pretty/table"
)

func main() {
    t := table.NewWriter()
    t.SetOutputMirror(os.Stdout)
    t.AppendHeader(table.Row{"#", "First Name", "Last Name", "Salary"})
    t.AppendRows([]table.Row{
        {1, "Arya", "Stark", 3000},
        {20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
    })
    t.AppendRow([]interface{}{300, "Tyrion", "Lannister", 5000})
    t.AppendFooter(table.Row{"", "", "Total", 10000})
    t.Render()
}

Running the above will result in:

+-----+------------+-----------+--------+-----------------------------+
|   # | FIRST NAME | LAST NAME | SALARY |                             |
+-----+------------+-----------+--------+-----------------------------+
|   1 | Arya       | Stark     |   3000 |                             |
|  20 | Jon        | Snow      |   2000 | You know nothing, Jon Snow! |
| 300 | Tyrion     | Lannister |   5000 |                             |
+-----+------------+-----------+--------+-----------------------------+
|     |            | TOTAL     |  10000 |                             |
+-----+------------+-----------+--------+-----------------------------+

Styles

You can customize almost every single thing about the table above. The previous example just defaulted to StyleDefault during Render(). You can use a ready-to-use style (as in style.go) or customize it as you want.

Ready-to-use Styles

Table comes with a bunch of ready-to-use Styles that make the table look really good. Set or Change the style using:

    t.SetStyle(table.StyleLight)
    t.Render()

to get:

┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
│   # │ FIRST NAME │ LAST NAME │ SALARY │                             │
├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
│   1 │ Arya       │ Stark     │   3000 │                             │
│  20 │ Jon        │ Snow      │   2000 │ You know nothing, Jon Snow! │
│ 300 │ Tyrion     │ Lannister │   5000 │                             │
├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
│     │            │ TOTAL     │  10000 │                             │
└─────┴────────────┴───────────┴────────┴─────────────────────────────┘

Or if you want to use a full-color mode, and don't care for boxes, use:

    t.SetStyle(table.StyleColoredBright)
    t.Render()

to get:

Roll your own Style

You can also roll your own style:

    t.SetStyle(table.Style{
        Name: "myNewStyle",
        Box: table.BoxStyle{
            BottomLeft:       "\\",
            BottomRight:      "/",
            BottomSeparator:  "v",
            Left:             "[",
            LeftSeparator:    "{",
            MiddleHorizontal: "-",
            MiddleSeparator:  "+",
            MiddleVertical:   "|",
            PaddingLeft:      "<",
            PaddingRight:     ">",
            Right:            "]",
            RightSeparator:   "}",
            TopLeft:          "(",
            TopRight:         ")",
            TopSeparator:     "^",
            UnfinishedRow:    " ~~~",
        },
        Color: table.ColorOptions{
            AutoIndexColumn: nil,
            FirstColumn:     nil,
            Footer:          text.Colors{text.BgCyan, text.FgBlack},
            Header:          text.Colors{text.BgHiCyan, text.FgBlack},
            Row:             text.Colors{text.BgHiWhite, text.FgBlack},
            RowAlternate:    text.Colors{text.BgWhite, text.FgBlack},
        },
        Format: table.FormatOptions{
            Footer: text.FormatUpper,
            Header: text.FormatUpper,
            Row:    text.FormatDefault,
        },
        Options: table.Options{
            DrawBorder:      true,
            SeparateColumns: true,
            SeparateFooter:  true,
            SeparateHeader:  true,
            SeparateRows:    false,
        },
    })

Or you can use one of the ready-to-use Styles, and just make a few tweaks:

    t.SetStyle(table.StyleLight)
    t.Style().Color.Header = text.Colors{text.BgHiCyan, text.FgBlack}
    t.Style().Format.Footer = text.FormatLower
    t.Style().Options.DrawBorder = false

Paging

You can limit then number of lines rendered in a single "Page". This logic can handle rows with multiple lines too. Here is a simple example:

    t.SetPageSize(1)
    t.Render()

to get:

+-----+------------+-----------+--------+-----------------------------+
|   # | FIRST NAME | LAST NAME | SALARY |                             |
+-----+------------+-----------+--------+-----------------------------+
|   1 | Arya       | Stark     |   3000 |                             |
+-----+------------+-----------+--------+-----------------------------+
|     |            | TOTAL     |  10000 |                             |
+-----+------------+-----------+--------+-----------------------------+

+-----+------------+-----------+--------+-----------------------------+
|   # | FIRST NAME | LAST NAME | SALARY |                             |
+-----+------------+-----------+--------+-----------------------------+
|  20 | Jon        | Snow      |   2000 | You know nothing, Jon Snow! |
+-----+------------+-----------+--------+-----------------------------+
|     |            | TOTAL     |  10000 |                             |
+-----+------------+-----------+--------+-----------------------------+

+-----+------------+-----------+--------+-----------------------------+
|   # | FIRST NAME | LAST NAME | SALARY |                             |
+-----+------------+-----------+--------+-----------------------------+
| 300 | Tyrion     | Lannister |   5000 |                             |
+-----+------------+-----------+--------+-----------------------------+
|     |            | TOTAL     |  10000 |                             |
+-----+------------+-----------+--------+-----------------------------+

Wrapping (or) Row/Column Width restrictions

You can restrict the maximum (text) width for a Row:

    t.SetAllowedRowLength(50)
    t.Render()

to get:

+-----+------------+-----------+--------+------- ~
|   # | FIRST NAME | LAST NAME | SALARY |        ~
+-----+------------+-----------+--------+------- ~
|   1 | Arya       | Stark     |   3000 |        ~
|  20 | Jon        | Snow      |   2000 | You kn ~
| 300 | Tyrion     | Lannister |   5000 |        ~
+-----+------------+-----------+--------+------- ~
|     |            | TOTAL     |  10000 |        ~
+-----+------------+-----------+--------+------- ~

Column Control - Alignment, Colors, Width and more

You can control a lot of things about individual cells/columns which overrides global properties/styles using the SetColumnConfig() interface:

  • Alignment (horizontal & vertical)
  • Colorization
  • Transform individual cells based on the content
  • Width (minimum & maximum)
    nameTransformer := text.Transformer(func(val interface{}) string {
    	return text.Bold.Sprint(val)
    })

    t.SetColumnConfigs([]ColumnConfig{
        {
            Name:              "First Name",
            Align:             text.AlignLeft,
            AlignFooter:       text.AlignLeft,
            AlignHeader:       text.AlignLeft,
            Colors:            text.Colors{text.BgBlack, text.FgRed},
            ColorsHeader:      text.Colors{text.BgRed, text.FgBlack, text.Bold},
            ColorsFooter:      text.Colors{text.BgRed, text.FgBlack},
            Transformer:       nameTransformer,
            TransformerFooter: nameTransformer,
            TransformerHeader: nameTransformer,
            VAlign:            text.VAlignMiddle,
            VAlignFooter:      text.VAlignTop,
            VAlignHeader:      text.VAlignBottom,
            WidthMin:          6,
            WidthMax:          64,
        }
    })

Render As ...

Tables can be rendered in other common formats such as:

... CSV

    t.RenderCSV()

to get:

,First Name,Last Name,Salary,
1,Arya,Stark,3000,
20,Jon,Snow,2000,"You know nothing\, Jon Snow!"
300,Tyrion,Lannister,5000,
,,Total,10000,

... HTML Table

    t.RenderHTML()

to get:

<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>&nbsp;</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td align="right">1</td>
    <td>Arya</td>
    <td>Stark</td>
    <td align="right">3000</td>
    <td>&nbsp;</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>&nbsp;</td>
  </tr>
  </tbody>
  <tfoot>
  <tr>
    <td align="right">&nbsp;</td>
    <td>&nbsp;</td>
    <td>Total</td>
    <td align="right">10000</td>
    <td>&nbsp;</td>
  </tr>
  </tfoot>
</table>

... Markdown Table

    t.RenderMarkdown()

to get:

| # | First Name | Last Name | Salary |  |
| ---:| --- | --- | ---:| --- |
| 1 | Arya | Stark | 3000 |  |
| 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! |
| 300 | Tyrion | Lannister | 5000 |  |
|  |  | Total | 10000 |  |

# Functions

AutoIndexColumnID returns a unique Column ID/Name for the given Column Number.
NewWriter initializes and returns a Writer.

# Constants

Asc sorts the column in Ascending order alphabetically.
AscNumeric sorts the column in Ascending order numerically.
DefaultHTMLCSSClass stores the css-class to use when none-provided via SetHTMLCSSClass(cssClass string).
Dsc sorts the column in Descending order alphabetically.
DscNumeric sorts the column in Descending order numerically.

# Variables

ColorOptionsBlackOnBlueWhite renders Black text on Blue/White background.
ColorOptionsBlackOnCyanWhite renders Black text on Cyan/White background.
ColorOptionsBlackOnGreenWhite renders Black text on Green/White background.
ColorOptionsBlackOnMagentaWhite renders Black text on Magenta/White background.
ColorOptionsBlackOnRedWhite renders Black text on Red/White background.
ColorOptionsBlackOnYellowWhite renders Black text on Yellow/White background.
ColorOptionsBlueWhiteOnBlack renders Blue/White text on Black background.
ColorOptionsBright renders dark text on bright background.
ColorOptionsCyanWhiteOnBlack renders Cyan/White text on Black background.
ColorOptionsDark renders bright text on dark background.
ColorOptionsDefault defines sensible ANSI color options - basically NONE.
ColorOptionsGreenWhiteOnBlack renders Green/White text on Black background.
ColorOptionsMagentaWhiteOnBlack renders Magenta/White text on Black background.
ColorOptionsRedWhiteOnBlack renders Red/White text on Black background.
ColorOptionsYellowWhiteOnBlack renders Yellow/White text on Black background.
FormatOptionsDefault defines sensible formatting options.
OptionsDefault defines sensible global options.
OptionsNoBorders sets up a table without any borders.
OptionsNoBordersAndSeparators sets up a table without any borders or separators.
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 ┃ ┃ ┗━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛.
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 ┃ ┃ ┗━━━━━┻━━━━━━━━━━━━┻━━━━━━━━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛.
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 | | +-----+------------+-----------+--------+-----------------------------+.
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 ║ ║ ╚═════╩════════════╩═══════════╩════════╩═════════════════════════════╝.
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 │ │ └─────┴────────────┴───────────┴────────┴─────────────────────────────┘.
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 │ │ ╰─────┴────────────┴───────────┴────────┴─────────────────────────────╯.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 | | +-----+------------+-----------+--------+-----------------------------+.
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 ║ ║ ╚═════╩════════════╩═══════════╩════════╩═════════════════════════════╝.
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 │ │ └─────┴────────────┴───────────┴────────┴─────────────────────────────┘.
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 │ │ ╰─────┴────────────┴───────────┴────────┴─────────────────────────────╯.
TitleOptionsBlackOnBlue renders Black text on Blue background.
TitleOptionsBlackOnCyan renders Black Bold text on Cyan background.
TitleOptionsBlackOnGreen renders Black Bold text onGreen background.
TitleOptionsBlackOnMagenta renders Black Bold text on Magenta background.
TitleOptionsBlackOnRed renders Black Bold text on Red background.
TitleOptionsBlackOnYellow renders Black Bold text on Yellow background.
TitleOptionsBlueOnBlack renders Blue Bold text on Black background.
TitleOptionsBright renders Bright Bold text on Dark background.
TitleOptionsCyanOnBlack renders Cyan Bold text on Black background.
TitleOptionsDark renders Dark Bold text on Bright background.
TitleOptionsDefault defines sensible title options - basically NONE.
TitleOptionsGreenOnBlack renders Green Bold text on Black background.
TitleOptionsMagentaOnBlack renders Magenta Bold text on Black background.
TitleOptionsRedOnBlack renders Red Bold text on Black background.
TitleOptionsYellowOnBlack renders Yellow Bold text on Black background.

# Structs

BoxStyle defines the characters/strings to use to render the borders and separators for the Table.
ColorOptions defines the ANSI colors to use for parts of the Table.
ColumnConfig contains configurations that determine and modify the way the contents of the column get rendered.
FormatOptions defines the text-formatting to perform on parts of the Table.
Options defines the global options that determine how the Table is rendered.
SortBy defines What to sort (Column Name or Number), and How to sort (Mode).
Style declares how to render the Table and provides very fine-grained control on how the Table gets rendered on the Console.
Table helps print a 2-dimensional array in a human readable pretty-table.
TitleOptions defines the way the title text is to be rendered.

# Interfaces

Writer declares the interfaces that can be used to setup and render a table.

# Type aliases

Row defines a single row in the Table.
RowPainter is a custom function that takes a Row as input and returns the text.Colors{} to use on the entire row.
SortMode defines How to sort.