ClickHouse/dbms/src/Common/TerminalSize.cpp

39 lines
1.1 KiB
C++
Raw Normal View History

2019-08-22 14:03:37 +00:00
#include <unistd.h>
#include <sys/ioctl.h>
#include <Common/Exception.h>
2019-08-23 15:47:27 +00:00
#include <Common/TerminalSize.h>
2019-08-22 14:03:37 +00:00
namespace po = boost::program_options;
namespace DB::ErrorCodes
{
extern const int SYSTEM_ERROR;
}
2019-08-23 13:19:12 +00:00
unsigned short int getTerminalWidth()
2019-08-22 14:03:37 +00:00
{
if (isatty(STDIN_FILENO))
{
winsize terminal_size {};
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &terminal_size))
DB::throwFromErrno("Cannot obtain terminal window size (ioctl TIOCGWINSZ)", DB::ErrorCodes::SYSTEM_ERROR);
2019-08-23 13:19:12 +00:00
return terminal_size.ws_col;
}
return 0;
}
2019-08-22 14:03:37 +00:00
2019-08-23 15:47:27 +00:00
po::options_description createOptionsDescription(const std::string &caption, unsigned short terminal_width)
2019-08-23 13:19:12 +00:00
{
unsigned line_length = po::options_description::m_default_line_length;
unsigned min_description_length = line_length / 2;
std::string longest_option_desc = "--http_native_compression_disable_checksumming_on_decompress";
2019-08-22 14:03:37 +00:00
2019-08-23 13:19:12 +00:00
line_length = std::max(static_cast<unsigned short>(longest_option_desc.size()), terminal_width);
min_description_length = std::min(min_description_length, line_length - 2);
2019-08-22 14:03:37 +00:00
return po::options_description(caption, line_length, min_description_length);
2019-08-23 13:25:46 +00:00
}