#!/usr/bin/perl
=head1 SYNOPSIS
hidiff old_file new_file
=head1 DESCRIPTION
Highlight the differences between two files using curses.
The result of thinking, wouldn't it be nice to do "watch -d"
on two files.
=head1 NOTES
This is not a diff-like utility, it doesn't yet doi Longest Common Sub
+string
cleverness so an insertion or deletion will throw the rest of the line
+.
If I can figure out a way (or ways) to represent a diff in curses
highlights and colours then I might implement it. Ideas welcome.
hidiff currently isn't rigorous about representing all differences,
tabs, line-endings and non-ascii may cause problems.
=head1 AUTHOR
Brad Bowman, hidiff at bereft net
=head1 COPYRIGHT
Copyright (c) 2007 Brad Bowman. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
use warnings;
use strict;
use Curses;
my ($old_name, $new_name) = @ARGV;
open my $old_fh, $old_name or die "Couldn't open $old_name: $!";
open my $new_fh, $new_name or die "Couldn't open $new_name: $!";
initscr;
# Draw files
while ( !eof($old_fh) || !eof($new_fh) ) {
my $old_line = <$old_fh>;
my $new_line = <$new_fh>;
chomp($old_line);
chomp($new_line);
my $old_len = length($old_line);
my $new_len = length($new_line);
#die join ' ', map { ord($_) } split //, $new_line;
my $max_len = ($old_len > $new_len) ? $old_len : $new_len;
for my $i (0..$max_len-1) {
my $oc = substr($old_line, $i, 1);
my $nc = substr($new_line, $i, 1);
if ($oc eq $nc) {
addch($oc);
} else {
standout;
addch( (ord($nc) != 0) ? $nc : ' ');
standend;
}
}
addch("\n");
}
refresh;
END {
endwin;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|