Created
November 12, 2024 17:43
-
-
Save certik/8e2270033a0bedbc2daca9b0e5ffd375 to your computer and use it in GitHub Desktop.
mydelta prototype implementation of `delta`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile with: | |
// g++ -O3 -march=native -o mydelta mydelta.cpp | |
// Use it like: | |
// git diff | ./mydelta | |
#include <iostream> | |
#include <string> | |
#include <regex> | |
// ANSI escape codes for colors | |
const std::string RED = "\033[31m"; | |
const std::string GREEN = "\033[32m"; | |
const std::string YELLOW = "\033[33m"; | |
const std::string BLUE = "\033[34m"; | |
const std::string MAGENTA = "\033[35m"; | |
const std::string CYAN = "\033[36m"; | |
const std::string RESET = "\033[0m"; | |
int main() { | |
std::string line; | |
std::regex diffRegex("^@@ -([0-9]+),([0-9]+) \\+([0-9]+),([0-9]+) @@"); | |
std::regex addRegex("^\\+"); | |
std::regex removeRegex("^-"); | |
while (std::getline(std::cin, line)) { | |
// Colorize diff header | |
if (std::regex_match(line, diffRegex)) { | |
std::cout << CYAN << line << RESET << std::endl; | |
} | |
// Colorize added lines | |
else if (std::regex_match(line, addRegex)) { | |
std::cout << GREEN << line << RESET << std::endl; | |
} | |
// Colorize removed lines | |
else if (std::regex_match(line, removeRegex)) { | |
std::cout << RED << line << RESET << std::endl; | |
} | |
// Print other lines as is | |
else { | |
std::cout << line << std::endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: one should use the following regex for better coloring: