ClickHouse/src/Common/TerminalSize.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

44 lines
1.4 KiB
C++
Raw Normal View History

2019-08-22 14:03:37 +00:00
#include <unistd.h>
#include <sys/ioctl.h>
#if defined(OS_SUNOS)
# include <sys/termios.h>
#endif
2019-08-22 14:03:37 +00:00
#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
{
2021-05-05 10:37:10 +00:00
struct winsize terminal_size {};
2019-08-22 14:03:37 +00:00
if (isatty(STDIN_FILENO))
{
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &terminal_size))
2023-12-15 18:25:49 +00:00
throw DB::ErrnoException(DB::ErrorCodes::SYSTEM_ERROR, "Cannot obtain terminal window size (ioctl TIOCGWINSZ)");
2019-08-23 13:19:12 +00:00
}
2021-05-05 15:52:03 +00:00
else if (isatty(STDERR_FILENO))
2021-05-05 10:37:10 +00:00
{
2021-05-05 15:52:03 +00:00
if (ioctl(STDERR_FILENO, TIOCGWINSZ, &terminal_size))
2023-12-15 18:25:49 +00:00
throw DB::ErrnoException(DB::ErrorCodes::SYSTEM_ERROR, "Cannot obtain terminal window size (ioctl TIOCGWINSZ)");
2021-05-05 10:37:10 +00:00
}
/// Default - 0.
return terminal_size.ws_col;
2019-08-23 13:19:12 +00:00
}
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
}