Debian Codegraph @

README_LEGACY.md

raw
1 ASCII Table Writer
2 =========
3
4 [![ci](https://github.com/olekukonko/tablewriter/workflows/ci/badge.svg?branch=master)](https://github.com/olekukonko/tablewriter/actions?query=workflow%3Aci)
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
8
9 ## Important Notice: Modernization in Progress
10
11 The `tablewriter` package is being modernized on the `prototype` branch with generics, streaming support, and a modular design, targeting `v0.2.0`. Until this is released:
12
13 **For Production Use**: Use the stable version `v0.0.5`:
14 ```bash
15 go get github.com/olekukonko/[email protected]
16 ```
17
18 ####
19
20 For Development Preview: Try the in-progress version (unstable)
21
22 ```bash
23 go get github.com/olekukonko/tablewriter@master
24 ```
25
26 #### Features
27
28 - Automatic Padding
29 - Support Multiple Lines
30 - Supports Alignment
31 - Support Custom Separators
32 - Automatic Alignment of numbers & percentage
33 - Write directly to http , file etc via `io.Writer`
34 - Read directly from CSV file
35 - Optional row line via `SetRowLine`
36 - Normalise table header
37 - Make CSV Headers optional
38 - Enable or disable table border
39 - Set custom footer support
40 - Optional identical cells merging
41 - Set custom caption
42 - Optional reflowing of paragraphs in multi-line cells.
43
44 #### Example 1 - Basic
45
46 ```go
47 data := [][]string{
48 []string{"A", "The Good", "500"},
49 []string{"B", "The Very very Bad Man", "288"},
50 []string{"C", "The Ugly", "120"},
51 []string{"D", "The Gopher", "800"},
52 }
53
54 table := tablewriter.NewWriter(os.Stdout)
55 table.SetHeader([]string{"Name", "Sign", "Rating"})
56
57 for _, v := range data {
58 table.Append(v)
59 }
60 table.Render() // Send output
61 ```
62
63 ##### Output 1
64
65 ```
66 +------+-----------------------+--------+
67 | NAME | SIGN | RATING |
68 +------+-----------------------+--------+
69 | A | The Good | 500 |
70 | B | The Very very Bad Man | 288 |
71 | C | The Ugly | 120 |
72 | D | The Gopher | 800 |
73 +------+-----------------------+--------+
74 ```
75
76 #### Example 2 - Without Border / Footer / Bulk Append
77
78 ```go
79 data := [][]string{
80 []string{"1/1/2014", "Domain name", "2233", "$10.98"},
81 []string{"1/1/2014", "January Hosting", "2233", "$54.95"},
82 []string{"1/4/2014", "February Hosting", "2233", "$51.00"},
83 []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
84 }
85
86 table := tablewriter.NewWriter(os.Stdout)
87 table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
88 table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer
89 table.EnableBorder(false) // Set Border to false
90 table.AppendBulk(data) // Add Bulk Data
91 table.Render()
92 ```
93
94 ##### Output 2
95
96 ```
97
98 DATE | DESCRIPTION | CV2 | AMOUNT
99 -----------+--------------------------+-------+----------
100 1/1/2014 | Domain name | 2233 | $10.98
101 1/1/2014 | January Hosting | 2233 | $54.95
102 1/4/2014 | February Hosting | 2233 | $51.00
103 1/4/2014 | February Extra Bandwidth | 2233 | $30.00
104 -----------+--------------------------+-------+----------
105 TOTAL | $146 93
106 --------+----------
107
108 ```
109
110 #### Example 3 - CSV
111
112 ```go
113 table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test_info.csv", true)
114 table.SetAlignment(tablewriter.ALIGN_LEFT) // Set Alignment
115 table.Render()
116 ```
117
118 ##### Output 3
119
120 ```
121 +----------+--------------+------+-----+---------+----------------+
122 | FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA |
123 +----------+--------------+------+-----+---------+----------------+
124 | user_id | smallint(5) | NO | PRI | NULL | auto_increment |
125 | username | varchar(10) | NO | | NULL | |
126 | password | varchar(100) | NO | | NULL | |
127 +----------+--------------+------+-----+---------+----------------+
128 ```
129
130 #### Example 4 - Custom Separator
131
132 ```go
133 table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test.csv", true)
134 table.SetRowLine(true) // Enable row line
135
136 // Change table lines
137 table.SetCenterSeparator("*")
138 table.SetColumnSeparator("╪")
139 table.SetRowSeparator("-")
140
141 table.SetAlignment(tablewriter.ALIGN_LEFT)
142 table.Render()
143 ```
144
145 ##### Output 4
146
147 ```
148 *------------*-----------*---------*
149 ╪ FIRST NAME ╪ LAST NAME ╪ SSN ╪
150 *------------*-----------*---------*
151 ╪ John ╪ Barry ╪ 123456 ╪
152 *------------*-----------*---------*
153 ╪ Kathy ╪ Smith ╪ 687987 ╪
154 *------------*-----------*---------*
155 ╪ Bob ╪ McCornick ╪ 3979870 ╪
156 *------------*-----------*---------*
157 ```
158
159 #### Example 5 - Markdown Format
160
161 ```go
162 data := [][]string{
163 []string{"1/1/2014", "Domain name", "2233", "$10.98"},
164 []string{"1/1/2014", "January Hosting", "2233", "$54.95"},
165 []string{"1/4/2014", "February Hosting", "2233", "$51.00"},
166 []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
167 }
168
169 table := tablewriter.NewWriter(os.Stdout)
170 table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
171 table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
172 table.SetCenterSeparator("|")
173 table.AppendBulk(data) // Add Bulk Data
174 table.Render()
175 ```
176
177 ##### Output 5
178
179 ```
180 | DATE | DESCRIPTION | CV2 | AMOUNT |
181 |----------|--------------------------|------|--------|
182 | 1/1/2014 | Domain name | 2233 | $10.98 |
183 | 1/1/2014 | January Hosting | 2233 | $54.95 |
184 | 1/4/2014 | February Hosting | 2233 | $51.00 |
185 | 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 |
186 ```
187
188 #### Example 6 - Identical cells merging
189
190 ```go
191 data := [][]string{
192 []string{"1/1/2014", "Domain name", "1234", "$10.98"},
193 []string{"1/1/2014", "January Hosting", "2345", "$54.95"},
194 []string{"1/4/2014", "February Hosting", "3456", "$51.00"},
195 []string{"1/4/2014", "February Extra Bandwidth", "4567", "$30.00"},
196 }
197
198 table := tablewriter.NewWriter(os.Stdout)
199 table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
200 table.SetFooter([]string{"", "", "Total", "$146.93"})
201 table.SetAutoMergeCells(true)
202 table.SetRowLine(true)
203 table.AppendBulk(data)
204 table.Render()
205 ```
206
207 ##### Output 6
208
209 ```
210 +----------+--------------------------+-------+---------+
211 | DATE | DESCRIPTION | CV2 | AMOUNT |
212 +----------+--------------------------+-------+---------+
213 | 1/1/2014 | Domain name | 1234 | $10.98 |
214 + +--------------------------+-------+---------+
215 | | January Hosting | 2345 | $54.95 |
216 +----------+--------------------------+-------+---------+
217 | 1/4/2014 | February Hosting | 3456 | $51.00 |
218 + +--------------------------+-------+---------+
219 | | February Extra Bandwidth | 4567 | $30.00 |
220 +----------+--------------------------+-------+---------+
221 | TOTAL | $146 93 |
222 +----------+--------------------------+-------+---------+
223 ```
224
225 #### Example 7 - Identical cells merging (specify the column index to merge)
226
227 ```go
228 data := [][]string{
229 []string{"1/1/2014", "Domain name", "1234", "$10.98"},
230 []string{"1/1/2014", "January Hosting", "1234", "$10.98"},
231 []string{"1/4/2014", "February Hosting", "3456", "$51.00"},
232 []string{"1/4/2014", "February Extra Bandwidth", "4567", "$30.00"},
233 }
234
235 table := tablewriter.NewWriter(os.Stdout)
236 table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
237 table.SetFooter([]string{"", "", "Total", "$146.93"})
238 table.SetAutoMergeCellsByColumnIndex([]int{2, 3})
239 table.SetRowLine(true)
240 table.AppendBulk(data)
241 table.Render()
242 ```
243
244 ##### Output 7
245
246 ```
247 +----------+--------------------------+-------+---------+
248 | DATE | DESCRIPTION | CV2 | AMOUNT |
249 +----------+--------------------------+-------+---------+
250 | 1/1/2014 | Domain name | 1234 | $10.98 |
251 +----------+--------------------------+ + +
252 | 1/1/2014 | January Hosting | | |
253 +----------+--------------------------+-------+---------+
254 | 1/4/2014 | February Hosting | 3456 | $51.00 |
255 +----------+--------------------------+-------+---------+
256 | 1/4/2014 | February Extra Bandwidth | 4567 | $30.00 |
257 +----------+--------------------------+-------+---------+
258 | TOTAL | $146.93 |
259 +----------+--------------------------+-------+---------+
260 ```
261
262 #### Table with color
263
264 ```go
265 data := [][]string{
266 []string{"1/1/2014", "Domain name", "2233", "$10.98"},
267 []string{"1/1/2014", "January Hosting", "2233", "$54.95"},
268 []string{"1/4/2014", "February Hosting", "2233", "$51.00"},
269 []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
270 }
271
272 table := tablewriter.NewWriter(os.Stdout)
273 table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
274 table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer
275 table.EnableBorder(false) // Set Border to false
276
277 table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.BgGreenColor},
278 tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold, tablewriter.BgBlackColor},
279 tablewriter.Colors{tablewriter.BgRedColor, tablewriter.FgWhiteColor},
280 tablewriter.Colors{tablewriter.BgCyanColor, tablewriter.FgWhiteColor})
281
282 table.SetColumnColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor},
283 tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiRedColor},
284 tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor},
285 tablewriter.Colors{tablewriter.Bold, tablewriter.FgBlackColor})
286
287 table.SetFooterColor(tablewriter.Colors{}, tablewriter.Colors{},
288 tablewriter.Colors{tablewriter.Bold},
289 tablewriter.Colors{tablewriter.FgHiRedColor})
290
291 table.AppendBulk(data)
292 table.Render()
293 ```
294
295 #### Table with color Output
296
297 ![Table with Color](https://cloud.githubusercontent.com/assets/6460392/21101956/bbc7b356-c0a1-11e6-9f36-dba694746efc.png)
298
299 #### Example - 8 Table Cells with Color
300
301 Individual Cell Colors from `func Rich` take precedence over Column Colors
302
303 ```go
304 data := [][]string{
305 []string{"Test1Merge", "HelloCol2 - 1", "HelloCol3 - 1", "HelloCol4 - 1"},
306 []string{"Test1Merge", "HelloCol2 - 2", "HelloCol3 - 2", "HelloCol4 - 2"},
307 []string{"Test1Merge", "HelloCol2 - 3", "HelloCol3 - 3", "HelloCol4 - 3"},
308 []string{"Test2Merge", "HelloCol2 - 4", "HelloCol3 - 4", "HelloCol4 - 4"},
309 []string{"Test2Merge", "HelloCol2 - 5", "HelloCol3 - 5", "HelloCol4 - 5"},
310 []string{"Test2Merge", "HelloCol2 - 6", "HelloCol3 - 6", "HelloCol4 - 6"},
311 []string{"Test2Merge", "HelloCol2 - 7", "HelloCol3 - 7", "HelloCol4 - 7"},
312 []string{"Test3Merge", "HelloCol2 - 8", "HelloCol3 - 8", "HelloCol4 - 8"},
313 []string{"Test3Merge", "HelloCol2 - 9", "HelloCol3 - 9", "HelloCol4 - 9"},
314 []string{"Test3Merge", "HelloCol2 - 10", "HelloCol3 -10", "HelloCol4 - 10"},
315 }
316
317 table := tablewriter.NewWriter(os.Stdout)
318 table.SetHeader([]string{"Col1", "Col2", "Col3", "Col4"})
319 table.SetFooter([]string{"", "", "Footer3", "Footer4"})
320 table.EnableBorder(false)
321
322 table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.BgGreenColor},
323 tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold, tablewriter.BgBlackColor},
324 tablewriter.Colors{tablewriter.BgRedColor, tablewriter.FgWhiteColor},
325 tablewriter.Colors{tablewriter.BgCyanColor, tablewriter.FgWhiteColor})
326
327 table.SetColumnColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor},
328 tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiRedColor},
329 tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor},
330 tablewriter.Colors{tablewriter.Bold, tablewriter.FgBlackColor})
331
332 table.SetFooterColor(tablewriter.Colors{}, tablewriter.Colors{},
333 tablewriter.Colors{tablewriter.Bold},
334 tablewriter.Colors{tablewriter.FgHiRedColor})
335
336 colorData1 := []string{"TestCOLOR1Merge", "HelloCol2 - COLOR1", "HelloCol3 - COLOR1", "HelloCol4 - COLOR1"}
337 colorData2 := []string{"TestCOLOR2Merge", "HelloCol2 - COLOR2", "HelloCol3 - COLOR2", "HelloCol4 - COLOR2"}
338
339 for i, row := range data {
340 if i == 4 {
341 table.Rich(colorData1, []tablewriter.Colors{tablewriter.Colors{}, tablewriter.Colors{tablewriter.Normal, tablewriter.FgCyanColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgWhiteColor}, tablewriter.Colors{}})
342 table.Rich(colorData2, []tablewriter.Colors{tablewriter.Colors{tablewriter.Normal, tablewriter.FgMagentaColor}, tablewriter.Colors{}, tablewriter.Colors{tablewriter.Bold, tablewriter.BgRedColor}, tablewriter.Colors{tablewriter.FgHiGreenColor, tablewriter.Italic, tablewriter.BgHiCyanColor}})
343 }
344 table.Append(row)
345 }
346
347 table.SetAutoMergeCells(true)
348 table.Render()
349
350 ```
351
352 ##### Table cells with color Output
353
354 ![Table cells with Color](https://user-images.githubusercontent.com/9064687/63969376-bcd88d80-ca6f-11e9-9466-c3d954700b25.png)
355
356 #### Example 9 - Set table caption
357
358 ```go
359 data := [][]string{
360 []string{"A", "The Good", "500"},
361 []string{"B", "The Very very Bad Man", "288"},
362 []string{"C", "The Ugly", "120"},
363 []string{"D", "The Gopher", "800"},
364 }
365
366 table := tablewriter.NewWriter(os.Stdout)
367 table.SetHeader([]string{"Name", "Sign", "Rating"})
368 table.SetCaption(true, "Movie ratings.")
369
370 for _, v := range data {
371 table.Append(v)
372 }
373 table.Render() // Send output
374 ```
375
376 Note: Caption text will wrap with total width of rendered table.
377
378 ##### Output 9
379
380 ```
381 +------+-----------------------+--------+
382 | NAME | SIGN | RATING |
383 +------+-----------------------+--------+
384 | A | The Good | 500 |
385 | B | The Very very Bad Man | 288 |
386 | C | The Ugly | 120 |
387 | D | The Gopher | 800 |
388 +------+-----------------------+--------+
389 Movie ratings.
390 ```
391
392 #### Example 10 - Set NoWhiteSpace and TablePadding option
393
394 ```go
395 data := [][]string{
396 {"node1.example.com", "Ready", "compute", "1.11"},
397 {"node2.example.com", "Ready", "compute", "1.11"},
398 {"node3.example.com", "Ready", "compute", "1.11"},
399 {"node4.example.com", "NotReady", "compute", "1.11"},
400 }
401
402 table := tablewriter.NewWriter(os.Stdout)
403 table.SetHeader([]string{"Name", "Status", "Role", "Version"})
404 table.SetAutoWrapText(false)
405 table.SetAutoFormatHeaders(true)
406 table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
407 table.SetAlignment(tablewriter.ALIGN_LEFT)
408 table.SetCenterSeparator("")
409 table.SetColumnSeparator("")
410 table.SetRowSeparator("")
411 table.SetHeaderLine(false)
412 table.EnableBorder(false)
413 table.SetTablePadding("\t") // pad with tabs
414 table.SetNoWhiteSpace(true)
415 table.AppendBulk(data) // Add Bulk Data
416 table.Render()
417 ```
418
419 ##### Output 10
420
421 ```
422 NAME STATUS ROLE VERSION
423 node1.example.com Ready compute 1.11
424 node2.example.com Ready compute 1.11
425 node3.example.com Ready compute 1.11
426 node4.example.com NotReady compute 1.11
427 ```
428
429 #### Render table into a string
430
431 Instead of rendering the table to `io.Stdout` you can also render it into a string. Go 1.10 introduced the
432 `strings.Builder` type which implements the `io.Writer` interface and can therefore be used for this task. Example:
433
434 ```go
435 package main
436
437 import (
438 "strings"
439 "fmt"
440
441 "github.com/olekukonko/tablewriter"
442 )
443
444 func main() {
445 tableString := &strings.Builder{}
446 table := tablewriter.NewWriter(tableString)
447
448 /*
449 * Code to fill the table
450 */
451
452 table.Render()
453
454 fmt.Println(tableString.String())
455 }
456 ```
457
458 #### TODO
459
460 - ~~Import Directly from CSV~~ - `done`
461 - ~~Support for `SetFooter`~~ - `done`
462 - ~~Support for `SetBorder`~~ - `done`
463 - ~~Support table with uneven rows~~ - `done`
464 - ~~Support custom alignment~~
465 - General Improvement & Optimisation
466 - `NewHTML` Parse table from HTML