3

2 つの文字列を比較して色の違いを表示できるようにしたい。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 に答える