Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

hidiff - char highlight diff utility

by bsb (Priest)
on Oct 31, 2007 at 00:53 UTC ( #648150=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info
Description: Highlight differences between 2 files using curses highlighting, a little like "watch -d".

  hidiff old_file new_file


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
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


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.

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: $!";


# Draw files
while ( !eof($old_fh) || !eof($new_fh) ) {
    my $old_line = <$old_fh>;
    my $new_line = <$new_fh>;
    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) {
        } else {
            addch( (ord($nc) != 0) ? $nc : ' ');



Replies are listed 'Best First'.
Re: hidiff - char highlight diff utility
by Fletch (Chancellor) on Oct 31, 2007 at 14:10 UTC
      Thanks Fletch

      I use colordiff, and sometimes vimdiff, for some tasks. I also like trac's character-wise diff within lines. A command-line tool for that would be nice, does one exist? It probably wouldn't be hard, the tricky bits are already on CPAN (Algorithm::Diff, Term::ANSIColor).

      hidiff was intended to show a file, but with it's differences to another highlighted. I think there's room to use Algorithm::Diff to improve how it does that. I need to work out what diff information to display and how, given the constraint that the "new" file is shown in it's entirety and correctly positioned. (While writing this I thought that flipping interactively from new to old may help, or be useful on it's own)

      I also just discovered "less -r" and "less -R", which make less display colordiff output usefully.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://648150]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-07-25 20:54 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (379 votes). Check out past polls.