table

package module
v0.0.0-...-3d13a82 Latest Latest
Warning

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

Go to latest
Published: May 3, 2020 License: MIT Imports: 3 Imported by: 0

README

Table

A fairly simple and somewhat flexible command line table implementation. Some features like alignment for strings is built-in. Complex types can override Cell for more control over output and layout like adding ANSI colors ;)

License

Licensed under the terms of the MIT license. See LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AlignCenter

type AlignCenter string

AlignCenter is a string based Cell that aligns to the center.

func CenterAlign

func CenterAlign(s string) *AlignCenter

CenterAlign creates a new AlignCenter.

func (*AlignCenter) Len

func (a *AlignCenter) Len() int

Len returns the length of the underlying string.

func (*AlignCenter) Pad

func (a *AlignCenter) Pad(l int)

Pad instructs the AlignCenter to add spaces to pad both the start and end of the string. If the string needs to pad an odd number the extra space goes to the end of the string.

func (*AlignCenter) String

func (a *AlignCenter) String() string

String will return the underlying string.

type AlignLeft

type AlignLeft string

AlignLeft is a string based Cell that aligns left (pads to the right).

func LeftAlign

func LeftAlign(s string) *AlignLeft

LeftAlign creates a new AlignLeft.

func (*AlignLeft) Len

func (a *AlignLeft) Len() int

Len returns the length of the underlying string.

func (*AlignLeft) Pad

func (a *AlignLeft) Pad(l int)

Pad instructs the AlignLeft to add spaces to the right.

func (*AlignLeft) String

func (a *AlignLeft) String() string

String will return the underlying string.

type AlignRight

type AlignRight string

AlignRight is a string based Cell that aligns right (pads to the left).

func RightAlign

func RightAlign(s string) *AlignRight

RightAlign creates a new AlignRight.

func (*AlignRight) Len

func (a *AlignRight) Len() int

Len returns the length of the underlying string.

func (*AlignRight) Pad

func (a *AlignRight) Pad(l int)

Pad instructs the AlignRight to add spaces to the left.

func (*AlignRight) String

func (a *AlignRight) String() string

String will return the underlying string.

type Cell

type Cell interface {
	// Pad called when the Table wishes to adjust the Cell size.
	Pad(size int)

	// Len is called when a Table is calculating the Cell sizes before padding.
	Len() int

	// String will return a string repesentation of the Cell.
	String() string

} //interface

Cell is the data in the Row and thus Table.

type Row

type Row struct {
	// contains filtered or unexported fields

} //struct

Row is a single row in a Table and contains columns of Cells.

func EmptyRow

func EmptyRow() *Row

EmptyRow returns a Row with no default column size.

func NewRow

func NewRow(size int) *Row

NewRow returns a Row with the provided default column size. Use this when the number of columns is known when the Row is constructed.

func (*Row) Add

func (r *Row) Add(c Cell)

Add a Cell.

func (*Row) Adjust

func (r *Row) Adjust(lens []int)

Adjust will loop through all the columns and attempt to pad the Cells using the provided lengths. The number of lengths is expected to match the number of columns/Cells in the Row

func (*Row) ColLen

func (r *Row) ColLen(index int) int

ColLen returns the length of the Cell at the provided index. If the index is invalid -1 is returned.

func (*Row) Cols

func (r *Row) Cols() int

Cols returns the number of columns in the Row.

func (*Row) String

func (r *Row) String() string

String will return a string of the Row.

func (Row) WriteTo

func (r Row) WriteTo(w io.Writer) (size int64, err error)

WriteTo will write the Row to the provided Writer.

type Table

type Table struct {
	// contains filtered or unexported fields

} //struct

Table contains all the Rows (and Cells).

func Empty

func Empty() *Table

Empty creates a Table with no default rows size.

func New

func New(size int) *Table

New creates a new Table with the provided default row size. Use this when the number of rows is knows when the Table is constructed.

func (*Table) Add

func (t *Table) Add(r *Row)

Add a Row.

func (*Table) Adjust

func (t *Table) Adjust()

Adjust will calculate the max column(Cell) length and the instruct all Cells to pad to the longest column.

func (*Table) NewRow

func (t *Table) NewRow(cells ...Cell)

NewRow adds a new Row with the provided Cells.

func (*Table) String

func (t *Table) String() string

String will return a string of the Table.

func (*Table) WriteTo

func (t *Table) WriteTo(w io.Writer) (size int64, err error)

WriteTo will write the Table to the provided Writer.

Jump to

Keyboard shortcuts

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