/* This file is part of highlight-diffpatch. * Copyright © 2022 tastytea * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ #include #include #include #include // Returns string with removed ANSI color codes std::string remove_colors(const std::string &text) { auto newtext{text}; size_t pos{0}; while ((pos = newtext.find('\x1b')) != std::string::npos) { const auto nchars{newtext.find('m', pos) + 1 - pos}; newtext.replace(pos, nchars, ""); } 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"}; static constexpr std::string_view green{"\x1b[92m"}; static constexpr std::string_view reset{"\x1b[m"}; std::string line; while (std::getline(std::cin, line)) { const auto line_nc{remove_colors(line)}; // Only look at lines starting with + or - if (line_nc.length() >= 2 && (line_nc[0] == '+' || line_nc[0] == '-')) { // Don't color +++ and --- lines if (line_nc.length() < 3 || (line_nc[2] != '+' && line_nc[2] != '-')) { 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; } } } std::cout << line << '\n'; } }