doc.odin 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. package table implements ascii/markdown/html/custom rendering of tables.
  3. ---
  4. Custom rendering example:
  5. ```odin
  6. tbl := init(&Table{})
  7. padding(tbl, 0, 1)
  8. row(tbl, "A_LONG_ENUM", "= 54,", "// A comment about A_LONG_ENUM")
  9. row(tbl, "AN_EVEN_LONGER_ENUM", "= 1,", "// A comment about AN_EVEN_LONGER_ENUM")
  10. build(tbl)
  11. for row in 0..<tbl.nr_rows {
  12. for col in 0..<tbl.nr_cols {
  13. write_table_cell(stdio_writer(), tbl, row, col)
  14. }
  15. io.write_byte(stdio_writer(), '\n')
  16. }
  17. ```
  18. This outputs:
  19. ```
  20. A_LONG_ENUM = 54, // A comment about A_LONG_ENUM
  21. AN_EVEN_LONGER_ENUM = 1, // A comment about AN_EVEN_LONGER_ENUM
  22. ```
  23. ---
  24. ASCII rendering example:
  25. ```odin
  26. tbl := init(&Table{})
  27. defer destroy(tbl)
  28. caption(tbl, "This is a table caption and it is very long")
  29. padding(tbl, 1, 1) // Left/right padding of cells
  30. header(tbl, "AAAAAAAAA", "B")
  31. header(tbl, "C") // Appends to previous header row. Same as if done header("AAAAAAAAA", "B", "C") from start.
  32. // Create a row with two values. Since there are three columns the third
  33. // value will become the empty string.
  34. //
  35. // NOTE: header() is not allowed anymore after this.
  36. row(tbl, 123, "foo")
  37. // Use `format()` if you need custom formatting. This will allocate into
  38. // the arena specified at init.
  39. row(tbl,
  40. format(tbl, "%09d", 5),
  41. format(tbl, "%.6f", 6.28318530717958647692528676655900576))
  42. // A row with zero values is allowed as long as a previous row or header
  43. // exist. The value and alignment of each cell can then be set
  44. // individually.
  45. row(tbl)
  46. set_cell_value_and_alignment(tbl, last_row(tbl), 0, "a", .Center)
  47. set_cell_value(tbl, last_row(tbl), 1, "bbb")
  48. set_cell_value(tbl, last_row(tbl), 2, "c")
  49. // Headers are regular cells, too. Use header_row() as row index to modify
  50. // header cells.
  51. set_cell_alignment(tbl, header_row(tbl), 1, .Center) // Sets alignment of 'B' column to Center.
  52. set_cell_alignment(tbl, header_row(tbl), 2, .Right) // Sets alignment of 'C' column to Right.
  53. build(tbl)
  54. write_ascii_table(stdio_writer(), tbl)
  55. write_markdown_table(stdio_writer(), tbl)
  56. ```
  57. This outputs:
  58. ```
  59. +-----------------------------------------------+
  60. | This is a table caption and it is very long |
  61. +------------------+-----------------+----------+
  62. | AAAAAAAAA | B | C |
  63. +------------------+-----------------+----------+
  64. | 123 | foo | |
  65. | 000000005 | 6.283185 | |
  66. | a | bbb | c |
  67. +------------------+-----------------+----------+
  68. ```
  69. and
  70. ```
  71. | AAAAAAAAA | B | C |
  72. |:-----------------|:---------------:|---------:|
  73. | 123 | foo | |
  74. | 000000005 | 6.283185 | |
  75. | a | bbb | c |
  76. ```
  77. respectively.
  78. */
  79. package text_table