| 1 |
package tblfmt_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"log" |
| 6 |
"os" |
| 7 |
|
| 8 |
"github.com/xo/tblfmt" |
| 9 |
) |
| 10 |
|
| 11 |
func Example() { |
| 12 |
res := getDatabaseResults() |
| 13 |
if err := tblfmt.EncodeAll(os.Stdout, res, map[string]string{ |
| 14 |
"format": "aligned", |
| 15 |
"border": "2", |
| 16 |
}); err != nil { |
| 17 |
log.Fatal(err) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
func ExampleEncodeAll() { |
| 22 |
res := getDatabaseResults() |
| 23 |
if err := tblfmt.EncodeAll(os.Stdout, res, map[string]string{ |
| 24 |
"format": "csv", |
| 25 |
"fieldsep": "|", |
| 26 |
"null": "<nil>", |
| 27 |
}); err != nil { |
| 28 |
log.Fatal(err) |
| 29 |
} |
| 30 |
// Output: |
| 31 |
// author_id,name,z |
| 32 |
// 14,"a b c d",<nil> |
| 33 |
// 15,"aoeu |
| 34 |
// test |
| 35 |
// ",<nil> |
| 36 |
// 2,"袈 袈 袈",<nil> |
| 37 |
} |
| 38 |
|
| 39 |
func ExampleNewTableEncoder() { |
| 40 |
res := getDatabaseResults() |
| 41 |
enc, err := tblfmt.NewTableEncoder( |
| 42 |
res, |
| 43 |
tblfmt.WithBorder(2), |
| 44 |
tblfmt.WithLineStyle(tblfmt.UnicodeDoubleLineStyle()), |
| 45 |
tblfmt.WithWidths(20, 20), |
| 46 |
tblfmt.WithSummary(tblfmt.DefaultTableSummary()), |
| 47 |
) |
| 48 |
if err != nil { |
| 49 |
log.Fatal(err) |
| 50 |
} |
| 51 |
if err := enc.EncodeAll(os.Stdout); err != nil { |
| 52 |
log.Fatal(err) |
| 53 |
} |
| 54 |
// Output: |
| 55 |
// ╔══════════════════════╦═══════════════════════════╦═══╗ |
| 56 |
// ║ author_id ║ name ║ z ║ |
| 57 |
// ╠══════════════════════╬═══════════════════════════╬═══╣ |
| 58 |
// ║ 14 ║ a b c d ║ ║ |
| 59 |
// ║ 15 ║ aoeu ↵║ ║ |
| 60 |
// ║ ║ test ↵║ ║ |
| 61 |
// ║ ║ ║ ║ |
| 62 |
// ║ 2 ║ 袈 袈 袈 ║ ║ |
| 63 |
// ╚══════════════════════╩═══════════════════════════╩═══╝ |
| 64 |
// (3 rows) |
| 65 |
} |
| 66 |
|
| 67 |
func ExampleEncodeTemplateAll() { |
| 68 |
res := getDatabaseResults() |
| 69 |
if err := tblfmt.EncodeTemplateAll(os.Stdout, res, tblfmt.WithTemplate("html")); err != nil { |
| 70 |
log.Fatal(err) |
| 71 |
} |
| 72 |
// Output: |
| 73 |
// <table> |
| 74 |
// <caption></caption> |
| 75 |
// <thead> |
| 76 |
// <tr> |
| 77 |
// <th align="left">author_id</th> |
| 78 |
// <th align="left">name</th> |
| 79 |
// <th align="left">z</th> |
| 80 |
// </tr> |
| 81 |
// </thead> |
| 82 |
// <tbody> |
| 83 |
// <tr> |
| 84 |
// <td align="right">14</td> |
| 85 |
// <td align="left">a b c d</td> |
| 86 |
// <td align="left"></td> |
| 87 |
// </tr> |
| 88 |
// <tr> |
| 89 |
// <td align="right">15</td> |
| 90 |
// <td align="left">aoeu |
| 91 |
// test |
| 92 |
// </td> |
| 93 |
// <td align="left"></td> |
| 94 |
// </tr> |
| 95 |
// <tr> |
| 96 |
// <td align="right">2</td> |
| 97 |
// <td align="left">袈 袈 袈</td> |
| 98 |
// <td align="left"></td> |
| 99 |
// </tr> |
| 100 |
// </tbody> |
| 101 |
// </table> |
| 102 |
} |
| 103 |
|
| 104 |
//func Example_fromMap() { |
| 105 |
// res := getDatabaseResults() |
| 106 |
// builder, opts := tblfmt.FromMap(map[string]string{ |
| 107 |
// "format": "table", |
| 108 |
// }) |
| 109 |
// enc, err := builder(res, opts...) |
| 110 |
// if err != nil { |
| 111 |
// log.Fatal(err) |
| 112 |
// } |
| 113 |
// var buf bytes.Buffer |
| 114 |
// if err := enc.EncodeAll(&buf); err != nil { |
| 115 |
// log.Fatal(err) |
| 116 |
// } |
| 117 |
// // extra padding to deal with Go's example output |
| 118 |
// b := append([]byte{'#'}, append(bytes.TrimSpace(buf.Bytes()), '#')...) |
| 119 |
// b = bytes.ReplaceAll(b, []byte{'\n'}, []byte{'#', '\n', '#'}) |
| 120 |
// if _, err := os.Stdout.Write(b); err != nil { |
| 121 |
// log.Fatal(err) |
| 122 |
// } |
| 123 |
// // Output: |
| 124 |
// // AUTHOR_ID NAME Z |
| 125 |
// // 14 a b c d |
| 126 |
// // 15 aoeu |
| 127 |
// // test |
| 128 |
// // |
| 129 |
// // 2 袈 袈 袈 |
| 130 |
// // (3 rows) |
| 131 |
//} |
| 132 |
|
| 133 |
// getDatabaseResults returns a tblfmt.ResultSet, which is an interface that is |
| 134 |
// compatible with Go's standard. |
| 135 |
func getDatabaseResults() tblfmt.ResultSet { |
| 136 |
return &result{ |
| 137 |
cols: []string{"author_id", "name", "z"}, |
| 138 |
vals: [][]any{ |
| 139 |
{14, "a\tb\tc\td", nil}, |
| 140 |
{15, "aoeu\ntest\n", nil}, |
| 141 |
{2, "袈\t袈\t\t袈", nil}, |
| 142 |
}, |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// result is a simple type providing a tblfmt.ResultSet. |
| 147 |
type result struct { |
| 148 |
pos int |
| 149 |
cols []string |
| 150 |
vals [][]any |
| 151 |
} |
| 152 |
|
| 153 |
// Columns satisfies the tblfmt.ResultSet interface. |
| 154 |
func (res *result) Columns() ([]string, error) { |
| 155 |
return res.cols, nil |
| 156 |
} |
| 157 |
|
| 158 |
// Next satisfies the tblfmt.ResultSet interface. |
| 159 |
func (res *result) Next() bool { |
| 160 |
return res.pos < len(res.vals) |
| 161 |
} |
| 162 |
|
| 163 |
// Scan satisfies the tblfmt.ResultSet interface. |
| 164 |
func (res *result) Scan(vals ...any) error { |
| 165 |
for i := range vals { |
| 166 |
x, ok := vals[i].(*any) |
| 167 |
if !ok { |
| 168 |
return fmt.Errorf("scan for col %d expected *interface{}, got: %T", i, vals[i]) |
| 169 |
} |
| 170 |
*x = res.vals[res.pos][i] |
| 171 |
} |
| 172 |
res.pos++ |
| 173 |
return nil |
| 174 |
} |
| 175 |
|
| 176 |
// Err satisfies the tblfmt.ResultSet interface. |
| 177 |
func (res *result) Err() error { |
| 178 |
return nil |
| 179 |
} |
| 180 |
|
| 181 |
// Close satisfies the tblfmt.ResultSet interface. |
| 182 |
func (res *result) Close() error { |
| 183 |
return nil |
| 184 |
} |
| 185 |
|
| 186 |
// NextResultSet satisfies the tblfmt.ResultSet interface. |
| 187 |
func (res *result) NextResultSet() bool { |
| 188 |
return false |
| 189 |
} |