From d4a067099873a2bf2b3754f71ef19d38c4764d7b Mon Sep 17 00:00:00 2001 From: sk-5 Date: Mon, 13 Jul 2015 14:39:00 -0700 Subject: [PATCH] pcprintf now returns string length without the ansi --- libpipecolors.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/libpipecolors.cpp b/libpipecolors.cpp index fd903df..bb345b0 100644 --- a/libpipecolors.cpp +++ b/libpipecolors.cpp @@ -70,7 +70,7 @@ namespace pipecolors { return isatty(fileno(stdout)); } - std::string replace_colors( std::string s) { + std::pair replace_colors( std::string s) { using namespace boost; @@ -81,13 +81,17 @@ namespace pipecolors { std::string::const_iterator start, end; start = s.begin(); end = s.end(); + std::string len(s); colors = getColors(); while(regex_search(start, end, match, re, flags)) { + if(colors[match[0]].empty() && PC_REMOVE_INVALID == false) goto skip; + len.erase(len.find(match[0]), match[0].length()); + if(has_colors() && !colors[match[0]].empty()) { s.replace(s.find(match[0]), match[0].length(), colors[match[0]]); } else { @@ -98,7 +102,8 @@ namespace pipecolors { start = match[0].second; flags |= boost::match_prev_avail | boost::match_not_bob; } - return(s); + return std::make_pair(s, len.length()); + } int pcprintf( const char * fmt, ...) @@ -118,9 +123,10 @@ namespace pipecolors { std::string s(buffer); free(buffer); - std::cout << replace_colors(s); + std::pair result = replace_colors(s); + std::cout << result.first; - return(ret); + return(result.second); } } // namespace