This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
tablewriter.go
78 lines (71 loc) · 1.78 KB
/
tablewriter.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package tablewriter
import (
"fmt"
"os"
"reflect"
"strings"
"text/tabwriter"
)
const structFieldTagKey = "table"
// StructValues tab delimits the values of a given struct.
//
// Tag a field `table:"-"` to hide it from output.
func StructValues(data interface{}) string {
v := reflect.ValueOf(data)
s := &strings.Builder{}
for i := 0; i < v.NumField(); i++ {
if shouldHideField(v.Type().Field(i)) {
continue
}
fmt.Fprintf(s, "%v\t", v.Field(i).Interface())
}
return s.String()
}
// StructFieldNames tab delimits the field names of a given struct.
//
// Tag a field `table:"-"` to hide it from output.
func StructFieldNames(data interface{}) string {
v := reflect.ValueOf(data)
s := &strings.Builder{}
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
if shouldHideField(field) {
continue
}
fmt.Fprintf(s, "%s\t", fieldName(field))
}
return s.String()
}
// WriteTable writes the given list elements to stdout in a human readable
// tabular format. Headers abide by the `table` struct tag.
//
// `table:"-"` omits the field and no tag defaults to the Go identifier.
func WriteTable(length int, each func(i int) interface{}) error {
if length < 1 {
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
defer func() { _ = w.Flush() }() // Best effort.
for ix := 0; ix < length; ix++ {
item := each(ix)
if ix == 0 {
if _, err := fmt.Fprintln(w, StructFieldNames(item)); err != nil {
return err
}
}
if _, err := fmt.Fprintln(w, StructValues(item)); err != nil {
return err
}
}
return nil
}
func fieldName(f reflect.StructField) string {
custom, ok := f.Tag.Lookup(structFieldTagKey)
if ok {
return custom
}
return f.Name
}
func shouldHideField(f reflect.StructField) bool {
return f.Tag.Get(structFieldTagKey) == "-"
}