p-ranav / tabulate

Table Maker for Modern C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possible to align the content of row vertically?

pramodk opened this issue · comments

Thanks for the nice library !

I am trying to print following table:

→ ./samples/mysample
+----+----------+---------+--------------+-------------+--------------------------+
| Id | Mod File |   Type  |    Kernel    |     Ops     |         Backends         |
|    |          |         |              |             |    SSE2  AVX2  AVX-512   |
+----+----------+---------+--------------+-------------+--------------------------+
|  1 |  hh.mod  | channel | my_fun.      |  write: 11  |      11  22.2  15.55     |
|    |          |         |              |   read: 5   |                          |
|    |          |         |              |    exp: 9   |                          |
|    |          |         |              |    div: 2   |                          |
|    |          |         |              |    +-x: 4   |                          |
+----+----------+---------+--------------+-------------+--------------------------+

What I would like to do is vertically align the each row i.e. as each record is taking 5 rows (due to Ops column), I would like to make rest of the column data in the middle.

Is this possible?

Hello,

The library does not directly support vertical align in the .format() API (which now seems like an oversight).

That said, this can be achieved in user code like so:

#include "tabulate.hpp"
#include <algorithm>
using namespace tabulate;

int main() {
  Table t;
  t.add_row({"Id",
             "Mod File",
             "Type",
             "Kernel",
             "Ops",
             "Backends\n SSE2  AVX2  AVX-512"});
  t.add_row({"1",
             "hh.mod",
             "channel",
             "my_fun.",
             "write: 11\nread: 5\nexp: 9\ndiv: 2\n+-x: 4", "11  22.2  15.55"});

  for (auto& row: t) {

    // Count number of newlines in ops cell
    const auto& ops_text = row[4].get_text();
    const auto height = std::count(ops_text.begin(), ops_text.end(), '\n');

    // All other cells need to be adjusted by half
    // the number of newlines in the ops cell
    for (const auto c: {0, 1, 2, 3, 5}) {
      row[c].set_text(std::string(height / 2, '\n') + row[c].get_text());
      row[c].format().font_align(FontAlign::center);
    }
  }

  t.row(0).format()
    .font_align(FontAlign::center);
  t.column(4).format()
    .font_align(FontAlign::center);

  std::cout << t << "\n";
}
→ ./samples/mysample                         
+----+----------+---------+---------+-----------+----------------------+
| Id | Mod File |   Type  |  Kernel |    Ops    |       Backends       |
|    |          |         |         |           |  SSE2  AVX2  AVX-512 |
+----+----------+---------+---------+-----------+----------------------+
|    |          |         |         | write: 11 |                      |
|    |          |         |         |  read: 5  |                      |
|  1 |  hh.mod  | channel | my_fun. |   exp: 9  |    11  22.2  15.55   |
|    |          |         |         |   div: 2  |                      |
|    |          |         |         |   +-x: 4  |                      |
+----+----------+---------+---------+-----------+----------------------+

The above code is adding newline characters to the cell text where needed - See the word wrapping part of the README for more information.

Hope this helps. Thanks for your request.

Hello @p-ranav !

forgot to respond here. That’s what I was looking for!

thanks!