doc.odin 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. The package `table` implements plain-text/markdown/HTML/custom rendering of tables.
  3. **Custom rendering example:**
  4. package main
  5. import "core:io"
  6. import "core:text/table"
  7. main :: proc() {
  8. stdout := table.stdio_writer()
  9. tbl := table.init(&table.Table{})
  10. table.padding(tbl, 0, 1)
  11. table.row(tbl, "A_LONG_ENUM", "= 54,", "// A comment about A_LONG_ENUM")
  12. table.row(tbl, "AN_EVEN_LONGER_ENUM", "= 1,", "// A comment about AN_EVEN_LONGER_ENUM")
  13. table.build(tbl, table.unicode_width_proc)
  14. for row in 0..<tbl.nr_rows {
  15. for col in 0..<tbl.nr_cols {
  16. table.write_table_cell(stdout, tbl, row, col)
  17. }
  18. io.write_byte(stdout, '\n')
  19. }
  20. }
  21. This outputs:
  22. A_LONG_ENUM = 54, // A comment about A_LONG_ENUM
  23. AN_EVEN_LONGER_ENUM = 1, // A comment about AN_EVEN_LONGER_ENUM
  24. **Plain-text rendering example:**
  25. package main
  26. import "core:fmt"
  27. import "core:io"
  28. import "core:text/table"
  29. main :: proc() {
  30. stdout := table.stdio_writer()
  31. tbl := table.init(&table.Table{})
  32. defer table.destroy(tbl)
  33. table.caption(tbl, "This is a table caption and it is very long")
  34. table.padding(tbl, 1, 1) // Left/right padding of cells
  35. table.header(tbl, "AAAAAAAAA", "B")
  36. table.header(tbl, "C") // Appends to previous header row. Same as if done header("AAAAAAAAA", "B", "C") from start.
  37. // Create a row with two values. Since there are three columns the third
  38. // value will become the empty string.
  39. //
  40. // NOTE: table.header() is not allowed anymore after this.
  41. table.row(tbl, 123, "foo")
  42. // Use `format()` if you need custom formatting. This will allocate into
  43. // the arena specified at init.
  44. table.row(tbl,
  45. table.format(tbl, "%09d", 5),
  46. table.format(tbl, "%.6f", 6.28318530717958647692528676655900576))
  47. // A row with zero values is allowed as long as a previous row or header
  48. // exist. The value and alignment of each cell can then be set
  49. // individually.
  50. table.row(tbl)
  51. table.set_cell_value_and_alignment(tbl, table.last_row(tbl), 0, "a", .Center)
  52. table.set_cell_value(tbl, table.last_row(tbl), 1, "bbb")
  53. table.set_cell_value(tbl, table.last_row(tbl), 2, "c")
  54. // Headers are regular cells, too. Use header_row() as row index to modify
  55. // header cells.
  56. table.set_cell_alignment(tbl, table.header_row(tbl), 1, .Center) // Sets alignment of 'B' column to Center.
  57. table.set_cell_alignment(tbl, table.header_row(tbl), 2, .Right) // Sets alignment of 'C' column to Right.
  58. table.write_plain_table(stdout, tbl)
  59. fmt.println()
  60. table.write_markdown_table(stdout, tbl)
  61. }
  62. This outputs:
  63. +-----------------------------------------------+
  64. | This is a table caption and it is very long |
  65. +------------------+-----------------+----------+
  66. | AAAAAAAAA | B | C |
  67. +------------------+-----------------+----------+
  68. | 123 | foo | |
  69. | 000000005 | 6.283185 | |
  70. | a | bbb | c |
  71. +------------------+-----------------+----------+
  72. and
  73. | AAAAAAAAA | B | C |
  74. |:-----------------|:---------------:|---------:|
  75. | 123 | foo | |
  76. | 000000005 | 6.283185 | |
  77. | a | bbb | c |
  78. respectively.
  79. Additionally, if you want to set the alignment and values in-line while
  80. constructing a table, you can use `aligned_row_of_values` or
  81. `row_of_aligned_values` like so:
  82. table.aligned_row_of_values(tbl, .Center, "Foo", "Bar")
  83. table.row_of_aligned_values(tbl, {{.Center, "Foo"}, {.Right, "Bar"}})
  84. **Caching Results:**
  85. If you only need to build a table once but display it potentially many times,
  86. it may be more efficient to cache the results of your write into a string.
  87. Here's an example of how you can do that:
  88. package main
  89. import "core:fmt"
  90. import "core:strings"
  91. import "core:text/table"
  92. main :: proc() {
  93. string_buffer := strings.builder_make()
  94. defer strings.builder_destroy(&string_buffer)
  95. {
  96. tbl: table.Table
  97. table.init(&tbl)
  98. defer table.destroy(&tbl)
  99. table.caption(&tbl, "Hellope!")
  100. table.row(&tbl, "Hellope", "World")
  101. builder_writer := strings.to_writer(&string_buffer)
  102. // The written table will be cached into the string builder after this call.
  103. table.write_plain_table(builder_writer, &tbl)
  104. }
  105. // The table is inaccessible, now that we're back in the first-level scope.
  106. // But now the results are stored in the string builder, which can be converted to a string.
  107. my_table_string := strings.to_string(string_buffer)
  108. // Remember that the string's allocated backing data lives in the
  109. // builder and must still be freed.
  110. //
  111. // The deferred call to `builder_destroy` will take care of that for us
  112. // in this simple example.
  113. fmt.println(my_table_string)
  114. }
  115. **Regarding `Width_Procs`:**
  116. If you know ahead of time that all the text you're parsing is ASCII, instead of
  117. Unicode, it is more efficient to use `table.ascii_width_proc` instead of the
  118. default `unicode_width_proc`, as that procedure has to perform in-depth lookups
  119. to determine multiple Unicode characteristics of the codepoints parsed in order
  120. to get the proper alignment for a variety of different scripts.
  121. For example, you may do this instead:
  122. table.write_plain_table(stdout, tbl, table.ascii_width_proc)
  123. table.write_markdown_table(stdout, tbl, table.ascii_width_proc)
  124. The output will still be the same, but the preprocessing is much faster.
  125. You may also supply your own `Width_Proc`s, if you know more about how the text
  126. is structured than what we can assume.
  127. simple_cjk_width_proc :: proc(str: string) -> (result: int) {
  128. for r in str {
  129. result += 2
  130. }
  131. return
  132. }
  133. table.write_plain_table(stdout, tbl, simple_cjk_width_proc)
  134. This procedure will output 2 times the number of UTF-8 runes in a string, a
  135. simple heuristic for CJK-only wide text.
  136. **Unicode Support:**
  137. This package makes use of the `grapheme_count` procedure from the
  138. `core:unicode/utf8` package. It is a complete, standards-compliant
  139. implementation for counting graphemes and calculating visual width of a Unicode
  140. grapheme cluster in monospace cells.
  141. Here is a full example of how well-supported Unicode is with this package:
  142. package main
  143. import "core:fmt"
  144. import "core:io"
  145. import "core:os"
  146. import "core:text/table"
  147. scripts :: proc(w: io.Writer) {
  148. t: table.Table
  149. table.init(&t)
  150. table.caption(&t, "Tést Suite")
  151. table.padding(&t, 1, 3)
  152. table.header_of_aligned_values(&t, {{.Left, "Script"}, {.Center, "Sample"}})
  153. table.row(&t, "Latin", "At vero eos et accusamus et iusto odio dignissimos ducimus,")
  154. table.row(&t, "Cyrillic", "Ру́сский язы́к — язык восточнославянской группы славянской")
  155. table.row(&t, "Greek", "Η ελληνική γλώσσα ανήκει στην ινδοευρωπαϊκή οικογένεια")
  156. table.row(&t, "Younger Futhark", "ᚴᚢᚱᛘᛦ ᚴᚢᚾᚢᚴᛦ ᚴᛅᚱᚦᛁ ᚴᚢᛒᛚ ᚦᚢᛋᛁ ᛅᚠᛏ ᚦᚢᚱᚢᛁ ᚴᚢᚾᚢ ᛋᛁᚾᛅ ᛏᛅᚾᛘᛅᚱᚴᛅᛦ ᛒᚢᛏ")
  157. table.row(&t, "Chinese hanzi", "官話為汉语的一支,主體分布在中国北部和西南部的大部分地区。")
  158. table.row(&t, "Japanese kana", "いろはにほへとちりぬるをわかよたれそつねならむ")
  159. table.row(&t, "Korean hangul", "한글, 조선글은 한국어의 공식문자로서, 세종이 한국어를")
  160. table.row(&t, "Thai", "ภาษาไทย หรือ ภาษาไทยกลาง เป็นภาษาในกลุ่มภาษาไท ซึ่งเป็นกลุ่มย่อยของตระกูลภาษาขร้า-ไท")
  161. table.row(&t, "Georgian", "ქართული ენა — ქართველურ ენათა ოჯახის ენა. ქართველების მშობლიური ენა,")
  162. table.row(&t, "Armenian", "Իր շուրջ հինգհազարամյա գոյության ընթացքում հայերենը շփվել է տարբեր")
  163. table.row(&t)
  164. table.row_of_aligned_values(&t, {{.Left, "Arabic"}, {.Right, "ٱللُّغَةُ ٱلْعَرَبِيَّة هي أكثر اللغات السامية تحدثًا، وإحدى أكثر"}})
  165. table.row_of_aligned_values(&t, {{.Left, "Hebrew"}, {.Right, "עִבְרִית היא שפה שמית, ממשפחת השפות האפרו-אסייתיות, הידועה"}})
  166. table.row(&t)
  167. table.row(&t, "Swedish", "Växjö [ˈvɛkːˌɧøː] är en tätort i södra Smålands inland samt centralort i Växjö kommun")
  168. table.row(&t, "Saxon", "Hwæt! We Gardena in geardagum, þeodcyninga, þrym gefrunon, hu ða æþelingas ellen fremedon.")
  169. table.row(&t)
  170. table.aligned_row_of_values(&t, .Center, "Emoji (Single codepoints)", "\U0001f4ae \U0001F600 \U0001F201 \U0001F21A")
  171. table.row(&t, "Excessive Diacritics", "H̷e̶l̵l̸o̴p̵e̷ ̸w̶o̸r̵l̶d̵!̴")
  172. table.write_plain_table(w, &t)
  173. fmt.println()
  174. }
  175. main :: proc() {
  176. stdout := os.stream_from_handle(os.stdout)
  177. scripts(stdout)
  178. }
  179. This will print out:
  180. +----------------------------------------------------------------------------------------------------------------------------+
  181. | Tést Suite |
  182. +-----------------------------+----------------------------------------------------------------------------------------------+
  183. | Script | Sample |
  184. +-----------------------------+----------------------------------------------------------------------------------------------+
  185. | Latin | At vero eos et accusamus et iusto odio dignissimos ducimus, |
  186. | Cyrillic | Ру́сский язы́к — язык восточнославянской группы славянской |
  187. | Greek | Η ελληνική γλώσσα ανήκει στην ινδοευρωπαϊκή οικογένεια |
  188. | Younger Futhark | ᚴᚢᚱᛘᛦ ᚴᚢᚾᚢᚴᛦ ᚴᛅᚱᚦᛁ ᚴᚢᛒᛚ ᚦᚢᛋᛁ ᛅᚠᛏ ᚦᚢᚱᚢᛁ ᚴᚢᚾᚢ ᛋᛁᚾᛅ ᛏᛅᚾᛘᛅᚱᚴᛅᛦ ᛒᚢᛏ |
  189. | Chinese hanzi | 官話為汉语的一支,主體分布在中国北部和西南部的大部分地区。 |
  190. | Japanese kana | いろはにほへとちりぬるをわかよたれそつねならむ |
  191. | Korean hangul | 한글, 조선글은 한국어의 공식문자로서, 세종이 한국어를 |
  192. | Thai | ภาษาไทย หรือ ภาษาไทยกลาง เป็นภาษาในกลุ่มภาษาไท ซึ่งเป็นกลุ่มย่อยของตระกูลภาษาขร้า-ไท |
  193. | Georgian | ქართული ენა — ქართველურ ენათა ოჯახის ენა. ქართველების მშობლიური ენა, |
  194. | Armenian | Իր շուրջ հինգհազարամյա գոյության ընթացքում հայերենը շփվել է տարբեր |
  195. | | |
  196. | Arabic | ٱللُّغَةُ ٱلْعَرَبِيَّة هي أكثر اللغات السامية تحدثًا، وإحدى أكثر |
  197. | Hebrew | עִבְרִית היא שפה שמית, ממשפחת השפות האפרו-אסייתיות, הידועה |
  198. | | |
  199. | Swedish | Växjö [ˈvɛkːˌɧøː] är en tätort i södra Smålands inland samt centralort i Växjö kommun |
  200. | Saxon | Hwæt! We Gardena in geardagum, þeodcyninga, þrym gefrunon, hu ða æþelingas ellen fremedon. |
  201. | | |
  202. | Emoji (Single codepoints) | 💮 😀 🈁 🈚 |
  203. | Excessive Diacritics | H̷e̶l̵l̸o̴p̵e̷ ̸w̶o̸r̵l̶d̵!̴ |
  204. +-----------------------------+----------------------------------------------------------------------------------------------+
  205. **Decorated Tables:**
  206. If you'd prefer to change the borders used by the plain-text table printing,
  207. there is the `write_decorated_table` procedure that allows you to change the
  208. corners and dividers.
  209. Here is a complete example:
  210. package main
  211. import "core:fmt"
  212. import "core:io"
  213. import "core:os"
  214. import "core:text/table"
  215. box_drawing :: proc(w: io.Writer) {
  216. t: table.Table
  217. table.init(&t)
  218. table.caption(&t, "Box Drawing Example")
  219. table.padding(&t, 2, 2)
  220. table.header_of_aligned_values(&t, {{.Left, "Operating System"}, {.Center, "Year Introduced"}})
  221. table.row(&t, "UNIX", "1973")
  222. table.row(&t, "MS-DOS", "1981")
  223. table.row(&t, "Commodore 64 KERNAL", "1982")
  224. table.row(&t, "Mac OS", "1984")
  225. table.row(&t, "Amiga", "1985")
  226. table.row(&t, "Windows 1.0", "1985")
  227. table.row(&t, "Linux", "1991")
  228. table.row(&t, "Windows 3.1", "1992")
  229. decorations := table.Decorations {
  230. "┌", "┬", "┐",
  231. "├", "┼", "┤",
  232. "└", "┴", "┘",
  233. "│", "─",
  234. }
  235. table.write_decorated_table(w, &t, decorations)
  236. fmt.println()
  237. }
  238. main :: proc() {
  239. stdout := os.stream_from_handle(os.stdout)
  240. box_drawing(stdout)
  241. }
  242. While the decorations support multi-codepoint Unicode graphemes, do note that
  243. each border character should not be larger than one monospace cell.
  244. */
  245. package text_table