Windows 通过WINAPI;GetConsoleScreenBufferInfo 实现
Linux 通过CAPI:ioctl(...,?TIOCGWINSZ...) 实现
bool GetConsoleWindowSize(int& x, int& y) noexcept {
x = 0;
y = 0;
#ifdef _WIN32
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if (NULL == hConsole) {
return false;
}
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!::GetConsoleScreenBufferInfo(hConsole, &csbi)) {
return false;
}
y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
x = csbi.srWindow.Right - csbi.srWindow.Left + 1;
#else
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
return false;
}
x = w.ws_row;
y = w.ws_col;
#endif
return true;
}