Introduction
git diff is a great way to export patches of the source code, and its outputs are colorized if you don't pass extra arguments to it.
However, when you want to review the exported file with colored output, it would be pretty hard. colordiff is an option, but it acts like the diff program itself, also, it is not "multiplatform" considering how many languages this community can code in.
Challenge
Write a program that takes a diff text input from the user and colorize it. On *nix, it might mean printing to standard output with ANSI escape sequences. The colored output must be clearly visible to users.
Details
- Only valid diff texts will be passed. You can make it so other cases are UB.
- The original file line starting with
---(e.g.---path/to/example) should be colored red and bolded. - The new file line starting with
+++(e.g.+++path/to/example) should be colored green and bolded. - The modification lines starting with
+or-(e.g.+correctand-wrong) should be colored green and red respectively, but not bolded. - You can color the hunk range information indicated by two
@@s at the start of a line (e.g.@@ -22,3 +23,7 @@) however you like, but it should not be the default color, and it must be consistent throughout the program. The hunk range indicators can be bolded. - The optional section heading after the hunk range information which is after the second
@@(e.g. "This is an optional heading" in@@ -22,3 +23,7 @@ This is an optional heading) should NOT be colored. - All other lines should NOT be colored.
- The program must output the exact text from the input. The only difference is the color.
- You may assume that only new file lines start with
+++, only original file lines start with---, only modification lines start with+or-(but not---or+++), and that all lines starting with@@denote a hunk information.
Restriction
Usage of diffutils, patchutils, git tools, or any other external programs are not allowed.
Test Cases
+++ Green and Bolded ++ Green, not Bolded + Green - Red -- Red, not Bolded --- Red and Bolded @@ -123,1 +123,1 @@ <- that was a color different to this color This should be a sentence that has the same color as the line above and after the second @@ This is not colored: @@ test ---+++ @@ because it doesn't start at the beginning of a line @@ -12,3 +12,3 @@ <- that should have the same color as the last hunk Winning Criterion
This is code-golf, so the shortest wins!
display, so that's a non-issue. \$\endgroup\$++and a removed line begins with--. \$\endgroup\$