Category: GUI
Author/Contact Info Clinton Pierce, clintp@geeksalad.org
Description: Visual diff between two text files, similar to the way that MS-Word's "track changes" feature works. Deleted words are red-strikethrough, added words are blue, and words consistant with both documents are black. It is just a skeleton of code, but functional.
#!/usr/bin/perl -w

# Visual diff in Tk
# Copyright Clinton Pierce 2001
#    Re-distributable under the same terms as Perl itself

use strict;
use Tk;
use Tk::Font;
use Algorithm::Diff qw(traverse_sequences);

die "$0: 2 Args" unless (@ARGV == 2);

sub fill {
        local(*A,$/);
        open(A, $_[0]) || die "$_[0]: $!";
        # Changing the split re changes how the document
        #     is diff'd.  Using \b goes word-by-word, using
        #     \n goes line-by-line, etc.
        return split(/\b/, <A>);
}

my $mw=new MainWindow;
my $tb=$mw->Scrolled('Text')->pack(-expand => 1,
                                   -fill => 'both');
my $fn=$mw->Font();
my $fo=$mw->Font(-overstrike => 1);

my @a1=fill(shift);
my @a2=fill(shift);

traverse_sequences(\@a1, \@a2, {
  MATCH => sub { $tb->insert('end', $a1[$_[0]], 'match'); },
  DISCARD_A => 
           sub { $tb->insert('end', $a1[$_[0]], 'disca'); },
  DISCARD_B => 
           sub { $tb->insert('end', $a2[$_[1]], 'discb'); },
});

$tb->tagConfigure('match', -foreground => 'black', 
                  -font => $fn);
$tb->tagConfigure('disca', -foreground => 'red',
                  -font => $fo);
$tb->tagConfigure('discb', -foreground => 'blue',
                  -font => $fn);
$mw->MainLoop;