Perl text diff color

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 16:42:38

问题


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


回答1:


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";


来源:https://stackoverflow.com/questions/10443242/perl-text-diff-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!