Documentation ¶
Overview ¶
Package tablist is a simple table formatter with colors for console.
Example ¶
package main import ( "github.com/sqp/godock/libs/text/tablist" ) func main() { // Create a table with some columns. lf := tablist.NewFormater( tablist.NewColRight(0, "column 0"), tablist.NewColLeft(0, "column 1"), tablist.NewColLeft(23, "column 2"), ) // Add a few lines of data. for _, item := range []struct { c0, c1, c2 string }{ {"content", "this is on the left", "also left"}, {"aligned", "with an empty cell", "with fixed size"}, {"on the right", "", "can truncate the content too large"}, } { line := lf.AddLine() line.Set(0, item.c0) // You better use constants to refer to your column ID. line.Set(1, item.c1) line.Set(2, item.c2) } lf.AddSeparator() sepInfo := lf.AddLine() sepInfo.Set(1, "AddSeparator") sepInfo.Set(2, "separator visible above") // Disable the group color for the example test. tablist.GroupColor = "" lf.AddGroup(1, "AddGroup") lineInfo := lf.AddLine() lineInfo.Set(1, "AddLine") lineInfo.Set(2, "a basic line") emptyFilled := lf.AddEmptyFilled() emptyFilled.Set(1, "AddEmptyFilled") emptyFilled.Set(2, "fills empty cells") // Prints the result to console. lf.Print() }
Output: ┌column 0──────┬column 1─────────────┬column 2─────────────────┐ │ content │ this is on the left │ also left │ │ aligned │ with an empty cell │ with fixed size │ │ on the right │ │ can truncate the conten │ ├──────────────┼─────────────────────┼─────────────────────────┤ │ │ AddSeparator │ separator visible above │ ├──────────────┼AddGroup─────────────┼─────────────────────────┤ │ │ AddLine │ a basic line │ │──────────────│ AddEmptyFilled │ fills empty cells │ └──────────────┴─────────────────────┴─────────────────────────┘
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var GroupColor = color.FgMagenta
GroupColor defines the common group text color.
Functions ¶
This section is empty.
Types ¶
type Base ¶
type Base struct {
// contains filtered or unexported fields
}
Base defines the TableFormater and the Line base with columns definition and the column size map.
func (*Base) SetColSize ¶
SetColSize sets the size of the given column (if it is larger than before).
func (*Base) WalkSprint ¶
WalkSprint runs the given call on each cell of the line and returns the line printable content.
sep[0] is added at the beginning of the line. sep[1] is added between each cell. sep[2] is added at the end of the line.
The result if like this:
sep0 cell0 sep1 cell1 sep1 cell2 sep1 cell3 sep2
type ColInfo ¶
ColInfo is the configuration of a table column.
func NewColLeft ¶
NewColLeft creates a column align on the left.
func NewColRight ¶
NewColRight creates a column align on the right.
type Line ¶
type Line struct { Base // contains filtered or unexported fields }
Line defines a table printer basic line.
type Liner ¶
type Liner interface { Sprint() string Set(row int, text string) *Line Colored(row int, newcolor, text string) *Line }
Liner defines the common line API.
type TableFormater ¶
type TableFormater struct { Base // contains filtered or unexported fields }
TableFormater format colored console display as table.
func NewFormater ¶
func NewFormater(columns ...ColInfo) *TableFormater
NewFormater create a TableFormater with some columns.
func (*TableFormater) AddEmptyFilled ¶
func (lf *TableFormater) AddEmptyFilled() Liner
AddEmptyFilled create and append a new line to format that fills empty fields.
func (*TableFormater) AddGroup ¶
func (lf *TableFormater) AddGroup(row int, name string)
AddGroup create and append a new group line to format.
func (*TableFormater) AddLine ¶
func (lf *TableFormater) AddLine() Liner
AddLine create and append a new line to format.
func (*TableFormater) AddSeparator ¶
func (lf *TableFormater) AddSeparator()
AddSeparator add a separator line.
func (*TableFormater) Count ¶
func (lf *TableFormater) Count() int
Count returns the number of lines.
func (*TableFormater) Print ¶
func (lf *TableFormater) Print()
Print prints the table content in console output.
func (*TableFormater) Sprint ¶
func (lf *TableFormater) Sprint() string
Sprint returns the table content text as if printed on console.