table

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2018 License: MIT Imports: 8 Imported by: 0

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
  • 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)
  • Completely customizable styles
    • Many ready-to-use styles: style.go
    • Colorize Headers/Body/Footers using github.com/fatih/color
    • 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
+-----+------------+-----------+--------+-----------------------------+
|   # | 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

TODO

  • Generic Cell Content Transformers (with some ready-made ones)
    • Base64 Decoder
    • Currency Formatter
    • UnixTime to Date & Time
    • Status Formatter (color "FAILED" in RED, etc.)

Documentation

Index

Constants

View Source
const (
	// DefaultHTMLCSSClass stores the css-class to use when none-provided via
	// SetHTMLCSSClass(cssClass string).
	DefaultHTMLCSSClass = "go-pretty-table"
)

Variables

View Source
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{
		BoxBottomLeft:       "+",
		BoxBottomRight:      "+",
		BoxBottomSeparator:  "+",
		BoxLeft:             "|",
		BoxLeftSeparator:    "+",
		BoxMiddleHorizontal: "-",
		BoxMiddleSeparator:  "+",
		BoxMiddleVertical:   "|",
		BoxPaddingLeft:      " ",
		BoxPaddingRight:     " ",
		BoxRight:            "|",
		BoxRightSeparator:   "+",
		BoxTopLeft:          "+",
		BoxTopRight:         "+",
		BoxTopSeparator:     "+",
		BoxUnfinishedRow:    " ~",
		FormatFooter:        text.FormatUpper,
		FormatHeader:        text.FormatUpper,
		FormatRows:          text.FormatDefault,
		Name:                "StyleDefault",
	}

	// 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{
		BoxBottomLeft:       text.BoxBottomLeftBold,
		BoxBottomRight:      text.BoxBottomRightBold,
		BoxBottomSeparator:  text.BoxBottomSeparatorBold,
		BoxLeft:             text.BoxLeftBold,
		BoxLeftSeparator:    text.BoxLeftSeparatorBold,
		BoxMiddleHorizontal: text.BoxHorizontalBold,
		BoxMiddleSeparator:  text.BoxSeparatorBold,
		BoxMiddleVertical:   text.BoxVerticalBold,
		BoxPaddingLeft:      " ",
		BoxPaddingRight:     " ",
		BoxRight:            text.BoxRightBold,
		BoxRightSeparator:   text.BoxRightSeparatorBold,
		BoxTopLeft:          text.BoxTopLeftBold,
		BoxTopRight:         text.BoxTopRightBold,
		BoxTopSeparator:     text.BoxTopSeparatorBold,
		BoxUnfinishedRow:    " " + text.BoxUnfinishedLine,
		FormatFooter:        text.FormatUpper,
		FormatHeader:        text.FormatUpper,
		FormatRows:          text.FormatDefault,
		Name:                "StyleBold",
	}

	// 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{
		BoxBottomLeft:       text.BoxBottomLeftDouble,
		BoxBottomRight:      text.BoxBottomRightDouble,
		BoxBottomSeparator:  text.BoxBottomSeparatorDouble,
		BoxLeft:             text.BoxLeftDouble,
		BoxLeftSeparator:    text.BoxLeftSeparatorDouble,
		BoxMiddleHorizontal: text.BoxHorizontalDouble,
		BoxMiddleSeparator:  text.BoxSeparatorDouble,
		BoxMiddleVertical:   text.BoxVerticalDouble,
		BoxPaddingLeft:      " ",
		BoxPaddingRight:     " ",
		BoxRight:            text.BoxRightDouble,
		BoxRightSeparator:   text.BoxRightSeparatorDouble,
		BoxTopLeft:          text.BoxTopLeftDouble,
		BoxTopRight:         text.BoxTopRightDouble,
		BoxTopSeparator:     text.BoxTopSeparatorDouble,
		BoxUnfinishedRow:    " " + text.BoxUnfinishedLine,
		FormatFooter:        text.FormatUpper,
		FormatHeader:        text.FormatUpper,
		FormatRows:          text.FormatDefault,
		Name:                "StyleDouble",
	}

	// 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{
		BoxBottomLeft:       text.BoxBottomLeft,
		BoxBottomRight:      text.BoxBottomRight,
		BoxBottomSeparator:  text.BoxBottomSeparator,
		BoxLeft:             text.BoxLeft,
		BoxLeftSeparator:    text.BoxLeftSeparator,
		BoxMiddleHorizontal: text.BoxHorizontal,
		BoxMiddleSeparator:  text.BoxSeparator,
		BoxMiddleVertical:   text.BoxVertical,
		BoxPaddingLeft:      " ",
		BoxPaddingRight:     " ",
		BoxRight:            text.BoxRight,
		BoxRightSeparator:   text.BoxRightSeparator,
		BoxTopLeft:          text.BoxTopLeft,
		BoxTopRight:         text.BoxTopRight,
		BoxTopSeparator:     text.BoxTopSeparator,
		BoxUnfinishedRow:    " " + text.BoxUnfinishedLine,
		FormatFooter:        text.FormatUpper,
		FormatHeader:        text.FormatUpper,
		FormatRows:          text.FormatDefault,
		Name:                "StyleLight",
	}

	// 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{
		BoxBottomLeft:       text.BoxBottomLeftRounded,
		BoxBottomRight:      text.BoxBottomRightRounded,
		BoxBottomSeparator:  text.BoxBottomSeparator,
		BoxLeft:             text.BoxLeft,
		BoxLeftSeparator:    text.BoxLeftSeparator,
		BoxMiddleHorizontal: text.BoxHorizontal,
		BoxMiddleSeparator:  text.BoxSeparator,
		BoxMiddleVertical:   text.BoxVertical,
		BoxPaddingLeft:      " ",
		BoxPaddingRight:     " ",
		BoxRight:            text.BoxRight,
		BoxRightSeparator:   text.BoxRightSeparator,
		BoxTopLeft:          text.BoxTopLeftRounded,
		BoxTopRight:         text.BoxTopRightRounded,
		BoxTopSeparator:     text.BoxTopSeparator,
		BoxUnfinishedRow:    " " + text.BoxUnfinishedLine,
		FormatFooter:        text.FormatUpper,
		FormatHeader:        text.FormatUpper,
		FormatRows:          text.FormatDefault,
		Name:                "StyleRounded",
	}
)

