-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtabulate.go
391 lines (347 loc) · 10.7 KB
/
tabulate.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Package tabulate formats data into tables.
//
// Example Usage
//
// The following example will show the table shown below:
//
// package main
//
// import (
// "fmt"
// "github.com/rgeoghegan/tabulate"
// )
//
// type Row struct {
// name string
// count int
// }
//
// func main() {
// table := []*Row{
// &Row{"alpha", 1},
// &Row{"bravo", 2},
// }
// asText, _ := tabulate.Tabulate(
// table, &tabulate.Layout{Format:tabulate.SimpleFormat},
// )
// fmt.Print(asText)
// }
//
// Which will print out the following:
//
// name count
// ----- -----
// alpha 1
// bravo 2
//
// You can also provide a slice of slice of strings:
//
// table := [][]string{
// []string{"alpha", "1"},
// []string{"bravo", "2"},
// }
// layout := &Layout{Headers:[]string{"name", "count"}, Format:tabulate.SimpleFormat}
// asText, err := tabulate.Tabulate(table, layout)
package tabulate
import (
"bytes"
"fmt"
"reflect"
"strings"
"golang.org/x/exp/utf8string"
)
const (
noHeaders int = iota
fromStruct
)
// Layout specifies the general layout of the table. Provide Headers to show a custom list of headings at the top of the table. Set HideHeaders to false to not show Headers.
type Layout struct {
Format TableFormatterInterface
HideHeaders bool
Headers []string
}
func getRowType(table interface{}) (reflect.Type, error) {
tableType := reflect.TypeOf(table)
if reflect.Slice == tableType.Kind() {
rowType := tableType.Elem()
if reflect.Ptr == rowType.Kind() {
rowType = rowType.Elem()
}
switch rowType.Kind() {
case reflect.Struct, reflect.Slice:
return rowType, nil
}
}
return nil, fmt.Errorf("Must pass in slice of struct pointers.")
}
type column struct {
header string
column []string
}
func fetchStructColumn(rowType reflect.Type, table reflect.Value, colDepth int,
customHeaders []string, index int) (*column, error) {
var col = &column{}
header := rowType.Field(index)
if customHeaders == nil {
col.header = header.Name
} else {
col.header = customHeaders[index]
}
caster, err := guessCaster(header.Type)
if err != nil {
return nil, err
}
for i := 0; i < colDepth; i++ {
row := table.Index(i).Elem()
col.column = append(col.column, caster(row.Field(index)))
}
if header.Type.Kind() == reflect.Float32 ||
header.Type.Kind() == reflect.Float64 {
alignFloats(col.column)
}
return col, nil
}
func fetchMatrixColumn(rowType reflect.Type, table reflect.Value, colDepth int,
customHeaders []string, hideHeaders bool, index int) (*column, error) {
col := &column{}
if !hideHeaders {
col.header = customHeaders[index]
}
cellType := rowType.Elem()
caster, err := guessCaster(cellType)
if err != nil {
return nil, err
}
for i := 0; i < colDepth; i++ {
cell := table.Index(i).Index(index)
col.column = append(col.column, caster(cell))
}
if cellType.Kind() == reflect.Float32 || cellType.Kind() == reflect.Float64 {
alignFloats(col.column)
}
return col, nil
}
type table []*column
func (t table) columnWidths(countHeaders bool) []int {
var colWidths []int
for _, col := range t {
colLength := 0
if countHeaders {
colLength = len(col.header)
}
for _, cell := range col.column {
if len(cell) > colLength {
colLength = len(cell)
}
}
colWidths = append(colWidths, colLength)
}
return colWidths
}
func (t table) align(widths []int, showHeaders bool) {
for colI, col := range t {
if showHeaders {
col.header = fmt.Sprintf("%[1]*[2]s", widths[colI], col.header)
}
for i := 0; i < len(col.column); i++ {
col.column[i] = fmt.Sprintf("%[1]*[2]s", widths[colI], col.column[i])
}
}
}
func (t table) draw(format TableFormatterInterface, showHeaders bool) string {
var output []string
columnWidths := t.columnWidths(showHeaders)
t.align(columnWidths, showHeaders)
format.RegisterWidths(columnWidths)
appendRow := func(rows []string, row string) []string {
if len(row) > 0 {
return append(rows, row)
}
return rows
}
joinTokens := func(parts []string) string {
return format.LinePrefix() +
strings.Join(parts, format.Spacer()) +
format.LinePostfix()
}
output = appendRow(output, format.AboveTable())
if showHeaders {
parts := make([]string, len(t))
for i, col := range t {
parts[i] = col.header
}
output = append(output, joinTokens(parts))
output = appendRow(output, format.BelowHeader())
}
for rowI := 0; rowI < len(t[0].column); rowI++ {
parts := make([]string, len(t))
for i, col := range t {
parts[i] = col.column[rowI]
}
output = appendRow(output, joinTokens(parts))
if rowI < len(t[0].column)-1 {
output = appendRow(output, format.BetweenRow(rowI))
}
}
output = appendRow(output, format.BelowTable())
return strings.Join(output, "\n") + "\n"
}
func buildTable(data interface{}, layout *Layout) (table, error) {
rowType, err := getRowType(data)
if err != nil {
panic(err)
}
tableV := reflect.ValueOf(data)
tableLength := tableV.Len()
var columns table
var isStruct bool
var colCount int
switch rowType.Kind() {
case reflect.Struct:
isStruct = true
colCount = rowType.NumField()
case reflect.Slice:
isStruct = false
if layout.HideHeaders {
// Take the length of the first row as the tables width
colCount = tableV.Index(0).Len()
} else {
if layout.Headers == nil {
return nil, fmt.Errorf(
"Must provide headers in layout with slice of slices.",
)
}
colCount = len(layout.Headers)
}
default:
return nil, fmt.Errorf(
"Inputted data must be a slice of slices or a slice of structs.",
)
}
for col := 0; col < colCount; col++ {
var rows *column
var err error
if isStruct {
rows, err = fetchStructColumn(
rowType, tableV, tableLength, layout.Headers, col,
)
} else {
rows, err = fetchMatrixColumn(
rowType, tableV, tableLength, layout.Headers,
layout.HideHeaders, col,
)
}
if err != nil {
return nil, fmt.Errorf("Error with col %d: %s", col, err)
}
columns = append(columns, rows)
}
return columns, nil
}
// Tabulate will tabulate the provided data with the given layout. If no
// format is specified in the layout, it will use a simple format by default.
//
// Data
//
// The data parameter must either be a slice of structs, and the table will
// use the field names of the struct as column names. If provided a slice
// of slices of strings, you will need to provide a list of Headers (mostly
// so it can figure out how many columns to size for).
//
func Tabulate(data interface{}, layout *Layout) (string, error) {
columns, err := buildTable(data, layout)
if err != nil {
return "", err
}
format := layout.Format
if format == nil {
format = SimpleFormat
}
return columns.draw(format, !layout.HideHeaders), nil
}
func writePadding(combined *bytes.Buffer, length int, padding string) {
for i := 0; i < length; i++ {
combined.WriteString(padding)
}
}
// CombineHorizontal place two tables next to one another
// like:
//
// ╒═══════════╤═══════════╤═══════════╕ ╒═══════════╤═══════════╤═══════════╕
// │ A │ B │ C │ │ A │ B │ C │
// ╞═══════════╪═══════════╪═══════════╡ ╞═══════════╪═══════════╪═══════════╡
// │ A value 1 │ B value 1 │ C value 1 │ │ A value 2 │ B value 2 │ C value 2 │
// ╘═══════════╧═══════════╧═══════════╛ ╘═══════════╧═══════════╧═══════════╛
func CombineHorizontal(left string, right string, padding string) string {
var combined bytes.Buffer
leftSplit := strings.Split(left, "\n")
rightSplit := strings.Split(right, "\n")
max := len(leftSplit)
if len(rightSplit) > max {
max = len(rightSplit)
}
for i := 0; i < max; i++ {
if i < len(leftSplit) && utf8Len(leftSplit[i]) == utf8Len(leftSplit[0]) {
combined.WriteString(leftSplit[i])
} else if i < len(rightSplit) && utf8Len(rightSplit[i]) == utf8Len(rightSplit[0]) {
writePadding(&combined, utf8Len(leftSplit[0]), padding)
}
if i < len(rightSplit) && utf8Len(rightSplit[i]) == utf8Len(rightSplit[0]) {
combined.WriteString(padding)
combined.WriteString(rightSplit[i])
} else if i < len(leftSplit) && utf8Len(leftSplit[i]) == utf8Len(leftSplit[0]) {
combined.WriteString(padding)
writePadding(&combined, utf8Len(rightSplit[0]), padding)
}
if i < max-1 {
combined.WriteString("\n")
}
}
return combined.String()
}
// CombineVertical place two tables verticaly
// like:
//
// ╒═══════════╤═══════════╤═══════════╕
// │ A │ B │ C │
// ╞═══════════╪═══════════╪═══════════╡
// │ A value 1 │ B value 1 │ C value 1 │
// ╘═══════════╧═══════════╧═══════════╛
// ╒═══════════╤═══════════╤═══════════╕
// │ A │ B │ C │
// ╞═══════════╪═══════════╪═══════════╡
// │ A value 2 │ B value 2 │ C value 2 │
// ╘═══════════╧═══════════╧═══════════╛
func CombineVertical(top string, bottom string, padding string) string {
var combined bytes.Buffer
topSplit := strings.Split(top, "\n")
bottomSplit := strings.Split(bottom, "\n")
length := utf8Len(topSplit[0])
if length < utf8Len(bottomSplit[0]) {
length = utf8Len(bottomSplit[0])
}
for i := 0; i < len(topSplit); i++ {
combined.WriteString(topSplit[i])
if i < len(topSplit)-1 {
writePadding(&combined, length-utf8Len(topSplit[i]), " ")
combined.WriteString("\n")
}
}
if padding != "" {
paddingUtf8 := utf8string.NewString(padding)
for i := 0; i < utf8Len(padding); i++ {
writePadding(&combined, utf8Len(topSplit[0]), string(paddingUtf8.At(i)))
combined.WriteString("\n")
}
}
for i := 0; i < len(bottomSplit); i++ {
combined.WriteString(bottomSplit[i])
if utf8Len(bottomSplit[i]) == len(bottomSplit[0]) {
writePadding(&combined, length-utf8Len(bottomSplit[i]), " ")
}
if i < len(bottomSplit)-1 {
combined.WriteString("\n")
}
}
return combined.String()
}