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_view>
// 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;