Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

ANSI highlighting of sdiff output

by diotalevi (Canon)
on Sep 17, 2007 at 19:09 UTC ( #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
Node Status?
node history
Node Type: sourcecode [id://639463]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2020-10-27 06:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (256 votes). Check out past polls.

    Notices?