Skip to content

Commit 2d783c1

Browse files
get help width from tty size
1 parent 8c5d0c5 commit 2d783c1

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/main.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
#include <filesystem>
1313
#include <iostream>
1414
#include <memory>
15+
#include <sys/ioctl.h>
1516
#include <sysexits.h>
1617

17-
//! Help output line width
18-
static constexpr std::size_t HELP_WIDTH = 120;
19-
2018
int main(int argc, char **argv) {
2119
const std::string exe_name = std::filesystem::path(*argv).filename().string();
2220
cxxopts::Options options(exe_name, "Writes the content of stdin to a named shared memory.");
@@ -61,7 +59,15 @@ int main(int argc, char **argv) {
6159

6260
// print usage
6361
if (args.count("help")) {
64-
options.set_width(HELP_WIDTH);
62+
static constexpr std::size_t MIN_HELP_SIZE = 80;
63+
if (isatty(STDIN_FILENO)) {
64+
struct winsize w {};
65+
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) { // NOLINT
66+
options.set_width(std::max(static_cast<decltype(w.ws_col)>(MIN_HELP_SIZE), w.ws_col));
67+
}
68+
} else {
69+
options.set_width(MIN_HELP_SIZE);
70+
}
6571
std::cout << options.help() << '\n';
6672
std::cout << "This application uses the following libraries:" << '\n';
6773
std::cout << " - cxxopts by jarro2783 (https://github.com/jarro2783/cxxopts)" << '\n';

0 commit comments

Comments
 (0)