doc.odin 2.8 KB

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