An open API service indexing awesome lists of open source software.

https://github.com/subchen/go-tableify

Pretty console printing of tabular data
https://github.com/subchen/go-tableify

console golang printing table

Last synced: 5 months ago
JSON representation

Pretty console printing of tabular data

Awesome Lists containing this project

README

          

# go-tableify

[![Go Report Card](https://goreportcard.com/badge/github.com/subchen/go-tableify)](https://goreportcard.com/report/github.com/subchen/go-tableify)
[![GoDoc](https://godoc.org/github.com/subchen/go-tableify?status.svg)](https://godoc.org/github.com/subchen/go-tableify)

Pretty console printing of tabular data

## Installation

Make sure you have a working Go environment. Follow the [Go install instructions](http://golang.org/doc/install.html).

To install `go-tableify`, simply run:

```
go get github.com/subchen/go-tableify
```

## Example

### Manunal Set

```go
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

```go
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
```