Small, easy to use library, that renders arbitrary data as table in console.
Library operates with two interfaces - table.Cell
and table.Inteface
. First one used to describe cell rendering
contents while the second describes whole table structure (same as Go native sort.Interface
)
Method table.Print
works only with table.Interface
, so you have to create structure, that implements it:
type Interface interface {
// Returns list of headers
Headers() []string
// Iterates over own contents, supplying each data row to provided callback function
EachRow(func(...Cell))
}
Example:
package main
import (
"github.com/mono83/table"
"github.com/mono83/table/cells"
"io/ioutil"
"os"
)
func main() {
files, err := ioutil.ReadDir(".")
if err != nil {
panic(err)
}
table.Print(filesTable(files), table.PrintOptions{
ColumnSeparator: " ",
NoHeader: true,
})
}
// filesTable is proxy structure, that implements table.Interface
type filesTable []os.FileInfo
func (f filesTable) Headers() []string {
return []string{
"Name",
"Type",
"Size",
}
}
func (f filesTable) EachRow(callback func(...table.Cell)) {
for _, info := range f {
var nodeType table.Cell
nodeType = cells.Empty{}
if info.IsDir() {
nodeType = cells.ColoredYellowHi(cells.String("dir"))
}
callback(
cells.ColoredString(cells.String(info.Name())),
nodeType,
cells.Bytes(info.Size()),
)
}
}
All cells in table must implement table.Cell interface
:
type Cell interface {
// Returns cell width
Width() int
// Returns plain string representation of cell contents
String() string
// Returns formatted value of cell
PrintFormat(width int) string
}
There are some implementations in cells
package, most of them can be obtained using simple type casting:
Type | Casts from | Usecase |
---|---|---|
cells.Empty |
none | Empty cells |
cells.String |
string |
String values |
cells.Int |
int |
int values |
cells.Int64 |
int64 |
int64 values |
cells.Bytes |
int64 |
Byte size of something. This implementation will format value and append suffixes like Kb, Mb, Gb |
There are some useful coloring adapters, designed as function, that takes table.Cell
as argument. Suffix hi
stands for high-intensity.
cells.ColoredRed
andcells.ColoredRedHi
- red cell coloringcells.ColoredGreen
andcells.ColoredGreenHi
- green cell coloringcells.ColoredCyan
andcells.ColoredCyanHi
- cyan cell coloringcells.ColoredYellow
andcells.ColoredYellowHi
- yellow cell coloringcells.ColoredBlue
andcells.ColoredBlueHi
- blue cell coloringcells.ColoredWhite
andcells.ColoredWhiteHi
- white cell coloringcells.ColoredString
- standard color for strings (currently - green)cells.ColoredInt
- standard color for integers (currently - cyan)
cells.AlignLeft
cells.AlignRight
cells.AlignCenter
cells.Sprintf
- shortcut to string cell, built usingfmt.Sprintf
Writer
-io.Writer
to use. If omitted,os.Stdout
will be chosenColumnSeparator
- string, used as column separatorNoDecoration
- if true, color formatting disabledNoHeader
- if true, table header with column names won't be renderedHeaderBorder
- string, used to render border between header and data