ClickHouse/dbms/Common/TerminalSize.cpp

38 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-26 16:42:20 +00:00
#include <boost/program_options.hpp>
2019-08-22 14:03:37 +00:00
namespace DB::ErrorCodes
{
extern const int SYSTEM_ERROR;
}
2019-08-26 12:47:19 +00:00
uint16_t 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-26 13:27:12 +00:00
po::options_description createOptionsDescription(const std::string & caption, uint16_t 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-26 12:47:19 +00:00
line_length = std::max(static_cast<uint16_t>(longest_option_desc.size()), terminal_width);
2019-08-23 13:19:12 +00:00
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
}