godock: github.com/sqp/godock/libs/text/tablist Index | Examples | Files

package tablist

import "github.com/sqp/godock/libs/text/tablist"

Package tablist is a simple table formatter with colors for console.

Code:

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

Package Files

tablist.go

Variables

var GroupColor = color.FgMagenta

GroupColor defines the common group text color.

type Base Uses

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 NewBase Uses

func NewBase(columns []ColInfo) *Base

NewBase creates a base for TableFormater with some columns.

func (*Base) ColSize Uses

func (o *Base) ColSize(col int) int

ColSize returns the size of the given column.

func (*Base) SetColSize Uses

func (o *Base) SetColSize(col, size int)

SetColSize sets the size of the given column (if it is larger than before).

func (*Base) WalkSprint Uses

func (o *Base) WalkSprint(sep []string, call func(int, ColInfo) string) string

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

func (*Base) WalkSprintf Uses

func (o *Base) WalkSprintf(sep []string, call func(int, ColInfo) (format, content string)) string

WalkSprintf runs the given get format call on each cell and prints the line.

type ColInfo Uses

type ColInfo struct {
    Size  int
    Left  bool
    Title string
}

ColInfo is the configuration of a table column.

func NewColLeft Uses

func NewColLeft(size int, title string) ColInfo

NewColLeft creates a column align on the left.

func NewColRight Uses

func NewColRight(size int, title string) ColInfo

NewColRight creates a column align on the right.

type Line Uses

type Line struct {
    Base
    // contains filtered or unexported fields
}

Line defines a table printer basic line.

func NewLine Uses

func NewLine(base Base) *Line

NewLine create a new line.

func (*Line) CellData Uses

func (line *Line) CellData(id int, _ ColInfo) (format, content string)

CellData returns fmt format and argument for the cell.

func (*Line) Colored Uses

func (line *Line) Colored(row int, newcolor, text string) *Line

Colored set colored text content for given row.

func (*Line) Set Uses

func (line *Line) Set(row int, text string) *Line

Set text content for given row.

func (*Line) Sprint Uses

func (line *Line) Sprint() string

Sprint prints the line content.

type Liner Uses

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 Uses

type TableFormater struct {
    Base
    // contains filtered or unexported fields
}

TableFormater format colored console display as table.

func NewFormater Uses

func NewFormater(columns ...ColInfo) *TableFormater

NewFormater create a TableFormater with some columns.

func (*TableFormater) AddEmptyFilled Uses

func (lf *TableFormater) AddEmptyFilled() Liner

AddEmptyFilled create and append a new line to format that fills empty fields.

func (*TableFormater) AddGroup Uses

func (lf *TableFormater) AddGroup(row int, name string)

AddGroup create and append a new group line to format.

func (*TableFormater) AddLine Uses

func (lf *TableFormater) AddLine() Liner

AddLine create and append a new line to format.

func (*TableFormater) AddSeparator Uses

func (lf *TableFormater) AddSeparator()

AddSeparator add a separator line.

func (*TableFormater) Count Uses

func (lf *TableFormater) Count() int

Count returns the number of lines.

func (*TableFormater) Print Uses

func (lf *TableFormater) Print()

Print prints the table content in console output.

func (*TableFormater) Sprint Uses

func (lf *TableFormater) Sprint() string

Sprint returns the table content text as if printed on console.

Package tablist imports 4 packages (graph) and is imported by 3 packages. Updated 2017-09-26. Refresh now. Tools for package owners.