-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage_test.go
118 lines (102 loc) · 3.84 KB
/
usage_test.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
// 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"
auto_table "go.pennock.tech/tabular/auto"
)
func Example_simple() {
t := auto_table.New("utf8-heavy")
t.AddHeaders("Fruit", "Price $/lb")
t.AddRowItems("bananas", "0.56")
t.AddRowItems("apples", "1.31")
t.AddRowItems("strawberries", "2.22")
t.AddRowItems("pears", "1.90")
if errs := t.Errors(); errs != nil {
for _, err := range errs {
fmt.Fprintf(os.Stderr, "table error: %s\n", err)
}
}
// This would get you the table as a string, instead of using an io.Writer:
// tString, err := t.Render()
err := t.RenderTo(os.Stdout)
if 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 ┃
// ┗━━━━━━━━━━━━━━┷━━━━━━━━━━━━┛
}
func populateTable(t auto_table.RenderTable) auto_table.RenderTable {
t.AddHeaders("Fruit", "Price $/lb")
t.AddRowItems("bananas", "0.56")
t.AddRowItems("apples", "1.31")
t.AddRowItems("strawberries", "2.22")
t.AddRowItems("pears", "1.90")
// Instead of checking for nil, as in Example_simple, which lets you decide
// whether or not to error out etc, if you're just going to iterate then
// you can just rely upon range over nil never invoking the body.
for _, err := range t.Errors() {
fmt.Fprintf(os.Stderr, "table error: %s\n", err)
}
return t
}
func Example_ascii() {
t := auto_table.New("ascii-simple")
t = populateTable(t)
err := t.RenderTo(os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "table rendering error: %s\n", err)
}
fmt.Printf("Table has %d rows and %d columns\n", t.NRows(), t.NColumns())
// Output:
// +--------------+------------+
// | Fruit | Price $/lb |
// +--------------+------------+
// | bananas | 0.56 |
// | apples | 1.31 |
// | strawberries | 2.22 |
// | pears | 1.90 |
// +--------------+------------+
// Table has 4 rows and 2 columns
}
func Example_html() {
// We can use `"html"` as a style when creating the table too, but the
// various styles let you embed other tables, and the auto package has
// package-level functions to take any table and render in any style.
t := auto_table.New("utf8-light-curved")
t = populateTable(t)
_ = t.RenderTo(os.Stdout)
_ = auto_table.RenderTo(t, os.Stdout, "html")
// Output:
// ╭──────────────┬────────────╮
// │ Fruit │ Price $/lb │
// ├──────────────┼────────────┤
// │ bananas │ 0.56 │
// │ apples │ 1.31 │
// │ strawberries │ 2.22 │
// │ pears │ 1.90 │
// ╰──────────────┴────────────╯
// <table>
// <colgroup><col class="col-Fruit" /><col class="col-Price-lb" /></colgroup>
// <thead>
// <tr><th>Fruit</th><th>Price $/lb</th></tr>
// </thead>
// <tbody>
// <tr><td>bananas</td><td>0.56</td></tr>
// <tr><td>apples</td><td>1.31</td></tr>
// <tr><td>strawberries</td><td>2.22</td></tr>
// <tr><td>pears</td><td>1.90</td></tr>
// </tbody>
// </table>
}