#include <stdio.h>
#include <iostream>
#include <sstream>
using namespace std;
constexpr int CMD_MAX_LEN = 256;
string getStringResultFromCommand( const char* cmd ) noexcept
{
string result;
char buffer[CMD_MAX_LEN] = {'\0',};
FILE* stream = popen( cmd, "r" );
if( stream ) {
while( fgets( buffer, CMD_MAX_LEN, stream ) != NULL ) result.append( buffer );
pclose( stream );
}
return result;
}
inline void getConsoleSize( int& width, int& height )
{
istringstream iss( getStringResultFromCommand( "stty size" ) );
string tokenString;
getline( iss, tokenString, ' ' );
height = stoi( tokenString );
getline( iss, tokenString, ' ' );
width = stoi( tokenString );
}
int getConsoleWidth()
{
try { return stoi( getStringResultFromCommand( "stty size | awk '{print $2}'" ) ); }
catch (...) { return 0; }
}
int getConsoleHeight()
{
try { return stoi( getStringResultFromCommand( "stty size | awk '{print $1}'" ) ); }
catch (...) { return 0; }
}
int main()
{
int consoleW, consoleH;
getConsoleSize( consoleW, consoleH );
printf("\n");
printf("[ getConsoleSize() ]\n");
printf("- Console Width : %d\n", consoleW);
printf("- Console Height : %d\n", consoleH);
printf("\n");
printf("[ getConsoleWidth() / getConsoleHeight() ]\n");
printf("- Console Width : %d\n", getConsoleWidth());
printf("- Console Height : %d\n", getConsoleHeight());
printf("\n");
return 0;
}