tablist

package
v0.0.0-...-0caaa62 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2017 License: GPL-3.0-or-later Imports: 4 Imported by: 0

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

View Source
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 NewBase

func NewBase(columns []ColInfo) *Base

NewBase creates a base for TableFormater with some columns.

func (*Base) ColSize

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

ColSize returns the size of the given column.

func (*Base) SetColSize

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

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

func (*Base) WalkSprint

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

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

type ColInfo struct {
	Size  int
	Left  bool
	Title string
}

ColInfo is the configuration of a table column.

func NewColLeft

func NewColLeft(size int, title string) ColInfo

NewColLeft creates a column align on the left.

func NewColRight

func NewColRight(size int, title string) ColInfo

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.

func NewLine

func NewLine(base Base) *Line

NewLine create a new line.

func (*Line) CellData

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

CellData returns fmt format and argument for the cell.

func (*Line) Colored

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

Colored set colored text content for given row.

func (*Line) Set

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

Set text content for given row.

func (*Line) Sprint

func (line *Line) Sprint() string

Sprint prints the line content.

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.

Jump to

Keyboard shortcuts

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