/
format.go
259 lines (227 loc) · 5.82 KB
/
format.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
package format
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"
"unicode"
"github.com/fatih/structs"
)
func getRowMaps(rows []interface{}) []map[string]interface{} {
result := []map[string]interface{}{}
for _, row := range rows {
result = append(result, structs.Map(row))
}
return result
}
func extractAttrs(item map[string]interface{}, attrs []string) ([]string, error) {
result := make([]string, len(attrs))
for i, attr := range attrs {
value, ok := item[attr]
if !ok {
return nil, fmt.Errorf("Item has no attribute: %s", attr)
}
switch value.(type) {
case map[string]interface{}:
txt, err := json.Marshal(value)
if err != nil {
return nil, err
}
result[i] = fmt.Sprintf("%+v", string(txt))
default:
result[i] = fmt.Sprintf("%v", value)
}
}
return result, nil
}
func extractSliceAttrs(items []map[string]interface{}, attrs []string) ([][]string, error) {
result := make([][]string, len(items))
for i, item := range items {
values, err := extractAttrs(item, attrs)
if err != nil {
return nil, err
}
result[i] = values
}
return result, nil
}
func extractColumn(rows [][]string, column int) []string {
values := make([]string, len(rows))
for i, row := range rows {
values[i] = row[column]
}
return values
}
func columnWidths(rows [][]string, columnLabels []string, includeCols bool, maxCellLength int) []int {
columnCount := len(columnLabels)
widths := make([]int, columnCount)
if includeCols || len(rows) == 0 {
for i, label := range columnLabels {
widths[i] = len(label)
}
}
for _, row := range rows {
for colIndex, colValue := range row {
valLen := len(colValue)
if maxCellLength > 0 && valLen > maxCellLength {
valLen = maxCellLength
}
if valLen > widths[colIndex] {
widths[colIndex] = valLen
}
}
}
return widths
}
func columnFormats(widths []int) []string {
formats := make([]string, len(widths))
for i, width := range widths {
formats[i] = fmt.Sprintf("%%-%ds", width)
}
return formats
}
func sum(items []int) int {
result := 0
for _, item := range items {
result += item
}
return result
}
// TableOpts are options used when rendering a table
type TableOpts struct {
Rows []interface{}
Columns []string
Separator string
ShowHeader bool
MaxCellWidth int
}
// Table builds a text table from the given data items and chosen columns.
// It returns a list of rows that can be printed.
func Table(opts TableOpts) ([]string, error) {
if len(opts.Columns) == 0 {
return nil, errors.New("No columns to display")
}
columnLabels := make([]string, len(opts.Columns))
for i, name := range opts.Columns {
columnLabels[i] = strings.ToUpper(toSnakeCase(name))
}
rowMaps := getRowMaps(opts.Rows)
tableData, err := extractSliceAttrs(rowMaps, opts.Columns)
if err != nil {
return nil, err
}
separator := " | "
if opts.Separator != "" {
separator = opts.Separator
}
separatorLen := len(separator)
columnWidths := columnWidths(tableData, columnLabels, opts.ShowHeader, opts.MaxCellWidth)
columnFormats := columnFormats(columnWidths)
tableWidth := sum(columnWidths) + separatorLen*(len(opts.Columns)-1)
rowCount := len(tableData)
rowOffset := 0
if opts.ShowHeader {
rowCount += 3
rowOffset = 3
}
rows := make([]string, rowCount)
if opts.ShowHeader {
headers := make([]string, len(opts.Columns))
for i, label := range columnLabels {
headers[i] = fmt.Sprintf(columnFormats[i], label)
}
rows[0] = strings.Repeat("=", tableWidth)
rows[1] = strings.Join(headers, separator)
rows[2] = strings.Repeat("=", tableWidth)
}
for i, row := range tableData {
rowItems := make([]string, len(row))
columnWidthProgress := 0
for h, item := range row {
if opts.MaxCellWidth > 0 {
itemLength := len(item)
columnWidth := columnWidths[h]
if itemLength > columnWidth {
item = cellWrap(item, uint(columnWidth), columnWidthProgress+len(separator))
}
columnWidthProgress += columnWidth
}
rowItems[h] = fmt.Sprintf(columnFormats[h], item)
}
rows[i+rowOffset] = strings.Join(rowItems, separator)
}
return rows, nil
}
// NormalizeStrings normalizes a slice of strings to all uppercase
func NormalizeStrings(input []string) []string {
output := make([]string, len(input))
for i, s := range input {
output[i] = strings.ToUpper(s)
}
return output
}
const nbsp = 0xA0
// Based off https://github.com/mitchellh/go-wordwrap
func cellWrap(s string, lim uint, indentLength int) string {
// Initialize a buffer with a slightly larger size to account for breaks
init := make([]byte, 0, len(s))
buf := bytes.NewBuffer(init)
var current uint
var wordBuf, spaceBuf bytes.Buffer
var wordBufLen, spaceBufLen uint
for _, char := range s {
if char == '\n' {
if wordBuf.Len() == 0 {
if current+spaceBufLen > lim {
current = 0
} else {
current += spaceBufLen
spaceBuf.WriteTo(buf)
}
spaceBuf.Reset()
spaceBufLen = 0
} else {
current += spaceBufLen + wordBufLen
spaceBuf.WriteTo(buf)
spaceBuf.Reset()
spaceBufLen = 0
wordBuf.WriteTo(buf)
wordBuf.Reset()
wordBufLen = 0
}
buf.WriteRune(char)
current = 0
} else if unicode.IsSpace(char) && char != nbsp {
if spaceBuf.Len() == 0 || wordBuf.Len() > 0 {
current += spaceBufLen + wordBufLen
spaceBuf.WriteTo(buf)
spaceBuf.Reset()
spaceBufLen = 0
wordBuf.WriteTo(buf)
wordBuf.Reset()
wordBufLen = 0
}
spaceBuf.WriteRune(char)
spaceBufLen++
} else {
wordBuf.WriteRune(char)
wordBufLen++
if current+wordBufLen+spaceBufLen > lim && wordBufLen < lim {
buf.WriteString("\n" + strings.Repeat(" ", indentLength))
current = 0
spaceBuf.Reset()
spaceBufLen = 0
}
}
}
if wordBuf.Len() == 0 {
if current+spaceBufLen <= lim {
spaceBuf.WriteTo(buf)
}
} else {
spaceBuf.WriteTo(buf)
wordBuf.WriteTo(buf)
}
return buf.String()
}