| 1 |
# About tblfmt |
| 2 |
|
| 3 |
Package `tblfmt` provides streaming table encoders for result sets (ie, from a |
| 4 |
database), creating tables like the following: |
| 5 |
|
| 6 |
```text |
| 7 |
author_id | name | z |
| 8 |
-----------+-----------------------+--- |
| 9 |
14 | a b c d | |
| 10 |
15 | aoeu +| |
| 11 |
| test +| |
| 12 |
| | |
| 13 |
16 | foo\bbar | |
| 14 |
17 | a b \r +| |
| 15 |
| a | |
| 16 |
18 | 袈 袈 袈 | |
| 17 |
19 | 袈 袈 袈+| a+ |
| 18 |
| | |
| 19 |
(6 rows) |
| 20 |
``` |
| 21 |
|
| 22 |
Additionally, there are standard encoders for JSON, CSV, HTML, unaligned and |
| 23 |
other display variants [supported by `usql`][usql]. |
| 24 |
|
| 25 |
[![Unit Tests][tblfmt-ci-status]][tblfmt-ci] |
| 26 |
[![Go Reference][goref-tblfmt-status]][goref-tblfmt] |
| 27 |
[![Discord Discussion][discord-status]][discord] |
| 28 |
|
| 29 |
[tblfmt-ci]: https://github.com/xo/tblfmt/actions/workflows/test.yml |
| 30 |
[tblfmt-ci-status]: https://github.com/xo/tblfmt/actions/workflows/test.yml/badge.svg |
| 31 |
[goref-tblfmt]: https://pkg.go.dev/github.com/xo/tblfmt |
| 32 |
[goref-tblfmt-status]: https://pkg.go.dev/badge/github.com/xo/tblfmt.svg |
| 33 |
[discord]: https://discord.gg/yJKEzc7prt (Discord Discussion) |
| 34 |
[discord-status]: https://img.shields.io/discord/829150509658013727.svg?label=Discord&logo=Discord&colorB=7289da&style=flat-square (Discord Discussion) |
| 35 |
|
| 36 |
## Installing |
| 37 |
|
| 38 |
Install in the usual [Go][go-project] fashion: |
| 39 |
|
| 40 |
```sh |
| 41 |
$ go get -u github.com/xo/tblfmt |
| 42 |
``` |
| 43 |
|
| 44 |
## Using |
| 45 |
|
| 46 |
`tblfmt` was designed for use by [`usql`][usql] and Go's native `database/sql` |
| 47 |
types, but will handle any type with the following interface: |
| 48 |
|
| 49 |
```go |
| 50 |
// ResultSet is the shared interface for a result set. |
| 51 |
type ResultSet interface { |
| 52 |
Next() bool |
| 53 |
Scan(...interface{}) error |
| 54 |
Columns() ([]string, error) |
| 55 |
Close() error |
| 56 |
Err() error |
| 57 |
NextResultSet() bool |
| 58 |
} |
| 59 |
``` |
| 60 |
|
| 61 |
`tblfmt` can be used similar to the following: |
| 62 |
|
| 63 |
```go |
| 64 |
// _example/example.go |
| 65 |
package main |
| 66 |
|
| 67 |
import ( |
| 68 |
"log" |
| 69 |
"os" |
| 70 |
|
| 71 |
_ "github.com/lib/pq" |
| 72 |
"github.com/xo/dburl" |
| 73 |
"github.com/xo/tblfmt" |
| 74 |
) |
| 75 |
|
| 76 |
func main() { |
| 77 |
db, err := dburl.Open("postgres://booktest:booktest@localhost") |
| 78 |
if err != nil { |
| 79 |
log.Fatal(err) |
| 80 |
} |
| 81 |
defer db.Close() |
| 82 |
res, err := db.Query("select * from authors") |
| 83 |
if err != nil { |
| 84 |
log.Fatal(err) |
| 85 |
} |
| 86 |
defer res.Close() |
| 87 |
enc, err := tblfmt.NewTableEncoder( |
| 88 |
res, |
| 89 |
// force minimum column widths |
| 90 |
tblfmt.WithWidths(20, 20), |
| 91 |
) |
| 92 |
if err = enc.EncodeAll(os.Stdout); err != nil { |
| 93 |
log.Fatal(err) |
| 94 |
} |
| 95 |
} |
| 96 |
``` |
| 97 |
|
| 98 |
Which can produce output like the following: |
| 99 |
|
| 100 |
```text |
| 101 |
╔══════════════════════╦═══════════════════════════╦═══╗ |
| 102 |
║ author_id ║ name ║ z ║ |
| 103 |
╠══════════════════════╬═══════════════════════════╬═══╣ |
| 104 |
║ 14 ║ a b c d ║ ║ |
| 105 |
║ 15 ║ aoeu ↵║ ║ |
| 106 |
║ ║ test ↵║ ║ |
| 107 |
║ ║ ║ ║ |
| 108 |
║ 2 ║ 袈 袈 袈 ║ ║ |
| 109 |
╚══════════════════════╩═══════════════════════════╩═══╝ |
| 110 |
(3 rows) |
| 111 |
``` |
| 112 |
|
| 113 |
Please see the [Go Reference][goref-tblfmt] for the full API. |
| 114 |
|
| 115 |
## Testing |
| 116 |
|
| 117 |
Run using standard `go test`: |
| 118 |
|
| 119 |
```sh |
| 120 |
$ go test -v |
| 121 |
``` |
| 122 |
|
| 123 |
[go-project]: https://golang.org/project |
| 124 |
[usql]: https://github.com/xo/usql |