diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index dec631b..977aa5f 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -139,6 +139,25 @@ namespace Term { //? --------------------------------------------------- FUNCTIONS ----------------------------------------------------- +namespace Fx { + string uncolor(const string& s) { + string out = s; + for (size_t offset = 0, start_pos = 0, end_pos = 0, next_pos = 0;;) { + if ((start_pos = next_pos > 0 ? next_pos : out.find('\x1b', offset)) == string::npos) + break; + offset = start_pos; + if ((end_pos = out.find('m', offset)) == string::npos) + break; + else if (next_pos = out.find('\x1b', offset + 1); end_pos - start_pos > next_pos - start_pos) + continue; + out.replace(start_pos, (end_pos - start_pos) + 1, ""); + next_pos = 0; + } + out.shrink_to_fit(); + return out; + } +} + namespace Tools { atomic active_locks (0); diff --git a/src/btop_tools.hpp b/src/btop_tools.hpp index d68d1f3..f802c77 100644 --- a/src/btop_tools.hpp +++ b/src/btop_tools.hpp @@ -64,7 +64,7 @@ namespace Fx { const regex color_regex("\033\\[\\d+;?\\d?;?\\d*;?\\d*;?\\d*(m){1}"); //* Return a string with all colors and text styling removed - inline string uncolor(const string& s) { return regex_replace(s, color_regex, ""); } + string uncolor(const string& s); }