8
\$\begingroup\$

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. +correct and -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 , so the shortest wins!

\$\endgroup\$
8
  • \$\begingroup\$ Could use some self-containedness with regards to how control sequences are specified, what precise colors are required, and perhaps what it means that "the colored output must be visible to users and no other steps should be needed to decode the output". Otherwise, nice challenge! \$\endgroup\$ – Unrelated String Feb 8 at 9:03
  • 1
    \$\begingroup\$ If not familiar with diff. What is "the optional section heading after the hunk range information"? Is it the "This is a section of the Program" part of "@@ -22,3 +23,7 @@"? Or the whole string including "@@ -22,3 +23,7 @@"? \$\endgroup\$ – tsh Feb 8 at 10:09
  • 4
    \$\begingroup\$ Can anyone who vote to close this question comment which part is unclear? I feel current description at least acceptable (to understand). \$\endgroup\$ – tsh Feb 8 at 10:11
  • 1
    \$\begingroup\$ @UnrelatedString I discussed this a little in chat while the question was in the sandbox, and I think that you can use non-standard invocation like with piping to display, so that's a non-issue. \$\endgroup\$ – user202729 Feb 8 at 11:43
  • 1
    \$\begingroup\$ I would highly recommend adding test cases, especially one where an added line begins with ++ and a removed line begins with --. \$\endgroup\$ – GammaFunction Feb 8 at 12:34
9
\$\begingroup\$

Perl 5 (-p), 70, 69 bytes

-1 byte dropping 0, as suggested by Dom Hastings

s/^([-+@])(\1\1)?(.*?@@|.*)/"[".($2&&"1;").((4*ord)%9+31)."m$&"/e

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ This will bold added lines beginning with ++ and removed lines beginning with --. \$\endgroup\$ – GammaFunction Feb 8 at 12:36
  • \$\begingroup\$ @GammaFunction, you're right, however the test cases were not in the description when i wrote this answer, fixed \$\endgroup\$ – Nahuel Fouilleul Feb 8 at 15:24
  • \$\begingroup\$ Nice, can't get much more concise! You can drop the 0 in \e[0m though! (en.wikipedia.org/wiki/…) \$\endgroup\$ – Dom Hastings Feb 8 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.