Skip to content

subchen/go-tableify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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