Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pkbehera opened this issue Nov 12, 2019 · 6 comments
Labels
enhancement New feature or request

Comments

@pkbehera
Copy link

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

@seleznevae
Copy link
Owner

seleznevae commented Nov 12, 2019

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.

@seleznevae seleznevae added the enhancement New feature or request label Nov 12, 2019
@pkbehera
Copy link
Author

pkbehera commented Nov 12, 2019

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 |
+--------------------+------------+----------+-----------+----------------------------------------------------+

@pkbehera pkbehera changed the title Is there a way to set max width of a column and/or wrap/crop the test accordingly Is there a way to set max width of a column and/or wrap/crop the text accordingly Nov 12, 2019
@rshamsnejad
Copy link

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 ?

@pkbehera
Copy link
Author

pkbehera commented Dec 21, 2019

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

@seleznevae
Copy link
Owner

seleznevae commented Dec 23, 2019

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.

@mariuszbarczak
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants