3

我希望能够比较两个文本字符串并显示颜色的差异。我尝试了String::Diff但无法以颜色显示差异。我正在使用带有 Active State perl 5 版本 12 的 Windows。

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

$string1 = "这是字符串 1" ;
$string2 = "这是字符串 2" ;

some_diff_cmd($string1,$string2) ;

我想要的输出(粗体的条目应该是红色的)

### 字符串不匹配####

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

4

1 回答 1

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";
于 2012-05-04T09:21:34.683 回答