Add function to get the start position; add some comments

This commit is contained in:
tastytea 2022-03-10 15:53:08 +01:00
parent 46cf5484fd
commit fe125532a4
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
1 changed files with 13 additions and 3 deletions

View File

@ -19,6 +19,7 @@
#include <string> #include <string>
#include <string_view> #include <string_view>
// Returns string with removed ANSI color codes
std::string remove_colors(const std::string &text) std::string remove_colors(const std::string &text)
{ {
auto newtext{text}; auto newtext{text};
@ -33,6 +34,15 @@ std::string remove_colors(const std::string &text)
return newtext; 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() int main()
{ {
static constexpr std::string_view red{"\x1b[91m"}; static constexpr std::string_view red{"\x1b[91m"};
@ -43,19 +53,19 @@ int main()
while (std::getline(std::cin, line)) while (std::getline(std::cin, line))
{ {
const auto line_nc{remove_colors(line)}; const auto line_nc{remove_colors(line)};
// Don't color +++ and --- lines
if (line_nc[2] != '+' && line_nc[2] != '-') 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] == '+') if (line_nc[1] == '+')
{ {
const auto pos{get_start_pos(line)};
std::cout << line.substr(0, pos) << green << line.substr(pos) std::cout << line.substr(0, pos) << green << line.substr(pos)
<< reset << '\n'; << reset << '\n';
continue; continue;
} }
if (line_nc[1] == '-') if (line_nc[1] == '-')
{ {
const auto pos{get_start_pos(line)};
std::cout << line.substr(0, pos) << red << line.substr(pos) std::cout << line.substr(0, pos) << red << line.substr(pos)
<< reset << '\n'; << reset << '\n';
continue; continue;