table

package
v0.0.0-...-de190fe Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

DefaultBorderRunes are the runes that will be used to draw table borders if not explicitly overridden via table options. This is an exported variable to allow overriding table borders globally.

Functions

This section is empty.

Types

type BorderMask

type BorderMask int

BorderMask controls which borders should be displayed.

const (
	BorderNone BorderMask = 1 << iota
	BorderLeft
	BorderColumn
	BorderRight
	BorderTop
	BorderRow
	BorderBottom
	BorderSection

	BorderAllHorizontal = BorderTop | BorderRow | BorderBottom | BorderSection
	BorderAllVertical   = BorderLeft | BorderColumn | BorderRight
	BorderAll           = BorderAllHorizontal | BorderAllVertical
)

BorderMask options.

func (BorderMask) Has

func (b BorderMask) Has(mask BorderMask) bool

Has returns true if b has the bits of mask set.

type BorderRune

type BorderRune int

BorderRune indicates the type of rune used for a border.

const (
	BorderRuneHorizontal                BorderRune = iota // ─
	BorderRuneVertical                                    // │
	BorderRuneCornerTopLeft                               // ┌
	BorderRuneCornerTopRight                              // ┐
	BorderRuneCornerBottomLeft                            // └
	BorderRuneCornerBottomRight                           // └
	BorderRuneIntersectionTop                             // ┬
	BorderRuneIntersectionBottom                          // ┴
	BorderRuneIntersectionLeft                            // ├
	BorderRuneIntersectionRight                           // ┤
	BorderRuneIntersectionCenter                          // ┼
	BorderRuneSectionHorizontal                           // ═
	BorderRuneSectionIntersectionLeft                     // ╞
	BorderRuneSectionIntersectionRight                    // ╡
	BorderRuneSectionIntersectionCenter                   // ╪
)

BorderRune elements that are needed to draw a table with corners, intersections, row separators (horizontal) and column separators (vertical borders). Can be configured on a table via the WithBorderRunes table option. See DefaultBorderRunes for a mapping of these constants to actual runes.

type BorderRunes

type BorderRunes map[BorderRune]rune

BorderRunes is a map of the BorderRune type to the actual rune that should be displayed.

type Option

type Option func(t *Table)

Option is a func for configuring a *Table.

func WithAlignment

func WithAlignment(alignment text.Alignment) Option

WithAlignment sets the default alignment for all table cells. Can be overridden per table column via WithColumnAlignment. The alignment is ignored for console.Renderable values passed to table.AddRow as these may have their own alignment rules. See the documentation of table.AddRow.

func WithBorderMask

func WithBorderMask(mask BorderMask) Option

WithBorderMask controls the borders that should be displayed using a bit mask.

func WithBorderRunes

func WithBorderRunes(runes BorderRunes) Option

WithBorderRunes sets the runes that should be rendered for the individual border elements, e.g. corners, vertical and horizontal lines and junctions. See the documentation of DefaultBorderRunes for an example. It is valid to pass a rune mapping for a subset of the required border runes. Missing runes will be filled with the corresponding runes from DefaultBorderRunes.

func WithBorderStyle

func WithBorderStyle(style *style.Style) Option

WithBorderStyle sets the style the should be applied to each border element. The default is to not apply any style.

func WithColumnAlignment

func WithColumnAlignment(alignment ...text.Alignment) Option

WithColumnAlignment configures the cell alignment per column. See documentation of WithAlignment.

func WithColumnStyle

func WithColumnStyle(style ...*style.Style) Option

WithColumnStyle configures the cell style per column. See documentation of WithStyle.

func WithColumnWordWrap

func WithColumnWordWrap(wrap ...bool) Option

WithWordWrap configures the cell word wrapping behaviour per column. See documentation of WithWordWrap.

func WithMargin

func WithMargin(margin int) Option

WithMargin sets the left and right margin of table rows. Defaults to 0.

func WithMaxWidth

func WithMaxWidth(maxWidth int) Option

WithMaxWidth sets the maximum table width. If maxWidth is <= 0, maxWidth is inferred from the table's underlying io.Writer if it is a console.FileWriter, otherwise a default of 80 is used.

func WithPadding

func WithPadding(padding int) Option

WithPadding sets the horizontal padding between adjacent table cells. Defaults to 1.

func WithStyle

func WithStyle(style *style.Style) Option

WithAlignment sets the default style for all table cells. Can be overridden per table column via WithColumnStyle. The style is ignored for console.Renderable values passed to table.AddRow as these may have their own style rules. See the documentation of table.AddRow.

func WithWordWrap

func WithWordWrap(wrap bool) Option

WithWordWrap sets the default word wrapping behaviour for all table cells. If true, cells are word wrapped if width constraints make this necessary. Otherwise, cells are not word wrapped but may be truncated if they exceed the width constraints. Can be overridden per table column via WithColumnWordWrap. The word wrap config is ignored for console.Renderable values passed to table.AddRow as these may have their own word wrapping rules. See the documentation of table.AddRow.

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table can render properly aligned columns and rows of information.

func New

func New(out io.Writer, opts ...Option) *Table

New creates a new *Table which will be rendered to the provided io.Writer using opts.

func (*Table) AddFooter

func (t *Table) AddFooter(columns ...interface{}) *Table

AddFooter adds a footer row to the table. Panics if the number of columns does not align with the number of columns of already existing table rows.

Columns implementing console.Renderable are NOT formatted using the cell and column specific options (e.g. style, alignment, word wrap) configured via the table.With* and table.WithColumn* option funcs. This allows users to add custom cell behaviour if needed.

func (*Table) AddHeader

func (t *Table) AddHeader(columns ...interface{}) *Table

AddHeader adds a header row to the table. Panics if the number of columns does not align with the number of columns of already existing table rows or if AddHeader is called after rows were added via AddRow or AddFooter.

Columns implementing console.Renderable are NOT formatted using the cell and column specific options (e.g. style, alignment, word wrap) configured via the table.With* and table.WithColumn* option funcs. This allows users to add custom cell behaviour if needed.

func (*Table) AddRow

func (t *Table) AddRow(columns ...interface{}) *Table

AddRow adds a row to the table. Panics if the number of columns does not align with the number of columns of already existing table rows or if AddRow is called after rows were added via AddFooter.

Columns implementing console.Renderable are NOT formatted using the cell and column specific options (e.g. style, alignment, word wrap) configured via the table.With* and table.WithColumn* option funcs. This allows users to add custom cell behaviour if needed.

func (*Table) Render

func (t *Table) Render() error

Render renders the table to the underlying io.Writer. Returns an error if rendering failed.

func (*Table) RenderLines

func (t *Table) RenderLines() (int, error)

Render renders the table to the underlying io.Writer. Returns the number of lines rendered and an error if rendering failed.

func (*Table) Reset

func (t *Table) Reset() *Table

Reset resets the table by clearing all rows. This is useful for creating multiple tables with the same options.

Jump to

Keyboard shortcuts

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