Functions

This section is empty.

Types

type Row

type Row []interface{}

Row defines a single row in the Table.

type RowStr

type RowStr []string

RowStr defines a single row in the Table comprised of just string objects.

type Style

type Style struct {
	BoxBottomLeft       string
	BoxBottomRight      string
	BoxBottomSeparator  string
	BoxLeft             string
	BoxLeftSeparator    string
	BoxMiddleHorizontal string
	BoxMiddleSeparator  string
	BoxMiddleVertical   string
	BoxPaddingLeft      string
	BoxPaddingRight     string
	BoxRight            string
	BoxRightSeparator   string
	BoxTopLeft          string
	BoxTopRight         string
	BoxTopSeparator     string
	BoxUnfinishedRow    string
	FormatHeader        text.Format
	FormatFooter        text.Format
	FormatRows          text.Format
	Name                string
}

Style declares how to render the Table.

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

func (t *Table) AppendFooter(row Row)

AppendFooter appends the row to the List of footers to render.

func (*Table) AppendHeader

func (t *Table) AppendHeader(row Row)

AppendHeader appends the row to the List of headers to render.

func (*Table) AppendRow

func (t *Table) AppendRow(row Row)

AppendRow appends the row to the List of rows to render.

func (*Table) AppendRows

func (t *Table) AppendRows(rows []Row)

AppendRows appends the rows to the List of rows to render.

func (*Table) Length

func (t *Table) Length() int

Length returns the number of rows to be rendered.

func (*Table) Render

func (t *Table) Render() string

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

func (t *Table) RenderCSV() string

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

func (t *Table) RenderHTML() string

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>&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>

func (*Table) RenderMarkdown

func (t *Table) RenderMarkdown() string

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) SetAlign

func (t *Table) SetAlign(align []text.Align)

SetAlign sets the horizontal-align for each column in all the rows.

func (*Table) SetAllowedColumnLengths

func (t *Table) SetAllowedColumnLengths(lengths []int)

SetAllowedColumnLengths sets the maximum allowed length for each column in all the rows. Columns with content longer than the allowed limit will be wrapped to fit the length. Length has to be a positive value to take effect.

func (*Table) SetAllowedRowLength

func (t *Table) SetAllowedRowLength(length int)

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.

func (*Table) SetAutoIndex

func (t *Table) SetAutoIndex(autoIndex bool)

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

func (t *Table) SetCaption(format string, a ...interface{})

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) SetColors

func (t *Table) SetColors(textColors []text.Colors)

SetColors sets the colors for the rows in the Body.

func (*Table) SetColorsFooter

func (t *Table) SetColorsFooter(textColors []text.Colors)

SetColorsFooter sets the colors for the rows in the Footer.

func (*Table) SetColorsHeader

func (t *Table) SetColorsHeader(textColors []text.Colors)

SetColorsHeader sets the colors for the rows in the Header.

func (*Table) SetHTMLCSSClass

func (t *Table) SetHTMLCSSClass(cssClass string)

SetHTMLCSSClass sets the the HTML CSS Class to use on the <table> node when rendering the Table in HTML format.

func (*Table) SetOutputMirror

func (t *Table) SetOutputMirror(mirror io.Writer)

SetOutputMirror sets an io.Writer for all the Render functions to "Write" to in addition to returning a string.

func (*Table) SetStyle

func (t *Table) SetStyle(style Style)

SetStyle overrides the DefaultStyle with the provided one.

func (*Table) SetVAlign

func (t *Table) SetVAlign(vAlign []text.VAlign)

SetVAlign sets the vertical-align for each column in all the rows.

func (*Table) ShowBorder

func (t *Table) ShowBorder(show bool)

ShowBorder enables or disables drawing the border around the Table. Example of a table where it is disabled (enabled by default):

   # │ 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) ShowSeparators

func (t *Table) ShowSeparators(show bool)

ShowSeparators enables or disable drawing separators between each row. Example of a table where it is enabled (disabled by default):

┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
│   # │ 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) Style

func (t *Table) Style() *Style

Style returns the current style.

type Writer

type Writer interface {
	AppendFooter(row Row)
	AppendHeader(row Row)
	AppendRow(row Row)
	AppendRows(rows []Row)
	Length() int
	Render() string
	RenderCSV() string
	RenderHTML() string
	RenderMarkdown() string
	SetAlign(align []text.Align)
	SetAllowedColumnLengths(lengths []int)
	SetAllowedRowLength(length int)
	SetAutoIndex(autoIndex bool)
	SetCaption(format string, a ...interface{})
	SetColors(colors []text.Colors)
	SetColorsFooter(colors []text.Colors)
	SetColorsHeader(colors []text.Colors)
	SetHTMLCSSClass(cssClass string)
	SetOutputMirror(mirror io.Writer)
	SetStyle(style Style)
	SetVAlign(vAlign []text.VAlign)
	ShowBorder(show bool)
	ShowSeparators(show bool)
	Style() *Style
}

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

func NewWriter

func NewWriter() Writer

NewWriter initializes and returns a Writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL