souser
souser

Reputation: 6124

Perl text diff color

I want to be able to compare two text string and display the difference in color. I tried String::Diff but could not get the difference to display in color. I am using Windows with Active State perl 5, version 12.

Edit : the ansi color etc do not help me with displaying the differences in color
Edit : Here is the result I want

$string1 = "This is string 1" ;
$string2 = "This is string 2" ;

some_diff_cmd($string1,$string2) ;

Output I want (the entries in bold should be in color say red)

### Strings do not match ####

string1 = This is string 1
string2 = This is string 2

Upvotes: 3

Views: 1960

Answers (1)

raina77ow
raina77ow

Reputation: 106443

How about this?

use Win32::Console::ANSI;
use String::Diff qw( diff );

my @strings = (
  'This is string 1', 'This is string 2'
);

my $BOLD_RED_MARK = "\e[1;31m"; # or \e[0;31m, if bold is not required
my $RESET_MARK    = "\e[0m";

my $diff = String::Diff::diff(@strings,
   remove_open  => $BOLD_RED_SIGN,
   remove_close => $RESET_SIGN,
   append_open  => $BOLD_RED_SIGN,
   append_close => $RESET_SIGN,
);

print $diff->[0], "\n";
print $diff->[1], "\n";

Upvotes: 5

Related Questions