#!/bin/sh # Color diff output, for human consumption # Author: # http://www.pixelbeat.org/ # Notes: # If 2 parameters are passed, then they are passed # to the `diff -Naru` command first. Otherwise the parameters # (or stdin) are assumed to be diff format and are colourised. # # VIM can be useful for viewing diffs also: # diff -Naru a b | vim -R - # vim -R a-b.diff # Changes: # V0.1, 12 Feb 2008, Initial release # V0.2, 18 Feb 2008, Use tput rather than hardcoding escape sequences. # V0.3, 24 Apr 2008, Support Mac OS X # V0.4, 30 Apr 2008, P@draigBrady.com # Use $PAGER if set # Manfred Schwarb # Support `diff -c` format fully. # Pointed out issues with less -EF options. # Suggested to use the less -S option. # less -K reportedly not available on older Mac OS X less -K -Ff /dev/null 2>/dev/null && CTRL_C_EXITS="-K" RED=1 GREEN=2 BLUE=4 BRIGHT='1;' tputc() { colour=$2 bright=$1 [ "$bright" ] && tput bold tput setaf $colour } TPUT_DEL="`tputc $BRIGHT $RED`" TPUT_ADD="`tputc $BRIGHT $GREEN`" TPUT_CHG="`tputc $BRIGHT $BLUE`" TPUT_NORM="`tput sgr0`" if [ "$#" -eq "2" ]; then diff -Naru "$@" else cat "$@" fi | sed " s/\(^\*\{3\}.*\*\{4\}\)/$TPUT_CHG\1$TPUT_NORM/;t s/\(^-\{3\}.*-\{4\}\)/$TPUT_CHG\1$TPUT_NORM/;t s/\(^@.*\)/$TPUT_CHG\1$TPUT_NORM/ s/\(^[0-9].*\)/$TPUT_CHG\1$TPUT_NORM/ s/\(^!.*\)/$TPUT_CHG\1$TPUT_NORM/ s/\(^-.*\)/$TPUT_DEL\1$TPUT_NORM/ s/\(^<.*\)/$TPUT_DEL\1$TPUT_NORM/ s/\(^\*.*\)/$TPUT_ADD\1$TPUT_NORM/ s/\(^\+.*\)/$TPUT_ADD\1$TPUT_NORM/ s/\(^>.*\)/$TPUT_ADD\1$TPUT_NORM/ " | ${PAGER:-less -QRS $CTRL_C_EXITS} # could use less -EFX also, but for large files or lots of scrolling, this # is a lot more obtrusive on the terminal as the [de]init codes not used.