Thursday, April 26, 2007

Colorize your (svn) diffs

People who know me, know that I love color on my computer screen[1]. It helps me reading code or output faster. I was not on a side track today, no, but when cleaning up my ~/bin, I just thought that my tiny colordiff script may be useful to others too. It (modestly) colorizes output from svn diff, or plain diff output.

Here is an example of svn diff on a directory with some modified files:

[dam@doffer:~/avandam_svn/phd/sw/fortran/src]$ svn diff . | colordiff
Index: mmadapt.f
--- mmadapt.f   (revision 1114)
+++ mmadapt.f   (working copy)
@@ -429,8 +429,8 @@
subroutine eval_monitor
    integer :: i1, i2, iq, id
    real(knd_double) :: alphap(nq, ndim)
-       call eval_monitor_nondir
-       return
+       !call eval_monitor_nondir
+       !return

    call cellaverages(x, x, xc) ! I use this result in move_mesh too!

Index: mmsolve.f

You can also use this on normal diff output:

[dam@doffer:~/avandam_svn/phd/sw/fortran/src]$ diff mmadapt.f mmadapt_bla.f | colordiff plain
<               end do ! i2
>               end do ! i2 foo
<               dq(i1,i2,1:nq,1) = (q(i1+1,i2,1:nq) - q(i1-1,i2,1:nq)) / (xc(i1+1,i2,1) - xc(i1-1,i2,1))
>       ! bla

The colordiff script only uses sed and looks like this:

# Mainly used to pipe output by 'svn diff' into
if [[ "$1" == "plain" ]] ; then # to use in plain diff usage
        sed -e 's/^\(>.*\)/ESC[1;34m\1ESC[0m/;s/^\(<.*\)/ESC[1;31m\1ESC[0m/;s/^\(diff .*\)/ESC[1;37;40m\1ESC[0m/'
else # for svn diff output
        sed -e 's/^\(\+.*\)/ESC[1;34m\1ESC[0m/;s/^\(\-.*\)/ESC[1;31m\1ESC[0m/;s/^\(Index: .*\)/ESC[1;37;40m\1ESC[0m/'

sed just inserts the appropriate escape characters producing color output[2] at the right places. To make sure the escape characters are correct do not copy-paste, but download colordiff here.

[1] color output for Stratego.
[2] Escape characters producing color output.

Labels: , , ,


Blogger Tom W said...

Hey, this is really helpful! Thanks for sharing!

9:18 AM  

Post a Comment

<< Home