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
- Host: GitHub
- URL: https://github.com/subchen/go-tableify
- Owner: subchen
- License: apache-2.0
- Created: 2018-01-10T05:03:13.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-08-24T12:55:57.000Z (almost 8 years ago)
- Last Synced: 2024-06-20T11:06:59.240Z (about 2 years ago)
- Topics: console, golang, printing, table
- Language: Go
- Size: 11.7 KB
- Stars: 5
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-tableify
[](https://goreportcard.com/report/github.com/subchen/go-tableify)
[](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
```