-
Notifications
You must be signed in to change notification settings - Fork 0
/
prettier.go
48 lines (43 loc) · 956 Bytes
/
prettier.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
45
46
47
48
package main
import (
"os"
"reflect"
"github.com/jedib0t/go-pretty/v6/table"
)
func makeTable(data []interface{}) {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
if len(data) > 0 {
var tr = table.Row{}
switch reflect.TypeOf(data[0]).Kind() {
case reflect.Struct:
val := reflect.ValueOf(data[0])
for i := 0; i < val.NumField(); i++ {
tr = append(tr, val.Type().Field(i).Name)
}
t.AppendHeader(tr)
t.AppendSeparator()
}
}
for _, d := range data {
var tr = table.Row{}
val := reflect.ValueOf(d)
for i := 0; i < val.NumField(); i++ {
tr = append(tr, val.Field(i).Interface())
}
t.AppendHeader(tr)
t.AppendSeparator()
}
t.SetStyle(table.StyleColoredBright)
t.Render()
}
func toSlice(i interface{}) []interface{} {
var out []interface{}
rv := reflect.ValueOf(i)
if rv.Kind() == reflect.Slice {
for i := 0; i < rv.Len(); i++ {
out = append(out, rv.Index(i).Interface())
}
}
return out
}