-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
table.go
370 lines (327 loc) · 8.2 KB
/
table.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
package table
// This is a temporary package - Table will move to github.com/onsi/consolable once some more dust settles
import (
"reflect"
"strings"
"unicode/utf8"
)
type AlignType uint
const (
AlignTypeLeft AlignType = iota
AlignTypeCenter
AlignTypeRight
)
type Divider string
type Row struct {
Cells []Cell
Divider string
Style string
}
func R(args ...interface{}) *Row {
r := &Row{
Divider: "-",
}
for _, arg := range args {
switch reflect.TypeOf(arg) {
case reflect.TypeOf(Divider("")):
r.Divider = string(arg.(Divider))
case reflect.TypeOf(r.Style):
r.Style = arg.(string)
case reflect.TypeOf(Cell{}):
r.Cells = append(r.Cells, arg.(Cell))
}
}
return r
}
func (r *Row) AppendCell(cells ...Cell) *Row {
r.Cells = append(r.Cells, cells...)
return r
}
func (r *Row) Render(widths []int, totalWidth int, tableStyle TableStyle, isLastRow bool) string {
out := ""
if len(r.Cells) == 1 {
out += strings.Join(r.Cells[0].render(totalWidth, r.Style, tableStyle), "\n") + "\n"
} else {
if len(r.Cells) != len(widths) {
panic("row vs width mismatch")
}
renderedCells := make([][]string, len(r.Cells))
maxHeight := 0
for colIdx, cell := range r.Cells {
renderedCells[colIdx] = cell.render(widths[colIdx], r.Style, tableStyle)
if len(renderedCells[colIdx]) > maxHeight {
maxHeight = len(renderedCells[colIdx])
}
}
for colIdx := range r.Cells {
for len(renderedCells[colIdx]) < maxHeight {
renderedCells[colIdx] = append(renderedCells[colIdx], strings.Repeat(" ", widths[colIdx]))
}
}
border := strings.Repeat(" ", tableStyle.Padding)
if tableStyle.VerticalBorders {
border += "|" + border
}
for lineIdx := 0; lineIdx < maxHeight; lineIdx++ {
for colIdx := range r.Cells {
out += renderedCells[colIdx][lineIdx]
if colIdx < len(r.Cells)-1 {
out += border
}
}
out += "\n"
}
}
if tableStyle.HorizontalBorders && !isLastRow && r.Divider != "" {
out += strings.Repeat(string(r.Divider), totalWidth) + "\n"
}
return out
}
type Cell struct {
Contents []string
Style string
Align AlignType
}
func C(contents string, args ...interface{}) Cell {
c := Cell{
Contents: strings.Split(contents, "\n"),
}
for _, arg := range args {
switch reflect.TypeOf(arg) {
case reflect.TypeOf(c.Style):
c.Style = arg.(string)
case reflect.TypeOf(c.Align):
c.Align = arg.(AlignType)
}
}
return c
}
func (c Cell) Width() (int, int) {
w, minW := 0, 0
for _, line := range c.Contents {
lineWidth := utf8.RuneCountInString(line)
if lineWidth > w {
w = lineWidth
}
for _, word := range strings.Split(line, " ") {
wordWidth := utf8.RuneCountInString(word)
if wordWidth > minW {
minW = wordWidth
}
}
}
return w, minW
}
func (c Cell) alignLine(line string, width int) string {
lineWidth := utf8.RuneCountInString(line)
if lineWidth == width {
return line
}
if lineWidth < width {
gap := width - lineWidth
switch c.Align {
case AlignTypeLeft:
return line + strings.Repeat(" ", gap)
case AlignTypeRight:
return strings.Repeat(" ", gap) + line
case AlignTypeCenter:
leftGap := gap / 2
rightGap := gap - leftGap
return strings.Repeat(" ", leftGap) + line + strings.Repeat(" ", rightGap)
}
}
return line
}
func (c Cell) splitWordToWidth(word string, width int) []string {
out := []string{}
n, subWord := 0, ""
for _, c := range word {
subWord += string(c)
n += 1
if n == width-1 {
out = append(out, subWord+"-")
n, subWord = 0, ""
}
}
return out
}
func (c Cell) splitToWidth(line string, width int) []string {
lineWidth := utf8.RuneCountInString(line)
if lineWidth <= width {
return []string{line}
}
outLines := []string{}
words := strings.Split(line, " ")
outWords := []string{words[0]}
length := utf8.RuneCountInString(words[0])
if length > width {
splitWord := c.splitWordToWidth(words[0], width)
lastIdx := len(splitWord) - 1
outLines = append(outLines, splitWord[:lastIdx]...)
outWords = []string{splitWord[lastIdx]}
length = utf8.RuneCountInString(splitWord[lastIdx])
}
for _, word := range words[1:] {
wordLength := utf8.RuneCountInString(word)
if length+wordLength+1 <= width {
length += wordLength + 1
outWords = append(outWords, word)
continue
}
outLines = append(outLines, strings.Join(outWords, " "))
outWords = []string{word}
length = wordLength
if length > width {
splitWord := c.splitWordToWidth(word, width)
lastIdx := len(splitWord) - 1
outLines = append(outLines, splitWord[:lastIdx]...)
outWords = []string{splitWord[lastIdx]}
length = utf8.RuneCountInString(splitWord[lastIdx])
}
}
if len(outWords) > 0 {
outLines = append(outLines, strings.Join(outWords, " "))
}
return outLines
}
func (c Cell) render(width int, style string, tableStyle TableStyle) []string {
out := []string{}
for _, line := range c.Contents {
out = append(out, c.splitToWidth(line, width)...)
}
for idx := range out {
out[idx] = c.alignLine(out[idx], width)
}
if tableStyle.EnableTextStyling {
style = style + c.Style
if style != "" {
for idx := range out {
out[idx] = style + out[idx] + "{{/}}"
}
}
}
return out
}
type TableStyle struct {
Padding int
VerticalBorders bool
HorizontalBorders bool
MaxTableWidth int
MaxColWidth int
EnableTextStyling bool
}
var DefaultTableStyle = TableStyle{
Padding: 1,
VerticalBorders: true,
HorizontalBorders: true,
MaxTableWidth: 120,
MaxColWidth: 40,
EnableTextStyling: true,
}
type Table struct {
Rows []*Row
TableStyle TableStyle
}
func NewTable() *Table {
return &Table{
TableStyle: DefaultTableStyle,
}
}
func (t *Table) AppendRow(row *Row) *Table {
t.Rows = append(t.Rows, row)
return t
}
func (t *Table) Render() string {
out := ""
totalWidth, widths := t.computeWidths()
for rowIdx, row := range t.Rows {
out += row.Render(widths, totalWidth, t.TableStyle, rowIdx == len(t.Rows)-1)
}
return out
}
func (t *Table) computeWidths() (int, []int) {
nCol := 0
for _, row := range t.Rows {
if len(row.Cells) > nCol {
nCol = len(row.Cells)
}
}
// lets compute the contribution to width from the borders + padding
borderWidth := t.TableStyle.Padding
if t.TableStyle.VerticalBorders {
borderWidth += 1 + t.TableStyle.Padding
}
totalBorderWidth := borderWidth * (nCol - 1)
// lets compute the width of each column
widths := make([]int, nCol)
minWidths := make([]int, nCol)
for colIdx := range widths {
for _, row := range t.Rows {
if colIdx >= len(row.Cells) {
// ignore rows with fewer columns
continue
}
w, minWid := row.Cells[colIdx].Width()
if w > widths[colIdx] {
widths[colIdx] = w
}
if minWid > minWidths[colIdx] {
minWidths[colIdx] = minWid
}
}
}
// do we already fit?
if sum(widths)+totalBorderWidth <= t.TableStyle.MaxTableWidth {
// yes! we're done
return sum(widths) + totalBorderWidth, widths
}
// clamp the widths and minWidths to MaxColWidth
for colIdx := range widths {
widths[colIdx] = min(widths[colIdx], t.TableStyle.MaxColWidth)
minWidths[colIdx] = min(minWidths[colIdx], t.TableStyle.MaxColWidth)
}
// do we fit now?
if sum(widths)+totalBorderWidth <= t.TableStyle.MaxTableWidth {
// yes! we're done
return sum(widths) + totalBorderWidth, widths
}
// hmm... still no... can we possibly squeeze the table in without violating minWidths?
if sum(minWidths)+totalBorderWidth >= t.TableStyle.MaxTableWidth {
// nope - we're just going to have to exceed MaxTableWidth
return sum(minWidths) + totalBorderWidth, minWidths
}
// looks like we don't fit yet, but we should be able to fit without violating minWidths
// lets start scaling down
n := 0
for sum(widths)+totalBorderWidth > t.TableStyle.MaxTableWidth {
budget := t.TableStyle.MaxTableWidth - totalBorderWidth
baseline := sum(widths)
for colIdx := range widths {
widths[colIdx] = max((widths[colIdx]*budget)/baseline, minWidths[colIdx])
}
n += 1
if n > 100 {
break // in case we somehow fail to converge
}
}
return sum(widths) + totalBorderWidth, widths
}
func sum(s []int) int {
out := 0
for _, v := range s {
out += v
}
return out
}
func min(a int, b int) int {
if a < b {
return a
}
return b
}
func max(a int, b int) int {
if a > b {
return a
}
return b
}