commit | e6d60cf7ba1f42d86d54cdf5508611c4aafb3970 | [log] [tgz] |
---|---|---|
author | mattn <[email protected]> | Fri Oct 26 07:14:10 2018 |
committer | GitHub <[email protected]> | Fri Oct 26 07:14:10 2018 |
tree | 338ff167dd6984fe7d62ddd84393a329b547b480 | |
parent | f1b6297c9ba8513b43a14a78f045b5d41ce3e2b2 [diff] | |
parent | 9809e132383b770b023db78e6ac09fe80c1a66ca [diff] |
Merge pull request #114 from WakeupTsai/master Let numbers with commas can also be aligned to right
Generate ASCII table on the fly ... Installation is simple as
go get github.com/olekukonko/tablewriter
io.Writer
SetRowLine
data := [][]string{ []string{"A", "The Good", "500"}, []string{"B", "The Very very Bad Man", "288"}, []string{"C", "The Ugly", "120"}, []string{"D", "The Gopher", "800"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Name", "Sign", "Rating"}) for _, v := range data { table.Append(v) } table.Render() // Send output
+------+-----------------------+--------+ | NAME | SIGN | RATING | +------+-----------------------+--------+ | A | The Good | 500 | | B | The Very very Bad Man | 288 | | C | The Ugly | 120 | | D | The Gopher | 800 | +------+-----------------------+--------+
data := [][]string{ []string{"1/1/2014", "Domain name", "2233", "$10.98"}, []string{"1/1/2014", "January Hosting", "2233", "$54.95"}, []string{"1/4/2014", "February Hosting", "2233", "$51.00"}, []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Date", "Description", "CV2", "Amount"}) table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer table.SetBorder(false) // Set Border to false table.AppendBulk(data) // Add Bulk Data table.Render()
DATE | DESCRIPTION | CV2 | AMOUNT +----------+--------------------------+-------+---------+ 1/1/2014 | Domain name | 2233 | $10.98 1/1/2014 | January Hosting | 2233 | $54.95 1/4/2014 | February Hosting | 2233 | $51.00 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 +----------+--------------------------+-------+---------+ TOTAL | $146 93 +-------+---------+
table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test_info.csv", true) table.SetAlignment(tablewriter.ALIGN_LEFT) // Set Alignment table.Render()
+----------+--------------+------+-----+---------+----------------+ | FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA | +----------+--------------+------+-----+---------+----------------+ | user_id | smallint(5) | NO | PRI | NULL | auto_increment | | username | varchar(10) | NO | | NULL | | | password | varchar(100) | NO | | NULL | | +----------+--------------+------+-----+---------+----------------+
table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test.csv", true) table.SetRowLine(true) // Enable row line // Change table lines table.SetCenterSeparator("*") table.SetColumnSeparator("╪") table.SetRowSeparator("-") table.SetAlignment(tablewriter.ALIGN_LEFT) table.Render()
*------------*-----------*---------* ╪ FIRST NAME ╪ LAST NAME ╪ SSN ╪ *------------*-----------*---------* ╪ John ╪ Barry ╪ 123456 ╪ *------------*-----------*---------* ╪ Kathy ╪ Smith ╪ 687987 ╪ *------------*-----------*---------* ╪ Bob ╪ McCornick ╪ 3979870 ╪ *------------*-----------*---------*
data := [][]string{ []string{"1/1/2014", "Domain name", "2233", "$10.98"}, []string{"1/1/2014", "January Hosting", "2233", "$54.95"}, []string{"1/4/2014", "February Hosting", "2233", "$51.00"}, []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Date", "Description", "CV2", "Amount"}) table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) table.SetCenterSeparator("|") table.AppendBulk(data) // Add Bulk Data table.Render()
| DATE | DESCRIPTION | CV2 | AMOUNT | |----------|--------------------------|------|--------| | 1/1/2014 | Domain name | 2233 | $10.98 | | 1/1/2014 | January Hosting | 2233 | $54.95 | | 1/4/2014 | February Hosting | 2233 | $51.00 | | 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 |
data := [][]string{
[]string{"1/1/2014", "Domain name", "1234", "$10.98"},
[]string{"1/1/2014", "January Hosting", "2345", "$54.95"},
[]string{"1/4/2014", "February Hosting", "3456", "$51.00"},
[]string{"1/4/2014", "February Extra Bandwidth", "4567", "$30.00"},
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
table.SetFooter([]string{"", "", "Total", "$146.93"})
table.SetAutoMergeCells(true)
table.SetRowLine(true)
table.AppendBulk(data)
table.Render()
+----------+--------------------------+-------+---------+ | DATE | DESCRIPTION | CV2 | AMOUNT | +----------+--------------------------+-------+---------+ | 1/1/2014 | Domain name | 1234 | $10.98 | + +--------------------------+-------+---------+ | | January Hosting | 2345 | $54.95 | +----------+--------------------------+-------+---------+ | 1/4/2014 | February Hosting | 3456 | $51.00 | + +--------------------------+-------+---------+ | | February Extra Bandwidth | 4567 | $30.00 | +----------+--------------------------+-------+---------+ | TOTAL | $146 93 | +----------+--------------------------+-------+---------+
data := [][]string{ []string{"1/1/2014", "Domain name", "2233", "$10.98"}, []string{"1/1/2014", "January Hosting", "2233", "$54.95"}, []string{"1/4/2014", "February Hosting", "2233", "$51.00"}, []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Date", "Description", "CV2", "Amount"}) table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer table.SetBorder(false) // Set Border to false table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.BgGreenColor}, tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold, tablewriter.BgBlackColor}, tablewriter.Colors{tablewriter.BgRedColor, tablewriter.FgWhiteColor}, tablewriter.Colors{tablewriter.BgCyanColor, tablewriter.FgWhiteColor}) table.SetColumnColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiRedColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgBlackColor}) table.SetFooterColor(tablewriter.Colors{}, tablewriter.Colors{}, tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{tablewriter.FgHiRedColor}) table.AppendBulk(data) table.Render()
data := [][]string{ []string{"A", "The Good", "500"}, []string{"B", "The Very very Bad Man", "288"}, []string{"C", "The Ugly", "120"}, []string{"D", "The Gopher", "800"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Name", "Sign", "Rating"}) table.SetCaption(true, "Movie ratings.") for _, v := range data { table.Append(v) } table.Render() // Send output
Note: Caption text will wrap with total width of rendered table.
+------+-----------------------+--------+ | NAME | SIGN | RATING | +------+-----------------------+--------+ | A | The Good | 500 | | B | The Very very Bad Man | 288 | | C | The Ugly | 120 | | D | The Gopher | 800 | +------+-----------------------+--------+ Movie ratings.
done
SetFooter
done
SetBorder
done
done
NewHTML
Parse table from HTML