Skip to content

Instantly share code, notes, and snippets.

@YannickFricke
Last active October 23, 2023 19:15
Show Gist options
  • Save YannickFricke/7973218925731310441f10d9bd4a7d35 to your computer and use it in GitHub Desktop.
Save YannickFricke/7973218925731310441f10d9bd4a7d35 to your computer and use it in GitHub Desktop.
A cross-plattform solution for getting the console columns and rows in C++
#ifdef WIN32
#include <windows.h>
#else
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#endif
class Console {
public:
int columns = 0;
int rows = 0;
Console() {}
void fetchConsoleDimensions() {
#ifdef WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
this->columns = columns;
this->rows = rows;
#else
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
this->rows = w.ws_row;
this->columns = w.ws_col;
#endif
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment