Context: I am writing a small library to output ansi formatted media to stdout (similar to ncurses). What i want to do is to remove the scrollbar of the terminal window independently of the plattform the code is compiled on (Win / MacOS / Unix). Heres a little example of what the window should look like (this was done with ncurses): example of scrollbar missing
To remove the scrollbar on Windows with c++, you simply set the screen buffer height to be the same size as the height of the window like so:
#include <Windows.h>
#include <iostream>
int main() {
CONSOLE_SCREEN_BUFFER_INFO screenBufferInfo;
// Get console handle and get screen buffer information from that handle.
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &screenBufferInfo);
// Get rid of the scrollbar by setting the screen buffer size the same as
// the console window size.
COORD new_screen_buffer_size;
// screenBufferInfo.srWindow allows us to obtain the width and height info
// of the visible console in character cells.
// That visible portion is what we want to set the screen buffer to, so that
// no scroll bars are needed to view the entire buffer.
new_screen_buffer_size.X = screenBufferInfo.srWindow.Right -
screenBufferInfo.srWindow.Left + 1; // Columns
new_screen_buffer_size.Y = screenBufferInfo.srWindow.Bottom -
screenBufferInfo.srWindow.Top + 1; // Rows
// Set new buffer size
SetConsoleScreenBufferSize(hConsole, new_screen_buffer_size);
std::cout << "There are no scrollbars in this console!" << std::endl;
return 0;
}
My question now is: How could I remove the scrollbar on other plattforms and use their respective method with #if defined(_whatever plattform_)
similar to another code snippet in my project (plain c):
// writes terminal dimensions to 'rows' and 'cols'
// returns 0 if successful
int window_dimensions()
{
#if defined(__APPLE__) || defined(__linux__)
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
rows = w.ws_row;
cols = w.ws_col;
return 0;
#elif defined(_WIN32)
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
return 0;
#endif
return 1;
}