Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

color coded diff (the return)

by iza (Monk)
on Feb 13, 2002 at 16:46 UTC ( [id://145199]=CUFP: print w/replies, xml ) Need Help??

This script uses tachyon's color coded diff code.
Tachyon's script was hilighting the lines that were differents between 2 files. This script hilights only the part of the line that is different from the line in the other file (just like emacs' compare 2 file does)
traverse_sequence is the only function i changed, that's why get and escape html functions are not re-printed here.
html output is netscape 4+ and ie4+ compliant
# stolen at http://www.perlmonks.org/index.pl?node_id=115928 # as well as the get and escapeHTML functions # get options and file contents as array refs my $old = get($old_file, 1, 1); my $new = get($file, 1, 1); open (SDIFF, "> $result_file") || &Error("Can't open $result_file: + $!"); # print out the colour coded diff - common code is black # code in $old_file but not $new_file is (bg) red # code in $new_file but not $old_file is (bg) green print SDIFF "<div><table align=center bgcolor=white celpadding=0 c +ellspacing=0><tr><th>Color Key:</th></tr>"; print SDIFF "<tr><td bgcolor=lightcoral>".escapeHTML($old_file)."< +/td></tr>"; print SDIFF "<tr><td bgcolor=darkseagreen>".escapeHTML($file)."</t +d></tr>\n"; print SDIFF "</table><br><table celpadding=0 cellspacing=0>"; my $a; my $b; my $a_lines; my $b_lines; traverse_sequences({ MATCH => sub { if ($a) { if ($b) { #$b_lines to compare with $a_lines # print a_lines with hilited diffs print SDIFF "<tr><td bgcolor=lightcoral>"; my @a_table=split(/ */, $a_lines); my @b_table=split(/ */, $b_lines); traverse_sequences( { MATCH => sub { print SDIFF escapeHTM +L($a_table[$_[0]]) }, DISCARD_A => sub { print SDIFF "<span st +yle=\"background-color:yellow\">" . escapeHTML($a_table[$_[0]])."</sp +an>"}, DISCARD_B => sub { print SDIFF "" }, }, \@a_table, \@b_table,); print SDIFF "</td></tr>"; # now print b_lines with hilited diffs print SDIFF "<tr><td bgcolor=darkseagreen>"; traverse_sequences( { MATCH => sub { print SDIFF escapeHTM +L($b_table[$_[1]]) }, DISCARD_A => sub { print SDIFF ""}, DISCARD_B => sub { print SDIFF "<span st +yle=\"background-color:yellow\">" . escapeHTML($b_table[$_[1]])."</sp +an>" }, }, \@a_table, \@b_table,); print SDIFF "</td></tr>"; $b=0; $b_lines=""; } else { print SDIFF "<tr><td bgcolor=lightcoral>".escap +eHTML($a_lines)."</td></tr>"; } $a=0; $a_lines=""; } else { if ($b) { print SDIFF "<tr><td bgcolor=darkseagreen>".esc +apeHTML($b_lines)."</td></tr>"; $b=0; $b_lines=""; } } print SDIFF "<tr><td bgcolor=white>".escapeHTML($ol +d->[$_[0]])."</td></tr>" }, DISCARD_A => sub { $a++; $a_lines.=$old->[$_[0]]; }, DISCARD_B => sub { $b++; $b_lines.=$new->[$_[1]]; }}, $old, $new, ); print SDIFF "</table>\n"; close SDIFF; last if $caught_sigint; }

Replies are listed 'Best First'.
Re: color coded diff (the return)
by tachyon (Chancellor) on Feb 14, 2002 at 14:57 UTC

    Nice job. Perhaps wise to mention that merlyn was the first to demonstrate this concept in Perl :-) BTW there is an online version of the original script here

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      i thought everyone knew that nothing would have been possible without merlyn ... ok, for those who don't know yet, this script was made possible thanks to merlyn, who was the first to ever demonstrate this concept in perl ;]
      just a note : the link you gave for the original version is offline since yesterday :-/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-11-10 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    chatterbot is...






    Results (37 votes). Check out past polls.