Add function to get the start position; add some comments
This commit is contained in:
parent
46cf5484fd
commit
fe125532a4
16
src/main.cpp
16
src/main.cpp
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user