PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/github.com/olekukonko/tablewriter/README.md

https://gitlab.com/GZGavinZhao/cf-tool
Markdown | 277 lines | 236 code | 41 blank | 0 comment | 0 complexity | 57ad4a2092aa4c31bd3b255120c855ba MD5 | raw file
  1. ASCII Table Writer
  2. =========
  3. [![Build Status](https://travis-ci.org/olekukonko/tablewriter.png?branch=master)](https://travis-ci.org/olekukonko/tablewriter)
  4. [![Total views](https://img.shields.io/sourcegraph/rrc/github.com/olekukonko/tablewriter.svg)](https://sourcegraph.com/github.com/olekukonko/tablewriter)
  5. [![Godoc](https://godoc.org/github.com/olekukonko/tablewriter?status.svg)](https://godoc.org/github.com/olekukonko/tablewriter)
  6. Generate ASCII table on the fly ... Installation is simple as
  7. go get github.com/olekukonko/tablewriter
  8. #### Features
  9. - Automatic Padding
  10. - Support Multiple Lines
  11. - Supports Alignment
  12. - Support Custom Separators
  13. - Automatic Alignment of numbers & percentage
  14. - Write directly to http , file etc via `io.Writer`
  15. - Read directly from CSV file
  16. - Optional row line via `SetRowLine`
  17. - Normalise table header
  18. - Make CSV Headers optional
  19. - Enable or disable table border
  20. - Set custom footer support
  21. - Optional identical cells merging
  22. - Set custom caption
  23. - Optional reflowing of paragrpahs in multi-line cells.
  24. #### Example 1 - Basic
  25. ```go
  26. data := [][]string{
  27. []string{"A", "The Good", "500"},
  28. []string{"B", "The Very very Bad Man", "288"},
  29. []string{"C", "The Ugly", "120"},
  30. []string{"D", "The Gopher", "800"},
  31. }
  32. table := tablewriter.NewWriter(os.Stdout)
  33. table.SetHeader([]string{"Name", "Sign", "Rating"})
  34. for _, v := range data {
  35. table.Append(v)
  36. }
  37. table.Render() // Send output
  38. ```
  39. ##### Output 1
  40. ```
  41. +------+-----------------------+--------+
  42. | NAME | SIGN | RATING |
  43. +------+-----------------------+--------+
  44. | A | The Good | 500 |
  45. | B | The Very very Bad Man | 288 |
  46. | C | The Ugly | 120 |
  47. | D | The Gopher | 800 |
  48. +------+-----------------------+--------+
  49. ```
  50. #### Example 2 - Without Border / Footer / Bulk Append
  51. ```go
  52. data := [][]string{
  53. []string{"1/1/2014", "Domain name", "2233", "$10.98"},
  54. []string{"1/1/2014", "January Hosting", "2233", "$54.95"},
  55. []string{"1/4/2014", "February Hosting", "2233", "$51.00"},
  56. []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
  57. }
  58. table := tablewriter.NewWriter(os.Stdout)
  59. table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
  60. table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer
  61. table.SetBorder(false) // Set Border to false
  62. table.AppendBulk(data) // Add Bulk Data
  63. table.Render()
  64. ```
  65. ##### Output 2
  66. ```
  67. DATE | DESCRIPTION | CV2 | AMOUNT
  68. +----------+--------------------------+-------+---------+
  69. 1/1/2014 | Domain name | 2233 | $10.98
  70. 1/1/2014 | January Hosting | 2233 | $54.95
  71. 1/4/2014 | February Hosting | 2233 | $51.00
  72. 1/4/2014 | February Extra Bandwidth | 2233 | $30.00
  73. +----------+--------------------------+-------+---------+
  74. TOTAL | $146 93
  75. +-------+---------+
  76. ```
  77. #### Example 3 - CSV
  78. ```go
  79. table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test_info.csv", true)
  80. table.SetAlignment(tablewriter.ALIGN_LEFT) // Set Alignment
  81. table.Render()
  82. ```
  83. ##### Output 3
  84. ```
  85. +----------+--------------+------+-----+---------+----------------+
  86. | FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA |
  87. +----------+--------------+------+-----+---------+----------------+
  88. | user_id | smallint(5) | NO | PRI | NULL | auto_increment |
  89. | username | varchar(10) | NO | | NULL | |
  90. | password | varchar(100) | NO | | NULL | |
  91. +----------+--------------+------+-----+---------+----------------+
  92. ```
  93. #### Example 4 - Custom Separator
  94. ```go
  95. table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test.csv", true)
  96. table.SetRowLine(true) // Enable row line
  97. // Change table lines
  98. table.SetCenterSeparator("*")
  99. table.SetColumnSeparator("╪")
  100. table.SetRowSeparator("-")
  101. table.SetAlignment(tablewriter.ALIGN_LEFT)
  102. table.Render()
  103. ```
  104. ##### Output 4
  105. ```
  106. *------------*-----------*---------*
  107. ╪ FIRST NAME ╪ LAST NAME ╪ SSN ╪
  108. *------------*-----------*---------*
  109. ╪ John ╪ Barry ╪ 123456 ╪
  110. *------------*-----------*---------*
  111. ╪ Kathy ╪ Smith ╪ 687987 ╪
  112. *------------*-----------*---------*
  113. ╪ Bob ╪ McCornick ╪ 3979870 ╪
  114. *------------*-----------*---------*
  115. ```
  116. #### Example 5 - Markdown Format
  117. ```go
  118. data := [][]string{
  119. []string{"1/1/2014", "Domain name", "2233", "$10.98"},
  120. []string{"1/1/2014", "January Hosting", "2233", "$54.95"},
  121. []string{"1/4/2014", "February Hosting", "2233", "$51.00"},
  122. []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
  123. }
  124. table := tablewriter.NewWriter(os.Stdout)
  125. table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
  126. table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
  127. table.SetCenterSeparator("|")
  128. table.AppendBulk(data) // Add Bulk Data
  129. table.Render()
  130. ```
  131. ##### Output 5
  132. ```
  133. | DATE | DESCRIPTION | CV2 | AMOUNT |
  134. |----------|--------------------------|------|--------|
  135. | 1/1/2014 | Domain name | 2233 | $10.98 |
  136. | 1/1/2014 | January Hosting | 2233 | $54.95 |
  137. | 1/4/2014 | February Hosting | 2233 | $51.00 |
  138. | 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 |
  139. ```
  140. #### Example 6 - Identical cells merging
  141. ```go
  142. data := [][]string{
  143. []string{"1/1/2014", "Domain name", "1234", "$10.98"},
  144. []string{"1/1/2014", "January Hosting", "2345", "$54.95"},
  145. []string{"1/4/2014", "February Hosting", "3456", "$51.00"},
  146. []string{"1/4/2014", "February Extra Bandwidth", "4567", "$30.00"},
  147. }
  148. table := tablewriter.NewWriter(os.Stdout)
  149. table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
  150. table.SetFooter([]string{"", "", "Total", "$146.93"})
  151. table.SetAutoMergeCells(true)
  152. table.SetRowLine(true)
  153. table.AppendBulk(data)
  154. table.Render()
  155. ```
  156. ##### Output 6
  157. ```
  158. +----------+--------------------------+-------+---------+
  159. | DATE | DESCRIPTION | CV2 | AMOUNT |
  160. +----------+--------------------------+-------+---------+
  161. | 1/1/2014 | Domain name | 1234 | $10.98 |
  162. + +--------------------------+-------+---------+
  163. | | January Hosting | 2345 | $54.95 |
  164. +----------+--------------------------+-------+---------+
  165. | 1/4/2014 | February Hosting | 3456 | $51.00 |
  166. + +--------------------------+-------+---------+
  167. | | February Extra Bandwidth | 4567 | $30.00 |
  168. +----------+--------------------------+-------+---------+
  169. | TOTAL | $146 93 |
  170. +----------+--------------------------+-------+---------+
  171. ```
  172. #### Table with color
  173. ```go
  174. data := [][]string{
  175. []string{"1/1/2014", "Domain name", "2233", "$10.98"},
  176. []string{"1/1/2014", "January Hosting", "2233", "$54.95"},
  177. []string{"1/4/2014", "February Hosting", "2233", "$51.00"},
  178. []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
  179. }
  180. table := tablewriter.NewWriter(os.Stdout)
  181. table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
  182. table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer
  183. table.SetBorder(false) // Set Border to false
  184. table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.BgGreenColor},
  185. tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold, tablewriter.BgBlackColor},
  186. tablewriter.Colors{tablewriter.BgRedColor, tablewriter.FgWhiteColor},
  187. tablewriter.Colors{tablewriter.BgCyanColor, tablewriter.FgWhiteColor})
  188. table.SetColumnColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor},
  189. tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiRedColor},
  190. tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor},
  191. tablewriter.Colors{tablewriter.Bold, tablewriter.FgBlackColor})
  192. table.SetFooterColor(tablewriter.Colors{}, tablewriter.Colors{},
  193. tablewriter.Colors{tablewriter.Bold},
  194. tablewriter.Colors{tablewriter.FgHiRedColor})
  195. table.AppendBulk(data)
  196. table.Render()
  197. ```
  198. #### Table with color Output
  199. ![Table with Color](https://cloud.githubusercontent.com/assets/6460392/21101956/bbc7b356-c0a1-11e6-9f36-dba694746efc.png)
  200. #### Example 6 - Set table caption
  201. ```go
  202. data := [][]string{
  203. []string{"A", "The Good", "500"},
  204. []string{"B", "The Very very Bad Man", "288"},
  205. []string{"C", "The Ugly", "120"},
  206. []string{"D", "The Gopher", "800"},
  207. }
  208. table := tablewriter.NewWriter(os.Stdout)
  209. table.SetHeader([]string{"Name", "Sign", "Rating"})
  210. table.SetCaption(true, "Movie ratings.")
  211. for _, v := range data {
  212. table.Append(v)
  213. }
  214. table.Render() // Send output
  215. ```
  216. Note: Caption text will wrap with total width of rendered table.
  217. ##### Output 6
  218. ```
  219. +------+-----------------------+--------+
  220. | NAME | SIGN | RATING |
  221. +------+-----------------------+--------+
  222. | A | The Good | 500 |
  223. | B | The Very very Bad Man | 288 |
  224. | C | The Ugly | 120 |
  225. | D | The Gopher | 800 |
  226. +------+-----------------------+--------+
  227. Movie ratings.
  228. ```
  229. #### TODO
  230. - ~~Import Directly from CSV~~ - `done`
  231. - ~~Support for `SetFooter`~~ - `done`
  232. - ~~Support for `SetBorder`~~ - `done`
  233. - ~~Support table with uneven rows~~ - `done`
  234. - ~~Support custom alignment~~
  235. - General Improvement & Optimisation
  236. - `NewHTML` Parse table from HTML