-
Notifications
You must be signed in to change notification settings - Fork 14
/
table_style.go
313 lines (296 loc) · 9.51 KB
/
table_style.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
package clif
import (
"fmt"
"strings"
)
var (
ClosedTableStyle = &TableStyle{
Bottom: "─",
ContentRenderer: func(content string) string { return content },
CrossBottom: "┴",
CrossInner: "┼",
CrossLeft: "├",
CrossRight: "┤",
CrossTop: "┬",
HeaderRenderer: func(content string) string { return fmt.Sprintf("\033[1;4m%s\033[0m", content) },
InnerHorizontal: "─",
InnerVertical: "│",
Left: "│",
LeftBottom: "└",
LeftTop: "┌",
Prefix: " ",
Right: "│",
RightBottom: "┘",
RightTop: "┐",
Suffix: " ",
Top: "─",
}
ClosedTableStyleLight = &TableStyle{
Bottom: "\033[38;5;234m─\033[0m",
ContentRenderer: func(content string) string { return content },
CrossBottom: "\033[38;5;234m┴\033[0m",
CrossInner: "\033[38;5;234m┼\033[0m",
CrossLeft: "\033[38;5;234m├\033[0m",
CrossRight: "\033[38;5;234m┤\033[0m",
CrossTop: "\033[38;5;234m┬\033[0m",
HeaderRenderer: func(content string) string { return fmt.Sprintf("\033[1;4m%s\033[0m", content) },
InnerHorizontal: "\033[38;5;234m─\033[0m",
InnerVertical: "\033[38;5;234m│\033[0m",
Left: "\033[38;5;234m│\033[0m",
LeftBottom: "\033[38;5;234m└\033[0m",
LeftTop: "\033[38;5;234m┌\033[0m",
Prefix: " ",
Right: "\033[38;5;234m│\033[0m",
RightBottom: "\033[38;5;234m┘\033[0m",
RightTop: "\033[38;5;234m┐\033[0m",
Suffix: " ",
Top: "\033[38;5;234m─\033[0m",
}
OpenTableStyle = &TableStyle{
Bottom: "",
ContentRenderer: func(content string) string { return content },
CrossBottom: "┴",
CrossInner: "┼",
CrossLeft: "",
CrossRight: "",
CrossTop: "┬",
HeaderRenderer: func(content string) string { return fmt.Sprintf("\033[1m%s\033[0m", content) },
InnerHorizontal: "─",
InnerVertical: "│",
Left: "",
LeftBottom: "",
LeftTop: "",
Prefix: " ",
Right: "",
RightBottom: "",
RightTop: "",
Suffix: " ",
Top: "",
}
OpenTableStyleLight = &TableStyle{
Bottom: "",
ContentRenderer: func(content string) string { return content },
CrossBottom: "\033[38;5;234m┴\033[0m",
CrossInner: "\033[38;5;234m┼\033[0m",
CrossLeft: "",
CrossRight: "",
CrossTop: "\033[38;5;234m┬\033[0m",
HeaderRenderer: func(content string) string { return fmt.Sprintf("\033[1m%s\033[0m", content) },
InnerHorizontal: "\033[38;5;234m─\033[0m",
InnerVertical: "\033[38;5;234m│\033[0m",
Left: "",
LeftBottom: "",
LeftTop: "",
Prefix: " ",
Right: "",
RightBottom: "",
RightTop: "",
Suffix: " ",
Top: "",
}
DefaultTableStyle *TableStyle
)
func NewDefaultTableStyle() *TableStyle {
return CopyTableStyle(DefaultTableStyle)
}
func CopyTableStyle(from *TableStyle) *TableStyle {
to := new(TableStyle)
to.Bottom = from.Bottom
to.ContentRenderer = from.ContentRenderer
to.CrossBottom = from.CrossBottom
to.CrossInner = from.CrossInner
to.CrossLeft = from.CrossLeft
to.CrossRight = from.CrossRight
to.CrossTop = from.CrossTop
to.HeaderRenderer = from.HeaderRenderer
to.InnerHorizontal = from.InnerHorizontal
to.InnerVertical = from.InnerVertical
to.Left = from.Left
to.LeftBottom = from.LeftBottom
to.LeftTop = from.LeftTop
to.Prefix = from.Prefix
to.Right = from.Right
to.RightBottom = from.RightBottom
to.RightTop = from.RightTop
to.Suffix = from.Suffix
to.Top = from.Top
return to
}
// Waste returns the amount of "wasted" characters (table render characters
// + prefix and suffix whitespaces) for given amount of columns
func (this *TableStyle) Waste(colCount int) int {
if colCount <= 1 {
colCount = 0
} else {
colCount--
}
perCol := StringLength(this.Prefix) + StringLength(this.Suffix)
return StringLength(this.Left) +
StringLength(this.Right) +
perCol*colCount +
(colCount-1)*StringLength(this.InnerVertical)
}
// Render renders given table with a given max width. Max width can not be lower
// than waste per row (see `Waste()`) and at least one character per column.
func (this *TableStyle) Render(table *Table, mw ...int) string {
//fmt.Printf("\n-----------------------\n\n+ RENDER START\n")
maxWidth := 0
if len(mw) > 0 {
maxWidth = mw[0]
}
colWidths := this.CalculateColWidths(table, maxWidth)
out := this.renderTopRow(colWidths)
out += this.renderHeaderRow(table.Headers, colWidths)
for _, row := range table.Rows {
out += this.renderDataRow(row, colWidths)
}
//out += "--"
out += this.renderBottomRow(colWidths)
//fmt.Printf("\n+ RENDER END\n")
return strings.TrimRight(out, "\n") + "\n"
}
// CalculateColWidths returns the widths of the cols of the table, for given max width
func (this *TableStyle) CalculateColWidths(table *Table, totalTableWidth int) []int {
if totalTableWidth == 0 {
totalTableWidth = TermWidthCurrent
}
waste := this.Waste(table.colAmount)
//fmt.Printf("\n+ CALC COL WIDTHS (MAX = %d, WASTE = %d)\n", totalTableWidth, waste)
if waste >= totalTableWidth {
totalTableWidth = 0
} else {
totalTableWidth -= waste
}
colWidths := make([]int, table.colAmount)
sumColWidth := 0
table.Headers.SetRenderer(this.HeaderRenderer)
rows := make([]*TableRow, table.rowAmount+1)
rows[0] = table.Headers
for idx, row := range table.Rows {
row.SetRenderer(this.ContentRenderer)
rows[idx+1] = row
}
for _, row := range rows {
widths := row.CalculateWidths(totalTableWidth)
//fmt.Printf(" ## ROW %d -> %v\n", idx, widths)
for idx, wd := range widths {
if wd > colWidths[idx] {
sumColWidth -= colWidths[idx]
colWidths[idx] = wd
sumColWidth += wd
}
}
}
if sumColWidth == 0 {
sumColWidth = 1
}
if totalTableWidth > 0 {
factor := float64(totalTableWidth) / float64(sumColWidth)
usedWidth := 0
lastWidthIdx := len(colWidths) - 1
for idx, width := range colWidths {
colWidths[idx] = int(float64(width) * factor)
usedWidth += colWidths[idx]
if idx == lastWidthIdx {
colWidths[idx] += totalTableWidth - usedWidth
}
}
}
//fmt.Printf("\n+ CALC COL WIDTHS DONE SUM=%d, (=%v)\n", sumColWidth, colWidths)
return colWidths
}
func (this *TableStyle) renderBorderRow(first, prefix, content, suffix, cross, last string, colWidths []int) string {
row := first
lastColIdx := len(colWidths) - 1
for idx, colWidth := range colWidths {
row += strings.Repeat(prefix, StringLength(this.Prefix))
if cw := colWidth; cw > 0 {
row += strings.Repeat(content, cw)
}
row += strings.Repeat(suffix, StringLength(this.Suffix))
if idx < lastColIdx {
row += cross
}
}
row += last
return row
}
func (this *TableStyle) renderContentRow(first, cross, last string, colContents []string, colWidths []int) string {
// transform (i in slice[string] to (i, j in slice[string][string]) in which each row i has the
// same amount of elements j (hence normalized)
colCount := len(colContents)
normalizedCols := make([][]string, colCount)
emptyCol := make([]string, colCount)
maxLines := 0
for idx, colContent := range colContents {
normalizedCols[idx] = strings.Split(colContent, "\n")
if l := len(normalizedCols[idx]); l > maxLines {
maxLines = l
}
emptyCol[idx] = strings.Repeat(" ", int(colWidths[idx]))
}
for idx, _ := range normalizedCols {
for i := len(normalizedCols[idx]); i < maxLines; i++ {
normalizedCols[idx] = append(normalizedCols[idx], emptyCol[idx])
}
//fmt.Printf("\nCOL %d: LINES = %d\n", idx, len(normalizedCols[idx]))
}
colAmount := len(normalizedCols) - 1
lineAmount := len(normalizedCols[0]) - 1
out := ""
for ldx := 0; ldx <= lineAmount; ldx++ {
for cdx := 0; cdx <= colAmount; cdx++ {
if cdx == 0 {
out += first
}
out += this.Prefix
out += normalizedCols[cdx][ldx]
out += this.Suffix
if cdx == colAmount {
out += last
} else {
out += cross
}
if cdx == colAmount {
//fmt.Printf(" LINE BREAK ON L=%d, C=%d = \"%s\"\n", ldx, cdx, normalizedCols[cdx][ldx])
out += "\n"
}
}
}
return out
}
func (this *TableStyle) renderTopRow(colWidths []int) string {
if this.Top != "" {
return this.renderBorderRow(this.LeftTop, this.Top, this.Top, this.Top, this.CrossTop, this.RightTop, colWidths) + "\n"
}
return ""
}
func (this *TableStyle) renderHeaderRow(row *TableRow, colWidths []int) string {
row.SetRenderer(this.HeaderRenderer)
rendered, _ := row.RenderWithWidths(colWidths)
/*for idx, text := range rendered {
rendered[idx] = this.HeaderRenderer(text)
}*/
return this.renderContentRow(this.Left, this.InnerVertical, this.Right, rendered, colWidths)
}
func (this *TableStyle) renderDataRow(row *TableRow, colWidths []int) string {
row.SetRenderer(this.ContentRenderer)
rendered, _ := row.RenderWithWidths(colWidths)
out := ""
out += this.renderBorderRow(this.CrossLeft, this.InnerHorizontal, this.InnerHorizontal, this.InnerHorizontal, this.CrossInner,
this.CrossRight, colWidths)
/*for idx, text := range rendered {
rendered[idx] = this.ContentRenderer(text)
}*/
out += "\n" + this.renderContentRow(this.Left, this.InnerVertical, this.Right, rendered, colWidths)
return out
}
func (this *TableStyle) renderBottomRow(colWidths []int) string {
if this.Bottom != "" {
return this.renderBorderRow(this.LeftBottom, this.Bottom, this.Bottom, this.Bottom, this.CrossBottom, this.RightBottom, colWidths)
}
return ""
}
func init() {
DefaultTableStyle = CopyTableStyle(ClosedTableStyle)
}