-
Notifications
You must be signed in to change notification settings - Fork 200
/
table.go
44 lines (38 loc) · 1.17 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package printer
import (
"fmt"
"io"
"reflect"
"text/tabwriter"
"github.com/pkg/errors"
)
func NewTableWriter(out io.Writer) *tabwriter.Writer {
return tabwriter.NewWriter(out, 0, 0, 1, ' ', tabwriter.TabIndent)
}
func PrintTable(out io.Writer, v interface{}, getRow func(row interface{}) []interface{}, headers ...interface{}) error {
if reflect.TypeOf(v).Kind() != reflect.Slice {
return errors.Errorf("invalid data passed to PrintTable, must be a slice but got %T", v)
}
rows := reflect.ValueOf(v)
table := NewTableWriter(out)
if len(headers) > 0 {
fmt.Fprintln(table, tabify(headers)...)
}
for i := 0; i < rows.Len(); i++ {
fmt.Fprintln(table, tabify(getRow(rows.Index(i).Interface()))...)
}
return table.Flush()
}
// tabify is a helper function which takes a slice and injects tab characters
// between each element such that tabwriter can work its magic
func tabify(untabified []interface{}) []interface{} {
var tabified []interface{}
for i := 0; i < len(untabified); i++ {
tabified = append(tabified, untabified[i])
// only append tab character if prior to last element
if i+1 < len(untabified) {
tabified = append(tabified, "\t")
}
}
return tabified
}