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

Printing to stringstream and retaining formatting #116

Open
mike-5345 opened this issue Apr 2, 2024 · 2 comments
Open

Printing to stringstream and retaining formatting #116

mike-5345 opened this issue Apr 2, 2024 · 2 comments

Comments

@mike-5345
Copy link

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

@p-ranav
Copy link
Owner

p-ranav commented Apr 2, 2024

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);

@suitmyself
Copy link

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

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

3 participants