Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

ANSI highlighting of sdiff output

by diotalevi (Canon)
on Sep 17, 2007 at 19:09 UTC ( [id://639463]=sourcecode: print w/replies, xml ) Need Help??
Category:
Author/Contact Info
Description: Takes sdiff output and adds highlighting - red for deletions, yellow for changes, green for additions.

The default pivot for sdiff is column 65 because of its default width of 130 characters. You'll want to adjust the --pivot parameter to match your sdiff.

There's no smarts in here about tabs. Use sdiff's -t option to print only spaces.

#!/usr/local/bin/perl
use strict;
use warnings;
use Term::ANSIColor 'colored';
use Getopt::Long 'GetOptions';
$Term::ANSIColor::EACHLINE = "\n";

# sdiff default is 130 columns
my $pivot = 65;
GetOptions(
    'pivot=i' => \ $pivot,
    'help'    => \ &help,
)
    or pod2usage( -verbose => 1 );

my $del_rx  = qr/^@{[ '.' x ( $pivot - 2 )]} < /;
my $diff_rx = qr/^@{[ '.' x ( $pivot - 2 )]} \| /;
my $add_rx  = qr/^@{[ '.' x ( $pivot - 2 )]} > /;

while (<>) {
    if ( /$del_rx/o ) {
        $_ = colored( $_, 'red on_black' );
    }
    elsif ( /$diff_rx/o ) {
        $_ = colored( $_, 'yellow on_black' );
    }
    elsif ( /$add_rx/o ) {
        $_ = colored( $_, 'green on_black' );
    }

    print;
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://639463]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-29 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found