Debian Codegraph @

README.md

raw
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 ## Differences from `psql`
116
117 `tblfmt` follows `psql`'s output closely. The differences below are deliberate;
118 anything else is a bug worth reporting.
119
120 ### Trailing space on the last column
121
122 `tblfmt` pads the last column of a bordered table, so that every line of a
123 table is the same width. `psql` pads the header but trims the data rows,
124 leaving them ragged.
125
126 For `select 42 as n, 'a'::text as t union all select 7, 'bb';`, with trailing
127 spaces written as `·` and the width of each line at the right:
128
129 ```text
130 psql 18.6 tblfmt
131 n | t · 9 n | t · 9
132 ----+---- 9 ----+---- 9
133 42 | a 7 42 | a · 9
134 7 | bb 8 7 | bb · 9
135 ```
136
137 `psql` is wrong here. A table whose lines do not share a width is awkward to
138 select from a terminal, to diff, and to lay out in anything that measures the
139 block, and the ragged edge carries no information. It is also inconsistent with
140 `psql`'s own header, which is padded. `tblfmt` will not follow it.
141
142 ## Testing
143
144 Run using standard `go test`:
145
146 ```sh
147 $ go test -v
148 ```
149
150 [go-project]: https://golang.org/project
151 [usql]: https://github.com/xo/usql