CIOF v1.2.0-build
Char Input Output Format - A simple Input and Output utility library
Loading...
Searching...
No Matches
ciof.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "CIOFpredefines.hpp"
4
5#if __CIOF_CPLUSPLUS >= CIOF_DEFAULT_CPP_STD
6
7#define __CIOF_OK
8
9#include <string>
10
11// This must be after all
12#ifdef CIOF_OS_WIN32
13# ifdef __out
14# undef __out
15# endif
16#endif
17
18namespace ciof
19{
20 /**
21 * @brief Set output configuration for ciof::impl::__out() function and its wrappers
22 */
23 typedef struct CIOFOutputConfig
24 {
25 // Add a space between parameter automaticalliy
26 bool spacing = false;
27
28 // Ignore custom and predefined formatting keywords
29 bool ignoreAllFormating = false;
30
31 // Process config
32 bool processing = true;
33
34 // Set delimiter for spacing
35 std::string spacingDelimeter = " ";
37
38 /**
39 * @brief Set output type for ciof::impl::__out() function and its helper functions
40 * Out : To Standard Output
41 * Err : To Standard Error
42 * Log : To Standard Log (Error, unbuffered)
43 */
44 enum class OutputType
45 {
46 Out, // std::cout
47 Err, // std::cerr
48 Log, // std::clog
49 };
50
51 /**
52 * @brief Set output style:
53 * Bold
54 * Italic
55 * Underline
56 * Strikethrough
57 */
65
66 /**
67 * @brief Set default output configuration
68 */
70
71 /**
72 * @brief Implementation functions
73 */
74 namespace impl
75 {
76 /**
77 * @brief Convert to string safely
78 * @param _val any convertable type to string
79 * @return std::string total converted strig
80 */
81 template <typename T>
82 std::string toString(const T &_val);
83
84 /**
85 * @brief Parse the params for printing and echoing
86 * @return _fmt parameter 1 to parse
87 * @return _args parameter 2 to the last one to parse
88 * @return std::string Total parsed
89 */
90 template <typename T, typename ... Args>
91 std::string parse(T _fmt, Args ... _args);
92
93 /**
94 * @brief Output the with its corresponding type either to:
95 * - Standard output
96 * - Standard error
97 * - Standard log (AKA: Error but unbuffered)
98 * @param _outType Type for OUT, ERR or LOG
99 * @param _t Text to add
100 */
101 template <typename T>
102 void __out(const OutputType &_outType, T _t);
103
104 /**
105 * @brief Output the with its corresponding type either to:
106 * - Standard output
107 * - Standard error
108 * - Standard log (AKA: Error but unbuffered)
109 * @param _outType Type for OUT, ERR or LOG
110 * @param _t Text to add
111 * @param _args More text to add
112 */
113 template <typename T, typename ... Args>
114 void __out(const OutputType &_outType, T _t, Args ... _args);
115 }
116
117 // NOTE: OUTPUT
118
119 // PRINT
120 /**
121 * @brief Print a new line
122 */
123 void print();
124
125 /**
126 * @brief Print to the standard output for `_t`
127 * @param _t Message to print
128 */
129 template <typename T>
130 void print(T _t);
131
132 /**
133 * @brief Print to the standard output for `_t`
134 * @param _t Message to print
135 * @param _args More messages to add
136 */
137 template <typename T, typename ... Args>
138 void print(T _t, Args ... _args);
139
140 /**
141 * @brief Print to the standard log for `_t`
142 * @param _t Message to print
143 */
144 template <typename T>
145 void printLog(T _t);
146
147 /**
148 * @brief Print to the standard log for `_t`
149 * @param _t Message to print
150 * @param _args More messages to add
151 */
152 template <typename T, typename ... Args>
153 void printLog(T _t, Args ... _args);
154
155 /**
156 * @brief Print to the standard error for `_t`
157 * @param _t Message to print
158 */
159 template <typename T>
160 void printError(T _t);
161
162 /**
163 * @brief Print to the standard error for `_t`
164 * @param _t Message to print
165 * @param _args More messages to add
166 */
167 template <typename T, typename ... Args>
168 void printError(T _t, Args ... _args);
169
170 // ECHO
171 /**
172 * @brief Echo to the standard output for `_t`
173 * @param _t Message to print
174 */
175 template <typename T>
176 void echo(T _t);
177
178 /**
179 * @brief Echo to the standard output for `_t`
180 * @param _t Message to print
181 * @param _args More messages to add
182 */
183 template <typename T, typename ... Args>
184 void echo(T _t, Args ... _args);
185
186 /**
187 * @brief Echo to the standard log for `_t`
188 * @param _t Message to print
189 */
190 template <typename T>
191 void echoLog(T _t);
192
193 /**
194 * @brief Echo to the standard log for `_t`
195 * @param _t Message to print
196 * @param _args More messages to add
197 */
198 template <typename T, typename ... Args>
199 void echoLog(T _t, Args ... _args);
200
201 /**
202 * @brief Echo to the standard error for `_t`
203 * @param _t Message to print
204 */
205 template <typename T>
206 void echoError(T _t);
207
208 /**
209 * @brief Echo to the standard log for `_t`
210 * @param _t Message to print
211 * @param _args More messages to add
212 */
213 template <typename T, typename ... Args>
214 void echoError(T _t, Args ... _args);
215
216 // NOTE: INPUT
217 /**
218 * @brief Get full input from the user with a prompt
219 * @param _prompt Prompt to user
220 * @param _var Variable to store the value
221 */
222 template <typename T>
223 void input(const std::string &_prompt, T *_var);
224
225 /**
226 * @brief Get full input from the user
227 * @param _prompt Prompt to user
228 * @param _var Variable to store the value
229 */
230 template <typename T>
231 void input(T *_var);
232
233 /**
234 * @brief Empty input() function; Do not save any input to a variable, just leave it as a blank input
235 */
236 void input();
237
238 // NOTE: UTILS
239 /**
240 * @brief Get everything as a string format
241 * @param _t First param of the string
242 * @param _args Rest of the params of the string
243 * @return std::string Return the formatted string
244 */
245 template <typename T, typename ... Args>
246 std::string format(T _t, Args ... _args);
247
248 /**
249 * @brief Set the styles according to the `OutputStyle` type
250 * @param _style Set the style (Bold, Italics, Underline, ...)
251 * @return std::string Escape code according to the style
252 */
253 std::string styleSet(const OutputStyle &_style);
254
255 /**
256 * @brief Reset all the styles and colors in the terminal
257 * @return std::string Escape code to ereset colors and styles from terminal
258 */
259 std::string styleReset();
260
261 /**
262 * @brief Set the colors from the default color palette from the terminal
263 * @param _color Integer for color
264 * @return std::string Escape code to set the code
265 */
266 std::string colorSet(int _color);
267
268 /**
269 * @brief Set the RGB color in the terminal
270 * @param r Red color
271 * @param g Green color
272 * @param b Blue color
273 * @return std::string Color escape sequence for RGB colors
274 */
275 std::string rgbSet(unsigned int r, unsigned int g, unsigned int b);
276
277 /**
278 * @brief Set the RGB color for background in the terminal
279 * @param r Red color
280 * @param g Green color
281 * @param b Blue color
282 * @return std::string Color escape sequence for RGB colors
283 */
284 std::string rgbBgSet(unsigned int r, unsigned int g, unsigned int b);
285
286 /**
287 * @brief Initialize ANSI text option in Windows 10 or later
288 * NOTE: ONLY WORKS IN WINDOWS 10 OR LATER
289 */
290 void initANSI();
291
292 /**
293 * @brief Get the set cursor position using _row and _col
294 * @param _row Row to set (X)
295 * @param _col Column to set (Y)
296 * @return std::string Return the string to set the position using ANSI
297 */
298 std::string getCursorPos(int _row, int _col);
299
300 /**
301 * @brief Get the set cursor position using _row and _col
302 * Essentially calls the `getCursorPos()` function and then sets the row and col according to the `getCursorPos()` function
303 * @param _row Row to set (X)
304 * @param _col Column to set (Y)
305 */
306 void cursorPos(int _row, int _col);
307
308 /**
309 * @brief Clear the contents of the terminal
310 * NOTE: In Windows; enable ANSI by doing `ciof::initANSI()`
311 */
312 void clear();
313
314 /**
315 * @brief Show or hide the cursor from the terminal
316 * NOTE: In Windows; enable ANSI by doing `ciof::initANSI()`
317 */
318 void showCursor(const bool &show);
319}
320
321#include "ciof.ipp"
322
323#else
324# error "Cannot compile with the current C++ standard"
325# pragma message("Use C++ " CIOF_TOSTRING(CIOF_DEFAULT_CPP_STD) " or higer instead of " CIOF_TOSTRING(__cplusplus))
326#endif // __cplusplus
327
Implementation functions.
Definition ciof.hpp:75
std::string parse(T _fmt, Args ... _args)
Parse the params for printing and echoing.
void __out(const OutputType &_outType, T _t)
Output the with its corresponding type either to:
std::string toString(const T &_val)
Convert to string safely.
Definition ciof.hpp:19
void initANSI()
Initialize ANSI text option in Windows 10 or later NOTE: ONLY WORKS IN WINDOWS 10 OR LATER.
Definition ciof.cpp:34
struct ciof::CIOFOutputConfig CIOFOutputConfig
Set output configuration for ciof::impl::__out() function and its wrappers.
std::string rgbBgSet(unsigned int r, unsigned int g, unsigned int b)
Set the RGB color for background in the terminal.
Definition ciof.cpp:86
void showCursor(const bool &show)
Show or hide the cursor from the terminal NOTE: In Windows; enable ANSI by doing ciof::initANSI().
Definition ciof.cpp:67
void cursorPos(int _row, int _col)
Get the set cursor position using _row and _col Essentially calls the getCursorPos() function and the...
Definition ciof.cpp:31
std::string getCursorPos(int _row, int _col)
Get the set cursor position using _row and _col.
Definition ciof.cpp:28
OutputType
Set output type for ciof::impl::__out() function and its helper functions Out : To Standard Output Er...
Definition ciof.hpp:45
std::string styleReset()
Reset all the styles and colors in the terminal.
Definition ciof.cpp:73
std::string rgbSet(unsigned int r, unsigned int g, unsigned int b)
Set the RGB color in the terminal.
Definition ciof.cpp:79
std::string format(T _t, Args ... _args)
Get everything as a string format.
void printLog(T _t)
Print to the standard log for _t.
OutputStyle
Set output style: Bold Italic Underline Strikethrough.
Definition ciof.hpp:59
void input()
Empty input() function; Do not save any input to a variable, just leave it as a blank input.
Definition ciof.cpp:21
CIOFOutputConfig outputConf
Set default output configuration.
Definition ciof.cpp:16
void printError(T _t)
Print to the standard error for _t.
void print()
Print a new line.
Definition ciof.cpp:18
std::string styleSet(const OutputStyle &_style)
Set the styles according to the OutputStyle type.
Definition ciof.cpp:52
void clear()
Clear the contents of the terminal NOTE: In Windows; enable ANSI by doing ciof::initANSI().
Definition ciof.cpp:64
void echo(T _t)
Echo to the standard output for _t.
std::string colorSet(int _color)
Set the colors from the default color palette from the terminal.
Definition ciof.cpp:76
void echoError(T _t)
Echo to the standard error for _t.
void echoLog(T _t)
Echo to the standard log for _t.
Set output configuration for ciof::impl::__out() function and its wrappers.
Definition ciof.hpp:24
std::string spacingDelimeter
Definition ciof.hpp:35