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) [![Total views](https://sourcegraph.com/api/repos/github.com/olekukonko/tablewriter/counters/views.png)](https://sourcegraph.com/github.com/olekukonko/tablewriter)
5
6Generate ASCII table on the fly ... Installation is simple as
7
8 go get github.com/olekukonko/tablewriter
9
10
11#### Features
12- Automatic Padding
13- Support Multiple Lines
14- Supports Alignment
15- Support Custom Separators
16- Automatic Alignment of numbers & percentage
17- Write directly to http , file etc via `io.Writer`
18- Read directly from CSV file
19- Optional row line via `SetRowLine`
20- Normalise table header
21- Make CSV Headers optional
22- Enable or disable table border
23- Set custom footer support
24- Optional identical cells merging
25
26
27#### Example 1 - Basic
28```go
29data := [][]string{
30 []string{"A", "The Good", "500"},
31 []string{"B", "The Very very Bad Man", "288"},
32 []string{"C", "The Ugly", "120"},
33 []string{"D", "The Gopher", "800"},
34}
35
36table := tablewriter.NewWriter(os.Stdout)
37table.SetHeader([]string{"Name", "Sign", "Rating"})
38
39for _, v := range data {
40 table.Append(v)
41}
42table.Render() // Send output
43```
44
45##### Output 1
46```
47+------+-----------------------+--------+
48| NAME | SIGN | RATING |
49+------+-----------------------+--------+
50| A | The Good | 500 |
51| B | The Very very Bad Man | 288 |
52| C | The Ugly | 120 |
53| D | The Gopher | 800 |
54+------+-----------------------+--------+
55```
56
57#### Example 2 - Without Border / Footer / Bulk Append
58```go
59data := [][]string{
60 []string{"1/1/2014", "Domain name", "2233", "$10.98"},
61 []string{"1/1/2014", "January Hosting", "2233", "$54.95"},
62 []string{"1/4/2014", "February Hosting", "2233", "$51.00"},
63 []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
64}
65
66table := tablewriter.NewWriter(os.Stdout)
67table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
68table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer
69table.SetBorder(false) // Set Border to false
70table.AppendBulk(data) // Add Bulk Data
71table.Render()
72```
73
74##### Output 2
75```
76
77 DATE | DESCRIPTION | CV2 | AMOUNT
78+----------+--------------------------+-------+---------+
79 1/1/2014 | Domain name | 2233 | $10.98
80 1/1/2014 | January Hosting | 2233 | $54.95
81 1/4/2014 | February Hosting | 2233 | $51.00
82 1/4/2014 | February Extra Bandwidth | 2233 | $30.00
83+----------+--------------------------+-------+---------+
84 TOTAL | $146 93
85 +-------+---------+
86
87```
88
89
90#### Example 3 - CSV
91```go
92table, _ := tablewriter.NewCSV(os.Stdout, "test_info.csv", true)
93table.SetAlignment(tablewriter.ALIGN_LEFT) // Set Alignment
94table.Render()
95```
96
97##### Output 3
98```
99+----------+--------------+------+-----+---------+----------------+
100| FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA |
101+----------+--------------+------+-----+---------+----------------+
102| user_id | smallint(5) | NO | PRI | NULL | auto_increment |
103| username | varchar(10) | NO | | NULL | |
104| password | varchar(100) | NO | | NULL | |
105+----------+--------------+------+-----+---------+----------------+
106```
107
108#### Example 4 - Custom Separator
109```go
110table, _ := tablewriter.NewCSV(os.Stdout, "test.csv", true)
111table.SetRowLine(true) // Enable row line
112
113// Change table lines
114table.SetCenterSeparator("*")
115table.SetColumnSeparator("‡")
116table.SetRowSeparator("-")
117
118table.SetAlignment(tablewriter.ALIGN_LEFT)
119table.Render()
120```
121
122##### Output 4
123```
124*------------*-----------*---------*
125╪ FIRST NAME ╪ LAST NAME ╪ SSN ╪
126*------------*-----------*---------*
127╪ John ╪ Barry ╪ 123456 ╪
128*------------*-----------*---------*
129╪ Kathy ╪ Smith ╪ 687987 ╪
130*------------*-----------*---------*
131╪ Bob ╪ McCornick ╪ 3979870 ╪
132*------------*-----------*---------*
133```
134
135##### Example 5 - Markdown Format
136```go
137data := [][]string{
138 []string{"1/1/2014", "Domain name", "2233", "$10.98"},
139 []string{"1/1/2014", "January Hosting", "2233", "$54.95"},
140 []string{"1/4/2014", "February Hosting", "2233", "$51.00"},
141 []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
142}
143
144table := tablewriter.NewWriter(os.Stdout)
145table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
146table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
147table.SetCenterSeparator("|")
148table.AppendBulk(data) // Add Bulk Data
149table.Render()
150```
151
152##### Output 5
153```
154| DATE | DESCRIPTION | CV2 | AMOUNT |
155|----------|--------------------------|------|--------|
156| 1/1/2014 | Domain name | 2233 | $10.98 |
157| 1/1/2014 | January Hosting | 2233 | $54.95 |
158| 1/4/2014 | February Hosting | 2233 | $51.00 |
159| 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 |
160```
161
162#### Example 6 - Identical cells merging
163```go
164data := [][]string{
165 []string{"1/1/2014", "Domain name", "1234", "$10.98"},
166 []string{"1/1/2014", "January Hosting", "2345", "$54.95"},
167 []string{"1/4/2014", "February Hosting", "3456", "$51.00"},
168 []string{"1/4/2014", "February Extra Bandwidth", "4567", "$30.00"},
169}
170
171table := NewWriter(os.Stdout)
172table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
173table.SetFooter([]string{"", "", "Total", "$146.93"})
174table.SetAutoMergeCells(true)
175table.SetRowLine(true)
176table.AppendBulk(data)
177table.Render()
178```
179
180##### Output 6
181```
182+----------+--------------------------+-------+---------+
183| DATE | DESCRIPTION | CV2 | AMOUNT |
184+----------+--------------------------+-------+---------+
185| 1/1/2014 | Domain name | 1234 | $10.98 |
186+ +--------------------------+-------+---------+
187| | January Hosting | 2345 | $54.95 |
188+----------+--------------------------+-------+---------+
189| 1/4/2014 | February Hosting | 3456 | $51.00 |
190+ +--------------------------+-------+---------+
191| | February Extra Bandwidth | 4567 | $30.00 |
192+----------+--------------------------+-------+---------+
193| TOTAL | $146 93 |
194+----------+--------------------------+-------+---------+
195```
196
197#### TODO
198- ~~Import Directly from CSV~~ - `done`
199- ~~Support for `SetFooter`~~ - `done`
200- ~~Support for `SetBorder`~~ - `done`
201- ~~Support table with uneven rows~~ - `done`
202- Support custom alignment
203- General Improvement & Optimisation
204- `NewHTML` Parse table from HTML
205