table.odin 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. Feoramund: Unicode support.
  7. */
  8. package text_table
  9. import "core:io"
  10. import "core:fmt"
  11. import "core:log"
  12. import "core:mem"
  13. import "core:mem/virtual"
  14. import "core:unicode/utf8"
  15. import "base:runtime"
  16. Cell :: struct {
  17. text: string,
  18. width: int,
  19. alignment: Cell_Alignment,
  20. }
  21. Cell_Alignment :: enum {
  22. Left,
  23. Center,
  24. Right,
  25. }
  26. Aligned_Value :: struct {
  27. alignment: Cell_Alignment,
  28. value: any,
  29. }
  30. // Determines the width of a string used in the table for alignment purposes.
  31. Width_Proc :: #type proc(str: string) -> int
  32. Table :: struct {
  33. lpad, rpad: int, // Cell padding (left/right)
  34. cells: [dynamic]Cell,
  35. caption: string,
  36. nr_rows, nr_cols: int,
  37. has_header_row: bool,
  38. table_allocator: runtime.Allocator, // Used for allocating cells/colw
  39. format_allocator: runtime.Allocator, // Used for allocating Cell.text when applicable
  40. // The following are computed on build()
  41. colw: [dynamic]int, // Width of each column (excluding padding and borders)
  42. tblw: int, // Width of entire table (including padding, excluding borders)
  43. }
  44. Decorations :: struct {
  45. // These are strings, because of multi-codepoint Unicode graphemes.
  46. // Connecting decorations:
  47. nw, n, ne,
  48. w, x, e,
  49. sw, s, se: string,
  50. // Straight lines:
  51. vert: string,
  52. horz: string,
  53. }
  54. ascii_width_proc :: proc(str: string) -> int {
  55. return len(str)
  56. }
  57. unicode_width_proc :: proc(str: string) -> (width: int) {
  58. _, _, width = #force_inline utf8.grapheme_count(str)
  59. return
  60. }
  61. init :: proc{init_with_allocator, init_with_virtual_arena, init_with_mem_arena}
  62. init_with_allocator :: proc(tbl: ^Table, format_allocator := context.temp_allocator, table_allocator := context.allocator) -> ^Table {
  63. tbl.table_allocator = table_allocator
  64. tbl.cells = make([dynamic]Cell, tbl.table_allocator)
  65. tbl.colw = make([dynamic]int, tbl.table_allocator)
  66. tbl.format_allocator = format_allocator
  67. return tbl
  68. }
  69. init_with_virtual_arena :: proc(tbl: ^Table, format_arena: ^virtual.Arena, table_allocator := context.allocator) -> ^Table {
  70. return init_with_allocator(tbl, virtual.arena_allocator(format_arena), table_allocator)
  71. }
  72. init_with_mem_arena :: proc(tbl: ^Table, format_arena: ^mem.Arena, table_allocator := context.allocator) -> ^Table {
  73. return init_with_allocator(tbl, mem.arena_allocator(format_arena), table_allocator)
  74. }
  75. destroy :: proc(tbl: ^Table) {
  76. free_all(tbl.format_allocator)
  77. delete(tbl.cells)
  78. delete(tbl.colw)
  79. }
  80. caption :: proc(tbl: ^Table, value: string) {
  81. tbl.caption = value
  82. }
  83. padding :: proc(tbl: ^Table, lpad, rpad: int) {
  84. tbl.lpad = lpad
  85. tbl.rpad = rpad
  86. }
  87. get_cell :: proc(tbl: ^Table, row, col: int, loc := #caller_location) -> ^Cell {
  88. assert(col >= 0 && col < tbl.nr_cols, "cell column out of range", loc)
  89. assert(row >= 0 && row < tbl.nr_rows, "cell row out of range", loc)
  90. resize(&tbl.cells, tbl.nr_cols * tbl.nr_rows)
  91. return &tbl.cells[row*tbl.nr_cols + col]
  92. }
  93. @private
  94. to_string :: #force_inline proc(tbl: ^Table, value: any, loc := #caller_location) -> (result: string) {
  95. switch val in value {
  96. case nil:
  97. result = ""
  98. case string:
  99. result = val
  100. case cstring:
  101. result = cast(string)val
  102. case:
  103. result = format(tbl, "%v", val)
  104. if result == "" {
  105. log.error("text/table.format() resulted in empty string (arena out of memory?)", location = loc)
  106. }
  107. }
  108. return
  109. }
  110. set_cell_value :: proc(tbl: ^Table, row, col: int, value: any, loc := #caller_location) {
  111. cell := get_cell(tbl, row, col, loc)
  112. cell.text = to_string(tbl, value, loc)
  113. }
  114. set_cell_alignment :: proc(tbl: ^Table, row, col: int, alignment: Cell_Alignment, loc := #caller_location) {
  115. cell := get_cell(tbl, row, col, loc)
  116. cell.alignment = alignment
  117. }
  118. set_cell_value_and_alignment :: proc(tbl: ^Table, row, col: int, value: any, alignment: Cell_Alignment, loc := #caller_location) {
  119. cell := get_cell(tbl, row, col, loc)
  120. cell.text = to_string(tbl, value, loc)
  121. cell.alignment = alignment
  122. }
  123. format :: proc(tbl: ^Table, _fmt: string, args: ..any) -> string {
  124. context.allocator = tbl.format_allocator
  125. return fmt.aprintf(_fmt, ..args)
  126. }
  127. header :: header_of_values
  128. header_of_values :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
  129. if (tbl.has_header_row && tbl.nr_rows != 1) || (!tbl.has_header_row && tbl.nr_rows != 0) {
  130. panic("Cannot add headers after rows have been added", loc)
  131. }
  132. if tbl.nr_rows == 0 {
  133. tbl.nr_rows += 1
  134. tbl.has_header_row = true
  135. }
  136. col := tbl.nr_cols
  137. tbl.nr_cols += len(values)
  138. for val in values {
  139. set_cell_value(tbl, header_row(tbl), col, val, loc)
  140. col += 1
  141. }
  142. }
  143. aligned_header_of_values :: proc(tbl: ^Table, alignment: Cell_Alignment, values: ..any, loc := #caller_location) {
  144. if (tbl.has_header_row && tbl.nr_rows != 1) || (!tbl.has_header_row && tbl.nr_rows != 0) {
  145. panic("Cannot add headers after rows have been added", loc)
  146. }
  147. if tbl.nr_rows == 0 {
  148. tbl.nr_rows += 1
  149. tbl.has_header_row = true
  150. }
  151. col := tbl.nr_cols
  152. tbl.nr_cols += len(values)
  153. for val in values {
  154. set_cell_value_and_alignment(tbl, header_row(tbl), col, val, alignment, loc)
  155. col += 1
  156. }
  157. }
  158. header_of_aligned_values :: proc(tbl: ^Table, aligned_values: []Aligned_Value, loc := #caller_location) {
  159. if (tbl.has_header_row && tbl.nr_rows != 1) || (!tbl.has_header_row && tbl.nr_rows != 0) {
  160. panic("Cannot add headers after rows have been added", loc)
  161. }
  162. if tbl.nr_rows == 0 {
  163. tbl.nr_rows += 1
  164. tbl.has_header_row = true
  165. }
  166. col := tbl.nr_cols
  167. tbl.nr_cols += len(aligned_values)
  168. for av in aligned_values {
  169. set_cell_value_and_alignment(tbl, header_row(tbl), col, av.value, av.alignment, loc)
  170. col += 1
  171. }
  172. }
  173. row :: row_of_values
  174. row_of_values :: proc(tbl: ^Table, values: ..any, loc := #caller_location) {
  175. if tbl.nr_cols == 0 {
  176. if len(values) == 0 {
  177. panic("Cannot create empty row unless the number of columns is known in advance")
  178. } else {
  179. tbl.nr_cols = len(values)
  180. }
  181. }
  182. tbl.nr_rows += 1
  183. for col in 0..<tbl.nr_cols {
  184. val := values[col] if col < len(values) else nil
  185. set_cell_value(tbl, last_row(tbl), col, val, loc)
  186. }
  187. }
  188. aligned_row_of_values :: proc(tbl: ^Table, alignment: Cell_Alignment, values: ..any, loc := #caller_location) {
  189. if tbl.nr_cols == 0 {
  190. if len(values) == 0 {
  191. panic("Cannot create empty row unless the number of columns is known in advance")
  192. } else {
  193. tbl.nr_cols = len(values)
  194. }
  195. }
  196. tbl.nr_rows += 1
  197. for col in 0..<tbl.nr_cols {
  198. val := values[col] if col < len(values) else nil
  199. set_cell_value_and_alignment(tbl, last_row(tbl), col, val, alignment, loc)
  200. }
  201. }
  202. row_of_aligned_values :: proc(tbl: ^Table, aligned_values: []Aligned_Value, loc := #caller_location) {
  203. if tbl.nr_cols == 0 {
  204. if len(aligned_values) == 0 {
  205. panic("Cannot create empty row unless the number of columns is known in advance")
  206. } else {
  207. tbl.nr_cols = len(aligned_values)
  208. }
  209. }
  210. tbl.nr_rows += 1
  211. for col in 0..<tbl.nr_cols {
  212. if col < len(aligned_values) {
  213. val := aligned_values[col].value
  214. alignment := aligned_values[col].alignment
  215. set_cell_value_and_alignment(tbl, last_row(tbl), col, val, alignment, loc)
  216. } else {
  217. set_cell_value_and_alignment(tbl, last_row(tbl), col, "", .Left, loc)
  218. }
  219. }
  220. }
  221. // TODO: This should work correctly when #3262 is fixed.
  222. // row :: proc {
  223. // row_of_values,
  224. // aligned_row_of_values,
  225. // row_of_aligned_values,
  226. // }
  227. last_row :: proc(tbl: ^Table) -> int {
  228. return tbl.nr_rows - 1
  229. }
  230. header_row :: proc(tbl: ^Table) -> int {
  231. return 0 if tbl.has_header_row else -1
  232. }
  233. first_row :: proc(tbl: ^Table) -> int {
  234. return header_row(tbl)+1 if tbl.has_header_row else 0
  235. }
  236. build :: proc(tbl: ^Table, width_proc: Width_Proc) {
  237. resize(&tbl.colw, tbl.nr_cols)
  238. mem.zero_slice(tbl.colw[:])
  239. for row in 0..<tbl.nr_rows {
  240. for col in 0..<tbl.nr_cols {
  241. cell := get_cell(tbl, row, col)
  242. cell.width = width_proc(cell.text)
  243. tbl.colw[col] = max(tbl.colw[col], cell.width)
  244. }
  245. }
  246. colw_sum := 0
  247. for v in tbl.colw {
  248. colw_sum += v + tbl.lpad + tbl.rpad
  249. }
  250. tbl.tblw = max(colw_sum, width_proc(tbl.caption) + tbl.lpad + tbl.rpad)
  251. // Resize columns to match total width of table
  252. remain := tbl.tblw-colw_sum
  253. for col := 0; remain > 0; col = (col + 1) % tbl.nr_cols {
  254. tbl.colw[col] += 1
  255. remain -= 1
  256. }
  257. return
  258. }
  259. write_html_table :: proc(w: io.Writer, tbl: ^Table) {
  260. io.write_string(w, "<table>\n")
  261. if tbl.caption != "" {
  262. io.write_string(w, "\t<caption>")
  263. io.write_string(w, tbl.caption)
  264. io.write_string(w, "</caption>\n")
  265. }
  266. align_attribute :: proc(cell: ^Cell) -> string {
  267. switch cell.alignment {
  268. case .Left: return ` align="left"`
  269. case .Center: return ` align="center"`
  270. case .Right: return ` align="right"`
  271. }
  272. unreachable()
  273. }
  274. if tbl.has_header_row {
  275. io.write_string(w, "\t<thead>\n")
  276. io.write_string(w, "\t\t<tr>\n")
  277. for col in 0..<tbl.nr_cols {
  278. cell := get_cell(tbl, header_row(tbl), col)
  279. io.write_string(w, "\t\t\t<th")
  280. io.write_string(w, align_attribute(cell))
  281. io.write_string(w, ">")
  282. io.write_string(w, cell.text)
  283. io.write_string(w, "</th>\n")
  284. }
  285. io.write_string(w, "\t\t</tr>\n")
  286. io.write_string(w, "\t</thead>\n")
  287. }
  288. io.write_string(w, "\t<tbody>\n")
  289. for row in 0..<tbl.nr_rows {
  290. if tbl.has_header_row && row == header_row(tbl) {
  291. continue
  292. }
  293. io.write_string(w, "\t\t<tr>\n")
  294. for col in 0..<tbl.nr_cols {
  295. cell := get_cell(tbl, row, col)
  296. io.write_string(w, "\t\t\t<td")
  297. io.write_string(w, align_attribute(cell))
  298. io.write_string(w, ">")
  299. io.write_string(w, cell.text)
  300. io.write_string(w, "</td>\n")
  301. }
  302. io.write_string(w, "\t\t</tr>\n")
  303. }
  304. io.write_string(w, "\t</tbody>\n")
  305. io.write_string(w, "</table>\n")
  306. }
  307. write_plain_table :: proc(w: io.Writer, tbl: ^Table, width_proc: Width_Proc = unicode_width_proc) {
  308. build(tbl, width_proc)
  309. write_caption_separator :: proc(w: io.Writer, tbl: ^Table) {
  310. io.write_byte(w, '+')
  311. write_byte_repeat(w, tbl.tblw + tbl.nr_cols - 1, '-')
  312. io.write_byte(w, '+')
  313. io.write_byte(w, '\n')
  314. }
  315. write_table_separator :: proc(w: io.Writer, tbl: ^Table) {
  316. for col in 0..<tbl.nr_cols {
  317. if col == 0 {
  318. io.write_byte(w, '+')
  319. }
  320. write_byte_repeat(w, tbl.colw[col] + tbl.lpad + tbl.rpad, '-')
  321. io.write_byte(w, '+')
  322. }
  323. io.write_byte(w, '\n')
  324. }
  325. if tbl.caption != "" {
  326. write_caption_separator(w, tbl)
  327. io.write_byte(w, '|')
  328. write_text_align(w, tbl.caption, .Center,
  329. tbl.lpad, tbl.rpad, tbl.tblw + tbl.nr_cols - 1 - width_proc(tbl.caption) - tbl.lpad - tbl.rpad)
  330. io.write_byte(w, '|')
  331. io.write_byte(w, '\n')
  332. }
  333. write_table_separator(w, tbl)
  334. for row in 0..<tbl.nr_rows {
  335. for col in 0..<tbl.nr_cols {
  336. if col == 0 {
  337. io.write_byte(w, '|')
  338. }
  339. write_table_cell(w, tbl, row, col)
  340. io.write_byte(w, '|')
  341. }
  342. io.write_byte(w, '\n')
  343. if tbl.has_header_row && row == header_row(tbl) {
  344. write_table_separator(w, tbl)
  345. }
  346. }
  347. write_table_separator(w, tbl)
  348. }
  349. write_decorated_table :: proc(w: io.Writer, tbl: ^Table, decorations: Decorations, width_proc: Width_Proc = unicode_width_proc) {
  350. draw_dividing_row :: proc(w: io.Writer, tbl: ^Table, left, horz, divider, right: string) {
  351. io.write_string(w, left)
  352. for col in 0..<tbl.nr_cols {
  353. for _ in 0..<tbl.colw[col] + tbl.lpad + tbl.rpad {
  354. io.write_string(w, horz)
  355. }
  356. if col < tbl.nr_cols-1 {
  357. io.write_string(w, divider)
  358. }
  359. }
  360. io.write_string(w, right)
  361. io.write_byte(w, '\n')
  362. }
  363. build(tbl, width_proc)
  364. // This determines whether or not to divide the top border.
  365. top_divider := decorations.n if len(tbl.caption) == 0 else decorations.horz
  366. // Draw the north border.
  367. draw_dividing_row(w, tbl, decorations.nw, decorations.horz, top_divider, decorations.ne)
  368. if len(tbl.caption) != 0 {
  369. // Draw the caption.
  370. io.write_string(w, decorations.vert)
  371. write_text_align(w, tbl.caption, .Center,
  372. tbl.lpad, tbl.rpad, tbl.tblw + tbl.nr_cols - 1 - width_proc(tbl.caption) - tbl.lpad - tbl.rpad)
  373. io.write_string(w, decorations.vert)
  374. io.write_byte(w, '\n')
  375. // Draw the divider between the caption and the table rows.
  376. draw_dividing_row(w, tbl, decorations.w, decorations.horz, decorations.n, decorations.e)
  377. }
  378. // Draw the header.
  379. start := 0
  380. if tbl.has_header_row {
  381. io.write_string(w, decorations.vert)
  382. row := header_row(tbl)
  383. for col in 0..<tbl.nr_cols {
  384. write_table_cell(w, tbl, row, col)
  385. io.write_string(w, decorations.vert)
  386. }
  387. io.write_byte(w, '\n')
  388. start += row + 1
  389. draw_dividing_row(w, tbl, decorations.w, decorations.horz, decorations.x, decorations.e)
  390. }
  391. // Draw the cells.
  392. for row in start..<tbl.nr_rows {
  393. for col in 0..<tbl.nr_cols {
  394. if col == 0 {
  395. io.write_string(w, decorations.vert)
  396. }
  397. write_table_cell(w, tbl, row, col)
  398. io.write_string(w, decorations.vert)
  399. }
  400. io.write_byte(w, '\n')
  401. }
  402. // Draw the south border.
  403. draw_dividing_row(w, tbl, decorations.sw, decorations.horz, decorations.s, decorations.se)
  404. }
  405. // Renders table according to GitHub Flavored Markdown (GFM) specification
  406. write_markdown_table :: proc(w: io.Writer, tbl: ^Table, width_proc: Width_Proc = unicode_width_proc) {
  407. // NOTE(oskar): Captions or colspans are not supported by GFM as far as I can tell.
  408. build(tbl, width_proc)
  409. write_row :: proc(w: io.Writer, tbl: ^Table, row: int, alignment: Cell_Alignment = .Left) {
  410. for col in 0..<tbl.nr_cols {
  411. cell := get_cell(tbl, row, col)
  412. if col == 0 {
  413. io.write_byte(w, '|')
  414. }
  415. write_text_align(w, cell.text, alignment, tbl.lpad, tbl.rpad, tbl.colw[col] - cell.width)
  416. io.write_string(w, "|")
  417. }
  418. io.write_byte(w, '\n')
  419. }
  420. start := 0
  421. if tbl.has_header_row {
  422. row := header_row(tbl)
  423. write_row(w, tbl, row, .Center)
  424. for col in 0..<tbl.nr_cols {
  425. cell := get_cell(tbl, row, col)
  426. if col == 0 {
  427. io.write_byte(w, '|')
  428. }
  429. divider_width := tbl.colw[col] + tbl.lpad + tbl.rpad - 1
  430. switch cell.alignment {
  431. case .Left:
  432. io.write_byte(w, ':')
  433. write_byte_repeat(w, max(1, divider_width), '-')
  434. case .Center:
  435. io.write_byte(w, ':')
  436. write_byte_repeat(w, max(1, divider_width - 1), '-')
  437. io.write_byte(w, ':')
  438. case .Right:
  439. write_byte_repeat(w, max(1, divider_width), '-')
  440. io.write_byte(w, ':')
  441. }
  442. io.write_byte(w, '|')
  443. }
  444. io.write_byte(w, '\n')
  445. start += row + 1
  446. }
  447. for row in start..<tbl.nr_rows {
  448. write_row(w, tbl, row)
  449. }
  450. }
  451. write_byte_repeat :: proc(w: io.Writer, n: int, b: byte) {
  452. for _ in 0..<n {
  453. io.write_byte(w, b)
  454. }
  455. }
  456. write_table_cell :: proc(w: io.Writer, tbl: ^Table, row, col: int) {
  457. cell := get_cell(tbl, row, col)
  458. write_text_align(w, cell.text, cell.alignment, tbl.lpad, tbl.rpad, tbl.colw[col] - cell.width)
  459. }
  460. write_text_align :: proc(w: io.Writer, text: string, alignment: Cell_Alignment, lpad, rpad, space: int) {
  461. write_byte_repeat(w, lpad, ' ')
  462. switch alignment {
  463. case .Left:
  464. io.write_string(w, text)
  465. write_byte_repeat(w, space, ' ')
  466. case .Center:
  467. write_byte_repeat(w, space/2, ' ')
  468. io.write_string(w, text)
  469. write_byte_repeat(w, space/2 + space & 1, ' ')
  470. case .Right:
  471. write_byte_repeat(w, space, ' ')
  472. io.write_string(w, text)
  473. }
  474. write_byte_repeat(w, rpad, ' ')
  475. }