C++ Utilities  4.17.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
commandlineutils.h
Go to the documentation of this file.
1 #ifndef APPLICATIONUTILITIES_COMMANDLINEUTILS_H
2 #define APPLICATIONUTILITIES_COMMANDLINEUTILS_H
3 
4 #include "../global.h"
5 
6 #include <ostream>
7 
8 #ifdef PLATFORM_WINDOWS
9 #include <memory>
10 #include <vector>
11 #endif
12 
13 namespace ApplicationUtilities {
14 
18 enum class Response { None, Yes, No };
19 
20 bool CPP_UTILITIES_EXPORT confirmPrompt(const char *message, Response defaultResponse = Response::None);
21 
22 #ifdef PLATFORM_WINDOWS
23 void CPP_UTILITIES_EXPORT startConsole();
24 std::pair<std::vector<std::unique_ptr<char[]>>, std::vector<char *>> CPP_UTILITIES_EXPORT convertArgsToUtf8();
25 #define CMD_UTILS_START_CONSOLE ::ApplicationUtilities::startConsole();
26 #define CMD_UTILS_CONVERT_ARGS_TO_UTF8 \
27  auto utf8Args = ::ApplicationUtilities::convertArgsToUtf8(); \
28  argv = utf8Args.second.data(); \
29  argc = static_cast<int>(utf8Args.second.size());
30 #else
31 #define CMD_UTILS_START_CONSOLE
32 #define CMD_UTILS_CONVERT_ARGS_TO_UTF8
33 #endif
34 
40 struct TerminalSize {
41  TerminalSize(unsigned short rows = 0, unsigned short columns = 0, unsigned short width = 0, unsigned short height = 0);
42 
44  unsigned short rows;
46  unsigned short columns;
48  unsigned short width;
50  unsigned short height;
51 };
52 
53 inline TerminalSize::TerminalSize(unsigned short rows, unsigned short columns, unsigned short width, unsigned short height)
54  : rows(rows)
55  , columns(columns)
56  , width(width)
57  , height(height)
58 {
59 }
60 
62 
67 public:
68  Indentation(unsigned char level = 4, char character = ' ')
69  : level(level)
70  , character(character)
71  {
72  }
73 
74  Indentation operator+(unsigned char level)
75  {
76  return Indentation(this->level + level, character);
77  }
78 
79  unsigned char level;
80  char character;
81 };
82 
83 inline CPP_UTILITIES_EXPORT std::ostream &operator<<(std::ostream &out, Indentation indentation)
84 {
85  for (unsigned char i = 0; i < indentation.level; ++i) {
86  out << indentation.character;
87  }
88  return out;
89 }
90 
91 } // namespace ApplicationUtilities
92 
93 #endif // APPLICATIONUTILITIES_COMMANDLINEUTILS_H
unsigned short rows
number of rows
#define CPP_UTILITIES_EXPORT
Contains currently only ArgumentParser and related classes.
TerminalSize(unsigned short rows=0, unsigned short columns=0, unsigned short width=0, unsigned short height=0)
unsigned short height
height in pixel
The Indentation class allows printing indentation conveniently, eg.
bool CPP_UTILITIES_EXPORT confirmPrompt(const char *message, Response defaultResponse=Response::None)
Prompts for confirmation displaying the specified message.
constexpr int i
Response
The Response enum is used to specify the default response for the confirmPrompt() method.
The TerminalSize struct describes a terminal size.
TerminalSize CPP_UTILITIES_EXPORT determineTerminalSize()
Returns the current size of the terminal.
unsigned short columns
number of columns
Indentation operator+(unsigned char level)
Indentation(unsigned char level=4, char character=' ')
CPP_UTILITIES_EXPORT std::ostream & operator<<(std::ostream &out, Indentation indentation)
unsigned short width
width in pixel