Added and integrated method "color_diff_hunk", which colors lines, and returns them in an array. Coloring bad whitespace is not yet supported. Signed-off-by: Dan Zwell <dzwell@xxxxxxxxx> --- git-add--interactive.perl | 93 ++++++++++++++++++++++++++++++++++---------- 1 files changed, 72 insertions(+), 21 deletions(-) diff --git a/git-add--interactive.perl b/git-add--interactive.perl index f76f008..ba9430c 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -4,9 +4,12 @@ use strict; use Git; my ($use_color, $prompt_color, $header_color, $help_color, $normal_color); +my ($diff_use_color, $new_color, $old_color, $fraginfo_color, + $metainfo_color, $whitespace_color); { my $repo = Git->repository(); + # set interactive color options: my $color_config = $repo->config('color.interactive'); $use_color = 0; @@ -21,27 +24,55 @@ my ($use_color, $prompt_color, $header_color, $help_color, $normal_color); $use_color = 1; } - if ($use_color) { + # set diff color options + my $diff_color_config = $repo->config('color.diff'); + if (!defined $diff_color_config) { + $diff_use_color = 0; + } + elsif ($diff_color_config =~ /true|always/) { + $diff_use_color = 1; + } + elsif ($diff_color_config eq 'auto' && -t STDOUT && + $ENV{'TERM'} ne 'dumb') { + $diff_use_color = 1; + } + + # load color library if needed + if ($use_color || $diff_use_color) { eval { require Term::ANSIColor; }; if ($@) { # library did not load. $use_color = 0; + $diff_use_color = 0; } - else { # set up colors - # Grab the 3 main colors in git color string format, with sane - # (visible) defaults: - $prompt_color = Git::color_to_ansi_code( - scalar $repo->config_default('color.interactive.prompt', - 'bold blue')); - $header_color = Git::color_to_ansi_code( - scalar $repo->config_default('color.interactive.header', - 'bold')); - $help_color = Git::color_to_ansi_code( - scalar $repo->config_default('color.interactive.help', - 'red bold')); + } - $normal_color = Git::color_to_ansi_code('normal'); - } + # convenience function: + sub get_color { + my ($key, $default) = @_; + return Git::color_to_ansi_code( + scalar $repo->config_default($key, $default)); + } + # set interactive colors + if ($use_color) { + # Grab the 3 main colors in git color string format, with sane + # (visible) defaults: + $prompt_color = get_color('color.interactive.prompt', 'bold blue'); + $header_color = get_color('color.interactive.header', 'bold'); + $help_color = get_color('color.interactive.help', 'red bold'); + $normal_color = Git::color_to_ansi_code('normal'); + } + + # set diff colors + if ($diff_use_color) { + $new_color = get_color('color.diff.new', 'green'); + $old_color = get_color('color.diff.old', 'red'); + $fraginfo_color = get_color('color.diff.frag', 'cyan'); + $metainfo_color = get_color('color.diff.meta', 'bold'); + $normal_color = Git::color_to_ansi_code('normal'); + # Not implemented: + #$whitespace_color = get_color('color.diff.whitespace', + #'normal red'); } } @@ -410,6 +441,30 @@ sub parse_diff { return @hunk; } +sub color_diff_hunk { + # return the colored text, so that it can be passed to print() + my ($text) = @_; + if (!$diff_use_color) { + return @$text; + } + + my @ret; + for (@$text) { + if (/^\+/) { + push @ret, colored($new_color, $_); + } elsif (/^\-/) { + push @ret, colored($old_color, $_); + } elsif (/^\@/) { + push @ret, colored($fraginfo_color, $_); + } elsif (/^ /) { + push @ret, colored($normal_color, $_); + } else { + push @ret, colored($metainfo_color, $_); + } + } + return @ret; +} + sub hunk_splittable { my ($text) = @_; @@ -632,9 +687,7 @@ sub patch_update_cmd { my ($ix, $num); my $path = $it->{VALUE}; my ($head, @hunk) = parse_diff($path); - for (@{$head->{TEXT}}) { - print; - } + print color_diff_hunk($head->{TEXT}); $num = scalar @hunk; $ix = 0; @@ -676,9 +729,7 @@ sub patch_update_cmd { if (hunk_splittable($hunk[$ix]{TEXT})) { $other .= '/s'; } - for (@{$hunk[$ix]{TEXT}}) { - print; - } + print color_diff_hunk($hunk[$ix]{TEXT}); print colored($prompt_color, "Stage this hunk [y/n/a/d$other/?]? "); my $line = <STDIN>; if ($line) { -- 1.5.3.5.565.gf0b83-dirty - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html