-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage_more_test.go
More file actions
497 lines (446 loc) · 17.5 KB
/
Copy pathusage_more_test.go
File metadata and controls
497 lines (446 loc) · 17.5 KB
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
// Copyright © 2018,2025 Pennock Tech, LLC.
// These examples are deliberately for use in your code, and so while this file
// is still covered under LICENSE.txt, only the liability disclaimers apply.
// Copy/paste freely without the license applying directly to your code. The
// license still applies to any bundled version of this library.
package examples
import (
"fmt"
"os"
"go.pennock.tech/tabular"
auto_table "go.pennock.tech/tabular/auto"
tab_csv "go.pennock.tech/tabular/csv"
tab_html "go.pennock.tech/tabular/html"
tab_json "go.pennock.tech/tabular/json"
tab_md "go.pennock.tech/tabular/markdown"
"go.pennock.tech/tabular/properties"
"go.pennock.tech/tabular/properties/align"
"go.pennock.tech/tabular/texttable/decoration"
)
func Example_inspection() {
t := auto_table.New("") // empty string is style-less, so a rendering error later
t = populateTable(t)
err := t.RenderTo(os.Stdout)
if err != nil {
// nb, we actually expect output, so using os.Stdout here this time
fmt.Fprintf(os.Stdout, "table rendering error: %s\n", err)
}
fmt.Printf("Table has %d rows and %d columns\n", t.NRows(), t.NColumns())
row := 3
column := 2
cell, err := t.CellAt(tabular.CellLocation{Row: row, Column: column})
loc := cell.Location()
fmt.Printf("At row %d column %d the value is %q, self-described as row %d column %d\n",
row, column, cell, loc.Row, loc.Column)
rowKiwi := tabular.NewRowWithCapacity(2)
rowKiwi.Add(tabular.NewCell("kiwis"))
rowKiwi.Add(tabular.NewCell("2.50"))
t.AddRow(rowKiwi)
kiwiLocation := rowKiwi.Location()
fmt.Printf("Kiwi row is row %d\n", kiwiLocation.Row)
fmt.Printf("\nManual style at render time:\n")
if err = auto_table.RenderTo(t, os.Stdout, "utf8-double"); err != nil {
fmt.Fprintf(os.Stdout, "second render, table rendering error: %s\n", err)
}
// nb: this highlights a limitation of tabular: we can only use characters
// which exist in Unicode, and the box-drawing double-lines are missing
// double left/up/right and single down, so you'll notice a display glitch
// below.
// Output:
// table rendering error: table has no decoration at all, can't render
// Table has 4 rows and 2 columns
// At row 3 column 2 the value is "2.22", self-described as row 3 column 2
// Kiwi row is row 5
//
// Manual style at render time:
// ╔══════════════╦════════════╗
// ║ Fruit ║ Price $/lb ║
// ╠══════════════╪════════════╣
// ║ bananas │ 0.56 ║
// ║ apples │ 1.31 ║
// ║ strawberries │ 2.22 ║
// ║ pears │ 1.90 ║
// ║ kiwis │ 2.50 ║
// ╚══════════════╧════════════╝
}
func Example_rows() {
// can use named constants so that typos can be caught at compile time, instead
// of strings, _if_ you're willing to import the extra package; we don't force
// callers to do this, letting them make the call on trade-offs.
t := auto_table.New(decoration.D_UTF8_LIGHT_CURVED)
t = populateTable(t)
extra := t.NewRowSizedFor()
// could call tabular.NewRow(), or tabular.NewRowWithCapacity(2), but the
// table-method supplies the current table column count to the latter,
// letting us just get the right size. Can always add cells to a row, and
// keep adding, which we'll show here, but this is getting the "right" size
// with pre-allocation.
c1 := tabular.NewCell("tomato")
c2 := tabular.NewCell("2.24")
extra = extra.Add(c1).Add(c2).Add(tabular.NewCell("but is it a fruit?"))
t.AddSeparator()
t.AddRow(extra)
fmt.Printf("Now have %d columns\n", t.NColumns())
if err := t.RenderTo(os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err)
}
// Output:
// Now have 3 columns
// ╭──────────────┬────────────┬────────────────────╮
// │ Fruit │ Price $/lb │ │
// ├──────────────┼────────────┼────────────────────┤
// │ bananas │ 0.56 │ │
// │ apples │ 1.31 │ │
// │ strawberries │ 2.22 │ │
// │ pears │ 1.90 │ │
// ├──────────────┼────────────┼────────────────────┤
// │ tomato │ 2.24 │ but is it a fruit? │
// ╰──────────────┴────────────┴────────────────────╯
}
func Example_alignment() {
t := auto_table.New(decoration.D_UTF8_LIGHT_CURVED)
t = populateTable(t)
t.AddRowItems("Sekai-ichi apple", "10.50")
t.Column(2).SetProperty(align.PropertyType, align.Right)
if err := t.RenderTo(os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err)
}
// Output:
// ╭──────────────────┬────────────╮
// │ Fruit │ Price $/lb │
// ├──────────────────┼────────────┤
// │ bananas │ 0.56 │
// │ apples │ 1.31 │
// │ strawberries │ 2.22 │
// │ pears │ 1.90 │
// │ Sekai-ichi apple │ 10.50 │
// ╰──────────────────┴────────────╯
}
func Example_alignment2() {
t := auto_table.New(decoration.D_UTF8_LIGHT_CURVED)
t = populateTable(t)
t.AddRowItems("Sekai-ichi apple", "10.50")
t.Column(0).SetProperty(align.PropertyType, align.Right)
t.Column(1).SetProperty(align.PropertyType, align.Center)
if err := t.RenderTo(os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err)
}
// Output:
// ╭──────────────────┬────────────╮
// │ Fruit │ Price $/lb │
// ├──────────────────┼────────────┤
// │ bananas │ 0.56 │
// │ apples │ 1.31 │
// │ strawberries │ 2.22 │
// │ pears │ 1.90 │
// │ Sekai-ichi apple │ 10.50 │
// ╰──────────────────┴────────────╯
}
func populateFourTable(t tabular.Table) {
t.AddHeaders("Person", "Age", "Score", "Color")
t.AddRowItems("Fred", 34, 0.6, "red")
t.AddRowItems("Gladys", 32, 0.8, "yellow")
t.AddRowItems("Bert", 57, 0.7, "green")
t.AddRowItems("Belinda", 58, 0.8, "blue")
for _, err := range t.Errors() {
fmt.Fprintf(os.Stderr, "table error: %s\n", err)
}
t.Column(2).SetProperty(align.PropertyType, align.Right)
// FIXME: add Column(3) align-float dot alignment
}
func Example_omit_text() {
t := auto_table.New(decoration.D_UTF8_LIGHT_CURVED)
populateFourTable(t)
show := func(tb auto_table.RenderTable) {
if err := tb.RenderTo(os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err)
}
}
fmt.Println("full:")
show(t)
fmt.Println("no column 3:")
t.Column(3).SetProperty(properties.Omit, true)
show(t)
fmt.Println("with column 3:")
t.Column(3).SetProperty(properties.Omit, false) // explicitly false
show(t)
fmt.Println("column 3 property removed:")
t.Column(3).SetProperty(properties.Omit, nil) // nil removes the property
show(t)
fmt.Println("drop last 2 columns:")
t.Column(3).SetProperty(properties.Omit, true)
t.Column(4).SetProperty(properties.Omit, true)
show(t)
fmt.Println("also drop second column:")
t.Column(2).SetProperty(properties.Omit, true)
show(t)
fmt.Println("expect no table here (no columns left)")
t.Column(1).SetProperty(properties.Omit, true)
if err := t.RenderTo(os.Stdout); err == nil || err != tabular.ErrNoColumnsToDisplay {
if err == nil {
fmt.Fprintf(os.Stderr, "table did not fail to render with no columns to display\n")
} else {
fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err)
}
}
// Delete all the properties, then set a default column property and override for the columns we _should_ display
fmt.Println("no columns by default, enable two columns by name:")
for i := range 4 {
t.Column(i).SetProperty(properties.Omit, nil)
}
t.Column(0).SetProperty(properties.Omit, true)
for _, cname := range []string{"Person", "Color"} {
c, err := t.ColumnNamed(cname)
if err != nil {
fmt.Fprintf(os.Stderr, "table lookup of column %q failed: %v\n", cname, err)
continue
}
c.SetProperty(properties.Omit, false)
}
show(t)
fmt.Println("also drop 3rd row:")
var r1, r2 *tabular.Row
if c, err := t.CellAt(tabular.CellLocation{Row: 3, Column: 1}); err == nil {
r1 = c.Row()
} else {
fmt.Fprintf(os.Stderr, "table lookup of Row 3 Column 1 failed: %v\n", err)
}
r2 = t.AllRows()[2]
if r1 != r2 {
fmt.Fprintf(os.Stderr, "row 3 via CellLocation != row 3 via AllRows\n\tCellLocation: %v\n\tAllRows: %v\n", r1, r2)
}
r1.SetProperty(properties.Omit, true)
show(t)
// Output:
// full:
// ╭─────────┬─────┬───────┬────────╮
// │ Person │ Age │ Score │ Color │
// ├─────────┼─────┼───────┼────────┤
// │ Fred │ 34 │ 0.6 │ red │
// │ Gladys │ 32 │ 0.8 │ yellow │
// │ Bert │ 57 │ 0.7 │ green │
// │ Belinda │ 58 │ 0.8 │ blue │
// ╰─────────┴─────┴───────┴────────╯
// no column 3:
// ╭─────────┬─────┬────────╮
// │ Person │ Age │ Color │
// ├─────────┼─────┼────────┤
// │ Fred │ 34 │ red │
// │ Gladys │ 32 │ yellow │
// │ Bert │ 57 │ green │
// │ Belinda │ 58 │ blue │
// ╰─────────┴─────┴────────╯
// with column 3:
// ╭─────────┬─────┬───────┬────────╮
// │ Person │ Age │ Score │ Color │
// ├─────────┼─────┼───────┼────────┤
// │ Fred │ 34 │ 0.6 │ red │
// │ Gladys │ 32 │ 0.8 │ yellow │
// │ Bert │ 57 │ 0.7 │ green │
// │ Belinda │ 58 │ 0.8 │ blue │
// ╰─────────┴─────┴───────┴────────╯
// column 3 property removed:
// ╭─────────┬─────┬───────┬────────╮
// │ Person │ Age │ Score │ Color │
// ├─────────┼─────┼───────┼────────┤
// │ Fred │ 34 │ 0.6 │ red │
// │ Gladys │ 32 │ 0.8 │ yellow │
// │ Bert │ 57 │ 0.7 │ green │
// │ Belinda │ 58 │ 0.8 │ blue │
// ╰─────────┴─────┴───────┴────────╯
// drop last 2 columns:
// ╭─────────┬─────╮
// │ Person │ Age │
// ├─────────┼─────┤
// │ Fred │ 34 │
// │ Gladys │ 32 │
// │ Bert │ 57 │
// │ Belinda │ 58 │
// ╰─────────┴─────╯
// also drop second column:
// ╭─────────╮
// │ Person │
// ├─────────┤
// │ Fred │
// │ Gladys │
// │ Bert │
// │ Belinda │
// ╰─────────╯
// expect no table here (no columns left)
// no columns by default, enable two columns by name:
// ╭─────────┬────────╮
// │ Person │ Color │
// ├─────────┼────────┤
// │ Fred │ red │
// │ Gladys │ yellow │
// │ Bert │ green │
// │ Belinda │ blue │
// ╰─────────┴────────╯
// also drop 3rd row:
// ╭─────────┬────────╮
// │ Person │ Color │
// ├─────────┼────────┤
// │ Fred │ red │
// │ Gladys │ yellow │
// │ Belinda │ blue │
// ╰─────────┴────────╯
}
func Example_omit_json() {
t := tabular.New()
populateFourTable(t)
show := func(tb tabular.Table) {
if err := tab_json.Wrap(tb).RenderTo(os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err)
}
}
show(t)
t.Column(3).SetProperty(properties.Omit, true)
fmt.Println("---")
show(t)
fmt.Println("---")
t.AllRows()[2].SetProperty(properties.Omit, true)
show(t)
// Output:
// [
// {"Person": "Fred", "Age": 34, "Score": 0.6, "Color": "red"},
// {"Person": "Gladys", "Age": 32, "Score": 0.8, "Color": "yellow"},
// {"Person": "Bert", "Age": 57, "Score": 0.7, "Color": "green"},
// {"Person": "Belinda", "Age": 58, "Score": 0.8, "Color": "blue"}
// ]
// ---
// [
// {"Person": "Fred", "Age": 34, "Color": "red"},
// {"Person": "Gladys", "Age": 32, "Color": "yellow"},
// {"Person": "Bert", "Age": 57, "Color": "green"},
// {"Person": "Belinda", "Age": 58, "Color": "blue"}
// ]
// ---
// [
// {"Person": "Fred", "Age": 34, "Color": "red"},
// {"Person": "Gladys", "Age": 32, "Color": "yellow"},
// {"Person": "Belinda", "Age": 58, "Color": "blue"}
// ]
}
func Example_omit_csv() {
t := tabular.New()
populateFourTable(t)
show := func(tb tabular.Table) {
if err := tab_csv.Wrap(tb).RenderTo(os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err)
}
}
show(t)
t.Column(3).SetProperty(properties.Omit, true)
fmt.Println("---")
show(t)
fmt.Println("---")
t.AllRows()[2].SetProperty(properties.Omit, true)
show(t)
// Output:
// "Person","Age","Score","Color"
// "Fred","34","0.6","red"
// "Gladys","32","0.8","yellow"
// "Bert","57","0.7","green"
// "Belinda","58","0.8","blue"
// ---
// "Person","Age","Color"
// "Fred","34","red"
// "Gladys","32","yellow"
// "Bert","57","green"
// "Belinda","58","blue"
// ---
// "Person","Age","Color"
// "Fred","34","red"
// "Gladys","32","yellow"
// "Belinda","58","blue"
}
func Example_omit_markdown() {
t := tabular.New()
populateFourTable(t)
show := func(tb tabular.Table) {
if err := tab_md.Wrap(tb).RenderTo(os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err)
}
}
show(t)
t.Column(3).SetProperty(properties.Omit, true)
fmt.Println("---")
show(t)
fmt.Println("---")
t.AllRows()[2].SetProperty(properties.Omit, true)
show(t)
// Output:
// | Person | Age | Score | Color |
// | ------- | ---:| ----- | ------ |
// | Fred | 34 | 0.6 | red |
// | Gladys | 32 | 0.8 | yellow |
// | Bert | 57 | 0.7 | green |
// | Belinda | 58 | 0.8 | blue |
// ---
// | Person | Age | Color |
// | ------- | ---:| ------ |
// | Fred | 34 | red |
// | Gladys | 32 | yellow |
// | Bert | 57 | green |
// | Belinda | 58 | blue |
// ---
// | Person | Age | Color |
// | ------- | ---:| ------ |
// | Fred | 34 | red |
// | Gladys | 32 | yellow |
// | Belinda | 58 | blue |
}
func Example_omit_html() {
t := tabular.New()
populateFourTable(t)
show := func(tb tabular.Table) {
if err := tab_html.Wrap(tb).RenderTo(os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err)
}
}
show(t)
t.Column(3).SetProperty(properties.Omit, true)
fmt.Println("---")
show(t)
fmt.Println("---")
t.AllRows()[2].SetProperty(properties.Omit, true)
show(t)
// Output:
// <table>
// <colgroup><col class="col-Person" /><col class="col-Age" /><col class="col-Score" /><col class="col-Color" /></colgroup>
// <thead>
// <tr><th>Person</th><th>Age</th><th>Score</th><th>Color</th></tr>
// </thead>
// <tbody>
// <tr><td>Fred</td><td>34</td><td>0.6</td><td>red</td></tr>
// <tr><td>Gladys</td><td>32</td><td>0.8</td><td>yellow</td></tr>
// <tr><td>Bert</td><td>57</td><td>0.7</td><td>green</td></tr>
// <tr><td>Belinda</td><td>58</td><td>0.8</td><td>blue</td></tr>
// </tbody>
// </table>
// ---
// <table>
// <colgroup><col class="col-Person" /><col class="col-Age" /><col class="col-Color" /></colgroup>
// <thead>
// <tr><th>Person</th><th>Age</th><th>Color</th></tr>
// </thead>
// <tbody>
// <tr><td>Fred</td><td>34</td><td>red</td></tr>
// <tr><td>Gladys</td><td>32</td><td>yellow</td></tr>
// <tr><td>Bert</td><td>57</td><td>green</td></tr>
// <tr><td>Belinda</td><td>58</td><td>blue</td></tr>
// </tbody>
// </table>
// ---
// <table>
// <colgroup><col class="col-Person" /><col class="col-Age" /><col class="col-Color" /></colgroup>
// <thead>
// <tr><th>Person</th><th>Age</th><th>Color</th></tr>
// </thead>
// <tbody>
// <tr><td>Fred</td><td>34</td><td>red</td></tr>
// <tr><td>Gladys</td><td>32</td><td>yellow</td></tr>
// <tr><td>Belinda</td><td>58</td><td>blue</td></tr>
// </tbody>
// </table>
}