seleznevae / libfort

C/C++ library to create formatted ASCII tables for console applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Built-in style for RST (reStructuredText Markup) table

pkbehera opened this issue · comments

+------------------------+------------+----------+----------+
| Header row, column 1   | Header 2   | Header 3 | Header 4 |
| (header rows optional) |            |          |          |
+========================+============+==========+==========+
| body row 1, column 1   | column 2   | column 3 | column 4 |
+------------------------+------------+----------+----------+
| body row 2             | ...        | ...      |          |
+------------------------+------------+----------+----------+

an RST table looks like this, see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#grid-tables

Do we have a build-in table style in libfort that gives result like above? If not can we add one (please name it RST_STYLE)

Hi!
At the moment it is not possible. I tried to add a new style but it produces a correct ReST only for a one row header like:

+------+--------------------------+------+--------+
| Rank | Title                    | Year | Rating |
+======+==========================+======+========+
|  1   | The Shawshank Redemption | 1994 |    9.5 |
+------+--------------------------+------+--------+
|  2   | 12 Angry Men             | 1957 |    8.8 |
+------+--------------------------+------+--------+

For headers with multiple rows it shows incorrect result:

+------+--------------------------+------+--------+
| Rank | Title                    | Year | Rating |
+======+==========================+======+========+
| Rank | Title                           | Rating |
+======+==========================+======+========+
| Rank | Title                    | Year | Rating |
+======+==========================+======+========+
|  1   | The Shawshank Redemption |          1994 |
+------+--------------------------+------+--------+
|  2   | 12 Angry Men                    |    8.8 |
+------+--------------------------+------+--------+

I believe that to implement it properly it is needed to refactor how styles are defined and maybe add an additional symbol to styles. It will take some time to implement it properly.

Maybe I'll add the current version that works ok for one row header and later take some time to improve it in another ticket. Not sure yet.

static constexpr struct ft_border_style RST_STYLE {
  {
      "-",  // top_border_ch
      "-",  // separator_ch
      "-",  // bottom_border_ch
      "|",  // side_border_ch
      "+",  // out_intersect_ch
      "+",  // in_intersect_ch
  },        // border_chs
      {
          "-",  // top_border_ch
          "=",  // separator_ch (for Sphinx to know that it's the table header)
          "-",  // bottom_border_ch
          "|",  // side_border_ch
          "+",  // out_intersect_ch
          "+",  // in_intersect_ch
      },        // header_border_chs
      "-"       // hor_separator_char
};

fort::table table;
table.set_border_style(&RST_STYLE);

My table header has only one row and the above did work for me. Can this be any better/shorter?

I did pretty much the same. It should be ok for a one-row header. Without an appropriate built-in style I think it is the simplest way to get what you want at the moment.

can you add a built-in style that works for one-row headers? We can extend that later for multi-row headers