catchorg / Clara

A simple to use, composable, command line parser for C++ 11 and beyond
Boost Software License 1.0
649 stars 67 forks source link

Respect COLUMNS environment variable #7

Open vadz opened 8 years ago

vadz commented 8 years ago

Determining the real terminal width is hard to do portably, but it's pretty simple to see if the COLUMNS env var is defined and use it instead of the hard coded 80 if it is.

I could make a PR if this is considered to be a good idea.

philsquared commented 8 years ago

Sounds like a useful tweak - yes please :-)

parnmatt commented 6 years ago

:+1:

parnmatt commented 5 years ago

As COLUMNS isn't always an environment variable (for example on mksh, you can access it as $COLUMNS, but it isn't stored in the environment); it would be best to fall back on more native approaches.

order of precedence: COLUMNS, native, 80

for example if a native solution isn't detected.

For linux (tested):

#include <sys/ioctl.h>
#include <unistd.h>

// ...

winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
CLARA_CONFIG_CONSOLE_WIDTH = w.ws.col;

For windows (untested)

#include <windows.h>

// ...

CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
CLARA_CONFIG_CONSOLE_WIDTH = csbi.srWindow.Right - csbi.srWindow.Left + 1;

of course this requires CLARA_CONFIG_CONSOLE_WIDTH to not be a #define but a variable. This code will likely have to be wrapped up in some function. Hell; it would be best if a variable wasn't used at all, and just a function was used, as terminal size can change. Though it's unlikely to effect the output of help, as it is usually only done once.

Might wish to fold this into #14