Add method to determine terminal size
This commit is contained in:
parent
027e784f1c
commit
6e80640db5
|
@ -3,7 +3,10 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#ifdef PLATFORM_WINDOWS
|
||||
#ifndef PLATFORM_WINDOWS
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#include <fcntl.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
@ -36,6 +39,30 @@ bool confirmPrompt(const char *message, Response defaultResponse)
|
|||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Returns the current size of the terminal.
|
||||
* \remarks Unknown members of the returned TerminalSize are set to zero.
|
||||
*/
|
||||
TerminalSize determineTerminalSize()
|
||||
{
|
||||
TerminalSize size;
|
||||
#ifndef PLATFORM_WINDOWS
|
||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, reinterpret_cast<winsize *>(&size));
|
||||
#else
|
||||
CONSOLE_SCREEN_BUFFER_INFO consoleBufferInfo;
|
||||
if (const HANDLE stdHandle = GetStdHandle(STD_OUTPUT_HANDLE)) {
|
||||
GetConsoleScreenBufferInfo(stdHandle, &consoleBufferInfo);
|
||||
if (consoleBufferInfo.dwSize.X > 0) {
|
||||
size.rows = static_cast<unsigned short>(consoleBufferInfo.dwSize.X);
|
||||
}
|
||||
if (consoleBufferInfo.dwSize.Y > 0) {
|
||||
size.columns = static_cast<unsigned short>(consoleBufferInfo.dwSize.Y);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return size;
|
||||
}
|
||||
|
||||
#ifdef PLATFORM_WINDOWS
|
||||
/*!
|
||||
* \brief Closes stdout, stdin and stderr and stops the console.
|
||||
|
|
|
@ -32,6 +32,34 @@ std::pair<std::vector<std::unique_ptr<char[]>>, std::vector<char *>> CPP_UTILITI
|
|||
#define CMD_UTILS_CONVERT_ARGS_TO_UTF8
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* \brief The TerminalSize struct describes a terminal size.
|
||||
* \remarks The same as the winsize structure is defined in `sys/ioctl.h`.
|
||||
* \sa determineTerminalSize()
|
||||
*/
|
||||
struct TerminalSize {
|
||||
TerminalSize(unsigned short rows = 0, unsigned short columns = 0, unsigned short width = 0, unsigned short height = 0);
|
||||
|
||||
/// \brief number of rows
|
||||
unsigned short rows;
|
||||
/// \brief number of columns
|
||||
unsigned short columns;
|
||||
/// \brief width in pixel
|
||||
unsigned short width;
|
||||
/// \brief height in pixel
|
||||
unsigned short height;
|
||||
};
|
||||
|
||||
inline TerminalSize::TerminalSize(unsigned short rows, unsigned short columns, unsigned short width, unsigned short height)
|
||||
: rows(rows)
|
||||
, columns(columns)
|
||||
, width(width)
|
||||
, height(height)
|
||||
{
|
||||
}
|
||||
|
||||
TerminalSize determineTerminalSize();
|
||||
|
||||
/*!
|
||||
* \brief The Indentation class allows printing indentation conveniently, eg. cout << Indentation(4) << ...
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue