|
| 1 | +(* @mdexp |
| 2 | +
|
| 3 | +# print-table |
| 4 | +
|
| 5 | +[](https://github.com/mbarbin/print-table/actions/workflows/ci.yml) |
| 6 | +[](https://coveralls.io/github/mbarbin/print-table?branch=main) |
| 7 | +[](https://ocaml.ci.dev/github/mbarbin/print-table) |
| 8 | +
|
| 9 | +Print_table provides a minimal library for rendering text tables with Unicode |
| 10 | +box-drawing characters and optional ANSI colors: |
| 11 | +*) |
| 12 | + |
| 13 | +(* @mdexp.code *) |
| 14 | +type row = string * int |
| 15 | + |
| 16 | +let columns : row Print_table.Column.t list = |
| 17 | + Print_table.O. |
| 18 | + [ Column.make ~header:"Name" (fun (name, _) -> Cell.text name) |
| 19 | + ; Column.make ~header:"Score" ~align:Right (fun (_, score) -> |
| 20 | + Cell.text (Int.to_string score)) |
| 21 | + ] |
| 22 | +;; |
| 23 | + |
| 24 | +let rows : row list = [ "Alice", 10; "Bob", 3 ] |
| 25 | +let table : Print_table.t = Print_table.make ~columns ~rows |
| 26 | + |
| 27 | +let%expect_test "to_string_text" = |
| 28 | + print_endline (Print_table.to_string_text table); |
| 29 | + [%expect |
| 30 | + {| |
| 31 | + ┌───────┬───────┐ |
| 32 | + │ Name │ Score │ |
| 33 | + ├───────┼───────┤ |
| 34 | + │ Alice │ 10 │ |
| 35 | + │ Bob │ 3 │ |
| 36 | + └───────┴───────┘ |
| 37 | + |}] |
| 38 | +;; |
| 39 | + |
| 40 | +(* @mdexp |
| 41 | +
|
| 42 | + Tables can be printed as Github-flavored Markdown: *) |
| 43 | + |
| 44 | +(* @mdexp.code *) |
| 45 | +let%expect_test "to_string_markdown" = |
| 46 | + print_endline (Print_table.to_string_markdown table); |
| 47 | + [%expect |
| 48 | + {| |
| 49 | + | Name | Score | |
| 50 | + |:------|------:| |
| 51 | + | Alice | 10 | |
| 52 | + | Bob | 3 | |
| 53 | + |}] |
| 54 | +;; |
| 55 | + |
| 56 | +(* @mdexp |
| 57 | +
|
| 58 | + which is rendered natively by GitHub like this: |
| 59 | +
|
| 60 | + @mdexp.end *) |
| 61 | +let%expect_test "snapshot" = |
| 62 | + print_endline (Print_table.to_string_markdown table); |
| 63 | + (* @mdexp.snapshot *) |
| 64 | + [%expect |
| 65 | + {| |
| 66 | + | Name | Score | |
| 67 | + |:------|------:| |
| 68 | + | Alice | 10 | |
| 69 | + | Bob | 3 | |
| 70 | + |}]; |
| 71 | + () |
| 72 | +;; |
| 73 | + |
| 74 | +(* @mdexp |
| 75 | +
|
| 76 | +## Code Documentation |
| 77 | +
|
| 78 | +The code documentation of the latest release is built with `odoc` and published to `GitHub` pages [here](https://mbarbin.github.io/print-table). |
| 79 | +
|
| 80 | +## Acknowledgments |
| 81 | +
|
| 82 | +This library has taken some inspiration from 2 existing and more feature-complete libraries, which we link to here for more advanced usages. |
| 83 | +
|
| 84 | +- [Printbox](https://github.com/c-cube/printbox) |
| 85 | +- [Ascii_table](https://github.com/janestreet/textutils) |
| 86 | +*) |
0 commit comments