table.odin 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. Copyright 2023 oskarnp <[email protected]>
  3. Made available under Odin's BSD-3 license.
  4. List of contributors:
  5. oskarnp: Initial implementation.
  6. */
  7. package text_table
  8. import "core:io"
  9. import "core:fmt"
  10. import "core:mem"
  11. import "core:mem/virtual"
  12. import "base:runtime"
  13. Cell :: struct {
  14. text: string,
  15. alignment: Cell_Alignment,
  16. }
  17. Cell_Alignment :: enum {
  18. Left,
  19. Center,
  20. Right,
  21. }
  22. Table :: struct {
  23. lpad, rpad: int, // Cell padding (left/right)
  24. cells: [dynamic]Cell,
  25. caption: string,
  26. nr_rows, nr_cols: int,
  27. has_header_row: bool,
  28. table_allocator: runtime.Allocator, // Used for allocating cells/colw
  29. format_allocator: runtime.Allocator, // Used for allocating Cell.text when applicable
  30. dirty: bool, // True if build() needs to be called before rendering
  31. // The following are computed on build()
  32. colw: [dynamic]int, // Width of each column (including padding, excluding borders)
  33. tblw: int, // Width of entire table (including padding, excluding borders)
  34. }
  35. init :: proc{init_with_allocator, init_with_virtual_arena, init_with_mem_arena}
  36. init_with_allocator :: proc(tbl: ^Table, format_allocator := context.temp_allocator, table_allocator := context.allocator) -> ^Table {
  37. tbl.table_allocator = table_allocator
  38. tbl.cells = make([dynamic]Cell, tbl.table_allocator)
  39. tbl.colw = make([dynamic]int, tbl.table_allocator)
  40. tbl.format_allocator = format_allocator
  41. return tbl
  42. }
  43. init_with_virtual_arena :: proc(tbl: ^Table, format_arena: ^virtual.Arena, table_allocator := context.allocator) -> ^Table {
  44. return init_with_allocator(tbl, virtual.arena_allocator(format_arena), table_allocator)
  45. }
  46. init_with_mem_arena :: proc(tbl: ^Table, format_arena: ^mem.Arena, table_allocator := context.allocator) -> ^Table {
  47. return init_with_allocator(tbl, mem.arena_allocator(format_arena), table_allocator)
  48. }
  49. destroy :: proc(tbl: ^Table) {
  50. free_all(tbl.format_allocator)
  51. delete(tbl.cells)
  52. delete(tbl.colw)
  53. }
  54. caption :: proc(tbl: ^Table, value: string) {
  55. tbl.caption = value
  56. tbl.dirty = true
  57. }
  58. padding :: proc(tbl: ^Table, lpad, rpad: int) {
  59. tbl.lpad = lpad
  60. tbl.rpad = rpad
  61. tbl.dirty = true
  62. }
  63. get_cell :: proc(tbl: ^Table, row, col: int, loc := #caller_location) -> ^Cell {
  64. assert(col >= 0 && col < tbl.nr_cols, "cell column out of range", loc)
  65. assert(row >= 0 && row < tbl.nr_rows, "cell row out of range", loc)
  66. resize(&tbl.cells, tbl.nr_cols * tbl.nr_rows)
  67. return &tbl.cells[row*tbl.nr_cols + col]
  68. }
  69. set_cell_value_and_alignment :: proc(tbl: ^Table, row, col: int, value: string, alignment: Cell_Alignment) {
  70. cell := get_cell(tbl, row, col)
  71. cell.text = format(tbl, "%v", value)
  72. cell.alignment = alignment
  73. tbl.dirty = true
  74. }
  75. set_cell_value :: proc(tbl: ^Table, row, col: int, value: any, loc := #caller_location) {
  76. cell := get_cell(tbl, row, col, loc)
  77. switch val in value {
  78. case nil:
  79. cell.text = ""
  80. case string:
  81. cell.text = string(val)
  82. case cstring:
  83. cell.text = string(val)
  84. case:
  85. cell.text = format(tbl, "%v", val)
  86. if cell.text == "" {
  87. fmt.eprintf("{} text/table: format() resulted in empty string (arena out of memory?)\n", loc)
  88. }
  89. }
  90. tbl.dirty = true
  91. }
  92. set_cell_alignment :: proc(tbl: ^Table, row, col: int, alignment: Cell_Alignment, loc := #caller_location) {
  93. cell := get_cell(tbl, row, col, loc)
  94. cell.alignment = alignment
  95. tbl.dirty = true
  96. }
  97. format :: proc(tbl: ^Table, _fmt: string, args: ..any, loc := #caller_location) -> string {
  98. context.allocator = tbl.format_allocator
  99. return fmt.aprintf(_fmt, ..args)
  100. }
  101. header :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
  102. if (tbl.has_header_row && tbl.nr_rows != 1) || (!tbl.has_header_row && tbl.nr_rows != 0) {
  103. panic("Cannot add headers after rows have been added", loc)
  104. }
  105. if tbl.nr_rows == 0 {
  106. tbl.nr_rows += 1
  107. tbl.has_header_row = true
  108. }
  109. col := tbl.nr_cols
  110. tbl.nr_cols += len(values)
  111. for val in values {
  112. set_cell_value(tbl, header_row(tbl), col, val, loc)
  113. col += 1
  114. }
  115. tbl.dirty = true
  116. }
  117. row :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
  118. if tbl.nr_cols == 0 {
  119. if len(values) == 0 {
  120. panic("Cannot create row without values unless knowing amount of columns in advance")
  121. } else {
  122. tbl.nr_cols = len(values)
  123. }
  124. }
  125. tbl.nr_rows += 1
  126. for col in 0..<tbl.nr_cols {
  127. val := values[col] if col < len(values) else nil
  128. set_cell_value(tbl, last_row(tbl), col, val)
  129. }
  130. tbl.dirty = true
  131. }
  132. last_row :: proc(tbl: ^Table) -> int {
  133. return tbl.nr_rows - 1
  134. }
  135. header_row :: proc(tbl: ^Table) -> int {
  136. return 0 if tbl.has_header_row else -1
  137. }
  138. first_row :: proc(tbl: ^Table) -> int {
  139. return header_row(tbl)+1 if tbl.has_header_row else 0
  140. }
  141. build :: proc(tbl: ^Table) {
  142. tbl.dirty = false
  143. resize(&tbl.colw, tbl.nr_cols)
  144. mem.zero_slice(tbl.colw[:])
  145. for row in 0..<tbl.nr_rows {
  146. for col in 0..<tbl.nr_cols {
  147. cell := get_cell(tbl, row, col)
  148. if w := len(cell.text) + tbl.lpad + tbl.rpad; w > tbl.colw[col] {
  149. tbl.colw[col] = w
  150. }
  151. }
  152. }
  153. colw_sum := 0
  154. for v in tbl.colw {
  155. colw_sum += v
  156. }
  157. tbl.tblw = max(colw_sum, len(tbl.caption) + tbl.lpad + tbl.rpad)
  158. // Resize columns to match total width of table
  159. remain := tbl.tblw-colw_sum
  160. for col := 0; remain > 0; col = (col + 1) % tbl.nr_cols {
  161. tbl.colw[col] += 1
  162. remain -= 1
  163. }
  164. return
  165. }
  166. write_html_table :: proc(w: io.Writer, tbl: ^Table) {
  167. if tbl.dirty {
  168. build(tbl)
  169. }
  170. io.write_string(w, "<table>\n")
  171. if tbl.caption != "" {
  172. io.write_string(w, "<caption>")
  173. io.write_string(w, tbl.caption)
  174. io.write_string(w, "</caption>\n")
  175. }
  176. align_attribute :: proc(cell: ^Cell) -> string {
  177. switch cell.alignment {
  178. case .Left: return ` align="left"`
  179. case .Center: return ` align="center"`
  180. case .Right: return ` align="right"`
  181. }
  182. unreachable()
  183. }
  184. if tbl.has_header_row {
  185. io.write_string(w, "<thead>\n")
  186. io.write_string(w, " <tr>\n")
  187. for col in 0..<tbl.nr_cols {
  188. cell := get_cell(tbl, header_row(tbl), col)
  189. io.write_string(w, " <th")
  190. io.write_string(w, align_attribute(cell))
  191. io.write_string(w, ">")
  192. io.write_string(w, cell.text)
  193. io.write_string(w, "</th>\n")
  194. }
  195. io.write_string(w, " </tr>\n")
  196. io.write_string(w, "</thead>\n")
  197. }
  198. io.write_string(w, "<tbody>\n")
  199. for row in 0..<tbl.nr_rows {
  200. if tbl.has_header_row && row == header_row(tbl) {
  201. continue
  202. }
  203. io.write_string(w, " <tr>\n")
  204. for col in 0..<tbl.nr_cols {
  205. cell := get_cell(tbl, row, col)
  206. io.write_string(w, " <td")
  207. io.write_string(w, align_attribute(cell))
  208. io.write_string(w, ">")
  209. io.write_string(w, cell.text)
  210. io.write_string(w, "</td>\n")
  211. }
  212. io.write_string(w, " </tr>\n")
  213. }
  214. io.write_string(w, " </tbody>\n")
  215. io.write_string(w, "</table>\n")
  216. }
  217. write_ascii_table :: proc(w: io.Writer, tbl: ^Table) {
  218. if tbl.dirty {
  219. build(tbl)
  220. }
  221. write_caption_separator :: proc(w: io.Writer, tbl: ^Table) {
  222. io.write_byte(w, '+')
  223. write_byte_repeat(w, tbl.tblw + tbl.nr_cols - 1, '-')
  224. io.write_byte(w, '+')
  225. io.write_byte(w, '\n')
  226. }
  227. write_table_separator :: proc(w: io.Writer, tbl: ^Table) {
  228. for col in 0..<tbl.nr_cols {
  229. if col == 0 {
  230. io.write_byte(w, '+')
  231. }
  232. write_byte_repeat(w, tbl.colw[col], '-')
  233. io.write_byte(w, '+')
  234. }
  235. io.write_byte(w, '\n')
  236. }
  237. if tbl.caption != "" {
  238. write_caption_separator(w, tbl)
  239. io.write_byte(w, '|')
  240. write_text_align(w, tbl.tblw - tbl.lpad - tbl.rpad + tbl.nr_cols - 1,
  241. tbl.lpad, tbl.rpad, tbl.caption, .Center)
  242. io.write_byte(w, '|')
  243. io.write_byte(w, '\n')
  244. }
  245. write_table_separator(w, tbl)
  246. for row in 0..<tbl.nr_rows {
  247. for col in 0..<tbl.nr_cols {
  248. if col == 0 {
  249. io.write_byte(w, '|')
  250. }
  251. write_table_cell(w, tbl, row, col)
  252. io.write_byte(w, '|')
  253. }
  254. io.write_byte(w, '\n')
  255. if tbl.has_header_row && row == header_row(tbl) {
  256. write_table_separator(w, tbl)
  257. }
  258. }
  259. write_table_separator(w, tbl)
  260. }
  261. // Renders table according to GitHub Flavored Markdown (GFM) specification
  262. write_markdown_table :: proc(w: io.Writer, tbl: ^Table) {
  263. // NOTE(oskar): Captions or colspans are not supported by GFM as far as I can tell.
  264. if tbl.dirty {
  265. build(tbl)
  266. }
  267. for row in 0..<tbl.nr_rows {
  268. for col in 0..<tbl.nr_cols {
  269. cell := get_cell(tbl, row, col)
  270. if col == 0 {
  271. io.write_byte(w, '|')
  272. }
  273. write_text_align(w, tbl.colw[col] - tbl.lpad - tbl.rpad, tbl.lpad, tbl.rpad, cell.text,
  274. .Center if tbl.has_header_row && row == header_row(tbl) else .Left)
  275. io.write_string(w, "|")
  276. }
  277. io.write_byte(w, '\n')
  278. if tbl.has_header_row && row == header_row(tbl) {
  279. for col in 0..<tbl.nr_cols {
  280. cell := get_cell(tbl, row, col)
  281. if col == 0 {
  282. io.write_byte(w, '|')
  283. }
  284. switch cell.alignment {
  285. case .Left:
  286. io.write_byte(w, ':')
  287. write_byte_repeat(w, max(1, tbl.colw[col]-1), '-')
  288. case .Center:
  289. io.write_byte(w, ':')
  290. write_byte_repeat(w, max(1, tbl.colw[col]-2), '-')
  291. io.write_byte(w, ':')
  292. case .Right:
  293. write_byte_repeat(w, max(1, tbl.colw[col]-1), '-')
  294. io.write_byte(w, ':')
  295. }
  296. io.write_byte(w, '|')
  297. }
  298. io.write_byte(w, '\n')
  299. }
  300. }
  301. }
  302. write_byte_repeat :: proc(w: io.Writer, n: int, b: byte) {
  303. for _ in 0..<n {
  304. io.write_byte(w, b)
  305. }
  306. }
  307. write_table_cell :: proc(w: io.Writer, tbl: ^Table, row, col: int) {
  308. if tbl.dirty {
  309. build(tbl)
  310. }
  311. cell := get_cell(tbl, row, col)
  312. write_text_align(w, tbl.colw[col]-tbl.lpad-tbl.rpad, tbl.lpad, tbl.rpad, cell.text, cell.alignment)
  313. }
  314. write_text_align :: proc(w: io.Writer, colw, lpad, rpad: int, text: string, alignment: Cell_Alignment) {
  315. write_byte_repeat(w, lpad, ' ')
  316. switch alignment {
  317. case .Left:
  318. io.write_string(w, text)
  319. write_byte_repeat(w, colw - len(text), ' ')
  320. case .Center:
  321. pad := colw - len(text)
  322. odd := pad & 1 != 0
  323. write_byte_repeat(w, pad/2, ' ')
  324. io.write_string(w, text)
  325. write_byte_repeat(w, pad/2 + 1 if odd else pad/2, ' ')
  326. case .Right:
  327. write_byte_repeat(w, colw - len(text), ' ')
  328. io.write_string(w, text)
  329. }
  330. write_byte_repeat(w, rpad, ' ')
  331. }