-
Notifications
You must be signed in to change notification settings - Fork 0
/
align.go
471 lines (462 loc) · 15 KB
/
align.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
package brimtext
import (
"bytes"
"strings"
)
type Alignment int
const (
Left Alignment = iota
Right
Center
)
type AlignOptions struct {
// Widths indicate the desired widths of each column. If nil or if a value
// is 0, no rewrapping will be done.
Widths []int
Alignments []Alignment
// FirstDR etc. control what is output for situations with a prepended
// display row, First row output with Down and Right connections, etc.
FirstDR string
FirstLR string
FirstFirstDLR string
FirstDLR string
FirstDL string
// RowFirstUD etc. control situations for each data row output.
RowFirstUD string
RowSecondUD string
RowUD string
RowLastUD string
// LeaveTrailingWhitespace should be set true if the last cell of data row
// needs spaces to fill to the end (usually needed when setting RowLastUD).
LeaveTrailingWhitespace bool
// FirstNilFirstUDR etc. control situations when the first nil data row is
// encountered. Can be used to separate the header from the rest of the
// rows.
FirstNilFirstUDR string
FirstNilLR string
FirstNilFirstUDLR string
FirstNilUDLR string
FirstNilLastUDL string
// NilFirstUDR etc. control situations when the second and subsequent nil
// data rows are encountered. Can be used to separate rows from each other.
NilFirstUDR string
NilLR string
NilFirstUDLR string
NilUDLR string
NilLastUDL string
// LastUR etc. control what is output for situations with an appended
// display row.
LastUR string
LastLR string
LastFirstULR string
LastULR string
LastUL string
// NilBetweenEveryRow will add a nil data row between all rows; use to emit
// FirstNil* and Nil* row separators.
NilBetweenEveryRow bool
}
// NewDefaultAlignOptions gives:
//
// &AlignOptions{RowSecondUD: " ", RowUD: " "}
//
// Which will format tables like:
//
// Bob Sue John
// Hometown San Antonio Austin New York
// Mother Bessie Mary Sarah
// Father Rick Dan Mike
func NewDefaultAlignOptions() *AlignOptions {
return &AlignOptions{RowSecondUD: " ", RowUD: " "}
}
// NewSimpleAlignOptions gives:
//
// return &AlignOptions{
// FirstDR: "+-",
// FirstLR: "-",
// FirstFirstDLR: "-+-",
// FirstDLR: "-+-",
// FirstDL: "-+",
// RowFirstUD: "| ",
// RowSecondUD: " | ",
// RowUD: " | ",
// RowLastUD: " |",
// LeaveTrailingWhitespace: true,
// FirstNilFirstUDR: "+-",
// FirstNilLR: "-",
// FirstNilFirstUDLR: "-+-",
// FirstNilUDLR: "-+-",
// FirstNilLastUDL: "-+",
// LastUR: "+-",
// LastLR: "-",
// LastFirstULR: "-+-",
// LastULR: "-+-",
// LastUL: "-+",
// }
//
// Which will format tables like:
//
// +----------+-------------+--------+----------+
// | | Bob | Sue | John |
// +----------+-------------+--------+----------+
// | Hometown | San Antonio | Austin | New York |
// | Mother | Bessie | Mary | Sarah |
// | Father | Rick | Dan | Mike |
// +----------+-------------+--------+----------+
func NewSimpleAlignOptions() *AlignOptions {
return &AlignOptions{
FirstDR: "+-",
FirstLR: "-",
FirstFirstDLR: "-+-",
FirstDLR: "-+-",
FirstDL: "-+",
RowFirstUD: "| ",
RowSecondUD: " | ",
RowUD: " | ",
RowLastUD: " |",
LeaveTrailingWhitespace: true,
FirstNilFirstUDR: "+-",
FirstNilLR: "-",
FirstNilFirstUDLR: "-+-",
FirstNilUDLR: "-+-",
FirstNilLastUDL: "-+",
LastUR: "+-",
LastLR: "-",
LastFirstULR: "-+-",
LastULR: "-+-",
LastUL: "-+",
}
}
// NewBoxedAlignOptions gives:
// &AlignOptions{
// FirstDR: "+=",
// FirstLR: "=",
// FirstFirstDLR: "=+=",
// FirstDLR: "=+=",
// FirstDL: "=+",
// RowFirstUD: "| ",
// RowSecondUD: " | ",
// RowUD: " | ",
// RowLastUD: " |",
// LeaveTrailingWhitespace: true,
// FirstNilFirstUDR: "+=",
// FirstNilLR: "=",
// FirstNilFirstUDLR: "=+=",
// FirstNilUDLR: "=+=",
// FirstNilLastUDL: "=+",
// NilFirstUDR: "+-",
// NilLR: "-",
// NilFirstUDLR: "-+-",
// NilUDLR: "-+-",
// NilLastUDL: "-+",
// LastUR: "+=",
// LastLR: "=",
// LastFirstULR: "=+=",
// LastULR: "=+=",
// LastUL: "=+",
// NilBetweenEveryRow: true,
// }
//
// Which will format tables like:
//
// +==========+=============+========+==========+
// | | Bob | Sue | John |
// +==========+=============+========+==========+
// | Hometown | San Antonio | Austin | New York |
// +----------+-------------+--------+----------+
// | Mother | Bessie | Mary | Sarah |
// +----------+-------------+--------+----------+
// | Father | Rick | Dan | Mike |
// +==========+=============+========+==========+
func NewBoxedAlignOptions() *AlignOptions {
return &AlignOptions{
FirstDR: "+=",
FirstLR: "=",
FirstFirstDLR: "=+=",
FirstDLR: "=+=",
FirstDL: "=+",
RowFirstUD: "| ",
RowSecondUD: " | ",
RowUD: " | ",
RowLastUD: " |",
LeaveTrailingWhitespace: true,
FirstNilFirstUDR: "+=",
FirstNilLR: "=",
FirstNilFirstUDLR: "=+=",
FirstNilUDLR: "=+=",
FirstNilLastUDL: "=+",
NilFirstUDR: "+-",
NilLR: "-",
NilFirstUDLR: "-+-",
NilUDLR: "-+-",
NilLastUDL: "-+",
LastUR: "+=",
LastLR: "=",
LastFirstULR: "=+=",
LastULR: "=+=",
LastUL: "=+",
NilBetweenEveryRow: true,
}
}
// NewUnicodeBoxedAlignOptions gives:
// &AlignOptions{
// FirstDR: "\u2554\u2550",
// FirstLR: "\u2550",
// FirstFirstDLR: "\u2550\u2566\u2550",
// FirstDLR: "\u2550\u2564\u2550",
// FirstDL: "\u2550\u2557",
// RowFirstUD: "\u2551 ",
// RowSecondUD: " \u2551 ",
// RowUD: " \u2502 ",
// RowLastUD: " \u2551",
// LeaveTrailingWhitespace: true,
// FirstNilFirstUDR: "\u2560\u2550",
// FirstNilLR: "\u2550",
// FirstNilFirstUDLR: "\u2550\u256c\u2550",
// FirstNilUDLR: "\u2550\u256a\u2550",
// FirstNilLastUDL: "\u2550\u2563",
// NilFirstUDR: "\u255f\u2500",
// NilLR: "\u2500",
// NilFirstUDLR: "\u2500\u256b\u2500",
// NilUDLR: "\u2500\u253c\u2500",
// NilLastUDL: "\u2500\u2562",
// LastUR: "\u255a\u2550",
// LastLR: "\u2550",
// LastFirstULR: "\u2550\u2569\u2550",
// LastULR: "\u2550\u2567\u2550",
// LastUL: "\u2550\u255d",
// NilBetweenEveryRow: true,
// }
//
// Which will format tables like:
//
// ╔══════════╦═════════════╤════════╤══════════╗
// ║ ║ Bob │ Sue │ John ║
// ╠══════════╬═════════════╪════════╪══════════╣
// ║ Hometown ║ San Antonio │ Austin │ New York ║
// ╟──────────╫─────────────┼────────┼──────────╢
// ║ Mother ║ Bessie │ Mary │ Sarah ║
// ╟──────────╫─────────────┼────────┼──────────╢
// ║ Father ║ Rick │ Dan │ Mike ║
// ╚══════════╩═════════════╧════════╧══════════╝
func NewUnicodeBoxedAlignOptions() *AlignOptions {
return &AlignOptions{
FirstDR: "\u2554\u2550",
FirstLR: "\u2550",
FirstFirstDLR: "\u2550\u2566\u2550",
FirstDLR: "\u2550\u2564\u2550",
FirstDL: "\u2550\u2557",
RowFirstUD: "\u2551 ",
RowSecondUD: " \u2551 ",
RowUD: " \u2502 ",
RowLastUD: " \u2551",
LeaveTrailingWhitespace: true,
FirstNilFirstUDR: "\u2560\u2550",
FirstNilLR: "\u2550",
FirstNilFirstUDLR: "\u2550\u256c\u2550",
FirstNilUDLR: "\u2550\u256a\u2550",
FirstNilLastUDL: "\u2550\u2563",
NilFirstUDR: "\u255f\u2500",
NilLR: "\u2500",
NilFirstUDLR: "\u2500\u256b\u2500",
NilUDLR: "\u2500\u253c\u2500",
NilLastUDL: "\u2500\u2562",
LastUR: "\u255a\u2550",
LastLR: "\u2550",
LastFirstULR: "\u2550\u2569\u2550",
LastULR: "\u2550\u2567\u2550",
LastUL: "\u2550\u255d",
NilBetweenEveryRow: true,
}
}
// Align will format a table according to options. If opts is nil,
// NewDefaultAlignOptions is used.
func Align(data [][]string, opts *AlignOptions) string {
if len(data) == 0 {
return ""
}
if opts == nil {
opts = NewDefaultAlignOptions()
}
newData := make([][]string, 0, len(data))
for _, row := range data {
if row == nil {
if !opts.NilBetweenEveryRow {
newData = append(newData, nil)
}
continue
}
if opts.Widths != nil {
newRow := make([]string, 0, len(row))
for col, cell := range row {
if col >= len(opts.Widths) || opts.Widths[col] <= 0 {
newRow = append(newRow, cell)
continue
}
newRow = append(newRow, Wrap(cell, opts.Widths[col], "", ""))
}
row = newRow
}
work := make([][]string, 0, len(row))
for _, cell := range row {
cell = strings.Replace(cell, "\r\n", "\n", -1)
work = append(work, strings.Split(cell, "\n"))
}
maxCells := 0
for _, cells := range work {
c := len(cells)
if c > maxCells {
maxCells = c
}
}
newRows := make([][]string, 0)
if opts.NilBetweenEveryRow && len(newData) != 0 {
newData = append(newData, nil)
}
for c := 0; c < maxCells; c++ {
newRow := make([]string, 0, len(work))
for col := 0; col < len(work); col++ {
if c < len(work[col]) {
newRow = append(newRow, work[col][c])
} else {
newRow = append(newRow, "")
}
}
newRows = append(newRows, newRow)
}
newData = append(newData, newRows...)
}
data = newData
widths := make([]int, 0, len(data[0]))
for _, row := range data {
if row == nil {
continue
}
for len(row) > len(widths) {
widths = append(widths, RuneLenStripANSIEscapes(row[len(widths)]))
}
for c, v := range row {
if RuneLenStripANSIEscapes(v) > widths[c] {
widths[c] = RuneLenStripANSIEscapes(v)
}
}
}
alignments := opts.Alignments
if alignments == nil || len(alignments) < len(widths) {
newal := append(make([]Alignment, 0, len(widths)), alignments...)
for len(newal) < len(widths) {
newal = append(newal, Left)
}
alignments = newal
}
est := RuneLenStripANSIEscapes(opts.RowFirstUD)
for _, w := range widths {
est += w + RuneLenStripANSIEscapes(opts.RowUD)
}
est += RuneLenStripANSIEscapes(opts.RowLastUD) + 1
est *= len(data)
buf := bytes.NewBuffer(make([]byte, 0, est))
if !AllEqual("", opts.FirstDR, opts.FirstFirstDLR, opts.FirstDLR, opts.FirstLR, opts.FirstDL) {
buf.WriteString(opts.FirstDR)
for col, width := range widths {
if col == 1 {
buf.WriteString(opts.FirstFirstDLR)
} else if col != 0 {
buf.WriteString(opts.FirstDLR)
}
for i := 0; i < width; i++ {
buf.WriteString(opts.FirstLR)
}
}
buf.WriteString(opts.FirstDL)
buf.WriteByte('\n')
}
firstNil := true
for _, row := range data {
if row == nil {
if firstNil {
if !AllEqual("", opts.FirstNilFirstUDR, opts.FirstNilFirstUDLR, opts.FirstNilUDLR, opts.FirstNilLR, opts.FirstNilLastUDL) {
buf.WriteString(opts.FirstNilFirstUDR)
for col, width := range widths {
if col == 1 {
buf.WriteString(opts.FirstNilFirstUDLR)
} else if col != 0 {
buf.WriteString(opts.FirstNilUDLR)
}
for i := 0; i < width; i++ {
buf.WriteString(opts.FirstNilLR)
}
}
buf.WriteString(opts.FirstNilLastUDL)
}
firstNil = false
} else {
if !AllEqual("", opts.NilFirstUDR, opts.NilFirstUDLR, opts.NilUDLR, opts.NilLR, opts.NilLastUDL) {
buf.WriteString(opts.NilFirstUDR)
for col, width := range widths {
if col == 1 {
buf.WriteString(opts.NilFirstUDLR)
} else if col != 0 {
buf.WriteString(opts.NilUDLR)
}
for i := 0; i < width; i++ {
buf.WriteString(opts.NilLR)
}
}
buf.WriteString(opts.NilLastUDL)
}
}
buf.WriteByte('\n')
continue
}
buf.WriteString(opts.RowFirstUD)
for c, v := range row {
if c == 1 {
buf.WriteString(opts.RowSecondUD)
} else if c != 0 {
buf.WriteString(opts.RowUD)
}
switch alignments[c] {
case Right:
for i := widths[c] - RuneLenStripANSIEscapes(v); i > 0; i-- {
buf.WriteRune(' ')
}
buf.WriteString(v)
case Center:
for i := (widths[c] - RuneLenStripANSIEscapes(v)) / 2; i > 0; i-- {
buf.WriteRune(' ')
}
buf.WriteString(v)
if opts.LeaveTrailingWhitespace || c < len(row)-1 {
for i := widths[c] - ((widths[c]-RuneLenStripANSIEscapes(v))/2 + RuneLenStripANSIEscapes(v)); i > 0; i-- {
buf.WriteRune(' ')
}
}
default:
buf.WriteString(v)
if opts.LeaveTrailingWhitespace || c < len(row)-1 {
for i := widths[c] - RuneLenStripANSIEscapes(v); i > 0; i-- {
buf.WriteRune(' ')
}
}
}
}
buf.WriteString(opts.RowLastUD)
buf.WriteByte('\n')
}
if !AllEqual("", opts.LastUR, opts.LastFirstULR, opts.LastULR, opts.LastLR, opts.LastUL) {
buf.WriteString(opts.LastUR)
for col, width := range widths {
if col == 1 {
buf.WriteString(opts.LastFirstULR)
} else if col != 0 {
buf.WriteString(opts.LastULR)
}
for i := 0; i < width; i++ {
buf.WriteString(opts.LastLR)
}
}
buf.WriteString(opts.LastUL)
buf.WriteByte('\n')
}
return buf.String()
}