A HEADER FILE project with macros that can change text color/style and move cursor in CONSOLE by ANSI Escape Codes, and some utility macros for CUDA and Qt.
Homepage: https://github.com/BH2WFR/ConsoleUtil
author: BH2WFR
LICENSE: MIT License
Reference of Ansi Escape Codes:
name | author | link | path | license |
---|---|---|---|---|
scope_guard | ricab | https://github.com/ricab/scope_guard | External/ScopeGuard.h |
Unlicense |
xorstr | JustasMasiulis | https://github.com/JustasMasiulis/xorstr | External/Xorstr.h |
Apache 2.0 |
yamc | yohhoy | https://github.com/yohhoy/yamc | include/LegacyUtil/yamc |
MIT |
span | tcbrindle | https://github.com/tcbrindle/span | External/Span.h |
BSL 1.0 |
strutil | Shot511 | https://github.com/Shot511/strutil | External/StrUtil.h |
MIT |
-
features using Ansi Escape code (like color customizing, or cursor moving macros in this header file) DO NOT SUPPORT Windows version lower than Windows 10 1511, otherwise it can't display properly in windows cmd.
- If you are using these operating systems, pls
#define CONSOLE_UTIL_ANSI_ESCAPE_UNSUPPORTED 1
before#include <ConsoleUtil/ConsoleUtil.h>
to disable features by printing Ansi Escape Code.
- If you are using these operating systems, pls
-
C language version
≥ C99
, C++ language≥ C++11
, with##__VA_ARGS__
extension support.(MSVC supports
##__VA_ARGS__
since VS2015 Update 3. if your MSVC or VS version is older, pls delete "##
", MSVC eats trailing comma before__VA_ARGS__
by default without/Zc::preprocessor
command) -
Pls #include the header file <
ConsoleUtil/ConsoleUtil.h
> after other header files, especially those from libraries such as Qt/fmtlib. DO NOT #include <ConsoleUtil/ConsoleUtil.h
> in header files. -
You can include <
ConsoleUtil/CppUtil.h
> in header files. No need to include <ConsoleUtil/CppUtil.h
> in source files if you already included <ConsoleUtil/ConsoleUtil.h
>, because it has been included in <ConsoleUtil/ConsoleUtil.h
>. -
DO NOT use macros with name starting with underscore
_
externally, such as_CUTIL_EXPAND_MSVC()
.
#include <ConsoleUtil/ConsoleUtil.h> // already wrapped in <ConsoleUtil/ConsoleUtil.h>
-
Set Front Color, Back Color, and font style for messages to print by printf/cout.
Forecolors:
FBlack
,FRed
,FGreen
,FYellow
,FBlue
,FMagenta
,FCyan
,FWhite
,FDefault
,FRgb(66, 66, 66)
Bright Forecolors:
FGray(=FGrey,FLBlack)
,FLRed
,FLGreen
,FLYellow
,FLBlue
,FLMagenta
,FLCyan
,FLWhite
;Backcolors:
BBlack
,BRed
,BGreen
,BYellow
,BBlue
,BMagenta
,BCyan
,BWhite
,BDefault
,BRgb(66, 66, 66)
;Bright Backcolors:
BGray(=BGrey,BLBlack)
,BLRed
,BLGreen
,BLYellow
,BLBlue
,BLMagenta
,BLCyan
,BLWhite
;Font Styles:
CBold
,CWeak
,CItalic
,CUnderLine
,CFlash
,CQFlash
,CInvert
(swap fg and bg colors),CHide
;Reset to Default:
CRst
orCReset
, you must append this to the end of the string literal to revert console style back to default.printf(BRed FLGreen CQFlash "test" CReset "\n"); // Red Background, Light Green Text Forecolor, and flashing quickly std::cout << CCyan "test" CReset << "\n"; // Text forecolor: cyan fmt::println(CYellow "test" CReset); // Text forecolor: yellow
-
Control text cursor location in console, or erase text.
Move cursor:
CUp(2)
,CDown(3)
,CFwd(4)
,CBack(5)
,CNextLn(1)
,CPrevLn(1)
,
CHorzPos(12)
(column X, absolute),CPos(44, 55)
(move the cursor to row 44, column 55)Just print them, use individual or append to string literals.
printf(CForward(2)); // move thr cursor 2 characters right, equals to `CRight(2)` or `CFwd(2)`. printf(CCursorPos(15, 20)); // move the text cursor to (15, 20) position CUTIL_CONSOLE_CURSOR_POS(15, 20); // equivalent, calls SetConsoleCursorPosition() in win32.
-
set console encoding, console window size (in windows), or console title, also with pause program and force abort the program.
CUTIL_CHCP_ENCODING_UTF8(); // switch console encoding to UTF-8 (windows) CUTIL_CONSOLE_TITLE("MyProject"); // set console window title (calls SetConsoleTitleA() or SetConsoleTitleW() for windows, and print ansi escape code "\033]0;%s\007" for linux) // CUTIL_CONSOLE_TITLE(_TEXT("MyProgram")); // CUTIL_CONSOLE_TITLE_A("MyProgram"); // CUTIL_CONSOLE_TITLE_W(L"MyProgram"); CUTIL_CONSOLE_SIZE(100, 30); // set console window size to with of 30 chars and height of 30 lines (only available in windows) CUTIL_CONSOLE_CLEAR(); // clear console (calls system("cls") ) printf("中文한글\n"); // you can correctly display this when the code saved in UTF-8 Encoding, especially in MSVC Compiler.
// available encodings: CUTIL_CHCP_ENCODING_UTF8(); CUTIL_CHCP_ENCODING_GB2312(); CUTIL_CHCP_ENCODING_BIG5(); CUTIL_CHCP_ENCODING_KOR(); CUTIL_CHCP_ENCODING_JIS(); CUTIL_CHCP_ENCODING_LATIN1(); CUTIL_CHCP_ENCODING_LATIN2(); CUTIL_CHCP_ENCODING_CYR(); CUTIL_CHCP_ENCODING_WIN1250(); CUTIL_CHCP_ENCODING_WIN1251(); CUTIL_CHCP_ENCODING_WIN1252();
- set program locale to
UTF-8
CUTIL_LOCALE_UTF8(); // setlocale(LC_ALL, ".UTF-8") CUTIL_LOCALE_DEFAULT() // setlocale(LC_ALL, "") CUTIL_LOCALE_UTF8_PRINT() // print if succeed or not
- set program locale to
-
Print Text only in Debug Build, and do Not Print in Release Build.
pls make sure that macro "
_DEBUG
" is defined in Debug Build, or macro "NDEBUG
" is defined in Release Build.int a{1}; CUTIL_DEBUG_PRINTLN("debug text {}", a); // calls fmt::println()(fmtlib) or std::println()(C++23) CUTIL_DEBUG_COUT("debug text " << a << '\n'); // calls std::cout << CUTIL_DEBUG_PRINTF("debug text %d", a); // calls printf() // these function-like macros will DO NOTHING IN RELEASE BUILD. CUTIL_DEBUG_PRINTLN_ERR("debug stderr text {}", a); // print to stderr CUTIL_DEBUG_CERR("debug stderr text " << a << '\n'); CUTIL_DEBUG_PRINTF_ERR("debug stderr text %d", a);
-
Flush the input buffer to ensure that subsequent "scanf()" or "cin" calls receive valid input.
(吸收输入缓存区内的其余字符, 以便下次 scanf 或 cin 时能够获取到正确的输入内容)
// #define CUTIL_FLUSH_INPUT_BUFFER() {char ch; while((ch = getchar()) != '\n') continue;} int num1, num2; scanf("%d", &num1); // you inputed "123ss", then still remains characters "ss" in the input buffer CUTIL_CONSOLE_FLUSH_INPUTBUFFER(); // flush input buffer (clear) scanf("%d", &num2); // you can normally input other contents.
-
print argc and argv arguments of main(int argc, char* argv[]) function in sequence.
int main(int argc, char* argv[]){ CUTIL_PRINT_ARGV(argc, argv);// print all argc and argv[n] of main() function return 0; }
-
Print custom Error Message with filename, line number and function name
打印错误信息,并输出当前文件名、行号、函数名
#include <print.h> // C++23
#include <windows.h> // include other headers first
#include <fmt/core.h> // include other headers first
#include <ConsoleUtil/ConsoleUtil.h> // include this header at last
int main(int argc, char* argv[]){
CUTIL_CHCP_ENCODING_UTF8(); // switch console encoding to UTF-8 (windows)
CUTIL_CONSOLE_TITLE("MyProject"); // set console window title
CUTIL_CONSOLE_SIZE(100, 30); // set console window size to with of 30 chars and height of 30 lines.
CUTIL_CONSOLE_CLEAR(); // clear console (system("cls"))
CUTIL_PRINT_ARGV(argc, argv); // print all argc and argv[n] of main() function
printf(FLGreen "Hello World!\n" CReset); // print "Hello World" with light yellow console color formatting
you should put "CReset" at the end of string to RESET console font color to DEFAULT
printf(CStyle(FLGreen, "Hello World!\n")); // Equivalent
std::cout << FLRed "ERROR\n" CReset; // print "ERROR" with font color light red, "CReset" is also needed to revert font color to default
std::cout << CStyle(FLRed, "ERROR\n");// Equivalent
printf(BRed FGreen CQFlash "test\n" CReset); // Print text with green font and red background, and quickly flashing
printf(CStyle(BRed FGreen CQFlash, "test\n"));// Equivalent
printf(CForward(2)); // move thr cursor 2 characters right
CUTIL_ERROR_MESSAGE("error occurred!"); // print an error message with filename, function name and line number ATTACHED.
CUTIL_CONSOLE_PAUSE(); // system("pause");
return 0;
}
Console Effects 控制台效果 :
![image-20231205152753735](./assets/Example Effects.png)