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

Color is not working in nested tables #63

Open
maximiliank opened this issue Mar 4, 2021 · 8 comments
Open

Color is not working in nested tables #63

maximiliank opened this issue Mar 4, 2021 · 8 comments

Comments

@maximiliank
Copy link

When I try to put a Table with a colored cell into another Table the color is not working anymore.

Here is an example:

#include "tabulate/tabulate.hpp"
#include <iostream>

int main()
{
    using namespace tabulate;

    Table inner;
    inner.add_row({"A"});
    inner[0][0].format().font_color(Color::green);
    std::cout << "Here 'A' is green:\n" << inner << std::endl;

    Table outer;
    outer.add_row({inner});
    std::cout << "Here 'A' is not green:\n" << outer << std::endl;
    return 0;
}

which gives the following output on Ubuntu:

Here 'A' is green:
+---+
| A |
+---+
Here 'A' is not green:
+-------+
| +---+ |
| | A | |
| +---+ |
+-------+
@p-ranav
Copy link
Owner

p-ranav commented Mar 4, 2021

Yeah this is a known issue and it has to do with std::stringstream not retaining the termcolor colors.

Essentially, how nested tables are implemented are as follows:

  • The inner table is converted to string (using std::stringstream as the stream, instead of std::cout)
    • Relevant source here.
  • The outer table cell value is now the inner table string, serialized in the previous step
  • The outer table is then fully printed to std::cout

If the inner table colors were preserved in the first step, this would work as we want. Unfortunately, this is not the case.

Nested table support was added as sort of an after-thought/bonus feature. I may have to just implement printing of nested tables with iteration (starting from the outer table and going in), which is non-ideal.

@maximiliank
Copy link
Author

Thank you for your reply.

I tried to add << termcolor::colorize, see ikalnytskyi/termcolor#20, to the inline void Printer::print_table(std::ostream &stream, TableInternal &table) function. The color works, but the right borders are messed up.
Do you think it could work this way?

@tqolf
Copy link

tqolf commented Oct 20, 2021

ansi escape sequence should be considered in get_sequence_length to support colorful nested table. I have implemented by myself https://github.com/vxfury/tabulate. and with true color supported, too.

@tqolf
Copy link

tqolf commented Oct 20, 2021

ansi escape sequence should be considered in get_sequence_length to support colorful nested table. I have implemented by myself https://github.com/vxfury/tabulate. and with true color supported, too.

截屏2021-10-20 11 58 22

@p-ranav
Copy link
Owner

p-ranav commented Oct 20, 2021

A pull request would be greatly appreciated :)

@791136190
Copy link

Is there a support plan for this feature?

@edisonhello
Copy link
Contributor

An easy bypass of this issue is to convert the table to string manually with termcolor::colorize, enable the multibyte char, and then limit the width manually.

@sandmeteor
Copy link

sandmeteor commented Jul 5, 2023

@edisonhello can you give code for this 'bypass' ?
@vxfury link https://github.com/vxfury/tabulate is not available - is it a chance to get this piece of code?

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

No branches or pull requests

6 participants