add Application::windowSize()

This commit is contained in:
Günter Obiltschnig
2021-04-11 16:00:53 +02:00
parent 8625b29f9f
commit 4066c4d794
2 changed files with 42 additions and 1 deletions

View File

@@ -118,6 +118,12 @@ public:
PRIO_SYSTEM = 100 PRIO_SYSTEM = 100
}; };
struct WindowSize
{
int width;
int height;
};
Application(); Application();
/// Creates the Application. /// Creates the Application.
@@ -294,6 +300,15 @@ public:
/// help information has been encountered and no other things /// help information has been encountered and no other things
/// besides displaying help shall be done. /// besides displaying help shall be done.
static WindowSize windowSize();
/// Returns the current window size of the console window,
/// if available.
///
/// Currently implemented for POSIX platforms (via TIOCGWINSZ ioctl())
/// and Windows (GetConsoleScreenBufferInfo()).
///
/// Returns zero width and height if the window size cannot be determined.
const char* name() const; const char* name() const;
protected: protected:

View File

@@ -40,6 +40,8 @@
#endif #endif
#if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS) #if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS)
#include "Poco/SignalHandler.h" #include "Poco/SignalHandler.h"
#include <stdio.h>
#include <sys/ioctl.h>
#endif #endif
#include "Poco/UnicodeConverter.h" #include "Poco/UnicodeConverter.h"
@@ -321,13 +323,37 @@ void Application::stopOptionsProcessing()
} }
Application::WindowSize Application::windowSize()
{
WindowSize size{0, 0};
#if defined(POCO_OS_FAMILY_WINDOWS)
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
{
size.width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
size.height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
}
#elif defined(POCO_OS_FAMILY_UNIX)
struct winsize winsz;
if (ioctl(0, TIOCGWINSZ , &winsz) != -1)
{
size.width = winsz.ws_col;
size.height = winsz.ws_row;
}
#endif
return size;
}
int Application::run() int Application::run()
{ {
int rc = EXIT_CONFIG; int rc = EXIT_CONFIG;
initialize(*this);
try try
{ {
initialize(*this);
rc = EXIT_SOFTWARE; rc = EXIT_SOFTWARE;
rc = main(_unprocessedArgs); rc = main(_unprocessedArgs);
} }