Skip to content

Instantly share code, notes, and snippets.

@xkr47
Last active December 12, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xkr47/4722508 to your computer and use it in GitHub Desktop.
Save xkr47/4722508 to your computer and use it in GitHub Desktop.
Colorify "diff -u" output
#!/usr/bin/perl
use File::Temp qw/ :mktemp /;
my @params = grep(/^-/,@ARGV);
@ARGV = grep(!/^-/,@ARGV);
my $tmp = $ENV{"TMP"};
$tmp = "/tmp" unless(defined($tmp));
($tmpfh, $tmpfile) = mkstemp($tmp."/diff_u.XXXXXX");
while(<>) {
$b=$a;
$a=s/^(-.*)/\033\[31;1m$1\033\[m/;
s/^(\+.*)/\033\[32;1m$1\033\[m/;
s/^(Index:)(.*)/$1\033\[40;37;1m$2\033\[K\033\[m/;
s/^/\033\[m/ if($b&&!$a);
print $tmpfh $_;
}
close $tmpfh;
if(-s $tmpfile) {
system("less", "-R", @params, $tmpfile);
}
unlink($tmpfile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment