@@ -2877,6 +2877,7 @@ function count_array_diff(
2877
2877
2878
2878
function generate_array_diff (array $ ar1 , array $ ar2 , bool $ is_reg , array $ w ): array
2879
2879
{
2880
+ global $ colorize ;
2880
2881
$ idx1 = 0 ;
2881
2882
$ cnt1 = @count ($ ar1 );
2882
2883
$ idx2 = 0 ;
@@ -2885,6 +2886,25 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
2885
2886
$ old1 = [];
2886
2887
$ old2 = [];
2887
2888
2889
+ $ format_expected_line = function (int $ line_number , string $ contents ) use ($ colorize ): string {
2890
+ $ output = sprintf ("%03d- " , $ line_number ) . $ contents ;
2891
+ if ($ colorize ) {
2892
+ // Reuse the colors used for `-` in other diff tools.
2893
+ // Here, red should be interpreted as "removed", and not "bad".
2894
+ return "\e[1;31m {$ output }\e[0m " ;
2895
+ }
2896
+ return $ output ;
2897
+ };
2898
+ $ format_actual_line = function (int $ line_number , string $ contents ) use ($ colorize ): string {
2899
+ $ output = sprintf ("%03d+ " , $ line_number ) . $ contents ;
2900
+ if ($ colorize ) {
2901
+ // Reuse the colors used for `+` in other diff tools.
2902
+ // Here, green should be interpreted as "added", and not "good".
2903
+ return "\e[1;32m {$ output }\e[0m " ;
2904
+ }
2905
+ return $ output ;
2906
+ };
2907
+
2888
2908
while ($ idx1 < $ cnt1 && $ idx2 < $ cnt2 ) {
2889
2909
if (comp_line ($ ar1 [$ idx1 ], $ ar2 [$ idx2 ], $ is_reg )) {
2890
2910
$ idx1 ++;
@@ -2895,12 +2915,12 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
2895
2915
$ c2 = @count_array_diff ($ ar1 , $ ar2 , $ is_reg , $ w , $ idx1 , $ idx2 + 1 , $ cnt1 , $ cnt2 , 10 );
2896
2916
2897
2917
if ($ c1 > $ c2 ) {
2898
- $ old1 [$ idx1 ] = sprintf ( " %03d- " , $ idx1 + 1 ) . $ w [$ idx1 ++];
2918
+ $ old1 [$ idx1 ] = $ format_expected_line ( $ idx1 + 1 , $ w [$ idx1 ++]) ;
2899
2919
} elseif ($ c2 > 0 ) {
2900
- $ old2 [$ idx2 ] = sprintf ( " %03d+ " , $ idx2 + 1 ) . $ ar2 [$ idx2 ++];
2920
+ $ old2 [$ idx2 ] = $ format_actual_line ( $ idx2 + 1 , $ ar2 [$ idx2 ++]) ;
2901
2921
} else {
2902
- $ old1 [$ idx1 ] = sprintf ( " %03d- " , $ idx1 + 1 ) . $ w [$ idx1 ++];
2903
- $ old2 [$ idx2 ] = sprintf ( " %03d+ " , $ idx2 + 1 ) . $ ar2 [$ idx2 ++];
2922
+ $ old1 [$ idx1 ] = $ format_expected_line ( $ idx1 + 1 , $ w [$ idx1 ++]) ;
2923
+ $ old2 [$ idx2 ] = $ format_actual_line ( $ idx2 + 1 , $ ar2 [$ idx2 ++]) ;
2904
2924
}
2905
2925
}
2906
2926
}
@@ -2933,11 +2953,11 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
2933
2953
}
2934
2954
2935
2955
while ($ idx1 < $ cnt1 ) {
2936
- $ diff [] = sprintf ( " %03d- " , $ idx1 + 1 ) . $ w [$ idx1 ++];
2956
+ $ diff [] = $ format_expected_line ( $ idx1 + 1 , $ w [$ idx1 ++]) ;
2937
2957
}
2938
2958
2939
2959
while ($ idx2 < $ cnt2 ) {
2940
- $ diff [] = sprintf ( " %03d+ " , $ idx2 + 1 ) . $ ar2 [$ idx2 ++];
2960
+ $ diff [] = $ format_actual_line ( $ idx2 + 1 , $ ar2 [$ idx2 ++]) ;
2941
2961
}
2942
2962
2943
2963
return $ diff ;
0 commit comments