-
Notifications
You must be signed in to change notification settings - Fork 31
/
table.go
359 lines (292 loc) · 6.53 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
package simpletable
import (
"fmt"
"math"
"strings"
)
// Table main table object
type Table struct {
Header *Header
Body *Body
Footer *Footer
style *Style
rows []*tblRow
columns []*tblColumn
spanned []*Cell
dividers []*dividerCell
}
//SetStyle sets table style
func (t *Table) SetStyle(style *Style) {
t.style = style
}
// String returns table as a toString
func (t *Table) String() string {
t.refresh()
// TODO: protect against wrong spans
t.initCellContent()
t.prepareRows()
t.prepareColumns()
t.resizeRows()
t.resizeColumns()
s := []string{}
b := t.borderTop()
if b != "" {
s = append(s, b)
}
for _, r := range t.rows {
s = append(s, t.borderLeftRight(r.toStringSlice(), r.isDivider())...)
}
b = t.borderBottom()
if b != "" {
s = append(s, b)
}
return strings.Join(s, "\n")
}
// Print prints table
func (t *Table) Print() {
fmt.Print(t.String())
}
// Println prints table with new line below
func (t *Table) Println() {
fmt.Println(t.String())
}
// refresh resets table meta data (t.rows, t.columns, t.spanned, t.dividers)
func (t *Table) refresh() {
t.rows = []*tblRow{}
t.columns = []*tblColumn{}
t.spanned = []*Cell{}
t.dividers = []*dividerCell{}
}
// borderTop returns top table border
func (t *Table) borderTop() string {
s := t.style.Border
return t.line(s.TopLeft, s.Top, s.TopRight, s.TopIntersection)
}
// borderBottom returns bottom table border
func (t *Table) borderBottom() string {
s := t.style.Border
return t.line(s.BottomLeft, s.Bottom, s.BottomRight, s.BottomIntersection)
}
// borderLeftRight returns bordered row as a toString
func (t *Table) borderLeftRight(s []string, d bool) []string {
if d {
return s
}
for i, v := range s {
s[i] = fmt.Sprintf("%s%s%s", t.style.Border.Left, v, t.style.Border.Right)
}
return s
}
// line returns line (border or divider) as a toString
func (t *Table) line(l, c, r, i string) string {
b := []string{}
for _, col := range t.columns {
b = append(b, strings.Repeat(c, col.getWidth()+2))
}
return fmt.Sprintf("%s%s%s", l, strings.Join(b, i), r)
}
// textSlice2CellSlice casts []*Cell to []cellInterface cause it's not possible do this:
// s := append([]cellInterface{}, t...)
func (t *Table) textSlice2CellSlice(c []*Cell) []cellInterface {
r := []cellInterface{}
for _, tc := range c {
r = append(r, tc)
}
return r
}
// initCellContent loads content for all Cells of table (t.Header.Cells, t.Footer.Cells and t.Body.Cells)
func (t *Table) initCellContent() {
t.initContent(t.Header.Cells...)
for _, c := range t.Body.Cells {
t.initContent(c...)
}
t.initContent(t.Footer.Cells...)
}
// initContent loads content in a slice of Cell
func (t *Table) initContent(s ...*Cell) {
for _, c := range s {
c.content = newContent(c.Text)
}
}
// prepareRows fills t.rows slice from t.Header, t.Body and t.Footer
func (t *Table) prepareRows() {
hlen := len(t.Header.Cells)
if hlen > 0 {
t.rows = append(t.rows, &tblRow{
Cells: t.textSlice2CellSlice(t.Header.Cells),
Table: t,
})
d := ÷rCell{
span: hlen,
}
if t.style.Divider.Center != "" {
t.rows = append(t.rows, &tblRow{
Cells: []cellInterface{
d,
},
Table: t,
})
}
t.dividers = append(t.dividers, d)
}
for _, r := range t.Body.Cells {
t.rows = append(t.rows, &tblRow{
Cells: t.textSlice2CellSlice(r),
Table: t,
})
}
flen := len(t.Footer.Cells)
if flen > 0 {
d := ÷rCell{
span: hlen,
}
if t.style.Divider.Center != "" {
t.rows = append(t.rows, &tblRow{
Cells: []cellInterface{
d,
},
Table: t,
})
}
t.dividers = append(t.dividers, d)
t.rows = append(t.rows, &tblRow{
Cells: t.textSlice2CellSlice(t.Footer.Cells),
Table: t,
})
}
}
// prepareColumns fills t.columns slice from t.rows
func (t *Table) prepareColumns() {
m := [][]cellInterface{}
for _, r := range t.rows {
row := []cellInterface{}
for _, c := range r.Cells {
row = append(row, c)
span := 0
var p cellInterface
var tc *Cell
switch v := c.(type) {
case *Cell:
span = v.Span
p = v
tc = v
case *dividerCell:
span = v.span
p = v
}
if span > 1 {
empty := []*emptyCell{}
for i := 1; i < span; i++ {
empty = append(empty, &emptyCell{
parent: p,
})
}
for _, c := range empty {
row = append(row, c)
}
if tc != nil {
t.spanned = append(t.spanned, tc)
}
switch v := c.(type) {
case *Cell:
v.children = empty
case *dividerCell:
v.children = empty
}
}
}
m = append(m, row)
}
m = t.transposeCells(m)
for _, r := range m {
c := &tblColumn{
Cells: r,
Table: t,
}
for _, cell := range c.Cells {
cell.setColumn(c)
}
t.columns = append(t.columns, c)
}
}
// transposeCells transposes cells matrix - needed for t.columns filling
func (t *Table) transposeCells(i [][]cellInterface) [][]cellInterface {
r := [][]cellInterface{}
for x := 0; x < len(i[0]); x++ {
r = append(r, make([]cellInterface, len(i)))
}
for x, row := range i {
for y, c := range row {
r[y][x] = c
}
}
return r
}
// resizeColumns calculate column height
func (t *Table) resizeRows() {
for _, r := range t.rows {
r.resize()
}
}
// resizeColumns calculate column width (text cells and dividers)
func (t *Table) resizeColumns() {
for _, c := range t.columns {
c.resize()
}
for _, c := range t.spanned {
c.resize()
}
for _, d := range t.dividers {
s := t.size()
d.setWidth(s)
}
}
// incrementColumns bulk increment columns for specified length
func (t *Table) incrementColumns(c []*tblColumn, length int) {
sizes := t.carve(length, len(c))
for i, col := range c {
col.incrementWidth(sizes[i])
}
}
// carve splits length into a slice of integers, the sum of which is equal to length, and the count - equal to parts
func (t *Table) carve(length, parts int) []int {
r := []int{}
step := int(math.Floor(float64(length) / float64(parts)))
if step*parts != length {
step++
}
for i := 0; i < parts; i++ {
var n int
if length < step {
n = length
} else {
n = step
}
length -= n
r = append(r, n)
}
return r
}
// size returns table content size.
func (t *Table) size() int {
return t.rows[0].len()
}
// New is a Table constructor. It loads struct data, ready to be manipulated.
func New() *Table {
return &Table{
style: StyleDefault,
Header: &Header{
Cells: []*Cell{},
},
Body: &Body{
Cells: [][]*Cell{},
},
Footer: &Footer{
Cells: []*Cell{},
},
rows: []*tblRow{},
columns: []*tblColumn{},
spanned: []*Cell{},
dividers: []*dividerCell{},
}
}