Perl文本差异颜色

时间:2012-05-04 05:40:42

标签: string perl colors diff

我希望能够比较两个文本字符串并显示颜色的差异。我试过String::Diff但是无法区分显示颜色。我在Windows上使用Active State perl 5,版本12。

编辑:ansi颜色等没有帮助我显示颜色的差异
编辑:这是我想要的结果

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

some_diff_cmd($string1,$string2) ;

我想要的输出(以粗体显示的条目应为彩色,表示红色)

###字符串与####

不匹配

string1 =这是字符串 1
string2 =这是字符串 2

1 个答案:

答案 0 :(得分:5)

这个怎么样?

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