I have a function which prints some text to an ostream& it receives. I would like to make it adapt to the terminal width, if the ostream targets a terminal, and otherwise default to some value.
What I do right now is the following:
- Get an
ofstream
from theostream
. - Get a
FILE*
from theofstream
. - Get an integer file descriptor from the
FILE*
. - Do
ioctl(file_descriptor, TIOCGWINSZ, &my_struct_winsize);
with steps 1 and 2 bring due to this SO answer:
Getting a FILE* from a std::fstream
but they are non-portable and GCC-specific (and I'm not sure they'll work for ostreams other than cout/cerr/cin). Can I do better than this?