1 | #include <unistd.h> |
---|---|
2 | #include <sys/ioctl.h> |
3 | #include <Common/Exception.h> |
4 | #include <Common/TerminalSize.h> |
5 | #include <boost/program_options.hpp> |
6 | |
7 | |
8 | namespace DB::ErrorCodes |
9 | { |
10 | extern const int SYSTEM_ERROR; |
11 | } |
12 | |
13 | uint16_t getTerminalWidth() |
14 | { |
15 | if (isatty(STDIN_FILENO)) |
16 | { |
17 | winsize terminal_size {}; |
18 | |
19 | if (ioctl(STDIN_FILENO, TIOCGWINSZ, &terminal_size)) |
20 | DB::throwFromErrno("Cannot obtain terminal window size (ioctl TIOCGWINSZ)", DB::ErrorCodes::SYSTEM_ERROR); |
21 | |
22 | return terminal_size.ws_col; |
23 | } |
24 | return 0; |
25 | } |
26 | |
27 | po::options_description createOptionsDescription(const std::string & caption, uint16_t terminal_width) |
28 | { |
29 | unsigned line_length = po::options_description::m_default_line_length; |
30 | unsigned min_description_length = line_length / 2; |
31 | std::string longest_option_desc = "--http_native_compression_disable_checksumming_on_decompress"; |
32 | |
33 | line_length = std::max(static_cast<uint16_t>(longest_option_desc.size()), terminal_width); |
34 | min_description_length = std::min(min_description_length, line_length - 2); |
35 | |
36 | return po::options_description(caption, line_length, min_description_length); |
37 | } |
38 |