-
Notifications
You must be signed in to change notification settings - Fork 6
/
table.go
936 lines (838 loc) · 22.9 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
package table
import (
"encoding/csv"
"fmt"
"io"
"os"
"strings"
runewidth "github.com/mattn/go-runewidth"
"golang.org/x/term"
)
// Table holds information required to render a table to the terminal
type Table struct {
w io.Writer
data [][]string
formatted []iRow
headers [][]string
footers [][]string
alignments []Alignment
headerAlignments []Alignment
footerAlignments []Alignment
borders Borders
lineStyle Style
dividers Dividers
maxColumnWidth int
padding int
cursorStyle Style
rowLines bool
autoMerge bool
autoMergeHeaders bool
headerStyle Style
headerColspans map[int][]int
contentColspans map[int][]int
footerColspans map[int][]int
availableWidth int
headerVerticalAlign Alignment
fillWidth bool
}
type iRow struct {
header bool
footer bool
cols []iCol
first bool
last bool
height int
}
type iCol struct {
original string
span int
lines []ansiBlob
width int
first bool
last bool
height int
mergeAbove bool
mergeBelow bool
alignment Alignment
}
func (c iCol) MaxWidth() int {
var maxWidth int
for _, line := range c.lines {
if line.Len() > maxWidth {
maxWidth = line.Len()
}
}
return maxWidth
}
// Borders dictates whether to draw lines at the extreme edges of the table
type Borders struct {
Left bool
Top bool
Right bool
Bottom bool
}
// New creates a new Table
func New(w io.Writer) *Table {
availableWidth := 80
if w == os.Stdout {
width, _, _ := term.GetSize(int(os.Stdout.Fd()))
if width > 0 {
availableWidth = width
}
}
return &Table{
w: w,
data: nil,
headers: nil,
footers: nil,
alignments: nil,
headerAlignments: nil,
footerAlignments: nil,
borders: Borders{
Left: true,
Top: true,
Right: true,
Bottom: true,
},
lineStyle: StyleNormal,
dividers: UnicodeDividers,
maxColumnWidth: 60,
padding: 1,
rowLines: true,
autoMerge: false,
headerColspans: make(map[int][]int),
contentColspans: make(map[int][]int),
footerColspans: make(map[int][]int),
availableWidth: availableWidth,
headerVerticalAlign: AlignTop,
}
}
// SetBorders enables/disables the border around the table
func (t *Table) SetBorders(enabled bool) {
t.borders = Borders{
Left: enabled,
Top: enabled,
Right: enabled,
Bottom: enabled,
}
}
// SetBorderLeft enables/disables the border line on the left edge of the table
func (t *Table) SetBorderLeft(enabled bool) {
t.borders.Left = enabled
}
// SetBorderRight enables/disables the border line on the right edge of the table
func (t *Table) SetBorderRight(enabled bool) {
t.borders.Right = enabled
}
// SetBorderTop enables/disables the border line on the top edge of the table
func (t *Table) SetBorderTop(enabled bool) {
t.borders.Top = enabled
}
// SetBorderBottom enables/disables the border line on the bottom edge of the table
func (t *Table) SetBorderBottom(enabled bool) {
t.borders.Bottom = enabled
}
// SetLineStyle sets the ANSI style of the lines used when drawing the table. e.g. StyleYellow
func (t *Table) SetLineStyle(s Style) {
t.lineStyle = s
}
// SetRowLines sets whether to render horizontal lines between rows
func (t *Table) SetRowLines(enabled bool) {
t.rowLines = enabled
}
// SetAutoMerge sets whether to merge cells vertically if their content is the same and non-empty
func (t *Table) SetAutoMerge(enabled bool) {
t.autoMerge = enabled
}
// SetFillWidth sets whether to fill the entire available width
func (t *Table) SetFillWidth(enabled bool) {
t.fillWidth = enabled
}
// SetAutoMergeHeaders sets whether to merge header cells vertically if their content is the same and non-empty
func (t *Table) SetAutoMergeHeaders(enabled bool) {
t.autoMergeHeaders = enabled
}
// SetAlignment sets the alignment of each column. Should be specified for each column in the supplied data.
// Default alignment for columns is AlignLeft
func (t *Table) SetAlignment(columns ...Alignment) {
t.alignments = columns
}
// SetHeaderAlignment sets the alignment of each header. Should be specified for each header in the supplied data.
// Default alignment for headers is AlignCenter
func (t *Table) SetHeaderAlignment(columns ...Alignment) {
t.headerAlignments = columns
}
func (t *Table) SetHeaderVerticalAlignment(a Alignment) {
t.headerVerticalAlign = a
}
// SetFooterAlignment sets the alignment of each footer. Should be specified for each footer in the supplied data.
// Default alignment for footers is AlignCenter
func (t *Table) SetFooterAlignment(columns ...Alignment) {
t.footerAlignments = columns
}
// SetHeaders set the headers used for the table.
func (t *Table) SetHeaders(headers ...string) {
t.headers = [][]string{headers}
}
// AddHeaders adds a row of headers to the table.
func (t *Table) AddHeaders(headers ...string) {
t.headers = append(t.headers, headers)
}
// SetHeaderStyle set the style used for headers
func (t *Table) SetHeaderStyle(s Style) {
t.headerStyle = s
}
// SetFooters set the footers used for the table.
func (t *Table) SetFooters(footers ...string) {
t.footers = [][]string{footers}
}
// AddFooters adds a row of footers to the table.
func (t *Table) AddFooters(footers ...string) {
t.footers = append(t.footers, footers)
}
// SetPadding sets the minimum number of spaces which must surround each column value (horizontally).
// For example, a padding of 3 would result in a column value such as " hello world " (3 spaces either side)
func (t *Table) SetPadding(padding int) {
t.padding = padding
}
// SetDividers allows customisation of the characters used to draw the table.
// There are several built-in options, such as UnicodeRoundedDividers.
// Specifying divider values containing more than 1 rune will result in undefined behaviour.
func (t *Table) SetDividers(d Dividers) {
t.dividers = d
}
// AddRow adds a row to the table. Each argument is a column value.
func (t *Table) AddRow(cols ...string) {
t.data = append(t.data, cols)
}
// SetAvailableWidth sets the available width for the table (defaults to terminal width when stdout is used)
func (t *Table) SetAvailableWidth(w int) {
t.availableWidth = w
}
// AddRows adds multiple rows to the table. Each argument is a row, i.e. a slice of column values.
func (t *Table) AddRows(rows ...[]string) {
t.data = append(t.data, rows...)
}
// SetColumnMaxWidth sets the max column width
func (t *Table) SetColumnMaxWidth(maxColumnWidth int) {
t.maxColumnWidth = maxColumnWidth
}
// LoadCSV loads CSV data from a reader and adds it to the table. Existing rows/headers/footers are retained.
func (t *Table) LoadCSV(r io.Reader, hasHeaders bool) error {
cr := csv.NewReader(r)
if hasHeaders {
headers, err := cr.Read()
if err != nil {
return err
}
t.SetHeaders(headers...)
}
for {
data, err := cr.Read()
if err != nil {
if err == io.EOF {
break
}
return err
}
t.AddRow(data...)
}
return nil
}
func (t *Table) getAlignment(colIndex int, header bool, footer bool) Alignment {
switch {
case header:
if colIndex >= len(t.headerAlignments) {
return AlignCenter
}
return t.headerAlignments[colIndex]
case footer:
if colIndex >= len(t.footerAlignments) {
return AlignCenter
}
return t.footerAlignments[colIndex]
default:
if colIndex >= len(t.alignments) {
return AlignLeft
}
return t.alignments[colIndex]
}
}
// find the most columns we have in any given row, header, or footer
func (t *Table) findMaxCols() int {
var maxCols int
for i, r := range t.headers {
rowTotal := 0
for c := range r {
rowTotal += t.getColspan(true, false, i, c)
}
if rowTotal > maxCols {
maxCols = rowTotal
}
}
for i, r := range t.data {
rowTotal := 0
for c := range r {
rowTotal += t.getColspan(false, false, i, c)
}
if rowTotal > maxCols {
maxCols = rowTotal
}
}
for i, r := range t.footers {
rowTotal := 0
for c := range r {
rowTotal += t.getColspan(false, true, i, c)
}
if rowTotal > maxCols {
maxCols = rowTotal
}
}
return maxCols
}
func (t *Table) formatData() {
var formatted []iRow
maxCols := t.findMaxCols()
// add headers
if len(t.headers) > 0 {
for i, headerSet := range t.headers {
headerRow := iRow{
header: true,
footer: false,
cols: nil,
first: i == 0,
last: i == len(t.headers)-1 && len(t.data)+len(t.footers) == 0,
}
for j, heading := range headerSet {
headerRow.cols = append(headerRow.cols, iCol{
original: heading,
width: runewidth.StringWidth(heading),
first: j == 0,
last: j == maxCols-1,
alignment: t.getAlignment(j, true, false),
span: t.getColspan(true, false, i, j),
})
}
formatted = append(formatted, headerRow)
}
}
// add rows
for rowIndex, cols := range t.data {
fRow := iRow{
header: false,
footer: false,
cols: nil,
first: rowIndex == 0 && len(formatted) == 0,
last: rowIndex == len(t.data)-1 && len(t.footers) == 0,
}
for colIndex, data := range cols {
fRow.cols = append(fRow.cols, iCol{
original: data,
width: runewidth.StringWidth(data),
first: colIndex == 0,
last: colIndex == maxCols-1,
alignment: t.getAlignment(colIndex, false, false),
span: t.getColspan(false, false, rowIndex, colIndex),
})
}
formatted = append(formatted, fRow)
}
// add footers
if len(t.footers) > 0 {
for i, footerSet := range t.footers {
footerRow := iRow{
header: false,
footer: true,
cols: nil,
first: len(formatted) == 0,
last: i == len(t.footers)-1,
}
for j, footing := range footerSet {
footerRow.cols = append(footerRow.cols, iCol{
original: footing,
width: runewidth.StringWidth(footing),
first: j == 0,
last: j == maxCols-1,
alignment: t.getAlignment(j, false, true),
span: t.getColspan(false, true, i, j),
})
}
formatted = append(formatted, footerRow)
}
}
formatted = t.equaliseRows(formatted, maxCols)
formatted = t.formatContent(formatted)
t.formatted = t.mergeContent(formatted)
}
func (t *Table) calcColumnWidth(rowIndex int, row iRow) int {
rowTotal := 0
for c := range row.cols {
rowTotal += t.getColspan(row.header, row.footer, rowIndex, c)
}
return rowTotal
}
func (t *Table) equaliseRows(formatted []iRow, maxCols int) []iRow {
// ensure all rows have the same number of columns (taking colspan into account)
for i, row := range formatted {
if len(row.cols) > 0 {
row.cols[len(row.cols)-1].last = false
}
for t.calcColumnWidth(i, row) < maxCols {
row.cols = append(row.cols, iCol{
first: len(row.cols) == 0,
span: 1,
})
}
if len(row.cols) > 0 {
row.cols[len(row.cols)-1].last = true
}
formatted[i] = row
}
return formatted
}
func (t *Table) formatContent(formatted []iRow) []iRow {
var maxWidth int
for _, row := range formatted {
rowWidth := 1
for _, col := range row.cols {
rowWidth += col.width + (t.padding * 2) + 1
}
if rowWidth > maxWidth {
maxWidth = rowWidth
}
}
enableWrapping := t.availableWidth < maxWidth
// wrap text
for r, row := range formatted {
maxLines := 0
for c, col := range row.cols {
wrapLen := t.maxColumnWidth
if !enableWrapping {
wrapLen = runewidth.StringWidth(col.original)
}
wrapped := wrapText(col.original, wrapLen)
formatted[r].cols[c].lines = wrapped
if len(wrapped) > maxLines {
maxLines = len(wrapped)
}
}
// ensure all cols have the same number of lines for a given row
for c, col := range row.cols {
col.lines = t.alignVertically(col.lines, AlignTop, maxLines)
col.height = len(col.lines)
formatted[r].cols[c] = col
}
// set height for row
formatted[r].height = maxLines
}
spares := make([]int, len(formatted))
for r, row := range formatted {
spare := t.availableWidth - 1
for c, col := range row.cols {
spare -= col.MaxWidth() + (t.getColspan(row.header, row.footer, r, c) * ((t.padding * 2) + 1))
}
if spare < 0 {
spare = 0
}
spares[r] = spare
}
var extra int
// set width of each col, and align text
for c := 0; c < t.calcColumnWidth(0, formatted[0]); c++ { // for each col
// find max width for column across all rows
maxWidth := 0
for r, row := range formatted {
if c >= len(row.cols) || row.cols[c].span > 1 {
// ignore columns with a colspan > 1 for now, we'll apply those next
continue
}
if t.fillWidth {
extra = spares[r] / len(row.cols)
}
width := row.cols[c].MaxWidth() + extra
if width > maxWidth {
maxWidth = width
}
}
// set uniform col width, and align all content
for r, row := range formatted {
if c >= len(row.cols) {
continue
}
row.cols[c].width = maxWidth
for l, line := range row.cols[c].lines {
row.cols[c].lines[l] = align(line, maxWidth, row.cols[c].alignment)
}
formatted[r] = row
}
}
return t.applyColSpans(formatted)
}
func (t *Table) alignVertically(lines []ansiBlob, alignment Alignment, maxLines int) []ansiBlob {
switch alignment {
case AlignBottom:
for len(lines) < maxLines {
lines = append([]ansiBlob{newANSI("")}, lines...)
}
default:
for len(lines) < maxLines {
lines = append(lines, newANSI(""))
}
}
return lines
}
// e.g. 5 rows, 2nd with span 5, input 7 returns 3rd row
func (t *Table) getRealIndex(row iRow, index int) int {
var relative int
for actual, col := range row.cols {
if relative == index {
return actual
}
relative += col.span
}
return len(row.cols)
}
func (t *Table) getRelativeIndex(row iRow, index int) int {
var relative int
for actual := 0; actual < len(row.cols) && actual < index; actual++ {
relative += row.cols[actual].span
}
return relative
}
func (t *Table) applyColSpans(formatted []iRow) []iRow {
type colSpanJob struct {
row int
relativeCol int
span int
}
var jobs []colSpanJob
for i, row := range formatted {
for j, col := range row.cols {
if col.span > 1 {
jobs = append(jobs, colSpanJob{
row: i,
relativeCol: t.getRelativeIndex(row, j),
span: col.span,
})
}
}
}
for _, job := range jobs {
realTargetColIndex := t.getRealIndex(formatted[job.row], job.relativeCol)
// grab the cell that has a col span applied
target := formatted[job.row].cols[realTargetColIndex]
// calculate the width required for this cell
targetWidth := target.MaxWidth()
// calculate width required to render the cells beneath/above this cell - plus padding/dividers between them
var childrenWidth int
for i, row := range formatted {
if i == job.row { // skip the row we're working on
continue
}
var rowWidth int
start := t.getRealIndex(row, job.relativeCol)
stop := t.getRealIndex(row, job.relativeCol+job.span)
for j := start; j < stop; j++ {
rowWidth += row.cols[j].width
}
if rowWidth > childrenWidth {
childrenWidth = rowWidth
}
}
childrenWidth += (job.span - 1) * (1 + (2 * t.padding))
switch {
case childrenWidth == targetWidth:
// everything is somehow magically fine
target.width = childrenWidth
formatted[job.row].cols[realTargetColIndex] = target
case childrenWidth > targetWidth:
// we need to grow our target cell to make it aligned with the children
for i := range target.lines {
target.lines[i] = align(target.lines[i], childrenWidth, target.alignment)
}
target.width = childrenWidth
formatted[job.row].cols[realTargetColIndex] = target
default:
// we need to extend the children to align with the wide cell
// we can do this by sharing the extra space between them
available := targetWidth - childrenWidth
share := available / job.span
remainder := available - (share * (job.span - 1))
// allocate each child some room
for i, row := range formatted {
if i == job.row { // skip the row we're working on
continue
}
start := t.getRealIndex(row, job.relativeCol)
stop := t.getRealIndex(row, job.relativeCol+job.span)
for j := start; j < stop; j++ {
amount := share
if j == stop-1 {
amount = remainder
}
row.cols[j].width += amount
for k, line := range row.cols[j].lines {
row.cols[j].lines[k] = align(line, row.cols[j].width, row.cols[j].alignment)
}
}
formatted[i] = row
}
target.width = targetWidth
formatted[job.row].cols[realTargetColIndex] = target
}
}
var lastColMaxWidth int
for r, row := range formatted {
col := row.cols[len(row.cols)-1]
if t.getColspan(row.header, row.footer, r, len(row.cols)-1) > 1 {
continue
}
width := col.width
if width > lastColMaxWidth {
lastColMaxWidth = width
}
}
for r, row := range formatted {
c := len(row.cols) - 1
if t.getColspan(row.header, row.footer, r, c) > 1 {
continue
}
width := row.cols[c].width
if width < lastColMaxWidth {
row.cols[c].width = lastColMaxWidth
for k, line := range row.cols[c].lines {
row.cols[c].lines[k] = align(line, lastColMaxWidth, row.cols[c].alignment)
}
formatted[r] = row
}
}
return formatted
}
func (t *Table) mergeContent(formatted []iRow) []iRow {
columnCount := t.calcColumnWidth(0, formatted[0])
lastValues := make([]string, columnCount)
lastIndexes := make([]int, columnCount)
// flag cols as mergeAbove where content matches and is non-empty
for c := 0; c < columnCount; c++ {
var prevHeader bool
var allowed bool
for r, row := range formatted {
// don't merge columns with colspan > 1
if t.getColspan(row.header, row.footer, r, c) > 1 {
continue
}
if c >= len(row.cols) {
continue
}
relativeIndex := t.getRelativeIndex(row, c)
allowed = (row.header && t.autoMergeHeaders) || (!row.header && !row.footer && !prevHeader && t.autoMerge)
prevHeader = row.header
current := row.cols[c].original
merge := current == lastValues[relativeIndex] && strings.TrimSpace(current) != ""
row.cols[c].mergeAbove = merge && allowed
if merge && allowed {
lastIndex := lastIndexes[relativeIndex]
formatted[r-1].cols[lastIndex].mergeBelow = true
if t.headerVerticalAlign == AlignBottom {
buffer := row.cols[c].lines
row.cols[c].lines = formatted[r-1].cols[lastIndex].lines
formatted[r-1].cols[lastIndex].lines = buffer
}
}
lastValues[relativeIndex] = current
lastIndexes[relativeIndex] = c
formatted[r] = row
}
}
return formatted
}
func (t *Table) renderRows() {
var lastRow iRow
for _, row := range t.formatted {
t.renderRow(row, lastRow)
lastRow = row
}
}
func (t *Table) renderRow(row iRow, prev iRow) {
t.renderLineAbove(row, prev)
for y := 0; y < row.height; y++ {
if t.borders.Left {
t.setStyle(t.lineStyle)
t.print(t.dividers.NS)
t.resetStyle()
}
for _, col := range row.cols {
if t.padding > 0 {
t.print(strings.Repeat(" ", t.padding))
}
if col.mergeAbove && t.headerVerticalAlign == AlignTop {
t.print(strings.Repeat(" ", col.width))
} else if col.mergeBelow && t.headerVerticalAlign == AlignBottom {
t.print(strings.Repeat(" ", col.width))
} else {
if row.header {
t.setStyle(t.headerStyle)
}
t.print(col.lines[y].String())
if row.header {
t.resetStyle()
}
}
if t.padding > 0 {
t.print(strings.Repeat(" ", t.padding))
}
if t.borders.Right || !col.last {
t.setStyle(t.lineStyle)
t.print(t.dividers.NS)
t.resetStyle()
}
}
t.print("\n")
}
t.renderLineBelow(row)
}
// SetHeaderColSpans sets a column span for each column in the given header row.
func (t *Table) SetHeaderColSpans(rowIndex int, colSpans ...int) {
t.headerColspans[rowIndex] = colSpans
}
// SetColSpans sets a column span for each column in the given row.
func (t *Table) SetColSpans(rowIndex int, colSpans ...int) {
t.contentColspans[rowIndex] = colSpans
}
// SetFooterColSpans sets a column span for each column in the given footer row.
func (t *Table) SetFooterColSpans(rowIndex int, colSpans ...int) {
t.footerColspans[rowIndex] = colSpans
}
func (t *Table) getColspan(header bool, footer bool, row int, col int) int {
var target map[int][]int
switch {
case header:
target = t.headerColspans
case footer:
target = t.footerColspans
default:
target = t.contentColspans
}
r, ok := target[row]
if !ok {
return 1
}
if col >= len(r) {
return 1
}
c := r[col]
if c < 1 {
return 1
}
return c
}
// renders the line above a row
func (t *Table) renderLineAbove(row iRow, prev iRow) {
// don't draw top border if disabled
if (row.first && !t.borders.Top) || (!prev.header && !t.rowLines && !row.first) {
return
}
t.setStyle(t.lineStyle)
for i, col := range row.cols {
prevIsMerged := i > 0 && row.cols[i-1].mergeAbove
relativeIndex := t.getRelativeIndex(row, i)
var aboveIsSpanned bool
for j, prevCol := range prev.cols {
prevRel := t.getRelativeIndex(prev, j)
if prevRel >= relativeIndex {
break
}
if prevRel+prevCol.span > relativeIndex {
aboveIsSpanned = true
break
}
}
switch {
case col.first && !t.borders.Left:
// hide border
case row.first && col.first:
t.print(t.dividers.ES)
case row.first:
t.print(t.dividers.ESW)
case col.first && col.mergeAbove:
t.print(t.dividers.NS)
case col.first:
t.print(t.dividers.NES)
case col.mergeAbove && prevIsMerged:
t.print(t.dividers.NS)
case col.mergeAbove && !aboveIsSpanned:
t.print(t.dividers.NSW)
case col.mergeAbove:
t.print(t.dividers.SW)
case prevIsMerged && !aboveIsSpanned:
t.print(t.dividers.NES)
case prevIsMerged:
t.print(t.dividers.ES)
case aboveIsSpanned:
t.print(t.dividers.ESW)
default:
t.print(t.dividers.ALL)
}
if col.mergeAbove {
t.print(strings.Repeat(" ", col.width+(t.padding*2)))
} else {
t.print(strings.Repeat(t.dividers.EW, col.width+(t.padding*2)))
}
switch {
case col.last && !t.borders.Right:
// hide border
case col.last && row.first:
t.print(t.dividers.SW)
case col.last && col.mergeAbove:
t.print(t.dividers.NS)
case col.last:
t.print(t.dividers.NSW)
}
}
t.resetStyle()
t.print("\n")
}
// renders the line below a row, if required
func (t *Table) renderLineBelow(row iRow) {
// we only draw lines below the last row (if borders are on)
if !row.last || !t.borders.Bottom {
return
}
t.setStyle(t.lineStyle)
for _, col := range row.cols {
switch {
case col.first && !t.borders.Left:
// hide
case col.first:
t.print(t.dividers.NE)
default:
t.print(t.dividers.NEW)
}
t.print(strings.Repeat(t.dividers.EW, col.width+(t.padding*2)))
if col.last && t.borders.Right {
t.print(t.dividers.NW)
}
}
t.resetStyle()
t.print("\n")
}
func (t *Table) print(data string) {
_, _ = fmt.Fprint(t.w, data)
}
func (t *Table) resetStyle() {
t.setStyle(StyleNormal)
}
func (t *Table) setStyle(s Style) {
if s != t.cursorStyle {
_, _ = fmt.Fprintf(t.w, "\x1b[%dm", s)
}
t.cursorStyle = s
}
// Render writes the table to the provider io.Writer
func (t *Table) Render() {
if len(t.headers) == 0 && len(t.footers) == 0 && len(t.data) == 0 {
return
}
t.formatData()
t.renderRows()
}