2019-09-04 18:45:48 +02:00
|
|
|
#ifndef APPLICATION_UTILITIES_COMMANDLINEUTILS_H
|
|
|
|
#define APPLICATION_UTILITIES_COMMANDLINEUTILS_H
|
2015-06-24 00:44:16 +02:00
|
|
|
|
2016-08-29 15:35:48 +02:00
|
|
|
#include "../global.h"
|
2015-06-24 00:44:16 +02:00
|
|
|
|
2016-10-02 21:53:58 +02:00
|
|
|
#include <ostream>
|
|
|
|
|
2016-12-18 17:19:57 +01:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2017-05-01 03:13:11 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2016-12-18 17:19:57 +01:00
|
|
|
#endif
|
|
|
|
|
2019-06-10 21:56:46 +02:00
|
|
|
namespace CppUtilities {
|
2015-06-24 00:44:16 +02:00
|
|
|
|
2016-07-02 02:02:47 +02:00
|
|
|
/*!
|
|
|
|
* \brief The Response enum is used to specify the default response for the confirmPrompt() method.
|
|
|
|
*/
|
2017-05-01 03:13:11 +02:00
|
|
|
enum class Response { None, Yes, No };
|
2015-06-24 00:44:16 +02:00
|
|
|
|
2020-01-29 18:30:02 +01:00
|
|
|
CPP_UTILITIES_EXPORT bool confirmPrompt(const char *message, Response defaultResponse = Response::None);
|
2015-06-24 00:44:16 +02:00
|
|
|
|
2015-09-01 20:05:38 +02:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2020-01-29 18:30:02 +01:00
|
|
|
CPP_UTILITIES_EXPORT void startConsole();
|
|
|
|
CPP_UTILITIES_EXPORT std::pair<std::vector<std::unique_ptr<char[]>>, std::vector<char *>> convertArgsToUtf8();
|
2019-06-20 22:55:29 +02:00
|
|
|
#define CMD_UTILS_START_CONSOLE ::CppUtilities::startConsole();
|
2017-05-01 03:13:11 +02:00
|
|
|
#define CMD_UTILS_CONVERT_ARGS_TO_UTF8 \
|
2019-06-20 22:55:29 +02:00
|
|
|
auto utf8Args = ::CppUtilities::convertArgsToUtf8(); \
|
2017-05-01 03:13:11 +02:00
|
|
|
argv = utf8Args.second.data(); \
|
2016-12-18 17:19:57 +01:00
|
|
|
argc = static_cast<int>(utf8Args.second.size());
|
2015-09-01 20:05:38 +02:00
|
|
|
#else
|
2017-05-01 03:13:11 +02:00
|
|
|
#define CMD_UTILS_START_CONSOLE
|
|
|
|
#define CMD_UTILS_CONVERT_ARGS_TO_UTF8
|
2015-09-01 20:05:38 +02:00
|
|
|
#endif
|
|
|
|
|
2017-09-29 20:56:50 +02:00
|
|
|
/*!
|
|
|
|
* \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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-01-29 18:30:02 +01:00
|
|
|
CPP_UTILITIES_EXPORT TerminalSize determineTerminalSize();
|
2017-09-29 20:56:50 +02:00
|
|
|
|
2016-10-02 21:53:58 +02:00
|
|
|
/*!
|
2016-12-18 17:19:57 +01:00
|
|
|
* \brief The Indentation class allows printing indentation conveniently, eg. cout << Indentation(4) << ...
|
2016-10-02 21:53:58 +02:00
|
|
|
*/
|
2017-05-01 03:13:11 +02:00
|
|
|
class CPP_UTILITIES_EXPORT Indentation {
|
2016-10-02 21:53:58 +02:00
|
|
|
public:
|
2017-05-01 03:13:11 +02:00
|
|
|
Indentation(unsigned char level = 4, char character = ' ')
|
|
|
|
: level(level)
|
|
|
|
, character(character)
|
|
|
|
{
|
|
|
|
}
|
2016-10-02 21:53:58 +02:00
|
|
|
|
2017-05-01 03:13:11 +02:00
|
|
|
Indentation operator+(unsigned char level)
|
2016-10-02 21:53:58 +02:00
|
|
|
{
|
|
|
|
return Indentation(this->level + level, character);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char level;
|
|
|
|
char character;
|
|
|
|
};
|
|
|
|
|
2017-05-01 03:13:11 +02:00
|
|
|
inline CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &out, Indentation indentation)
|
2016-10-02 21:53:58 +02:00
|
|
|
{
|
2017-05-01 03:13:11 +02:00
|
|
|
for (unsigned char i = 0; i < indentation.level; ++i) {
|
2016-10-02 21:53:58 +02:00
|
|
|
out << indentation.character;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-06-10 21:56:46 +02:00
|
|
|
} // namespace CppUtilities
|
2015-06-24 00:44:16 +02:00
|
|
|
|
2019-09-04 18:45:48 +02:00
|
|
|
#endif // APPLICATION_UTILITIES_COMMANDLINEUTILS_H
|