diff --git a/src/main.cpp b/src/main.cpp index 1ce1180..0101119 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include #include +// Returns string with removed ANSI color codes std::string remove_colors(const std::string &text) { auto newtext{text}; @@ -33,6 +34,15 @@ std::string remove_colors(const std::string &text) return newtext; } +// Returns the position of the 2nd + or - +size_t get_start_pos(const std::string &line) +{ + auto pos{std::min(line.find('+'), line.find('-')) + 1}; + pos = std::min(line.find('+', pos), line.find('-', pos)); + + return pos; +} + int main() { static constexpr std::string_view red{"\x1b[91m"}; @@ -43,19 +53,19 @@ int main() while (std::getline(std::cin, line)) { const auto line_nc{remove_colors(line)}; + // Don't color +++ and --- lines if (line_nc[2] != '+' && line_nc[2] != '-') { - auto pos{std::min(line.find('+'), line.find('-')) + 1}; - pos = std::min(line.find('+', pos), line.find('-', pos)); - if (line_nc[1] == '+') { + const auto pos{get_start_pos(line)}; std::cout << line.substr(0, pos) << green << line.substr(pos) << reset << '\n'; continue; } if (line_nc[1] == '-') { + const auto pos{get_start_pos(line)}; std::cout << line.substr(0, pos) << red << line.substr(pos) << reset << '\n'; continue;