tableify

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2018 License: Apache-2.0 Imports: 4 Imported by: 0

README

go-tableify

Go Report Card GoDoc

Pretty console printing of tabular data

Installation

Make sure you have a working Go environment. Follow the Go install instructions.

To install go-tableify, simply run:

go get github.com/subchen/go-tableify

Example

Manunal Set
package main

import (
	"github.com/subchen/go-tableify"
)

func main() {
	t := tableify.New()
	t.SetHeaders("Name", "Files", "Updated")
	t.SetWidths(10, 0, 0) // optional
	t.EmptyText = "no data in table"

	t.AddRow("yum-repo", 45, "2018-01-06T07:45:22Z")
	t.AddRow("deb-repo", 12, "2018-01-06T08:05:09Z")

	t.Print()
}
Using Struct
package main

import (
	"github.com/subchen/go-tableify"
)

type Repo struct {
	Name        string   `json:"name" tableify:"-"`
	Desc        string   `json:"desc"`
	Files       int      `json:"files" tableify:"-,5"`
	LastUpdated string   `json:"lastUpdated" tableify:"Updated"`
}

func main() {
	repolist := []Repo{
		{
			Name:         "yum-repo",
			Files:        45,
			LastUpdated:  "2018-01-06T07:45:22Z",
		},
		{
			Name:         "deb-repo",
			Files:        12,
			LastUpdated:  "2018-01-06T08:05:09Z",
		},
	}

	t := tableify.New()
	t.SetHeadersFromStruct(new(Repo))
	t.AddRowObjectList(repolist)
	t.Print()
}

Struct Field Tag formats:

  • name or -

    eg: tableify:"-", tableify:"NAME"

  • name,width

    eg: tableify:"-,20"

  • name,width,format

    eg: tableify:"-,0,%.2f"

Output

Name         Files   Updated
-----------------------------------------
yum-repo     45      2018-01-06T07:45:22Z
deb-repo     12      2018-01-06T08:05:09Z

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Table

type Table struct {
	Margin    int
	SplitLine bool
	EmptyText string

	FormatFunc func(header string, value interface{}) string
	// contains filtered or unexported fields
}

func New

func New() *Table

func (*Table) AddRow

func (t *Table) AddRow(values ...interface{})

func (*Table) AddRowList

func (t *Table) AddRowList(rows ...[]string)

func (*Table) AddRowObject added in v1.1.0

func (t *Table) AddRowObject(obj interface{})

func (*Table) AddRowObjectList added in v1.1.0

func (t *Table) AddRowObjectList(objs interface{})

func (*Table) Print

func (t *Table) Print()

func (*Table) SetFormats added in v1.1.0

func (t *Table) SetFormats(formats ...string)

func (*Table) SetHeaders

func (t *Table) SetHeaders(headers ...string)

func (*Table) SetHeadersFromStruct added in v1.1.0

func (t *Table) SetHeadersFromStruct(obj interface{})

SetStructHeaders to add headers from struct tags

Example struct {
     Name        string  `tableify:"-"`         // header is fieldname: Name
     Files       int     `tableify:"-"`
     Age         float64 `tableify:"-,0,%.2f"`  // width is 0, format is "%.2f"
     LastUpdated string  `tableify:"Updated"`   // header is "Updated"
     Desc        string                         // not as header

func (*Table) SetWidths

func (t *Table) SetWidths(widths ...int)

Jump to

Keyboard shortcuts

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