25 # include <sys/ioctl.h> 44 CONSOLE_SCREEN_BUFFER_INFO csbi;
45 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
46 columns = csbi.dwSize.X;
47 lines = csbi.dwSize.Y;
52 if (_dupenv_s(&COLUMNS, &sz,
"COLUMNS") == 0 && COLUMNS) {
53 columns = atoi(COLUMNS);
60 if (_dupenv_s(&LINES, &sz,
"LINES") == 0 && LINES) {
61 columns = atoi(LINES);
67 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) {
72 const char* COLUMNS = getenv(
"COLUMNS");
73 if (COLUMNS) columns = atoi(COLUMNS);
76 const char* LINES = getenv(
"LINES");
77 if (LINES) columns = atoi(LINES);
105 assert(indent + offset < width);
107 int text_length =
static_cast<int>(text.length());
108 int allowed_length = width - indent;
117 while (start < text_length) {
119 int line_length = min<int>(text_length - start, allowed_length);
120 if (line_length == allowed_length) {
121 while (line_length >= 0 &&
122 text[start + line_length] !=
' ' &&
123 text[start + line_length] !=
',' &&
124 text[start + line_length] !=
'|' ) {
128 if (line_length <= 0) line_length = allowed_length;
130 for (
int i = 0; i < line_length; i++) {
131 if (text[start + i] ==
'\n') line_length = i + 1;
134 for (
int i = 0; i < indent; i++ ) os <<
" ";
135 os << text.substr(start, line_length) << endl;
139 allowed_length -= offset;
142 start += line_length;
145 while (text[start] ==
' ' && start < text_length) start++;
void get_terminal_size(int &lines, int &columns)
Get size of terminal window.
#define assert(condition)
Assertion without custom message.
System related macro definitions.
Defines macros used for assertions.
int get_terminal_lines()
Get maximum number of lines of terminal window.
std::ostream & print_wrapped(std::ostream &os, const std::string &text, int width, int indent, int offset)
Print text, wrapped at a fixed maximum number of columns.
int get_terminal_columns()
Get maximum number of columns of terminal window.