• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

csv2table/H05-Nov-2019-

testdata/H05-Nov-2019-

.gitignoreH A D05-Nov-2019256

.travis.ymlH A D05-Nov-2019109

LICENSE.mdH A D05-Nov-20191 KiB

README.mdH A D05-Nov-201910.8 KiB

csv.goH A D05-Nov-20191.3 KiB

go.modH A D05-Nov-2019143

go.sumH A D05-Nov-2019362

table.goH A D05-Nov-201920.1 KiB

table_test.goH A D05-Nov-201939.5 KiB

table_with_color.goH A D05-Nov-20192.4 KiB

util.goH A D05-Nov-20192.2 KiB

wrap.goH A D05-Nov-20192.6 KiB

wrap_test.goH A D05-Nov-20191.4 KiB

README.md

1ASCII Table Writer
2=========
3
4[![Build Status](https://travis-ci.org/olekukonko/tablewriter.png?branch=master)](https://travis-ci.org/olekukonko/tablewriter)
5[![Total views](https://img.shields.io/sourcegraph/rrc/github.com/olekukonko/tablewriter.svg)](https://sourcegraph.com/github.com/olekukonko/tablewriter)
6[![Godoc](https://godoc.org/github.com/olekukonko/tablewriter?status.svg)](https://godoc.org/github.com/olekukonko/tablewriter)
7
8Generate ASCII table on the fly ...  Installation is simple as
9
10    go get github.com/olekukonko/tablewriter
11
12
13#### Features
14- Automatic Padding
15- Support Multiple Lines
16- Supports Alignment
17- Support Custom Separators
18- Automatic Alignment of numbers & percentage
19- Write directly to http , file etc via `io.Writer`
20- Read directly from CSV file
21- Optional row line via `SetRowLine`
22- Normalise table header
23- Make CSV Headers optional
24- Enable or disable table border
25- Set custom footer support
26- Optional identical cells merging
27- Set custom caption
28- Optional reflowing of paragrpahs in multi-line cells.
29
30#### Example   1 - Basic
31```go
32data := [][]string{
33    []string{"A", "The Good", "500"},
34    []string{"B", "The Very very Bad Man", "288"},
35    []string{"C", "The Ugly", "120"},
36    []string{"D", "The Gopher", "800"},
37}
38
39table := tablewriter.NewWriter(os.Stdout)
40table.SetHeader([]string{"Name", "Sign", "Rating"})
41
42for _, v := range data {
43    table.Append(v)
44}
45table.Render() // Send output
46```
47
48##### Output  1
49```
50+------+-----------------------+--------+
51| NAME |         SIGN          | RATING |
52+------+-----------------------+--------+
53|  A   |       The Good        |    500 |
54|  B   | The Very very Bad Man |    288 |
55|  C   |       The Ugly        |    120 |
56|  D   |      The Gopher       |    800 |
57+------+-----------------------+--------+
58```
59
60#### Example 2 - Without Border / Footer / Bulk Append
61```go
62data := [][]string{
63    []string{"1/1/2014", "Domain name", "2233", "$10.98"},
64    []string{"1/1/2014", "January Hosting", "2233", "$54.95"},
65    []string{"1/4/2014", "February Hosting", "2233", "$51.00"},
66    []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
67}
68
69table := tablewriter.NewWriter(os.Stdout)
70table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
71table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer
72table.SetBorder(false)                                // Set Border to false
73table.AppendBulk(data)                                // Add Bulk Data
74table.Render()
75```
76
77##### Output 2
78```
79
80    DATE   |       DESCRIPTION        |  CV2  | AMOUNT
81-----------+--------------------------+-------+----------
82  1/1/2014 | Domain name              |  2233 | $10.98
83  1/1/2014 | January Hosting          |  2233 | $54.95
84  1/4/2014 | February Hosting         |  2233 | $51.00
85  1/4/2014 | February Extra Bandwidth |  2233 | $30.00
86-----------+--------------------------+-------+----------
87                                        TOTAL | $146 93
88                                      --------+----------
89
90```
91
92
93#### Example 3 - CSV
94```go
95table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test_info.csv", true)
96table.SetAlignment(tablewriter.ALIGN_LEFT)   // Set Alignment
97table.Render()
98```
99
100##### Output 3
101```
102+----------+--------------+------+-----+---------+----------------+
103|  FIELD   |     TYPE     | NULL | KEY | DEFAULT |     EXTRA      |
104+----------+--------------+------+-----+---------+----------------+
105| user_id  | smallint(5)  | NO   | PRI | NULL    | auto_increment |
106| username | varchar(10)  | NO   |     | NULL    |                |
107| password | varchar(100) | NO   |     | NULL    |                |
108+----------+--------------+------+-----+---------+----------------+
109```
110
111#### Example 4  - Custom Separator
112```go
113table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test.csv", true)
114table.SetRowLine(true)         // Enable row line
115
116// Change table lines
117table.SetCenterSeparator("*")
118table.SetColumnSeparator("╪")
119table.SetRowSeparator("-")
120
121table.SetAlignment(tablewriter.ALIGN_LEFT)
122table.Render()
123```
124
125##### Output 4
126```
127*------------*-----------*---------*
128╪ FIRST NAME ╪ LAST NAME ╪   SSN   ╪
129*------------*-----------*---------*
130╪ John       ╪ Barry     ╪ 123456  ╪
131*------------*-----------*---------*
132╪ Kathy      ╪ Smith     ╪ 687987  ╪
133*------------*-----------*---------*
134╪ Bob        ╪ McCornick ╪ 3979870 ╪
135*------------*-----------*---------*
136```
137
138#### Example 5 - Markdown Format
139```go
140data := [][]string{
141	[]string{"1/1/2014", "Domain name", "2233", "$10.98"},
142	[]string{"1/1/2014", "January Hosting", "2233", "$54.95"},
143	[]string{"1/4/2014", "February Hosting", "2233", "$51.00"},
144	[]string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
145}
146
147table := tablewriter.NewWriter(os.Stdout)
148table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
149table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
150table.SetCenterSeparator("|")
151table.AppendBulk(data) // Add Bulk Data
152table.Render()
153```
154
155##### Output 5
156```
157|   DATE   |       DESCRIPTION        | CV2  | AMOUNT |
158|----------|--------------------------|------|--------|
159| 1/1/2014 | Domain name              | 2233 | $10.98 |
160| 1/1/2014 | January Hosting          | 2233 | $54.95 |
161| 1/4/2014 | February Hosting         | 2233 | $51.00 |
162| 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 |
163```
164
165#### Example 6  - Identical cells merging
166```go
167data := [][]string{
168  []string{"1/1/2014", "Domain name", "1234", "$10.98"},
169  []string{"1/1/2014", "January Hosting", "2345", "$54.95"},
170  []string{"1/4/2014", "February Hosting", "3456", "$51.00"},
171  []string{"1/4/2014", "February Extra Bandwidth", "4567", "$30.00"},
172}
173
174table := tablewriter.NewWriter(os.Stdout)
175table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
176table.SetFooter([]string{"", "", "Total", "$146.93"})
177table.SetAutoMergeCells(true)
178table.SetRowLine(true)
179table.AppendBulk(data)
180table.Render()
181```
182
183##### Output 6
184```
185+----------+--------------------------+-------+---------+
186|   DATE   |       DESCRIPTION        |  CV2  | AMOUNT  |
187+----------+--------------------------+-------+---------+
188| 1/1/2014 | Domain name              |  1234 | $10.98  |
189+          +--------------------------+-------+---------+
190|          | January Hosting          |  2345 | $54.95  |
191+----------+--------------------------+-------+---------+
192| 1/4/2014 | February Hosting         |  3456 | $51.00  |
193+          +--------------------------+-------+---------+
194|          | February Extra Bandwidth |  4567 | $30.00  |
195+----------+--------------------------+-------+---------+
196|                                       TOTAL | $146 93 |
197+----------+--------------------------+-------+---------+
198```
199
200
201#### Table with color
202```go
203data := [][]string{
204	[]string{"1/1/2014", "Domain name", "2233", "$10.98"},
205	[]string{"1/1/2014", "January Hosting", "2233", "$54.95"},
206	[]string{"1/4/2014", "February Hosting", "2233", "$51.00"},
207	[]string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
208}
209
210table := tablewriter.NewWriter(os.Stdout)
211table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
212table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer
213table.SetBorder(false)                                // Set Border to false
214
215table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.BgGreenColor},
216	tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold, tablewriter.BgBlackColor},
217	tablewriter.Colors{tablewriter.BgRedColor, tablewriter.FgWhiteColor},
218	tablewriter.Colors{tablewriter.BgCyanColor, tablewriter.FgWhiteColor})
219
220table.SetColumnColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor},
221	tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiRedColor},
222	tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor},
223	tablewriter.Colors{tablewriter.Bold, tablewriter.FgBlackColor})
224
225table.SetFooterColor(tablewriter.Colors{}, tablewriter.Colors{},
226	tablewriter.Colors{tablewriter.Bold},
227	tablewriter.Colors{tablewriter.FgHiRedColor})
228
229table.AppendBulk(data)
230table.Render()
231```
232
233#### Table with color Output
234![Table with Color](https://cloud.githubusercontent.com/assets/6460392/21101956/bbc7b356-c0a1-11e6-9f36-dba694746efc.png)
235
236#### Example 7 - Set table caption
237```go
238data := [][]string{
239    []string{"A", "The Good", "500"},
240    []string{"B", "The Very very Bad Man", "288"},
241    []string{"C", "The Ugly", "120"},
242    []string{"D", "The Gopher", "800"},
243}
244
245table := tablewriter.NewWriter(os.Stdout)
246table.SetHeader([]string{"Name", "Sign", "Rating"})
247table.SetCaption(true, "Movie ratings.")
248
249for _, v := range data {
250    table.Append(v)
251}
252table.Render() // Send output
253```
254
255Note: Caption text will wrap with total width of rendered table.
256
257##### Output 7
258```
259+------+-----------------------+--------+
260| NAME |         SIGN          | RATING |
261+------+-----------------------+--------+
262|  A   |       The Good        |    500 |
263|  B   | The Very very Bad Man |    288 |
264|  C   |       The Ugly        |    120 |
265|  D   |      The Gopher       |    800 |
266+------+-----------------------+--------+
267Movie ratings.
268```
269
270#### Example 8 - Set NoWhiteSpace and TablePadding option
271```go
272data := [][]string{
273    {"node1.example.com", "Ready", "compute", "1.11"},
274    {"node2.example.com", "Ready", "compute", "1.11"},
275    {"node3.example.com", "Ready", "compute", "1.11"},
276    {"node4.example.com", "NotReady", "compute", "1.11"},
277}
278
279table := tablewriter.NewWriter(os.Stdout)
280table.SetHeader([]string{"Name", "Status", "Role", "Version"})
281table.SetAutoWrapText(false)
282table.SetAutoFormatHeaders(true)
283table.SetHeaderAlignment(ALIGN_LEFT)
284table.SetAlignment(ALIGN_LEFT)
285table.SetCenterSeparator("")
286table.SetColumnSeparator("")
287table.SetRowSeparator("")
288table.SetHeaderLine(false)
289table.SetBorder(false)
290table.SetTablePadding("\t") // pad with tabs
291table.SetNoWhiteSpace(true)
292table.AppendBulk(data) // Add Bulk Data
293table.Render()
294```
295
296##### Output 8
297```
298NAME             	STATUS  	ROLE   	VERSION
299node1.example.com	Ready   	compute	1.11
300node2.example.com	Ready   	compute	1.11
301node3.example.com	Ready   	compute	1.11
302node4.example.com	NotReady	compute	1.11
303```
304
305#### Render table into a string
306
307Instead of rendering the table to `io.Stdout` you can also render it into a string. Go 1.10 introduced the `strings.Builder` type which implements the `io.Writer` interface and can therefore be used for this task. Example:
308
309```go
310package main
311
312import (
313    "strings"
314    "fmt"
315
316    "github.com/olekukonko/tablewriter"
317)
318
319func main() {
320    tableString := &strings.Builder{}
321    table := tablewriter.NewWriter(tableString)
322
323    /*
324     * Code to fill the table
325     */
326
327    table.Render()
328
329    fmt.Println(tableString.String())
330}
331```
332
333#### TODO
334- ~~Import Directly from CSV~~  - `done`
335- ~~Support for `SetFooter`~~  - `done`
336- ~~Support for `SetBorder`~~  - `done`
337- ~~Support table with uneven rows~~ - `done`
338- ~~Support custom alignment~~
339- General Improvement & Optimisation
340- `NewHTML` Parse table from HTML
341