From 92bc623fffebab2e2b4639b347f7a314df40e166 Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Mon, 8 Aug 2022 16:09:58 +0200 Subject: [PATCH] color_out: added a C++ API Added some basic C++ API removing the need to use rang. --- src/utils/color_out.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/utils/color_out.h b/src/utils/color_out.h index e24d1f375..b5d9f0a47 100644 --- a/src/utils/color_out.h +++ b/src/utils/color_out.h @@ -90,6 +90,10 @@ #define TERM_FG_MAGENTA "\e[34m" #define TERM_FG_RESET "\e[39m" +#define TBOLD(x) TERM_BOLD x TERM_RESET +#define TGREEN(x) TERM_FG_GREEN x TERM_FG_RESET +#define TRED(x) TERM_FG_RED x TERM_FG_RESET + #ifdef __cplusplus extern "C" { #endif @@ -107,6 +111,29 @@ bool isMsysPty(int fd); } // extern "C" #endif +#ifdef __cplusplus +#include +/** + * Class wrapping color output to terminal. Sample usage: + * + * col() << TERM_RED << "Red message: " << TERM_RESET << "normal font.\n" + */ +class col +{ +public: + template + col &operator<< (T val) { + oss << val; + return *this; + } + + inline ~col() { + color_printf("%s", oss.str().c_str()); + } +private: + std::ostringstream oss; +}; +#endif #endif // defined COLOR_OUT_H_