seleznevae / libfort

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to set max width of a column and/or wrap/crop the text accordingly

pkbehera opened this issue · comments

I saw set_cell_min_width API to set minimum width but there is nothing to set maximum width

Hi! No at the moment that is not possible.
I had some thoughts about adding it some time ago. As far as I remember it is doable but there was a problem that I wasn't able to decide what to do with the line in case it is too big. So I decided to postpone it until somebody needs it.
If you want you can write here how you see it should work in case string is too big. It will be great to get some feedback from possible user's point of view.

I tried the following code and the result was as expected. So I guess you can just put new line chars at the required max_width, blindly to start with without caring for word boundaries.

#include <iostream>
#include "src/fort.hpp"
int main() {
    fort::char_table table;
    table << fort::header << "N"
          << "Driver"
          << "Time"
          << "Avg Speed"
          << "Note" << fort::endr << "1"
          << "Ricciardo"
          << "1:25.945"
          << "47.362"
          << "XXXXXXXXXXXX\nXXXXXXXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXX\nXXXXXXXXXXXXXXXX"
             "XXXXXXXX"
             "XXXXXXX\nXXXXXXXXXXXXXXXXXXXXXXXXX"
          << fort::endr << "2"
          << "Hamilton"
          << "1:26.373"
          << "35.02"
          << "XXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXXXXXXXXXXXXX\nXXXXXXXXXXXX"
             "XXXXXX"
             "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
          << fort::endr;

    table[0][0].set_cell_min_width(20);
    // table[0][0].set_cell_max_width(40);
    std::cout << table.to_string() << std::endl;
}
+--------------------+------------+----------+-----------+----------------------------------------------------+
| N                  | Driver     | Time     | Avg Speed | Note                                               |
+--------------------+------------+----------+-----------+----------------------------------------------------+
| 1                  | Ricciardo  | 1:25.945 | 47.362    | XXXXXXXXXXXX                                       |
|                    |            |          |           | XXXXXXXXXXXXXXXXXXXXXXX                            |
|                    |            |          |           | XXXXXXXXXXXXXXXXXXXXXXXX                           |
|                    |            |          |           | XXXXXXXXXXXX                                       |
|                    |            |          |           | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                    |
|                    |            |          |           | XXXXXXXXXXXXXXXXXXXXXXXXX                          |
| 2                  | Hamilton   | 1:26.373 | 35.02     | XXXXXXXXXXXXXX                                     |
|                    |            |          |           | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX           |
|                    |            |          |           | XXXXXXXXXXXXXXXXXXXXXXX                            |
|                    |            |          |           | XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
+--------------------+------------+----------+-----------+----------------------------------------------------+

Hi! No at the moment that is not possible.
I had some thoughts about adding it some time ago. As far as I remember it is doable but there was a problem that I wasn't able to decide what to do with the line in case it is too big. So I decided to postpone it until somebody needs it.
If you want you can write here how you see it should work in case string is too big. It will be great to get some feedback from possible user's point of view.

Hi, I am also interested in such a feature. In my opinion I would like the text to wrap in case it's too long, but maybe it could be settable with a flag or something ?

Hi! No at the moment that is not possible.
I had some thoughts about adding it some time ago. As far as I remember it is doable but there was a problem that I wasn't able to decide what to do with the line in case it is too big. So I decided to postpone it until somebody needs it.
If you want you can write here how you see it should work in case string is too big. It will be great to get some feedback from possible user's point of view.

Hi, I am also interested in such a feature. In my opinion I would like the text to wrap in case it's too long, but maybe it could be settable with a flag or something ?

yes crop/wrap should be configurable. This can be used to wrap the text near word boundaries:

https://www.rosettacode.org/wiki/Word_wrap#C.2B.2B

Great. Thanks for the link. I think it will help a lot at the end.
I'm working on the task at the moment. It turned out to be more complex than I thought at the beginning. I've implemented a simplified version. I works ok. But I it doesn't scale well with different strategies of limiting width of the column and with different custom user defined functions to estimate length of UTF-8 strings. So I need to rethink the implementation.

Hi Guys, a pretty old thread :D
Waiting for this feature. I'd like to read console window width and then adjust table width (columns width) to avoid breaking of the table in the narrow window.