diff --git a/lib/include/bofstd/bofconio.h b/lib/include/bofstd/bofconio.h index ec87f89..9872bad 100644 --- a/lib/include/bofstd/bofconio.h +++ b/lib/include/bofstd/bofconio.h @@ -24,8 +24,8 @@ #include BEGIN_BOF_NAMESPACE() -#define PrintfAt(x, y, pFormat, ...) PrintfAtColor(mForeColor_E, x, y, pFormat, ##__VA_ARGS__) -#define PrintfColor(Color, pFormat, ...) PrintfAtColor(Color, 0, 0, pFormat, ##__VA_ARGS__) +// #define PrintfAt(x, y, pFormat, ...) PrintfAtColor(mForeColor_E, x, y, pFormat, ##__VA_ARGS__) +// #define PrintfColor(Color, pFormat, ...) PrintfAtColor(Color, 0, 0, pFormat, ##__VA_ARGS__) enum CONIO_MODIFIER_KEY_FLAG : uint32_t { @@ -159,54 +159,50 @@ struct BOF_CONIO_PARAM class BOFSTD_EXPORT BofConio final { -private: - BOF_CONIO_PARAM mBofConioParam_X; - uint32_t mConsoleWidth_U32 = 0; - uint32_t mConsoleHeight_U32 = 0; public: - BofConio(const BOF_CONIO_PARAM &_rBofConioParam_X); - virtual ~BofConio(); - - // delete copy and move constructors and assign operators - BofConio(BofConio const &) = delete; // Copy construct - BofConio(BofConio &&) = delete; // Move construct - BofConio &operator=(BofConio const &) = delete; // Copy assign - BofConio &operator=(BofConio &&) = delete; // Move assign - - uint32_t KbHit(uint32_t _TimeOutInMs_U32); - uint32_t GetCh(bool _OnlyModifier_B); - std::string KeyToString(uint32_t _Key_U32); - - BOFERR Readline(const std::string &_rPrompt_S, std::string &_rInputLine_S); - - BOFERR SetTextAttribute(uint32_t _TextAttributeFlag_U32); // Use CONIO_TEXT_ATTRIBUTE_FLAG enum ored value - BOFERR SetForegroundTextColor(CONIO_TEXT_COLOR _ForegroundColor_E); - BOFERR SetBackgroundTextColor(CONIO_TEXT_COLOR _BackgroundColor_E); - BOFERR SetForegroundTextColor(BOF_RGBA _ForegroundColor_E); - BOFERR SetBackgroundTextColor(BOF_RGBA _BackgroundColor_X); - - BOFERR SetTextCursorState(CONIO_TEXT_CURSOR_STATE _CursorState_E); - BOFERR SetTextCursorPosition(uint32_t _x_U32, uint32_t _y_U32); - BOFERR GetTextCursorPosition(uint32_t &_rX_U32, uint32_t &_rY_U32); - BOFERR SetTextWindowTitle(const std::string &_rTitle_S); - BOFERR Clear(CONIO_CLEAR _ClearType_E); /* - create 24 bit color - position cursor - erase terminal - erale line - */ - - BOFERR Printf(const char *_pFormat_c, ...); - void PrintfAtColor(CONIO_TEXT_COLOR _ForeColor_E, uint32_t _x_U32, uint32_t _y_U32, const char *_pFormat_c, ...); - - BOFERR Reset(); + BofConio(const BOF_CONIO_PARAM &_rBofConioParam_X); + virtual ~BofConio(); + + // delete copy and move constructors and assign operators + BofConio(BofConio const &) = delete; // Copy construct + BofConio(BofConio &&) = delete; // Move construct + BofConio &operator=(BofConio const &) = delete; // Copy assign + BofConio &operator=(BofConio &&) = delete; // Move assign + */ + static bool S_Initialize(const BOF_CONIO_PARAM &_rBofConioParam_X); + static bool S_Shutdown(); + static uint32_t S_KbHit(uint32_t _TimeOutInMs_U32); + static uint32_t S_GetCh(bool _OnlyModifier_B); + static std::string S_KeyToString(uint32_t _Key_U32); + + static BOFERR S_Readline(const std::string &_rPrompt_S, std::string &_rInputLine_S); + + static BOFERR S_SetTextAttribute(uint32_t _TextAttributeFlag_U32); // Use CONIO_TEXT_ATTRIBUTE_FLAG enum ored value + static BOFERR S_SetForegroundTextColor(CONIO_TEXT_COLOR _ForegroundColor_E); + static BOFERR S_SetBackgroundTextColor(CONIO_TEXT_COLOR _BackgroundColor_E); + static BOFERR S_SetForegroundTextColor(BOF_RGBA _ForegroundColor_E); + static BOFERR S_SetBackgroundTextColor(BOF_RGBA _BackgroundColor_X); + + static BOFERR S_SetTextCursorState(CONIO_TEXT_CURSOR_STATE _CursorState_E); + static BOFERR S_SetTextCursorPosition(uint32_t _x_U32, uint32_t _y_U32); + static BOFERR S_GetTextCursorPosition(uint32_t &_rX_U32, uint32_t &_rY_U32); + static BOFERR S_SetTextWindowTitle(const char *_pTitle_c); + static BOFERR S_Clear(CONIO_CLEAR _ClearType_E); + + static BOFERR S_PrintLine(const char *_pFormat_c, ...); + static void S_PrintLineColorAt(CONIO_TEXT_COLOR _ForegroundColor_E, uint32_t _x_U32, uint32_t _y_U32, const char *_pFormat_c, ...); private: - uint32_t GetChAfterOneChar(bool _OnlyModifier_B, char _FirstChar_c); - - CONIO_TEXT_COLOR mForeColor_E = CONIO_TEXT_COLOR::CONIO_TEXT_COLOR_BLACK; + static uint32_t S_GetChAfterOneChar(bool _OnlyModifier_B, char _FirstChar_c); + + static BOF_CONIO_PARAM S_mBofConioParam_X; + static uint32_t S_mConsoleWidth_U32; + static uint32_t S_mConsoleHeight_U32; + static char S_mpTextForeAttribute_c[0x40]; + static char S_mpTextBackAttribute_c[0x40]; + static char S_mpTextAttribute_c[0x40]; }; END_BOF_NAMESPACE() \ No newline at end of file diff --git a/lib/include/bofstd/bofshell.h b/lib/include/bofstd/bofshell.h index f9acdca..89fe645 100644 --- a/lib/include/bofstd/bofshell.h +++ b/lib/include/bofstd/bofshell.h @@ -39,9 +39,16 @@ ls(true,false,false) #include BEGIN_BOF_NAMESPACE() +enum class SHELL_OUTPUT_STREAM +{ + SHELL_OUTPUT_STREAM_INPUT, + SHELL_OUTPUT_STREAM_OUTPUT, + SHELL_OUTPUT_STREAM_ERROR, + SHELL_OUTPUT_STREAM_MAX +}; using BOF_SHELL_INPUT_CMD = std::function; -using BOF_SHELL_OUTPUT_RES = std::function; +using BOF_SHELL_OUTPUT_RES = std::function; using BOF_SHELL_CMD_CALLBACK = std::function; // const std::vector &_rCmdArgCollection) > ; struct BOF_SHELL_CMD @@ -162,6 +169,7 @@ class BOFSTD_EXPORT BofShell BOFERR ShellCmdArg(void *_pArg, std::string &_rShellResult_S); std::string mEchoText_S; BOFERR ShellEcho(void *_pArg, std::string &_rShellResult_S); + BOFERR ShellGetCh(void *_pArg, std::string &_rShellResult_S); BOFERR ShellDoYouWantToContinue(void *_pArg, std::string &_rShellResult_S); BOF_SHELL_SPAWN_PARAM mSpawnParam_X; BOFERR ShellSpawn(void *_pArg, std::string &_rShellResult_S); @@ -179,6 +187,7 @@ class BOFSTD_EXPORT BofShell class BOFSTD_EXPORT BofShellConsole { + private: BOF_SHELL_CONSOLE_PARAM mShellConsoleParam_X; @@ -187,9 +196,8 @@ class BOFSTD_EXPORT BofShellConsole virtual ~BofShellConsole(); BOFERR InputStream(const std::string &_rPrompt_S, std::string &_rShellCmd_S); - BOFERR OutputStream(bool _Error_B, const std::string &_rShellRes_S); + BOFERR OutputStream(SHELL_OUTPUT_STREAM _ShellOutputStream_E, const std::string &_rShellRes_S); private: - std::unique_ptr mpuConio = nullptr; }; END_BOF_NAMESPACE() \ No newline at end of file diff --git a/lib/src/bofconio.cpp b/lib/src/bofconio.cpp index 6c82c55..e374a1f 100644 --- a/lib/src/bofconio.cpp +++ b/lib/src/bofconio.cpp @@ -24,15 +24,15 @@ Unit test: std::string Input_S; BOF_CONIO_PARAM ConioParam_X; std::unique_ptr puConio = std::make_unique(ConioParam_X); - puConio->SetForegroundTextColor(CONIO_TEXT_COLOR::CONIO_TEXT_COLOR_BRIGHT_YELLOW); + puConio->S_SetForegroundTextColor(CONIO_TEXT_COLOR::CONIO_TEXT_COLOR_BRIGHT_YELLOW); puConio->SetBackgroundTextColor(CONIO_TEXT_COLOR::CONIO_TEXT_COLOR_RED); - puConio->Clear(CONIO_CLEAR::CONIO_CLEAR_ALL); - puConio->SetTextCursorPosition(4, 10); - puConio->Printf("Hello world"); - puConio->SetTextCursorPosition(5, 9); - puConio->Printf("!!!"); - puConio->SetTextCursorState(CONIO_TEXT_CURSOR_STATE::CONIO_TEXT_CURSOR_STATE_BLINK_OFF); - puConio->SetTextWindowTitle("M F S"); + puConio->S_Clear(CONIO_CLEAR::CONIO_CLEAR_ALL); + puConio->S_SetTextCursorPosition(4, 10); + puConio->S_Printf("Hello world"); + puConio->S_SetTextCursorPosition(5, 9); + puConio->S_Printf("!!!"); + puConio->S_SetTextCursorState(CONIO_TEXT_CURSOR_STATE::CONIO_TEXT_CURSOR_STATE_BLINK_OFF); + puConio->S_SetTextWindowTitle("M F S"); */ // https://solarianprogrammer.com/2019/04/08/c-programming-ansi-escape-codes-windows-macos-linux-terminals/ // https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences @@ -62,18 +62,54 @@ Unit test: #include BEGIN_BOF_NAMESPACE() +BOF_CONIO_PARAM BofConio::S_mBofConioParam_X; +uint32_t BofConio::S_mConsoleWidth_U32 = 0; +uint32_t BofConio::S_mConsoleHeight_U32 = 0; +char BofConio::S_mpTextForeAttribute_c[0x40]; +char BofConio::S_mpTextBackAttribute_c[0x40]; +char BofConio::S_mpTextAttribute_c[0x40]; static std::map S_KeyLookupCollection{ - {CONIO_FUNCTION_KEY_F1, "F01"}, {CONIO_FUNCTION_KEY_F2, "F02"}, {CONIO_FUNCTION_KEY_F3, "F03"}, {CONIO_FUNCTION_KEY_F4, "F04"}, {CONIO_FUNCTION_KEY_F5, "F05"}, {CONIO_FUNCTION_KEY_F6, "F06"}, - {CONIO_FUNCTION_KEY_F7, "F07"}, {CONIO_FUNCTION_KEY_F8, "F08"}, {CONIO_FUNCTION_KEY_F9, "F09"}, {CONIO_FUNCTION_KEY_F10, "F10"}, {CONIO_FUNCTION_KEY_F11, "F11"}, {CONIO_FUNCTION_KEY_F12, "F12"}, - {CONIO_SPECIAL_KEY_ESC, "Esc"}, {CONIO_SPECIAL_KEY_BACKSPACE, "Bs"}, {CONIO_SPECIAL_KEY_TAB, "Tab"}, {CONIO_SPECIAL_KEY_ENTER, "Enter"}, {CONIO_SPECIAL_KEY_INSERT, "Ins"}, {CONIO_SPECIAL_KEY_HOME, "Home"}, - {CONIO_SPECIAL_KEY_PAGE_UP, "PgUp"}, {CONIO_SPECIAL_KEY_DELETE, "Del"}, {CONIO_SPECIAL_KEY_END, "End"}, {CONIO_SPECIAL_KEY_PAGE_DOWN, "PgDw"}, {CONIO_SPECIAL_KEY_ARROW_UP, "Up"}, {CONIO_SPECIAL_KEY_ARROW_LEFT, "Left"}, - {CONIO_SPECIAL_KEY_ARROW_DOWN, "Down"}, {CONIO_SPECIAL_KEY_ARROW_RIGHT, "Right"}, {CONIO_SPECIAL_KEY_NONE, "None"}, + {CONIO_FUNCTION_KEY_F1, "F01"}, + {CONIO_FUNCTION_KEY_F2, "F02"}, + {CONIO_FUNCTION_KEY_F3, "F03"}, + {CONIO_FUNCTION_KEY_F4, "F04"}, + {CONIO_FUNCTION_KEY_F5, "F05"}, + {CONIO_FUNCTION_KEY_F6, "F06"}, + {CONIO_FUNCTION_KEY_F7, "F07"}, + {CONIO_FUNCTION_KEY_F8, "F08"}, + {CONIO_FUNCTION_KEY_F9, "F09"}, + {CONIO_FUNCTION_KEY_F10, "F10"}, + {CONIO_FUNCTION_KEY_F11, "F11"}, + {CONIO_FUNCTION_KEY_F12, "F12"}, + {CONIO_SPECIAL_KEY_ESC, "Esc"}, + {CONIO_SPECIAL_KEY_BACKSPACE, "Bs"}, + {CONIO_SPECIAL_KEY_TAB, "Tab"}, + {CONIO_SPECIAL_KEY_ENTER, "Enter"}, + {CONIO_SPECIAL_KEY_INSERT, "Ins"}, + {CONIO_SPECIAL_KEY_HOME, "Home"}, + {CONIO_SPECIAL_KEY_PAGE_UP, "PgUp"}, + {CONIO_SPECIAL_KEY_DELETE, "Del"}, + {CONIO_SPECIAL_KEY_END, "End"}, + {CONIO_SPECIAL_KEY_PAGE_DOWN, "PgDw"}, + {CONIO_SPECIAL_KEY_ARROW_UP, "Up"}, + {CONIO_SPECIAL_KEY_ARROW_LEFT, "Left"}, + {CONIO_SPECIAL_KEY_ARROW_DOWN, "Down"}, + {CONIO_SPECIAL_KEY_ARROW_RIGHT, "Right"}, + {CONIO_SPECIAL_KEY_NONE, "None"}, }; -BofConio::BofConio(const BOF_CONIO_PARAM &_rBofConioParam_X) +bool BofConio::S_Initialize(const BOF_CONIO_PARAM &_rBofConioParam_X) { - mBofConioParam_X = _rBofConioParam_X; + bool Rts_B = true; + + S_mBofConioParam_X = _rBofConioParam_X; + S_mpTextForeAttribute_c[0] = 0; + S_mpTextBackAttribute_c[0] = 0; + S_mpTextAttribute_c[0] = 0; + S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL); + S_SetForegroundTextColor(CONIO_TEXT_COLOR_BRIGHT_WHITE); + S_SetBackgroundTextColor(CONIO_TEXT_COLOR_BLACK); #if defined(_WIN32) CONSOLE_SCREEN_BUFFER_INFO ScreenBufferInfo_X; @@ -91,13 +127,25 @@ BofConio::BofConio(const BOF_CONIO_PARAM &_rBofConioParam_X) // mConsoleHeight_U32 = ScreenBufferInfo_X.ws_col; // mConsoleHeight_U32 = atoi(getenv("LINES")); // mConsoleWidth_U32 = atoi(getenv("COLUMNS")); - mConsoleHeight_U32 = 80; - mConsoleWidth_U32 = 24; + S_mConsoleHeight_U32 = 80; + S_mConsoleWidth_U32 = 24; + struct winsize ScreenBufferInfo_X; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ScreenBufferInfo_X) == 0) + { + S_mConsoleHeight_U32 = ScreenBufferInfo_X.ws_row; + S_mConsoleWidth_U32 = ScreenBufferInfo_X.ws_col; + } + else + { + // Fallback to environment variables if ioctl fails + S_mConsoleHeight_U32 = atoi(getenv("LINES")); + S_mConsoleWidth_U32 = atoi(getenv("COLUMNS")); + } #endif - linenoise::SetMultiLine(mBofConioParam_X.EditOnMultiLine_B); - linenoise::SetHistoryMaxLen(mBofConioParam_X.HistoryMaxLength_U32); - linenoise::LoadHistory(mBofConioParam_X.HistoryPathname.FullPathName(false).c_str()); + linenoise::SetMultiLine(S_mBofConioParam_X.EditOnMultiLine_B); + linenoise::SetHistoryMaxLen(S_mBofConioParam_X.HistoryMaxLength_U32); + linenoise::LoadHistory(S_mBofConioParam_X.HistoryPathname.FullPathName(false).c_str()); // Setup completion words every time when a user types tab linenoise::SetCompletionCallback([](const char *_pEditBuffer_c, std::vector &_rCompletionCollection) { @@ -110,14 +158,17 @@ BofConio::BofConio(const BOF_CONIO_PARAM &_rBofConioParam_X) } */ }); + return Rts_B; } -BofConio::~BofConio() +bool BofConio::S_Shutdown() { - linenoise::SaveHistory(mBofConioParam_X.HistoryPathname.FullPathName(false).c_str()); + bool Rts_B = true; + linenoise::SaveHistory(S_mBofConioParam_X.HistoryPathname.FullPathName(false).c_str()); + return Rts_B; } -std::string BofConio::KeyToString(uint32_t _Key_U32) +std::string BofConio::S_KeyToString(uint32_t _Key_U32) { std::string Rts_S, Modifier_S; @@ -186,7 +237,7 @@ std::string BofConio::KeyToString(uint32_t _Key_U32) return Rts_S; } -uint32_t BofConio::KbHit(uint32_t _TimeOutInMs_U32) +uint32_t BofConio::S_KbHit(uint32_t _TimeOutInMs_U32) { uint32_t Rts_U32 = 0; @@ -224,7 +275,7 @@ uint32_t BofConio::KbHit(uint32_t _TimeOutInMs_U32) { if (read(STDIN_FILENO, &Ch_c, 1) == 1) { - Rts_U32 = GetChAfterOneChar(false, Ch_c); + Rts_U32 = S_GetChAfterOneChar(false, Ch_c); } linenoise::disableRawMode(STDIN_FILENO); } @@ -239,7 +290,7 @@ uint32_t BofConio::KbHit(uint32_t _TimeOutInMs_U32) return Rts_U32; } -uint32_t BofConio::GetChAfterOneChar(bool _OnlyModifier_B, char _FirstChar_c) +uint32_t BofConio::S_GetChAfterOneChar(bool _OnlyModifier_B, char _FirstChar_c) { uint32_t Rts_U32, Modifier_U32; @@ -388,160 +439,160 @@ uint32_t BofConio::GetChAfterOneChar(bool _OnlyModifier_B, char _FirstChar_c) { switch (pEscSeq_c[0]) { - case 'O': - switch (pEscSeq_c[1]) - { - case 'P': - Rts_U32 = CONIO_FUNCTION_KEY_F1; - break; - - case 'Q': - Rts_U32 = CONIO_FUNCTION_KEY_F2; - break; - - case 'R': - Rts_U32 = CONIO_FUNCTION_KEY_F3; - break; - - case 'S': - Rts_U32 = CONIO_FUNCTION_KEY_F4; - break; - - default: - break; - } - break; - - case '[': - if (pEscSeq_c[1] >= '0' && pEscSeq_c[1] <= '9') - { - /* Extended escape, read additional byte. */ - if (read(STDIN_FILENO, &pEscSeq_c[2], 1) != 1) - { - break; - } - } - switch (pEscSeq_c[1]) - { - case '1': - switch (pEscSeq_c[2]) - { - case '5': - Rts_U32 = CONIO_FUNCTION_KEY_F5; - break; - - case '7': // Not a mistake (no 6) - Rts_U32 = CONIO_FUNCTION_KEY_F6; - break; - - case '8': - Rts_U32 = CONIO_FUNCTION_KEY_F7; - break; - - case '9': - Rts_U32 = CONIO_FUNCTION_KEY_F8; - break; - - default: - break; - } - break; - - case '2': - switch (pEscSeq_c[2]) + case 'O': + switch (pEscSeq_c[1]) { - case '0': - Rts_U32 = CONIO_FUNCTION_KEY_F9; - break; - - case '1': - Rts_U32 = CONIO_FUNCTION_KEY_F10; - break; - - case '3': // Or 4 can't test F11 fullscreen - Rts_U32 = CONIO_FUNCTION_KEY_F11; - break; - - case '4': - Rts_U32 = CONIO_FUNCTION_KEY_F12; - break; + case 'P': + Rts_U32 = CONIO_FUNCTION_KEY_F1; + break; - case '~': - Rts_U32 = CONIO_SPECIAL_KEY_INSERT; - break; + case 'Q': + Rts_U32 = CONIO_FUNCTION_KEY_F2; + break; - default: - break; - } - break; + case 'R': + Rts_U32 = CONIO_FUNCTION_KEY_F3; + break; - case '3': - switch (pEscSeq_c[2]) - { - case '~': - Rts_U32 = CONIO_SPECIAL_KEY_DELETE; - break; + case 'S': + Rts_U32 = CONIO_FUNCTION_KEY_F4; + break; - default: - break; + default: + break; } break; - case '5': - switch (pEscSeq_c[2]) + case '[': + if (pEscSeq_c[1] >= '0' && pEscSeq_c[1] <= '9') { - case '~': - Rts_U32 = CONIO_SPECIAL_KEY_PAGE_UP; - break; - - default: - break; + /* Extended escape, read additional byte. */ + if (read(STDIN_FILENO, &pEscSeq_c[2], 1) != 1) + { + break; + } } - break; - - case '6': - switch (pEscSeq_c[2]) + switch (pEscSeq_c[1]) { - case '~': - Rts_U32 = CONIO_SPECIAL_KEY_PAGE_DOWN; - break; - - default: - break; + case '1': + switch (pEscSeq_c[2]) + { + case '5': + Rts_U32 = CONIO_FUNCTION_KEY_F5; + break; + + case '7': // Not a mistake (no 6) + Rts_U32 = CONIO_FUNCTION_KEY_F6; + break; + + case '8': + Rts_U32 = CONIO_FUNCTION_KEY_F7; + break; + + case '9': + Rts_U32 = CONIO_FUNCTION_KEY_F8; + break; + + default: + break; + } + break; + + case '2': + switch (pEscSeq_c[2]) + { + case '0': + Rts_U32 = CONIO_FUNCTION_KEY_F9; + break; + + case '1': + Rts_U32 = CONIO_FUNCTION_KEY_F10; + break; + + case '3': // Or 4 can't test F11 fullscreen + Rts_U32 = CONIO_FUNCTION_KEY_F11; + break; + + case '4': + Rts_U32 = CONIO_FUNCTION_KEY_F12; + break; + + case '~': + Rts_U32 = CONIO_SPECIAL_KEY_INSERT; + break; + + default: + break; + } + break; + + case '3': + switch (pEscSeq_c[2]) + { + case '~': + Rts_U32 = CONIO_SPECIAL_KEY_DELETE; + break; + + default: + break; + } + break; + + case '5': + switch (pEscSeq_c[2]) + { + case '~': + Rts_U32 = CONIO_SPECIAL_KEY_PAGE_UP; + break; + + default: + break; + } + break; + + case '6': + switch (pEscSeq_c[2]) + { + case '~': + Rts_U32 = CONIO_SPECIAL_KEY_PAGE_DOWN; + break; + + default: + break; + } + break; + + case 'A': + Rts_U32 = CONIO_SPECIAL_KEY_ARROW_UP; + break; + + case 'B': + Rts_U32 = CONIO_SPECIAL_KEY_ARROW_DOWN; + break; + + case 'C': + Rts_U32 = CONIO_SPECIAL_KEY_ARROW_RIGHT; + break; + + case 'D': + Rts_U32 = CONIO_SPECIAL_KEY_ARROW_LEFT; + break; + + case 'F': + Rts_U32 = CONIO_SPECIAL_KEY_END; + break; + + case 'H': + Rts_U32 = CONIO_SPECIAL_KEY_HOME; + break; + + default: + break; } - break; - - case 'A': - Rts_U32 = CONIO_SPECIAL_KEY_ARROW_UP; - break; - - case 'B': - Rts_U32 = CONIO_SPECIAL_KEY_ARROW_DOWN; - break; - - case 'C': - Rts_U32 = CONIO_SPECIAL_KEY_ARROW_RIGHT; - break; - - case 'D': - Rts_U32 = CONIO_SPECIAL_KEY_ARROW_LEFT; - break; - - case 'F': - Rts_U32 = CONIO_SPECIAL_KEY_END; - break; - - case 'H': - Rts_U32 = CONIO_SPECIAL_KEY_HOME; - break; - - default: - break; - } } // switch - } // if ((read(STDIN_FILENO, &pEscSeq_c[0], 1) == 1) && (read(STDIN_FILENO, &pEscSeq_c[1], 1) == 1)) - } // if (poll(&Poll_X, 1, 1) == 1) - } // else if (Rts_U32==CONIO_SPECIAL_KEY_ESC) + } // if ((read(STDIN_FILENO, &pEscSeq_c[0], 1) == 1) && (read(STDIN_FILENO, &pEscSeq_c[1], 1) == 1)) + } // if (poll(&Poll_X, 1, 1) == 1) + } // else if (Rts_U32==CONIO_SPECIAL_KEY_ESC) if (_OnlyModifier_B) { Rts_U32 = 0; @@ -551,7 +602,7 @@ uint32_t BofConio::GetChAfterOneChar(bool _OnlyModifier_B, char _FirstChar_c) return Rts_U32; } -uint32_t BofConio::GetCh(bool _OnlyModifier_B) +uint32_t BofConio::S_GetCh(bool _OnlyModifier_B) { uint32_t Rts_U32, Modifier_U32; @@ -559,7 +610,7 @@ uint32_t BofConio::GetCh(bool _OnlyModifier_B) Rts_U32 = 0; #if defined(_WIN32) - Rts_U32 = GetChAfterOneChar(_OnlyModifier_B, _OnlyModifier_B ? 0 : _getch()); + Rts_U32 = S_GetChAfterOneChar(_OnlyModifier_B, _OnlyModifier_B ? 0 : _getch()); #else char Ch_c; if (isatty(STDIN_FILENO)) @@ -568,7 +619,7 @@ uint32_t BofConio::GetCh(bool _OnlyModifier_B) { if (read(STDIN_FILENO, &Ch_c, 1) == 1) { - Rts_U32 = GetChAfterOneChar(_OnlyModifier_B, Ch_c); + Rts_U32 = S_GetChAfterOneChar(_OnlyModifier_B, Ch_c); } linenoise::disableRawMode(STDIN_FILENO); if (_OnlyModifier_B) @@ -583,7 +634,7 @@ uint32_t BofConio::GetCh(bool _OnlyModifier_B) } // use ctrl-c to exit -BOFERR BofConio::Readline(const std::string &_rPrompt_S, std::string &_rInputLine_S) +BOFERR BofConio::S_Readline(const std::string &_rPrompt_S, std::string &_rInputLine_S) { BOFERR Rts_E; @@ -597,353 +648,353 @@ BOFERR BofConio::Readline(const std::string &_rPrompt_S, std::string &_rInputLin return Rts_E; } - -BOFERR BofConio::SetTextAttribute(uint32_t _TextAttributeFlag_U32) // Use CONIO_TEXT_ATTRIBUTE_FLAG enum ored value +/* +Text Attributes: +Bold: \033[1m +Faint: \033[2m +Italic: \033[3m +Underline: \033[4m +Blink Slow: \033[5m +Blink Fast: \033[6m +Reverse: \033[7m +Conceal: \033[8m +Strike: \033[9m +Frame: \033[51m +Encircle: \033[52m +Overline: \033[53m + +Reset Attributes: +Reset All: \033[0m +Reset Bold/Faint: \033[22m +Reset Italic: \033[23m +Reset Underline: \033[24m +Reset Blink: \033[25m +Reset Reverse: \033[27m +Reset Conceal: \033[28m +Reset Strike: \033[29m +Reset Frame/Encircle/Overline: \033[54m +*/ +BOFERR BofConio::S_SetTextAttribute(uint32_t _TextAttributeFlag_U32) // Use CONIO_TEXT_ATTRIBUTE_FLAG enum ored value { - BOFERR Rts_E = BOF_ERR_NOT_SUPPORTED; + BOFERR Rts_E = BOF_ERR_NO_ERROR; + char *p_c; + p_c = S_mpTextAttribute_c; -#if defined(_WIN32) - char pAttribute_c[64], *p_c; - p_c = pAttribute_c; + // CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL + *p_c++ = 033; + *p_c++ = '['; + *p_c++ = '0'; // Reset all attributes - if ((_TextAttributeFlag_U32 & ~(CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL | CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE)) == 0) + if (_TextAttributeFlag_U32 & (CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_FAINT | CONIO_TEXT_ATTRIBUTE_FLAG_ITALIC | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_BLINK_SLOW | CONIO_TEXT_ATTRIBUTE_FLAG_BLINK_FAST | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE | CONIO_TEXT_ATTRIBUTE_FLAG_CONCEAL | CONIO_TEXT_ATTRIBUTE_FLAG_STRIKE | CONIO_TEXT_ATTRIBUTE_FLAG_FRAME | CONIO_TEXT_ATTRIBUTE_FLAG_ENCIRCLE | CONIO_TEXT_ATTRIBUTE_FLAG_OVERLINE)) { - *p_c++ = 0x1B; - *p_c++ = '['; - *p_c++ = (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_BOLD) ? '1' : '0'; - *p_c++ = ';'; + auto AppendAttribute = [&](const char *_pAttr_c) { + *p_c++ = ';'; + while (*_pAttr_c) + { + *p_c++ = *_pAttr_c++; + } + }; + + if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_BOLD) + AppendAttribute("1"); + if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_FAINT) + AppendAttribute("2"); + if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_ITALIC) + AppendAttribute("3"); if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE) - { - *p_c++ = '4'; - } - else - { - *p_c++ = '2'; - *p_c++ = '4'; - } - *p_c++ = ';'; - + AppendAttribute("4"); + if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_BLINK_SLOW) + AppendAttribute("5"); + if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_BLINK_FAST) + AppendAttribute("6"); if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE) - { - *p_c++ = '7'; - } - else - { - *p_c++ = '2'; - *p_c++ = '7'; - } - *p_c++ = 'm'; - *p_c = 0; - printf("%s", pAttribute_c); - - Rts_E = BOF_ERR_NO_ERROR; + AppendAttribute("7"); + if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_CONCEAL) + AppendAttribute("8"); + if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_STRIKE) + AppendAttribute("9"); + if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_FRAME) + AppendAttribute("51"); + if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_ENCIRCLE) + AppendAttribute("52"); + if (_TextAttributeFlag_U32 & CONIO_TEXT_ATTRIBUTE_FLAG_OVERLINE) + AppendAttribute("53"); } -#else - _TextAttributeFlag_U32 = _TextAttributeFlag_U32; -#endif + *p_c = 0; + printf("%s%s%sm", S_mpTextAttribute_c, S_mpTextForeAttribute_c, S_mpTextBackAttribute_c); return Rts_E; } -BOFERR BofConio::SetForegroundTextColor(CONIO_TEXT_COLOR _ForegroundColor_E) +BOFERR BofConio::S_SetForegroundTextColor(CONIO_TEXT_COLOR _ForegroundColor_E) { BOFERR Rts_E = BOF_ERR_NO_ERROR; - char pAttribute_c[64], *p_c; - p_c = pAttribute_c; - *p_c++ = 0x1B; - *p_c++ = '['; + char *p_c; + p_c = S_mpTextForeAttribute_c; + *p_c++ = ';'; switch (_ForegroundColor_E) { - case CONIO_TEXT_COLOR_BLACK: - *p_c++ = '3'; - *p_c++ = '0'; - break; - - case CONIO_TEXT_COLOR_RED: - *p_c++ = '3'; - *p_c++ = '1'; - break; - - case CONIO_TEXT_COLOR_GREEN: - *p_c++ = '3'; - *p_c++ = '2'; - break; - - case CONIO_TEXT_COLOR_YELLOW: - *p_c++ = '3'; - *p_c++ = '3'; - break; - - case CONIO_TEXT_COLOR_BLUE: - *p_c++ = '3'; - *p_c++ = '4'; - break; - - case CONIO_TEXT_COLOR_MAGENTA: - *p_c++ = '3'; - *p_c++ = '5'; - break; - - case CONIO_TEXT_COLOR_CYAN: - *p_c++ = '3'; - *p_c++ = '6'; - break; - - case CONIO_TEXT_COLOR_WHITE: - *p_c++ = '3'; - *p_c++ = '7'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_BLACK: - *p_c++ = '9'; - *p_c++ = '0'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_RED: - *p_c++ = '9'; - *p_c++ = '1'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_GREEN: - *p_c++ = '9'; - *p_c++ = '2'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_YELLOW: - *p_c++ = '9'; - *p_c++ = '3'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_BLUE: - *p_c++ = '9'; - *p_c++ = '4'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_MAGENTA: - *p_c++ = '9'; - *p_c++ = '5'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_CYAN: - *p_c++ = '9'; - *p_c++ = '6'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_WHITE: - default: - *p_c++ = '9'; - *p_c++ = '7'; - break; + case CONIO_TEXT_COLOR_BLACK: + *p_c++ = '3'; + *p_c++ = '0'; + break; + + case CONIO_TEXT_COLOR_RED: + *p_c++ = '3'; + *p_c++ = '1'; + break; + + case CONIO_TEXT_COLOR_GREEN: + *p_c++ = '3'; + *p_c++ = '2'; + break; + + case CONIO_TEXT_COLOR_YELLOW: + *p_c++ = '3'; + *p_c++ = '3'; + break; + + case CONIO_TEXT_COLOR_BLUE: + *p_c++ = '3'; + *p_c++ = '4'; + break; + + case CONIO_TEXT_COLOR_MAGENTA: + *p_c++ = '3'; + *p_c++ = '5'; + break; + + case CONIO_TEXT_COLOR_CYAN: + *p_c++ = '3'; + *p_c++ = '6'; + break; + + case CONIO_TEXT_COLOR_WHITE: + *p_c++ = '3'; + *p_c++ = '7'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_BLACK: + *p_c++ = '9'; + *p_c++ = '0'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_RED: + *p_c++ = '9'; + *p_c++ = '1'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_GREEN: + *p_c++ = '9'; + *p_c++ = '2'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_YELLOW: + *p_c++ = '9'; + *p_c++ = '3'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_BLUE: + *p_c++ = '9'; + *p_c++ = '4'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_MAGENTA: + *p_c++ = '9'; + *p_c++ = '5'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_CYAN: + *p_c++ = '9'; + *p_c++ = '6'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_WHITE: + default: + *p_c++ = '9'; + *p_c++ = '7'; + break; } - *p_c++ = 'm'; *p_c = 0; - printf("%s", pAttribute_c); - + printf("%s%s%sm", S_mpTextAttribute_c, S_mpTextForeAttribute_c, S_mpTextBackAttribute_c); return Rts_E; } -BOFERR BofConio::SetBackgroundTextColor(CONIO_TEXT_COLOR _BackgroundColor_E) +BOFERR BofConio::S_SetBackgroundTextColor(CONIO_TEXT_COLOR _BackgroundColor_E) { BOFERR Rts_E = BOF_ERR_NO_ERROR; - char pAttribute_c[64], *p_c; - p_c = pAttribute_c; - *p_c++ = 0x1B; - *p_c++ = '['; + char *p_c; + p_c = S_mpTextBackAttribute_c; + *p_c++ = ';'; switch (_BackgroundColor_E) { - case CONIO_TEXT_COLOR_BLACK: - *p_c++ = '4'; - *p_c++ = '0'; - break; - - case CONIO_TEXT_COLOR_RED: - *p_c++ = '4'; - *p_c++ = '1'; - break; - - case CONIO_TEXT_COLOR_GREEN: - *p_c++ = '4'; - *p_c++ = '2'; - break; - - case CONIO_TEXT_COLOR_YELLOW: - *p_c++ = '4'; - *p_c++ = '3'; - break; - - case CONIO_TEXT_COLOR_BLUE: - *p_c++ = '4'; - *p_c++ = '4'; - break; - - case CONIO_TEXT_COLOR_MAGENTA: - *p_c++ = '4'; - *p_c++ = '5'; - break; - - case CONIO_TEXT_COLOR_CYAN: - *p_c++ = '4'; - *p_c++ = '6'; - break; - - case CONIO_TEXT_COLOR_WHITE: - *p_c++ = '4'; - *p_c++ = '7'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_BLACK: - *p_c++ = '1'; - *p_c++ = '0'; - *p_c++ = '0'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_RED: - *p_c++ = '1'; - *p_c++ = '0'; - *p_c++ = '1'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_GREEN: - *p_c++ = '1'; - *p_c++ = '0'; - *p_c++ = '2'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_YELLOW: - *p_c++ = '1'; - *p_c++ = '0'; - *p_c++ = '3'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_BLUE: - *p_c++ = '1'; - *p_c++ = '0'; - *p_c++ = '4'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_MAGENTA: - *p_c++ = '1'; - *p_c++ = '0'; - *p_c++ = '5'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_CYAN: - *p_c++ = '1'; - *p_c++ = '0'; - *p_c++ = '6'; - break; - - case CONIO_TEXT_COLOR_BRIGHT_WHITE: - default: - *p_c++ = '1'; - *p_c++ = '0'; - *p_c++ = '7'; - break; + case CONIO_TEXT_COLOR_BLACK: + *p_c++ = '4'; + *p_c++ = '0'; + break; + + case CONIO_TEXT_COLOR_RED: + *p_c++ = '4'; + *p_c++ = '1'; + break; + + case CONIO_TEXT_COLOR_GREEN: + *p_c++ = '4'; + *p_c++ = '2'; + break; + + case CONIO_TEXT_COLOR_YELLOW: + *p_c++ = '4'; + *p_c++ = '3'; + break; + + case CONIO_TEXT_COLOR_BLUE: + *p_c++ = '4'; + *p_c++ = '4'; + break; + + case CONIO_TEXT_COLOR_MAGENTA: + *p_c++ = '4'; + *p_c++ = '5'; + break; + + case CONIO_TEXT_COLOR_CYAN: + *p_c++ = '4'; + *p_c++ = '6'; + break; + + case CONIO_TEXT_COLOR_WHITE: + *p_c++ = '4'; + *p_c++ = '7'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_BLACK: + *p_c++ = '1'; + *p_c++ = '0'; + *p_c++ = '0'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_RED: + *p_c++ = '1'; + *p_c++ = '0'; + *p_c++ = '1'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_GREEN: + *p_c++ = '1'; + *p_c++ = '0'; + *p_c++ = '2'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_YELLOW: + *p_c++ = '1'; + *p_c++ = '0'; + *p_c++ = '3'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_BLUE: + *p_c++ = '1'; + *p_c++ = '0'; + *p_c++ = '4'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_MAGENTA: + *p_c++ = '1'; + *p_c++ = '0'; + *p_c++ = '5'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_CYAN: + *p_c++ = '1'; + *p_c++ = '0'; + *p_c++ = '6'; + break; + + case CONIO_TEXT_COLOR_BRIGHT_WHITE: + default: + *p_c++ = '1'; + *p_c++ = '0'; + *p_c++ = '7'; + break; } - *p_c++ = 'm'; *p_c = 0; - printf("%s", pAttribute_c); - + printf("%s%s%sm", S_mpTextAttribute_c, S_mpTextForeAttribute_c, S_mpTextBackAttribute_c); return Rts_E; } -BOFERR BofConio::SetForegroundTextColor(BOF_RGBA _ForegroundColor_X) +BOFERR BofConio::S_SetForegroundTextColor(BOF_RGBA _ForegroundColor_X) { BOFERR Rts_E = BOF_ERR_NO_ERROR; - char pAttribute_c[64]; - sprintf(pAttribute_c, "\x1b[38;2;%d;%d;%dm", _ForegroundColor_X.r, _ForegroundColor_X.g, _ForegroundColor_X.b); - printf("%s", pAttribute_c); + sprintf(S_mpTextForeAttribute_c, ";38;2;%d;%d;%d", _ForegroundColor_X.r, _ForegroundColor_X.g, _ForegroundColor_X.b); + printf("%s%s%sm", S_mpTextAttribute_c, S_mpTextForeAttribute_c, S_mpTextBackAttribute_c); return Rts_E; } -BOFERR BofConio::SetBackgroundTextColor(BOF_RGBA _BackgroundColor_X) +BOFERR BofConio::S_SetBackgroundTextColor(BOF_RGBA _BackgroundColor_X) { BOFERR Rts_E = BOF_ERR_NO_ERROR; - char pAttribute_c[64]; - sprintf(pAttribute_c, "\x1b[48;2;%d;%d;%dm", _BackgroundColor_X.r, _BackgroundColor_X.g, _BackgroundColor_X.b); - printf("%s", pAttribute_c); + sprintf(S_mpTextBackAttribute_c, ";48;2;%d;%d;%d", _BackgroundColor_X.r, _BackgroundColor_X.g, _BackgroundColor_X.b); + printf("%s%s%sm", S_mpTextAttribute_c, S_mpTextForeAttribute_c, S_mpTextBackAttribute_c); return Rts_E; } -BOFERR BofConio::SetTextCursorState(CONIO_TEXT_CURSOR_STATE _CursorState_E) +BOFERR BofConio::S_SetTextCursorState(CONIO_TEXT_CURSOR_STATE _CursorState_E) { BOFERR Rts_E = BOF_ERR_NO_ERROR; char pAttribute_c[64], *p_c; p_c = pAttribute_c; - *p_c++ = 0x1B; + *p_c++ = 033; *p_c++ = '['; *p_c++ = '?'; switch (_CursorState_E) { - case CONIO_TEXT_CURSOR_STATE_BLINK_ON: - *p_c++ = '1'; - *p_c++ = '2'; - *p_c++ = 'h'; - break; - - case CONIO_TEXT_CURSOR_STATE_BLINK_OFF: - *p_c++ = '1'; - *p_c++ = '2'; - *p_c++ = 'l'; - break; - - case CONIO_TEXT_CURSOR_STATE_OFF: - *p_c++ = '2'; - *p_c++ = '5'; - *p_c++ = 'l'; - break; - - default: - case CONIO_TEXT_CURSOR_STATE_ON: - *p_c++ = '2'; - *p_c++ = '5'; - *p_c++ = 'h'; - break; + case CONIO_TEXT_CURSOR_STATE_BLINK_ON: + *p_c++ = '1'; + *p_c++ = '2'; + *p_c++ = 'h'; + break; + + case CONIO_TEXT_CURSOR_STATE_BLINK_OFF: + *p_c++ = '1'; + *p_c++ = '2'; + *p_c++ = 'l'; + break; + + case CONIO_TEXT_CURSOR_STATE_OFF: + *p_c++ = '2'; + *p_c++ = '5'; + *p_c++ = 'l'; + break; + + default: + case CONIO_TEXT_CURSOR_STATE_ON: + *p_c++ = '2'; + *p_c++ = '5'; + *p_c++ = 'h'; + break; } *p_c = 0; printf("%s", pAttribute_c); return Rts_E; } -BOFERR BofConio::SetTextCursorPosition(uint32_t _x_U32, uint32_t _y_U32) +BOFERR BofConio::S_SetTextCursorPosition(uint32_t _x_U32, uint32_t _y_U32) { BOFERR Rts_E = BOF_ERR_EINVAL; - if ((_x_U32 >= 1) && (_x_U32 <= mConsoleWidth_U32) && (_y_U32 >= 1) && (_y_U32 <= mConsoleHeight_U32)) + if ((_x_U32 >= 1) && (_x_U32 <= S_mConsoleWidth_U32) && (_y_U32 >= 1) && (_y_U32 <= S_mConsoleHeight_U32)) { Rts_E = BOF_ERR_NO_ERROR; - printf("\x1b[%d;%df", _y_U32, _x_U32); + printf("\033[%d;%df", _y_U32, _x_U32); } return Rts_E; } // https://github.com/sol-prog/ansi-escape-codes-windows-posix-terminals-c-programming-examples/blob/master/ansi_escapes.c - -BOFERR BofConio::GetTextCursorPosition(uint32_t &_rX_U32, uint32_t &_rY_U32) +BOFERR BofConio::S_GetTextCursorPosition(uint32_t &_rX_U32, uint32_t &_rY_U32) { BOFERR Rts_E = BOF_ERR_INPUT; #if defined(_WIN32) - -#if 0 - char pAttribute_c[64]; - int Char_i, Index_i; - printf("\x1b[6n"); - - Index_i = 0; - do - { - Char_i = getchar(); - pAttribute_c[Index_i++] = (char)Char_i; - if (Char_i == 'R') - { - pAttribute_c[Index_i] = 0; - sscanf(pAttribute_c, "\x1b[%d;%dR", &_rY_U32, &_rX_U32); - Rts_E = BOF_ERR_NO_ERROR; - break; - } - } while (Index_i < sizeof(pAttribute_c) - 4); - fseek(stdin, 0, SEEK_END); -#else HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; if (GetConsoleScreenBufferInfo(console, &csbi)) @@ -951,84 +1002,117 @@ BOFERR BofConio::GetTextCursorPosition(uint32_t &_rX_U32, uint32_t &_rY_U32) _rX_U32 = csbi.dwCursorPosition.X; _rY_U32 = csbi.dwCursorPosition.Y; } -#endif #else - _rX_U32 = 1; - _rY_U32 = 1; + // Save the current terminal settings + struct termios IosOriginalAttribute_X; + struct termios IosNewAttribute_X; + fd_set ReadFd_X; + struct timeval To_X; + int Sts_i; + char pResponse_c[0x100]; + + tcgetattr(STDIN_FILENO, &IosOriginalAttribute_X); + + // Set the terminal to raw mode + IosNewAttribute_X = IosOriginalAttribute_X; + IosNewAttribute_X.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &IosNewAttribute_X); + + // Write the cursor position query to the terminal + printf("\033[6n"); + fflush(stdout); + + // Use select to wait for input with a To_X + FD_ZERO(&ReadFd_X); + FD_SET(STDIN_FILENO, &ReadFd_X); + + To_X.tv_sec = 1; + To_X.tv_usec = 0; + _rX_U32 = 0; + _rY_U32 = 0; + Sts_i = select(STDIN_FILENO + 1, &ReadFd_X, NULL, NULL, &To_X); + if ((Sts_i > 0) && (FD_ISSET(STDIN_FILENO, &ReadFd_X))) + { + // Read the pResponse_c from the terminal + if (read(STDIN_FILENO, pResponse_c, sizeof(pResponse_c)) > 0) + { + // Parse the pResponse_c + if (sscanf(pResponse_c, "\033[%u;%uR", &_rY_U32, &_rX_U32) == 2) + { + Rts_E = BOF_ERR_NO_ERROR; + } + } + } + // Restore the original terminal settings + tcsetattr(STDIN_FILENO, TCSANOW, &IosOriginalAttribute_X); #endif return Rts_E; } -BOFERR BofConio::SetTextWindowTitle(const std::string &_rTitle_S) +BOFERR BofConio::S_SetTextWindowTitle(const char *_pTitle_c) { - BOFERR Rts_E = BOF_ERR_NO_ERROR; - std::string Attribute_S; - Attribute_S = Bof_Sprintf("\x1b]2;%s\x07", _rTitle_S.c_str()); - printf("%s", Attribute_S.c_str()); - - // char pAttribute_c[64], *p_c; - // sprintf(pAttribute_c, "\x1b]2;%s\x07", _rTitle_S.c_str()); - // printf("%s", pAttribute_c); + BOFERR Rts_E = BOF_ERR_EINVAL; + char pAttribute_c[64]; + if (_pTitle_c) + { + Rts_E = BOF_ERR_NO_ERROR; + sprintf(pAttribute_c, "033]2;%s\x07", _pTitle_c); + printf("%s", pAttribute_c); + } return Rts_E; } -BOFERR BofConio::Clear(CONIO_CLEAR _ClearType_E) +BOFERR BofConio::S_Clear(CONIO_CLEAR _ClearType_E) { BOFERR Rts_E = BOF_ERR_NO_ERROR; char pAttribute_c[64], *p_c; p_c = pAttribute_c; - *p_c++ = 0x1B; + *p_c++ = 033; *p_c++ = '['; switch (_ClearType_E) { - case CONIO_CLEAR_LINE_FROM_CURSOR_TO_END: - *p_c++ = '0'; - *p_c++ = 'd'; - *p_c++ = 'K'; - break; - - case CONIO_CLEAR_LINE_FROM_CURSOR_TO_BEGIN: - *p_c++ = '1'; - *p_c++ = 'd'; - *p_c++ = 'K'; - break; - - case CONIO_CLEAR_LINE: - *p_c++ = 'K'; - break; - - case CONIO_CLEAR_ALL_FROM_CURSOR_TO_END: - *p_c++ = '0'; - *p_c++ = 'J'; - break; - - case CONIO_CLEAR_ALL_FROM_CURSOR_TO_BEGIN: - *p_c++ = '1'; - *p_c++ = 'J'; - break; - - default: - case CONIO_CLEAR_ALL: - SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL); - SetTextCursorPosition(1, 1); - *p_c++ = '2'; - *p_c++ = 'J'; - break; + case CONIO_CLEAR_LINE_FROM_CURSOR_TO_END: + *p_c++ = '0'; + *p_c++ = 'd'; + *p_c++ = 'K'; + break; + + case CONIO_CLEAR_LINE_FROM_CURSOR_TO_BEGIN: + *p_c++ = '1'; + *p_c++ = 'd'; + *p_c++ = 'K'; + break; + + case CONIO_CLEAR_LINE: + *p_c++ = 'K'; + break; + + case CONIO_CLEAR_ALL_FROM_CURSOR_TO_END: + *p_c++ = '0'; + *p_c++ = 'J'; + break; + + case CONIO_CLEAR_ALL_FROM_CURSOR_TO_BEGIN: + *p_c++ = '1'; + *p_c++ = 'J'; + break; + + default: + case CONIO_CLEAR_ALL: + S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL); + S_SetTextCursorPosition(1, 1); + *p_c++ = '2'; + *p_c++ = 'J'; + break; } *p_c = 0; printf("%s", pAttribute_c); return Rts_E; } -BOFERR BofConio::Reset() -{ - BOFERR Rts_E = BOF_ERR_NO_ERROR; - printf("\x1b[0m"); - return Rts_E; -} -BOFERR BofConio::Printf(const char *_pFormat_c, ...) +BOFERR BofConio::S_PrintLine(const char *_pFormat_c, ...) { BOFERR Rts_E = BOF_ERR_NO_ERROR; char pText_c[0x1000]; @@ -1037,106 +1121,38 @@ BOFERR BofConio::Printf(const char *_pFormat_c, ...) va_start(Arg, _pFormat_c); vsnprintf(pText_c, sizeof(pText_c), _pFormat_c, Arg); va_end(Arg); - - printf("%s", pText_c); - + printf("%s%s%sm%s", S_mpTextAttribute_c, S_mpTextForeAttribute_c, S_mpTextBackAttribute_c, pText_c); + // printf("%s%sm%s", S_mpTextAttribute_c, S_mpTextForeAttribute_c, pText_c); + // printf("%s", pText_c); /* - std::cout << (bold, underline) << "Testing common SGR codes:\n"; - std::cout << (bold) << "Bold text\n"; - std::cout << (underline) << "Underlined text\n"; - std::cout << (reverse) << "Reversed text\n\n"; - - std::cout << (bold, underline) << "Testing unusual codes:\n"; - std::cout << (faint) << "Faint"; - std::cout << " "; - std::cout << (italic) << "Italic"; - std::cout << " "; - std::cout << (blink_slow) << "Blinking slowly"; - std::cout << " "; - std::cout << (blink_fast) << "Blinking quickly"; - std::cout << "\n"; - std::cout << (conceal) << "Concealed"; - std::cout << " (concealed)"; - std::cout << " "; - std::cout << (strike) << "Crossed out"; - std::cout << " "; - std::cout << (frame) << "Framed"; - std::cout << " "; - std::cout << (encircle) << "Encircled"; - std::cout << "\n"; - std::cout << (overline) << "Overlined"; - std::cout << "\n"; - - std::cout << (bold, underline) << "Testing colors:\n"; - std::cout << (red_fg) << "Red foreground"; - std::cout << "\n"; - std::cout << (cyan_bg) << "Cyan fackground"; - std::cout << "\n"; - std::cout << (white_fg, black_bg) << "White foreground, black background"; - std::cout << "\n"; - std::cout << (blue_fg) << "Blue foreground"; - std::cout << "\n"; - std::cout << (b_green_fg) << "Bright green foreground"; - std::cout << "\n\n"; - - std::cout << (bold, underline) << "Testing 24-bit color:\n"; - - for(int r = 0; r <= 255; r+=51) - { - for(int g = 0; g <= 255; g+=51) - { - for(int b = 0; b <= 255; b+=51) - { - int color = (b + (g << 8) + (r << 16)); - std::cout << color::fg(r,g,b) + bold << std::setfill('0') << - std::showbase << std::setw(8) << std::hex << std::internal << color; - std::cout << " "; - } - std::cout << "\n"; - } - std::cout << "\n"; - } - */ + printf("\033[0;95;40m%s", pText_c); + printf("\033[0;95;41m%s", pText_c); + printf("\033[0;95;42m%s", pText_c); + printf("\033[0;95;43m%s", pText_c); + printf("\033[0;95;44m%s", pText_c); + printf("\033[0;95;45m%s", pText_c); + printf("\033[0;95;46m%s", pText_c); + printf("\033[0;95;47m%s", pText_c); + printf("\033[0;95;100m%s", pText_c); + printf("\033[0;95;101m%s", pText_c); + */ return Rts_E; } -void BofConio::PrintfAtColor(CONIO_TEXT_COLOR _ForeColor_E, uint32_t _x_U32, uint32_t _y_U32, const char *_pFormat_c, ...) +void BofConio::S_PrintLineColorAt(CONIO_TEXT_COLOR _ForegroundColor_E, uint32_t _x_U32, uint32_t _y_U32, const char *_pFormat_c, ...) { uint32_t x_U32, y_U32; - char pText_c[0x10000]; + char pText_c[0x1000]; - if (_ForeColor_E != mForeColor_E) - { - if (SetForegroundTextColor(_ForeColor_E) == BOF_ERR_NO_ERROR) - { - mForeColor_E = _ForeColor_E; - } - } - if ((_x_U32) || (_y_U32)) + if ((_x_U32) && (_y_U32)) { - if ((_x_U32) && (_y_U32)) - { - SetTextCursorPosition(_x_U32, _y_U32); - } - else - { - if (GetTextCursorPosition(x_U32, y_U32) == BOF_ERR_NO_ERROR) - { - if (_x_U32) - { - SetTextCursorPosition(_x_U32, y_U32); - } - else - { - SetTextCursorPosition(x_U32, _y_U32); - } - } - } + S_SetTextCursorPosition(_x_U32, _y_U32); } + S_SetForegroundTextColor(_ForegroundColor_E); va_list Arg; va_start(Arg, _pFormat_c); vsnprintf(pText_c, sizeof(pText_c), _pFormat_c, Arg); va_end(Arg); - Printf("%s", pText_c); + S_PrintLine(pText_c); } END_BOF_NAMESPACE() \ No newline at end of file diff --git a/lib/src/boffs.cpp b/lib/src/boffs.cpp index 1f951a1..0d37c8b 100644 --- a/lib/src/boffs.cpp +++ b/lib/src/boffs.cpp @@ -405,7 +405,7 @@ BOFERR Bof_DirectoryParser(const BofPath &_rPath, const std::string &_rPattern_S ItIsADirectory_B = (S_ISDIR(FileStat_X.st_mode)) ? true : false; FileFound_X.Path = _rPath; FileFound_X.Path.Combine(pDirEntry_X->d_name); - // printf("2:EntryOk_B %d ItIsADirectory_B %d\n",EntryOk_B,ItIsADirectory_B); + // pr6intf("2:EntryOk_B %d ItIsADirectory_B %d\n",EntryOk_B,ItIsADirectory_B); } // printf("3:EntryOk_B %d ItIsADirectory_B %d\n",EntryOk_B,ItIsADirectory_B); } @@ -781,7 +781,6 @@ BOFERR Bof_ReadLine(uintptr_t _Io, uint32_t &_rNb_U32, char *_pBuffer_c) return (Rts_E); } - BOFERR Bof_ReadLine(const BOF::BofPath &_rPath, std::vector &_rLineCollection) { BOFERR Rts_E = BOF_ERR_ENOENT; @@ -990,7 +989,7 @@ BOFERR Bof_ReadFile(const BofPath &_rPath, std::string &_rRawData_S) } #else // 200 ms - BOF_BUFFER BufferToDeleteAfterUsage_X(false,false); + BOF_BUFFER BufferToDeleteAfterUsage_X(false, false); Rts_E = Bof_ReadFile(_rPath, BufferToDeleteAfterUsage_X); if (Rts_E == BOF_ERR_NO_ERROR) { @@ -1018,7 +1017,7 @@ BOFERR Bof_WriteFile(const BOF_FILE_PERMISSION _Permission_E, const BofPath &_rP BOFERR Bof_WriteFile(const BOF_FILE_PERMISSION _Permission_E, const BofPath &_rPath, const std::string &_rRawData_S) { BOFERR Rts_E; - BOF_BUFFER Buffer_X(false,false); + BOF_BUFFER Buffer_X(false, false); Buffer_X.SetStorage(_rRawData_S.size(), _rRawData_S.size(), (uint8_t *)(_rRawData_S.c_str())); Rts_E = Bof_WriteFile(_Permission_E, _rPath, Buffer_X); diff --git a/lib/src/bofprocess.cpp b/lib/src/bofprocess.cpp index b1d19f3..aeba7ec 100644 --- a/lib/src/bofprocess.cpp +++ b/lib/src/bofprocess.cpp @@ -27,34 +27,34 @@ // #include #if defined(_WIN32) - #define popen _popen - #define pclose _pclose - /* If WIFEXITED(STATUS), NOT IN WINDOWS:the low-order 8 bits of the status.*/ - #define WEXITSTATUS(status) (status) +#define popen _popen +#define pclose _pclose +/* If WIFEXITED(STATUS), NOT IN WINDOWS:the low-order 8 bits of the status.*/ +#define WEXITSTATUS(status) (status) - /* If WIFSIGNALED(STATUS), the terminating signal. */ - #define WTERMSIG(status) ((status)&0x7f) +/* If WIFSIGNALED(STATUS), the terminating signal. */ +#define WTERMSIG(status) ((status) & 0x7f) - /* If WIFSTOPPED(STATUS), the signal that stopped the child. */ - #define WSTOPSIG(status) WEXITSTATUS(status) +/* If WIFSTOPPED(STATUS), the signal that stopped the child. */ +#define WSTOPSIG(status) WEXITSTATUS(status) - /* Nonzero if STATUS indicates normal termination. */ - #define WIFEXITED(status) (WTERMSIG(status) == 0) +/* Nonzero if STATUS indicates normal termination. */ +#define WIFEXITED(status) (WTERMSIG(status) == 0) #else - #include - #include - #include - #include - #include - #include - #include +#include +#include +#include +#include +#include +#include +#include #if defined(__EMSCRIPTEN__) - #else - #include - #endif - #include - #include - extern char **environ; +#else +#include +#endif +#include +#include +extern char **environ; #endif BEGIN_BOF_NAMESPACE() @@ -539,7 +539,7 @@ BOFERR BofProcess::S_Execute(char *_pOutput_c, uint32_t _Size_U32, const char *_ { return BofProcess::S_Execute(_pOutput_c, _Size_U32, _pCommand_c, BofProcess::S_mDefaultTimeout_U32, _Mode_E, _rPid_X, _rExitCode_i); } -//More secure than the next one +// More secure than the next one BOFERR BofProcess::S_Execute_popen(const std::string &_rCommand_S, std::string &_rOutput_S, int &_rExitCode_i) { BOFERR Rts_E = BOF_ERR_EINVAL; @@ -569,8 +569,8 @@ BOFERR BofProcess::S_Execute_popen(const std::string &_rCommand_S, std::string & } } _rOutput_S = Output.str(); - //printf("Output: %zd:%s\n", _rOutput_S.size(), _rOutput_S.c_str()); - // Grab the forked status + // printf("Output: %zd:%s\n", _rOutput_S.size(), _rOutput_S.c_str()); + // Grab the forked status _rExitCode_i = pclose(pPipe_X); Rts_E = (_rExitCode_i >= 0) ? BOF_ERR_NO_ERROR : BOF_ERR_CLOSE; // Convert it to get application return code @@ -623,14 +623,14 @@ BOFERR BofProcess::S_Execute(char *_pOutput_c, uint32_t _Size_U32, const char *_ { std::string Output_S; uint32_t Len_U32; - //More secure than the next one - Rts_E=S_Execute_popen(_pCommand_c, Output_S, _rExitCode_i); + // More secure than the next one + Rts_E = S_Execute_popen(_pCommand_c, Output_S, _rExitCode_i); if (_pOutput_c) { - Len_U32 = (Output_S.size() > _Size_U32) ? _Size_U32:Output_S.size(); + Len_U32 = (Output_S.size() > _Size_U32) ? _Size_U32 : Output_S.size(); memcpy(_pOutput_c, Output_S.c_str(), Len_U32); } - //Rts_E = BofProcess::S_Execute_popen(_pOutput_c, _Size_U32, _pCommand_c, _Timeout_U32, _rPid_X, _rExitCode_i); + // Rts_E = BofProcess::S_Execute_popen(_pOutput_c, _Size_U32, _pCommand_c, _Timeout_U32, _rPid_X, _rExitCode_i); break; } @@ -1013,7 +1013,7 @@ BOFERR BofProcess::S_Execute_vfork(char *_pOutput_c, uint32_t _Size_U32, const c int File_i = 0; char *Args_c[] = {(char *)"/bin/sh", (char *)"-c", (char *)_pCommand_c, nullptr}; ssize_t Read_i = 0; - char pTempFileName_c[0x1000]; + char pTempFileName_c[0x980]; char pRedirect_c[0x1000]; bool CaptureOutput_B = (_pOutput_c != nullptr) && (_Size_U32 > 0); @@ -1335,9 +1335,9 @@ BOFERR BofProcess::S_KillProcess(const char *_pProcessName_c) snprintf(pCmd_c, sizeof(pCmd_c), "pkill %s", pProcessname_c ? pProcessname_c + 1 : _pProcessName_c); #endif std::string Output_S; - //More secure than the next one + // More secure than the next one Rts_E = S_Execute_popen(pCmd_c, Output_S, ExitCode_i); - //Rts_E = S_Execute_popen(nullptr, 0, pCmd_c, 0, Pid_X, ExitCode_i); + // Rts_E = S_Execute_popen(nullptr, 0, pCmd_c, 0, Pid_X, ExitCode_i); if (Rts_E == BOF_ERR_NO_ERROR) { if (ExitCode_i) @@ -1346,9 +1346,9 @@ BOFERR BofProcess::S_KillProcess(const char *_pProcessName_c) #if defined(_WIN32) #else snprintf(pCmd_c, sizeof(pCmd_c), "killall %s", pProcessname_c ? pProcessname_c + 1 : _pProcessName_c); - //More secure than the next one + // More secure than the next one Rts_E = S_Execute_popen(pCmd_c, Output_S, ExitCode_i); - //Rts_E = S_Execute_popen(nullptr, 0, pCmd_c, 0, Pid_X, ExitCode_i); + // Rts_E = S_Execute_popen(nullptr, 0, pCmd_c, 0, Pid_X, ExitCode_i); if (Rts_E == BOF_ERR_NO_ERROR) { if (ExitCode_i) diff --git a/lib/src/bofshell.cpp b/lib/src/bofshell.cpp index c88fb07..f086de9 100644 --- a/lib/src/bofshell.cpp +++ b/lib/src/bofshell.cpp @@ -44,15 +44,20 @@ BofShell::BofShell(BOF_SHELL_PARAM &_rShellParam_X) AddCommand("sleep", BOF_SHELL_CMD("Sleep the script for a given number of ms.", {"SleepTimeInMs"}, &mSleepTime_U32, BOF_BIND_2_ARG_TO_METHOD(this, BofShell::ShellSleep))); AddCommandArgument("sleep", "SleepTimeInMs", BOF::BOFPARAMETER(nullptr, "", "Specify the number of millisecond to sleep", "", "", BOF::BOFPARAMETER_ARG_FLAG::CMDLINE_LONGOPT_NEED_ARG, BOF_PARAM_DEF_VARIABLE(mSleepTime_U32, UINT32, 0, 0))); + AddCommand("exec", BOF_SHELL_CMD("Execute a script from a script.", {"ScripFilename"}, &mScriptPath, BOF_BIND_2_ARG_TO_METHOD(this, BofShell::ShellExec))); AddCommandArgument("exec", "ScripFilename", BOF::BOFPARAMETER(nullptr, "", "Specify the script to execute", "", "", BOF::BOFPARAMETER_ARG_FLAG::CMDLINE_LONGOPT_NEED_ARG | BOF::BOFPARAMETER_ARG_FLAG::PATH_MUST_EXIST, BOF_PARAM_DEF_VARIABLE(mScriptPath, PATH, 0, 0))); AddCommand("echo", BOF_SHELL_CMD("Display the argument string in the output channel.", {"EchoText"}, &mEchoText_S, BOF_BIND_2_ARG_TO_METHOD(this, BofShell::ShellEcho))); AddCommandArgument("echo", "EchoText", - BOF::BOFPARAMETER(nullptr, "", "Specify which shell command must show its argument", "", "", BOF::BOFPARAMETER_ARG_FLAG::CMDLINE_LONGOPT_NEED_ARG, BOF_PARAM_DEF_VARIABLE(mEchoText_S, STDSTRING, 0, 0))); + BOF::BOFPARAMETER(nullptr, "", "Specify the text to display", "", "", BOF::BOFPARAMETER_ARG_FLAG::CMDLINE_LONGOPT_NEED_ARG, BOF_PARAM_DEF_VARIABLE(mEchoText_S, STDSTRING, 0, 0))); + + AddCommand("getch", BOF_SHELL_CMD("Wait for a key press.", {"EchoText"}, nullptr, BOF_BIND_2_ARG_TO_METHOD(this, BofShell::ShellGetCh))); + AddCommandArgument("getch", "EchoText", + BOF::BOFPARAMETER(nullptr, "", "Specify the text to display before waiting for a key", "", "", BOF::BOFPARAMETER_ARG_FLAG::CMDLINE_LONGOPT_NEED_ARG, BOF_PARAM_DEF_VARIABLE(mEchoText_S, STDSTRING, 0, 0))); - AddCommand("dywtc?", BOF_SHELL_CMD("Ask if the user want to continue.", {}, &mEchoText_S, BOF_BIND_2_ARG_TO_METHOD(this, BofShell::ShellDoYouWantToContinue))); + AddCommand("dywtc?", BOF_SHELL_CMD("Ask if the user want to continue.", {}, nullptr, BOF_BIND_2_ARG_TO_METHOD(this, BofShell::ShellDoYouWantToContinue))); } BofShell::~BofShell() { @@ -195,11 +200,25 @@ BOFERR BofShell::ShellCmdArg(void *_pArg, std::string &_rShellResult_S) BOFERR BofShell::ShellEcho(void *_pArg, std::string &_rShellResult_S) { BOFERR Rts_E = BOF_ERR_NO_ERROR; - std::string Cmd_S, Prefix_S, ArgName_S; - int Index_i; mEchoText_S = Bof_StringTrim(mEchoText_S); - _rShellResult_S = mEchoText_S; + _rShellResult_S = mEchoText_S + "\n"; + return Rts_E; +} +BOFERR BofShell::ShellGetCh(void *_pArg, std::string &_rShellResult_S) +{ + BOFERR Rts_E = BOF_ERR_NO_ERROR; + + if ((mShellParam_X.OutputStream) && (mEchoText_S != "")) + { + if (mShellParam_X.OutputStream(SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_INPUT, mEchoText_S + '\n') == BOF_ERR_NO_ERROR) + { + } + } + + BOF::BofConio::S_GetCh(false); + _rShellResult_S = ""; + return Rts_E; } @@ -238,7 +257,7 @@ BOFERR BofShell::ShellDoYouWantToContinue(void *_pArg, std::string &_rShellResul if ((mShellParam_X.InputStream) && (mShellParam_X.OutputStream)) { - if (mShellParam_X.OutputStream(false, "Are you sure you want to continue (Y/N) ?\n") == BOF_ERR_NO_ERROR) + if (mShellParam_X.OutputStream(SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_OUTPUT, "Are you sure you want to continue (Y/N) ?\n") == BOF_ERR_NO_ERROR) { if (mShellParam_X.InputStream("", _rShellResult_S) == BOF_ERR_NO_ERROR) { @@ -386,7 +405,7 @@ BOFERR BofShell::Interpreter(const std::string &_rFirstCommand_S) CmdLine_S = _rFirstCommand_S; if (mShellParam_X.OutputStream) { - mShellParam_X.OutputStream(false, (mExecuteOnlyOneCommand_B ? "EXEC: " : "FIRST: ") + CmdLine_S + '\n'); + mShellParam_X.OutputStream(SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_INPUT, (mExecuteOnlyOneCommand_B ? "EXEC: " : "FIRST: ") + CmdLine_S + '\n'); // std::string Cwd_S; // Bof_GetCurrentDirectory(Cwd_S); // printf("cwd %s\n", Cwd_S.c_str()); @@ -409,7 +428,7 @@ BOFERR BofShell::Interpreter(const std::string &_rFirstCommand_S) Rts_E = Parser(CmdLine_S.c_str(), ShellRes_S); if ((ShellRes_S != "") && (mShellParam_X.OutputStream)) { - mShellParam_X.OutputStream(false, ShellRes_S + '\n'); + mShellParam_X.OutputStream(SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_OUTPUT, ShellRes_S + '\n'); } } else @@ -420,7 +439,7 @@ BOFERR BofShell::Interpreter(const std::string &_rFirstCommand_S) { if (mShellParam_X.OutputStream) { - mShellParam_X.OutputStream(true, BOF::Bof_Sprintf("Error %d -> %s\n", Rts_E, Bof_ErrorCode(Rts_E))); + mShellParam_X.OutputStream(SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_ERROR, BOF::Bof_Sprintf("Error %d -> %s\n", Rts_E, Bof_ErrorCode(Rts_E))); } if (mExecScript_B) { @@ -439,14 +458,14 @@ BOFERR BofShell::Interpreter(const std::string &_rFirstCommand_S) Bof_GetCurrentDirectory(Pwd_S); if (mShellParam_X.OutputStream) { - mShellParam_X.OutputStream(false, BOF::Bof_Sprintf("Execute '%s' script from '%s'\n", mScriptPath.FullPathName(false).c_str(), Pwd_S.c_str())); + mShellParam_X.OutputStream(SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_INPUT, BOF::Bof_Sprintf("Execute '%s' script from '%s'\n", mScriptPath.FullPathName(false).c_str(), Pwd_S.c_str())); } Rts_E = Bof_OpenFile(mScriptPath, true, false, mIoScript); if (Rts_E != BOF_ERR_NO_ERROR) { if (mShellParam_X.OutputStream) { - mShellParam_X.OutputStream(true, BOF::Bof_Sprintf("Cannot open script file (%s)\n", Bof_ErrorCode(Rts_E))); + mShellParam_X.OutputStream(SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_ERROR, BOF::Bof_Sprintf("Cannot open script file (%s)\n", Bof_ErrorCode(Rts_E))); } mIoScript = BOF_INVALID_HANDLE_VALUE; mExecScript_B = false; @@ -460,7 +479,8 @@ BOFERR BofShell::Interpreter(const std::string &_rFirstCommand_S) ScriptLine_S = Bof_StringTrim(ScriptLine_S); if (mShellParam_X.OutputStream) { - mShellParam_X.OutputStream(false, BOF::Bof_Sprintf("[%s]: %s\n", mScriptFilename_S.c_str(), ScriptLine_S.c_str())); + mShellParam_X.OutputStream(SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_INPUT, BOF::Bof_Sprintf("[%s]: %s\n", mScriptFilename_S.c_str(), ScriptLine_S.c_str())); + // BofConio::S_PrintLineColorAt(mShellConsoleParam_X.ShellCmdInputColor_E, 0, 0, "%s", ""); } CmdLine_S = ScriptLine_S; } @@ -485,28 +505,29 @@ BofShellConsole::BofShellConsole(BOF_SHELL_CONSOLE_PARAM &_rShellConsoleParam_X) if (mShellConsoleParam_X.AnsiMode_B) { - mpuConio = std::make_unique(mShellConsoleParam_X.ConioParam_X); + BofConio::S_Initialize(mShellConsoleParam_X.ConioParam_X); if (mShellConsoleParam_X.ShellCmdInputColor_E != CONIO_TEXT_COLOR_NONE) { - mpuConio->SetForegroundTextColor(mShellConsoleParam_X.ShellCmdInputColor_E); + BofConio::S_SetForegroundTextColor(mShellConsoleParam_X.ShellCmdInputColor_E); } if (mShellConsoleParam_X.ShellBackColor_E != CONIO_TEXT_COLOR_NONE) { - mpuConio->SetBackgroundTextColor(mShellConsoleParam_X.ShellBackColor_E); + BofConio::S_SetBackgroundTextColor(mShellConsoleParam_X.ShellBackColor_E); } if (mShellConsoleParam_X.ClearScreen_B) { - mpuConio->Clear(CONIO_CLEAR::CONIO_CLEAR_ALL); - mpuConio->SetTextCursorPosition(1, 1); + BofConio::S_Clear(CONIO_CLEAR::CONIO_CLEAR_ALL); + BofConio::S_SetTextCursorPosition(1, 1); } if (mShellConsoleParam_X.WindowTitle_S != "") { - mpuConio->SetTextWindowTitle(mShellConsoleParam_X.WindowTitle_S); + BofConio::S_SetTextWindowTitle(mShellConsoleParam_X.WindowTitle_S.c_str()); } } } BofShellConsole::~BofShellConsole() { + BofConio::S_Shutdown(); } BOFERR BofShellConsole::InputStream(const std::string &_rPrompt_S, std::string &_rShellCmd_S) { @@ -515,8 +536,9 @@ BOFERR BofShellConsole::InputStream(const std::string &_rPrompt_S, std::string & if (mShellConsoleParam_X.AnsiMode_B) { - mpuConio->PrintfColor(mShellConsoleParam_X.ShellCmdInputColor_E, "%s", ""); - Rts_E = mpuConio->Readline(_rPrompt_S, _rShellCmd_S); + // mpuConio->PrintfColor(mShellConsoleParam_X.ShellCmdInputColor_E, "%s", ""); + BofConio::S_PrintLineColorAt(mShellConsoleParam_X.ShellCmdInputColor_E, 0, 0, "%s", ""); + Rts_E = BofConio::S_Readline(_rPrompt_S, _rShellCmd_S); } else { @@ -533,13 +555,27 @@ BOFERR BofShellConsole::InputStream(const std::string &_rPrompt_S, std::string & return Rts_E; } -BOFERR BofShellConsole::OutputStream(bool _Error_B, const std::string &_rShellRes_S) +BOFERR BofShellConsole::OutputStream(SHELL_OUTPUT_STREAM _ShellOutputStream_E, const std::string &_rShellRes_S) { BOFERR Rts_E = BOF_ERR_NO_ERROR; + CONIO_TEXT_COLOR ForegroundColor_E; if (mShellConsoleParam_X.AnsiMode_B) { - mpuConio->PrintfColor(_Error_B ? mShellConsoleParam_X.ShellErrorColor_E : mShellConsoleParam_X.ShellCmdOutputColor_E, "%s", _rShellRes_S.c_str()); + switch (_ShellOutputStream_E) + { + default: + case SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_INPUT: + ForegroundColor_E = mShellConsoleParam_X.ShellCmdInputColor_E; + break; + case SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_OUTPUT: + ForegroundColor_E = mShellConsoleParam_X.ShellCmdOutputColor_E; + break; + case SHELL_OUTPUT_STREAM::SHELL_OUTPUT_STREAM_ERROR: + ForegroundColor_E = mShellConsoleParam_X.ShellErrorColor_E; + break; + } + BofConio::S_PrintLineColorAt(ForegroundColor_E, 0, 0, "%s", _rShellRes_S.c_str()); } else { diff --git a/lib/src/bofstring.cpp b/lib/src/bofstring.cpp index 9d3e592..6603adf 100644 --- a/lib/src/bofstring.cpp +++ b/lib/src/bofstring.cpp @@ -18,8 +18,8 @@ * * V 1.00 Jan 19 2017 BHA : Initial release */ -//https://codingtidbit.com/2020/02/09/c17-codecvt_utf8-is-deprecated/ -//not working #define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING +// https://codingtidbit.com/2020/02/09/c17-codecvt_utf8-is-deprecated/ +// not working #define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING #if defined(__EMSCRIPTEN__) || defined(__linux__) #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif @@ -221,6 +221,12 @@ std::vector Bof_StringSplit(const std::string &_rStr_S, const std:: if (EndPos == std::string::npos) { Rts.push_back(_rStr_S.substr(StartPos, _rStr_S.length())); + /*Regression dans path combine,findfile,... + if (StartPos < _rStr_S.length()) + { + Rts.push_back(_rStr_S.substr(StartPos, _rStr_S.length())); + } + */ } } while (EndPos != std::string::npos); } @@ -719,6 +725,4 @@ std::string Bof_Sprintf(const char *_pFormat_c, ...) return Rts_S; } - - END_BOF_NAMESPACE() \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3a7eadf..d057c3a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -217,13 +217,13 @@ target_include_directories(ut_bofstd ) ### VLD ########################################### -set(CMAKE_VLD_TARGET ut_bofstd) -configure_file( - ${CMAKE_SOURCE_DIR}/vld.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/vld.cmake - @ONLY -) -include(${CMAKE_CURRENT_BINARY_DIR}/vld.cmake) +#set(CMAKE_VLD_TARGET ut_bofstd) +#configure_file( +# ${CMAKE_SOURCE_DIR}/vld.cmake.in +# ${CMAKE_CURRENT_BINARY_DIR}/vld.cmake +# @ONLY +#) +#include(${CMAKE_CURRENT_BINARY_DIR}/vld.cmake) ### VLD ######################################## target_link_libraries(ut_bofstd diff --git a/tests/src/main.cpp b/tests/src/main.cpp index 978e5a2..f527566 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -138,7 +138,8 @@ int main(int argc, char *argv[]) //::testing::GTEST_FLAG(filter) = "RawCircularBuffer_Test.FillWrapOverwrite"; //::testing::GTEST_FLAG(filter) = "RawCircularBuffer_Test.*:CircularBuffer_Test.*:RawCircularBufferInSlotMode_Test.*"; //::testing::GTEST_FLAG(filter) = "BofThreadPool_Test.Dispatch"; - //::testing::GTEST_FLAG(filter) = "Api_Test.PatternCompare"; + //::testing::GTEST_FLAG(filter) = "ConIo_Test.*"; + //::testing::GTEST_FLAG(filter) = "String_Test.*"; // ::testing::GTEST_FLAG(filter) = "RawCircularBufferAlwaysContiguous_Test.*:RawCircularBuffer_Test.*:RawCircularBufferInSlotMode_Test.*"; // std::string CrtDir_S; // BOF::Bof_GetCurrentDirectory(CrtDir_S); diff --git a/tests/src/ut_circularbuffer.cpp b/tests/src/ut_circularbuffer.cpp index 1d6d504..77e1c87 100644 --- a/tests/src/ut_circularbuffer.cpp +++ b/tests/src/ut_circularbuffer.cpp @@ -659,7 +659,7 @@ TEST(CircularBuffer_Test, Perf) Index_U32 = 0; } } - printf("%llu Push Min %llu Mean %llu Max %llu Lck %llu\n", Profiler.GetNbSample(0), Profiler.GetMin(0), Profiler.GetMean(0), Profiler.GetMax(0), Profiler.GetLockCount(0)); + printf("%zu Push Min %zu Mean %zu Max %zu Lck %zu\n", Profiler.GetNbSample(0), Profiler.GetMin(0), Profiler.GetMean(0), Profiler.GetMax(0), Profiler.GetLockCount(0)); Index_U32 = 0; for (i_U32 = 0; i_U32 < NB_QUEUE_OP; i_U32++) @@ -676,7 +676,7 @@ TEST(CircularBuffer_Test, Perf) } } EXPECT_NE(Cb.Pop(&Item_X, 1000, nullptr, nullptr), BOF_ERR_NO_ERROR); - printf("%llu Pop Min %llu Mean %llu Max %llu Lck %llu\n", Profiler.GetNbSample(1), Profiler.GetMin(1), Profiler.GetMean(1), Profiler.GetMax(1), Profiler.GetLockCount(1)); + printf("%zu Pop Min %zu Mean %zu Max %zu Lck %zu\n", Profiler.GetNbSample(1), Profiler.GetMin(1), Profiler.GetMean(1), Profiler.GetMax(1), Profiler.GetLockCount(1)); } TEST(CircularBuffer_Test, PerfThread) @@ -702,7 +702,7 @@ TEST(CircularBuffer_Test, PerfThread) { pThread[i_U32] = std::thread([&](uint32_t _Id_U32) { QUEUE_ITEM Item_X; - uint32_t j_U32=0; + uint32_t j_U32 = 0; while (!S_AllThreadReady_B) { @@ -765,22 +765,22 @@ TEST(CircularBuffer_Test, PerfThread) for (i_U32 = 0; i_U32 < NB_QUEUE_PRODUCER_CONSUMER; i_U32++) { NbLastMax_U32 = Profiler.GetLastMax(i_U32, pLastMax_X); - printf("NbOp %llu NbMax %d Push[%d] Min %llu Mean %llu Max %llu Lck %llu\n", Profiler.GetNbSample(i_U32), NbLastMax_U32, i_U32, Profiler.GetMin(i_U32), Profiler.GetMean(i_U32), Profiler.GetMax(i_U32), Profiler.GetLockCount(i_U32)); + printf("NbOp %zu NbMax %d Push[%d] Min %zu Mean %zu Max %zu Lck %zu\n", Profiler.GetNbSample(i_U32), NbLastMax_U32, i_U32, Profiler.GetMin(i_U32), Profiler.GetMean(i_U32), Profiler.GetMax(i_U32), Profiler.GetLockCount(i_U32)); EXPECT_EQ(Profiler.GetNbSample(i_U32), NB_QUEUE_OP); for (j_U32 = 0; j_U32 < NbLastMax_U32; j_U32++) { - printf(" Index %llu Max %llu\n", pLastMax_X[j_U32].MaxIndex_U64, pLastMax_X[j_U32].Max); + printf(" Index %zu Max %zu\n", pLastMax_X[j_U32].MaxIndex_U64, pLastMax_X[j_U32].Max); } } printf("---POP---------------------------\n"); for (i_U32 = NB_QUEUE_PRODUCER_CONSUMER; i_U32 < (NB_QUEUE_PRODUCER_CONSUMER + NB_QUEUE_PRODUCER_CONSUMER); i_U32++) { NbLastMax_U32 = Profiler.GetLastMax(i_U32, pLastMax_X); - printf("NbOp %llu NbMax %d Pop[%d] Min %llu Mean %llu Max %llu Lck %llu\n", Profiler.GetNbSample(i_U32), NbLastMax_U32, i_U32, Profiler.GetMin(i_U32), Profiler.GetMean(i_U32), Profiler.GetMax(i_U32), Profiler.GetLockCount(i_U32)); + printf("NbOp %zu NbMax %d Pop[%d] Min %zu Mean %zu Max %zu Lck %zu\n", Profiler.GetNbSample(i_U32), NbLastMax_U32, i_U32, Profiler.GetMin(i_U32), Profiler.GetMean(i_U32), Profiler.GetMax(i_U32), Profiler.GetLockCount(i_U32)); EXPECT_EQ(Profiler.GetNbSample(i_U32), NB_QUEUE_OP); for (j_U32 = 0; j_U32 < NbLastMax_U32; j_U32++) { - printf(" Index %llu Max %llu\n", pLastMax_X[j_U32].MaxIndex_U64, pLastMax_X[j_U32].Max); + printf(" Index %zu Max %zu\n", pLastMax_X[j_U32].MaxIndex_U64, pLastMax_X[j_U32].Max); } } } \ No newline at end of file diff --git a/tests/src/ut_conio.cpp b/tests/src/ut_conio.cpp index a0770d9..5371dbb 100644 --- a/tests/src/ut_conio.cpp +++ b/tests/src/ut_conio.cpp @@ -37,68 +37,118 @@ TEST(ConIo_Test, Output) BOF_RGBA ForeColor_X, BackColor_X; BOF_CONIO_PARAM BofConioParam_X; - std::unique_ptr puBofConio; - puBofConio.reset(new BofConio(BofConioParam_X)); - - puBofConio->SetTextWindowTitle("puBofConio->SetTextWindowTitle"); - puBofConio->GetTextCursorPosition(X_U32, Y_U32); - puBofConio->SetTextCursorState(CONIO_TEXT_CURSOR_STATE::CONIO_TEXT_CURSOR_STATE_ON); - puBofConio->SetTextCursorState(CONIO_TEXT_CURSOR_STATE::CONIO_TEXT_CURSOR_STATE_BLINK_OFF); - puBofConio->SetTextCursorState(CONIO_TEXT_CURSOR_STATE::CONIO_TEXT_CURSOR_STATE_BLINK_ON); - puBofConio->SetTextCursorState(CONIO_TEXT_CURSOR_STATE::CONIO_TEXT_CURSOR_STATE_OFF); - - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); - - puBofConio->Clear(CONIO_CLEAR::CONIO_CLEAR_ALL); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); - puBofConio->GetTextCursorPosition(X_U32, Y_U32); - puBofConio->SetTextCursorPosition(X_U32 + 30, Y_U32 - 1); - puBofConio->Clear(CONIO_CLEAR::CONIO_CLEAR_ALL_FROM_CURSOR_TO_BEGIN); - puBofConio->SetTextCursorPosition(X_U32 + 32, Y_U32 - 1); - puBofConio->Clear(CONIO_CLEAR::CONIO_CLEAR_ALL_FROM_CURSOR_TO_END); - - puBofConio->SetTextCursorPosition(1, 1); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); - puBofConio->Clear(CONIO_CLEAR::CONIO_CLEAR_LINE); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); - puBofConio->Clear(CONIO_CLEAR::CONIO_CLEAR_LINE_FROM_CURSOR_TO_BEGIN); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); - puBofConio->Clear(CONIO_CLEAR::CONIO_CLEAR_LINE_FROM_CURSOR_TO_END); - - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD\n"); - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE\n"); - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL\n"); - - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); - - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE\n"); - - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE\n"); - - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); - puBofConio->Printf("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE\n"); - - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL); + BofConio::S_Initialize(BofConioParam_X); + + BofConio::S_SetForegroundTextColor(CONIO_TEXT_COLOR_BRIGHT_WHITE); + BofConio::S_SetBackgroundTextColor(CONIO_TEXT_COLOR_BLACK); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD (1)\n"); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD (2)\n"); + + BofConio::S_SetTextWindowTitle("BofConio::S_SetTextWindowTitle"); + BofConio::S_GetTextCursorPosition(X_U32, Y_U32); + BofConio::S_SetTextCursorState(CONIO_TEXT_CURSOR_STATE::CONIO_TEXT_CURSOR_STATE_ON); + BofConio::S_SetTextCursorState(CONIO_TEXT_CURSOR_STATE::CONIO_TEXT_CURSOR_STATE_BLINK_OFF); + BofConio::S_SetTextCursorState(CONIO_TEXT_CURSOR_STATE::CONIO_TEXT_CURSOR_STATE_BLINK_ON); + BofConio::S_SetTextCursorState(CONIO_TEXT_CURSOR_STATE::CONIO_TEXT_CURSOR_STATE_OFF); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD (1)\n"); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD (2)\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_FAINT); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_FAINT\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_ITALIC); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_ITALIC\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE (1)\n"); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE (2)\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BLINK_SLOW); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BLINK_SLOW\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BLINK_FAST); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BLINK_FAST\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_CONCEAL); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_CONCEAL\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_STRIKE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_STRIKE\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_FRAME); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_FRAME\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_ENCIRCLE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_ENCIRCLE\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_OVERLINE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_OVERLINE\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_FAINT | CONIO_TEXT_ATTRIBUTE_FLAG_ITALIC | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_FAINT | CONIO_TEXT_ATTRIBUTE_FLAG_ITALIC | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); + + BofConio::S_Clear(CONIO_CLEAR::CONIO_CLEAR_ALL); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); + BofConio::S_GetTextCursorPosition(X_U32, Y_U32); + BofConio::S_SetTextCursorPosition(X_U32 + 30, Y_U32 - 1); + BofConio::S_Clear(CONIO_CLEAR::CONIO_CLEAR_ALL_FROM_CURSOR_TO_BEGIN); + BofConio::S_SetTextCursorPosition(X_U32 + 32, Y_U32 - 1); + BofConio::S_Clear(CONIO_CLEAR::CONIO_CLEAR_ALL_FROM_CURSOR_TO_END); + + BofConio::S_SetTextCursorPosition(1, 1); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); + BofConio::S_Clear(CONIO_CLEAR::CONIO_CLEAR_LINE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); + BofConio::S_Clear(CONIO_CLEAR::CONIO_CLEAR_LINE_FROM_CURSOR_TO_BEGIN); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); + BofConio::S_Clear(CONIO_CLEAR::CONIO_CLEAR_LINE_FROM_CURSOR_TO_END); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD\n"); + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE\n"); + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE\n"); + + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); + BofConio::S_PrintLine("CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE\n"); + + // BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL); + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); for (i = 0; i < 16; i++) { + BofConio::S_SetForegroundTextColor(static_cast(i)); for (j = 0; j < 16; j++) { - puBofConio->SetForegroundTextColor(static_cast(i)); - puBofConio->SetBackgroundTextColor(static_cast(j)); - puBofConio->Printf("Text in color %d,%d\n", i, j); + BofConio::S_SetBackgroundTextColor(static_cast(j)); + BofConio::S_PrintLine("Text in color %d,%d (1)\nText in color %d,%d (2)\n", i, j); + // BofConio::S_PrintLine("Text in color %d,%d (2)\n", i, j); } } - puBofConio->SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL); + // BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_NORMAL); + BofConio::S_SetTextAttribute(CONIO_TEXT_ATTRIBUTE_FLAG_BOLD | CONIO_TEXT_ATTRIBUTE_FLAG_UNDERLINE | CONIO_TEXT_ATTRIBUTE_FLAG_REVERSE); for (ForeColor_X.r = 0; ForeColor_X.r < 250; ForeColor_X.r = static_cast(ForeColor_X.r + 50)) { BackColor_X.r = static_cast(255 - ForeColor_X.r); @@ -109,35 +159,37 @@ TEST(ConIo_Test, Output) { BackColor_X.b = static_cast(255 - ForeColor_X.b); Color_U32 = (static_cast(ForeColor_X.b) + (static_cast(ForeColor_X.g) << 8) + (static_cast(ForeColor_X.r) << 16)); - puBofConio->SetForegroundTextColor(ForeColor_X); - puBofConio->SetBackgroundTextColor(BackColor_X); - puBofConio->Printf("%08X ", Color_U32); + BofConio::S_SetForegroundTextColor(ForeColor_X); + BofConio::S_SetBackgroundTextColor(BackColor_X); + BofConio::S_PrintLine("%08X (1)\n", Color_U32); + BofConio::S_PrintLine("%08X (2)\n", Color_U32); } - puBofConio->Printf("\n"); + BofConio::S_PrintLine("\n"); } - puBofConio->Printf("\n"); + BofConio::S_PrintLine("\n"); } + BofConio::S_Shutdown(); } // Cannot be run under teamcity as it need keystrokes in the terminal to succeed TEST(ConIo_Test, DISABLED_Input) +// TEST(ConIo_Test, Input) { BOF_CONIO_PARAM BofConioParam_X; - std::unique_ptr puBofConio = nullptr; uint32_t Key_U32, KeyEnd_U32; std::string InputLine_S; - puBofConio.reset(new BofConio(BofConioParam_X)); - puBofConio->Readline("Prompt>", InputLine_S); + BofConio::S_Initialize(BofConioParam_X); + BofConio::S_Readline("Prompt>", InputLine_S); do { printf("Wait for a key press (ESC to leave)\n"); do { - Key_U32 = puBofConio->KbHit(100); + Key_U32 = BofConio::S_KbHit(100); } while (Key_U32 == 0); - printf("You press %s/%08X\n", puBofConio->KeyToString(Key_U32).c_str(), Key_U32); + printf("You press %s/%08X\n", BofConio::S_KeyToString(Key_U32).c_str(), Key_U32); } while (Key_U32 != CONIO_SPECIAL_KEY_ESC); #if defined(_WIN32) @@ -149,13 +201,14 @@ TEST(ConIo_Test, DISABLED_Input) #endif do { - Key_U32 = puBofConio->GetCh(false); - printf("You press '%s'/0x%08X (0x%08X will exit)\n", puBofConio->KeyToString(Key_U32).c_str(), Key_U32, KeyEnd_U32); + Key_U32 = BofConio::S_GetCh(false); + printf("You press '%s'/0x%08X (0x%08X will exit)\n", BofConio::S_KeyToString(Key_U32).c_str(), Key_U32, KeyEnd_U32); } while (Key_U32 != KeyEnd_U32); printf("Edit line and history, Type 'quit' to leave\n"); do { - puBofConio->Readline("Prompt>", InputLine_S); + BofConio::S_Readline("Prompt>", InputLine_S); } while (InputLine_S != "quit"); + BofConio::S_Shutdown(); } diff --git a/tests/src/ut_string.cpp b/tests/src/ut_string.cpp index b130201..b6de328 100644 --- a/tests/src/ut_string.cpp +++ b/tests/src/ut_string.cpp @@ -27,8 +27,8 @@ USE_BOF_NAMESPACE() TEST(String_Test, StrToBin) { -//Trig an assert in c lib EXPECT_EQ(Bof_StrToBin(1, "111"), 0); -//Trig an assert in c lib EXPECT_EQ(Bof_StrToBin(60, "111"), 0); + // Trig an assert in c lib EXPECT_EQ(Bof_StrToBin(1, "111"), 0); + // Trig an assert in c lib EXPECT_EQ(Bof_StrToBin(60, "111"), 0); EXPECT_EQ(Bof_StrToBin(10, "0x111"), 0); EXPECT_EQ(Bof_StrToBin(0, "111"), 111); @@ -231,6 +231,13 @@ TEST(String_Test, StringIsAllTheSameChar) TEST(String_Test, StringSplit) { std::vector SplittedStr_S; + SplittedStr_S = Bof_StringSplit("AB\nC\n\ndernier\n", "\n"); + EXPECT_EQ(SplittedStr_S.size(), 5); + EXPECT_EQ(SplittedStr_S[0], "AB"); + EXPECT_EQ(SplittedStr_S[1], "C"); + EXPECT_EQ(SplittedStr_S[2], ""); + EXPECT_EQ(SplittedStr_S[3], "dernier"); + EXPECT_EQ(SplittedStr_S[4], ""); SplittedStr_S = Bof_StringSplit(" AB ", " \a\f\n\r\t\v"); EXPECT_EQ(SplittedStr_S.size(), 5); @@ -423,8 +430,9 @@ TEST(String_Test, MultipleKeyValueString) EXPECT_EQ(Bof_GetUnsignedIntegerFromMultipleKeyValueString("AA=1;\r\nBB=2;\nCC=3;\n\rDD=", ";\r\n", "CC", '=', Val_U32), 0); EXPECT_EQ(Val_U32, 3); + EXPECT_EQ(Bof_GetUnsignedIntegerFromMultipleKeyValueString("AA=1;\r\nBB=2;\nCC=3;\n\rDD=4", ";\r\n", "DD", '=', Val_U32), 0); + EXPECT_EQ(Val_U32, 4); EXPECT_EQ(Bof_GetUnsignedIntegerFromMultipleKeyValueString("AA=1;\r\nBB=2;\nCC=3;\n\rDD=", ";\r\n", "DD", '=', Val_U32), 0); - EXPECT_EQ(Val_U32, 0); EXPECT_EQ(Bof_GetUnsignedIntegerFromMultipleKeyValueString("TO=100;CON=HELLO WORLD", ";", "TO", '=', Val_U32), 0); EXPECT_EQ(Val_U32, 100);