diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index 20d8514..e913739 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -141,29 +141,30 @@ namespace Term { //? --------------------------------------------------- FUNCTIONS ----------------------------------------------------- -namespace Fx { - string uncolor(const string& s) { - string out = s; - for (size_t offset = 0, start_pos = 0, end_pos = 0;;) { - start_pos = (offset == 0) ? out.find('\x1b') : offset; - if (start_pos == string::npos) - break; - offset = start_pos + 1; - end_pos = out.find('m', offset); - if (end_pos == string::npos) - break; - else if (auto next_pos = out.find('\x1b', offset); not isdigit(out[end_pos - 1]) or end_pos > next_pos) { - offset = next_pos; - continue; - } +// ! Dsiabled due to issue when compiling with musl, reverted back to using regex +// namespace Fx { +// string uncolor(const string& s) { +// string out = s; +// for (size_t offset = 0, start_pos = 0, end_pos = 0;;) { +// start_pos = (offset == 0) ? out.find('\x1b') : offset; +// if (start_pos == string::npos) +// break; +// offset = start_pos + 1; +// end_pos = out.find('m', offset); +// if (end_pos == string::npos) +// break; +// else if (auto next_pos = out.find('\x1b', offset); not isdigit(out[end_pos - 1]) or end_pos > next_pos) { +// offset = next_pos; +// continue; +// } - out.erase(start_pos, (end_pos - start_pos)+1); - offset = 0; - } - out.shrink_to_fit(); - return out; - } -} +// out.erase(start_pos, (end_pos - start_pos)+1); +// offset = 0; +// } +// out.shrink_to_fit(); +// return out; +// } +// } namespace Tools { diff --git a/src/btop_tools.hpp b/src/btop_tools.hpp index e11775c..63723cc 100644 --- a/src/btop_tools.hpp +++ b/src/btop_tools.hpp @@ -22,6 +22,7 @@ tab-size = 4 #include #include #include +#include #include #include #include @@ -64,8 +65,15 @@ namespace Fx { //* Reset text effects and restore theme foregrund and background color extern string reset; + //* Regex for matching color, style and cursor move escape sequences + const std::regex escape_regex("\033\\[\\d+;?\\d?;?\\d*;?\\d*;?\\d*(m|f|s|u|C|D|A|B){1}"); + + //* Regex for matching only color and style escape sequences + const std::regex color_regex("\033\\[\\d+;?\\d?;?\\d*;?\\d*;?\\d*(m){1}"); + //* Return a string with all colors and text styling removed - string uncolor(const string& s); + inline string uncolor(const string& s) { return std::regex_replace(s, color_regex, ""); } + // string uncolor(const string& s); }