-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablify.go
172 lines (138 loc) · 3.06 KB
/
tablify.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package tablify
import (
"fmt"
"reflect"
"strconv"
)
type tablify struct {
h string
v string
j string
}
type Opts struct {
Horizontal string
Vertical string
Join string
}
func New() tablify {
return NewFromOpts(Opts{})
}
func NewFromOpts(opts Opts) tablify {
t := tablify{
h: opts.Horizontal,
v: opts.Vertical,
j: opts.Join,
}
if t.h == "" {
t.h = horizontalDefault
}
if t.v == "" {
t.v = verticalDefault
}
if t.j == "" {
t.j = joinDefault
}
return t
}
func (t tablify) Struct(vals interface{}) {
t.printRowsStruct(interfaceToInterfaceSlice(vals))
}
func interfaceToInterfaceSlice(vals interface{}) []interface{} {
r := reflect.ValueOf(vals)
len := r.Len()
fmt.Println(len)
is := make([]interface{}, len)
for i := 0; i < len; i++ {
is[i] = r.Index(i).Interface()
}
return is
}
func (t tablify) createSeparator(lens reflect.Value) string {
fieldCount := lens.NumField()
s := t.j
for i := 0; i < fieldCount; i++ {
fieldLen := int(lens.Field(i).Int())
for j := 0; j < int(fieldLen); j++ {
s = s + t.h
}
s = s + t.j
}
return s
}
// fieldTitle gets the name that should appear at the top of the column. If the struct
// field has a `tablify` tag attached, this will be used instead of the field name.
//
// Returns two values, `name` and `preferred`.
func fieldTitle(field reflect.StructField) (string, string) {
if field.Tag.Get("tablify") != "" {
return field.Name, field.Tag.Get("tablify")
}
return field.Name, field.Name
}
func (t tablify) printTitle(values interface{}, l reflect.Value) {
title := t.v
row := reflect.ValueOf(values)
for i := 0; i < row.NumField(); i++ {
n, p := fieldTitle(row.Type().Field(i))
title = title + t.padForLength(p, int(l.FieldByName(n).Int())) + t.v
}
fmt.Println(title)
}
func (t tablify) printRow(value interface{}, l reflect.Value) {
val := t.v
row := reflect.ValueOf(value)
for i := 0; i < row.NumField(); i++ {
v := row.Field(i).String()
if row.Field(i).Kind() == reflect.Int {
v = strconv.Itoa(int(row.Field(i).Int()))
}
if row.Field(i).Kind() == reflect.Bool {
if row.Field(i).Bool() {
v = "true"
} else {
v = "false"
}
}
n := row.Type().Field(i).Name
val = val + t.padForLength(v, int(l.FieldByName(n).Int())) + t.v
}
fmt.Println(val)
}
func (t tablify) printRowsStruct(values []interface{}) {
if !t.validate(values) {
return
}
l := lengths(values)
sep := t.createSeparator(l)
// Start the table.
fmt.Println(sep)
vs := make([]interface{}, len(values))
for i, v := range values {
vs[i] = v
}
t.printTitle(vs[0], l)
for _, v := range values {
fmt.Println(sep)
t.printRow(v, l)
}
// End the table.
fmt.Println(sep)
}
func (t tablify) padForLength(value string, length int) string {
value = " " + value
if len(value) == length {
return value
}
spaces := length - len(value)
for i := 0; i < spaces; i++ {
value = value + " "
}
return value
}
func (t tablify) validate(values []interface{}) bool {
if len(values) == 0 {
fmt.Println("Unable to render table. No data exists.")
return false
}
return true
}