Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 892
souser Avatar asked May 30 '26 02:05

souser


1 Answers

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";
like image 185
raina77ow Avatar answered May 31 '26 16:05

raina77ow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!