-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathrender_tsv.go
70 lines (56 loc) · 1.46 KB
/
render_tsv.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
package table
import (
"fmt"
"strings"
)
func (t *Table) RenderTSV() string {
t.initForRender()
var out strings.Builder
if t.numColumns > 0 {
if t.title != "" {
out.WriteString(t.title)
}
if t.autoIndex && len(t.rowsHeader) == 0 {
t.tsvRenderRow(&out, t.getAutoIndexColumnIDs(), renderHint{isAutoIndexRow: true, isHeaderRow: true})
}
t.tsvRenderRows(&out, t.rowsHeader, renderHint{isHeaderRow: true})
t.tsvRenderRows(&out, t.rows, renderHint{})
t.tsvRenderRows(&out, t.rowsFooter, renderHint{isFooterRow: true})
if t.caption != "" {
out.WriteRune('\n')
out.WriteString(t.caption)
}
}
return t.render(&out)
}
func (t *Table) tsvRenderRow(out *strings.Builder, row rowStr, hint renderHint) {
if out.Len() > 0 {
out.WriteRune('\n')
}
for idx, col := range row {
if idx == 0 && t.autoIndex {
if hint.isRegularRow() {
out.WriteString(fmt.Sprint(hint.rowNumber))
}
out.WriteRune('\t')
}
if idx > 0 {
out.WriteRune('\t')
}
if strings.ContainsAny(col, "\t\n\"") || strings.Contains(col, " ") {
col = strings.ReplaceAll(col, "\"", "\"\"") // fix double-quotes
out.WriteString(fmt.Sprintf("\"%s\"", col))
} else {
out.WriteString(col)
}
}
for colIdx := len(row); colIdx < t.numColumns; colIdx++ {
out.WriteRune('\t')
}
}
func (t *Table) tsvRenderRows(out *strings.Builder, rows []rowStr, hint renderHint) {
for idx, row := range rows {
hint.rowNumber = idx + 1
t.tsvRenderRow(out, row, hint)
}
}