table
Produces a string that represents slice of structs data in a text table, inspired by gajus/table.
Features:
- No dependency.
- Cell content aligned.
- Column width self-adaptation
- Support type of struct field: int, float, string, bool, slice, struct, map, time.Time and everything.
- Support custom table header by declaring optional tag:
table
.(Thanks @skyfireitdiy)
Installation
$ go get github.com/modood/table
Quick start
package main
import (
"github.com/modood/table"
)
type House struct {
Name string `table:"Name"`
Sigil string
Motto string
}
func main() {
hs := []House{
{"Stark", "direwolf", "Winter is coming"},
{"Targaryen", "dragon", "Fire and Blood"},
{"Lannister", "lion", "Hear Me Roar"},
}
// Output to stdout
table.Output(hs)
// Or just return table string and then do something
s := table.Table(hs)
_ = s
}
output:
โโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโ
โ Name โ Sigil โ Motto โ
โโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโค
โ Stark โ direwolf โ Winter is coming โ
โ Targaryen โ dragon โ Fire and Blood โ
โ Lannister โ lion โ Hear Me Roar โ
โโโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโ
Document
-
func Output(slice interface{})
formats slice of structs data and writes to standard output.(Using box drawing characters)
-
func OutputA(slice interface{})
formats slice of structs data and writes to standard output.(Using standard ascii characters)
-
func Table(slice interface{}) string
formats slice of structs data and returns the resulting string.(Using box drawing characters)
-
func AsciiTable(slice interface{}) string
formats slice of structs data and returns the resulting string.(Using standard ascii characters)
-
compare box drawing characters with standard ascii characters
box drawing:
โโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโ โ Name โ Sigil โ Motto โ โโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโค โ Stark โ direwolf โ Winter is coming โ โ Targaryen โ dragon โ Fire and Blood โ โ Lannister โ lion โ Hear Me Roar โ โโโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโ
standard ascii:
+-----------+----------+------------------+ | Name | Sigil | Motto | +-----------+----------+------------------+ | Stark | direwolf | Winter is coming | | Targaryen | dragon | Fire and Blood | | Lannister | lion | Hear Me Roar | +-----------+----------+------------------+
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
this repo is released under the MIT License.