tabulate icon indicating copy to clipboard operation
tabulate copied to clipboard

Printing to stringstream and retaining formatting

Open mike-5345 opened this issue 1 year ago • 3 comments

Hello,

I want to be able to print a table to a stringstream but when I do this the special formatting (colour, boldness, etc.) is not retained.

I want to be able to pass around the table as a string in memory. Is there a way to do this?

Thanks, Mike

mike-5345 avatar Apr 02 '24 22:04 mike-5345

Could you redirect std::cout to a buffer? I'm not sure if this works but it's worth a try.

std::stringstream buffer;
std::streambuf* prevcoutbuf = std::cout.rdbuf(buffer.rdbuf());

// print your table
std::cout << yourTable << std::endl;

// Get the table as a string
std::string text = buffer.str();

// Restore original buffer before exiting
std::cout.rdbuf(prevcoutbuf);

p-ranav avatar Apr 02 '24 23:04 p-ranav

Could you redirect std::cout to a buffer? I'm not sure if this works but it's worth a try.

std::stringstream buffer;
std::streambuf* prevcoutbuf = std::cout.rdbuf(buffer.rdbuf());

// print your table
std::cout << yourTable << std::endl;

// Get the table as a string
std::string text = buffer.str();

// Restore original buffer before exiting
std::cout.rdbuf(prevcoutbuf);

not work

suitmyself avatar Oct 15 '24 12:10 suitmyself