diff --git a/git-add--interactive.perl b/git-add--interactive.perl index dc30380..22904e5 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -2,12 +2,32 @@ use strict; +my %DIFF_COLOR = ( + reset => "\033[m", # reset + plain => "", # PLAIN (normal) + meta => "\033[1m", # METAINFO (bold) + frag => "\033[36m", # FRAGINFO (cyan) + old => "\033[31m", # OLD (red) + new => "\033[32m", # NEW (green) + commit => "\033[33m", # COMMIT (yellow) + whitespace=> "\033[41m",# WHITESPACE (red background) +); + +my $use_color; + sub run_cmd_pipe { my $fh = undef; open($fh, '-|', @_) or die; return <$fh>; } +sub parse_color { + $use_color = run_cmd_pipe(qw(git config --get diff.color)); + if ($use_color ne "false") { + $use_color = 1; + } +} + my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir)); if (!defined $GIT_DIR) { @@ -563,7 +583,7 @@ sub patch_update_cmd { my $path = $it->{VALUE}; my ($head, @hunk) = parse_diff($path); for (@{$head->{TEXT}}) { - print; + print $DIFF_COLOR{meta} . $_ . $DIFF_COLOR{reset}; } $num = scalar @hunk; $ix = 0; @@ -606,8 +626,19 @@ sub patch_update_cmd { if (hunk_splittable($hunk[$ix]{TEXT})) { $other .= '/s'; } + + my $color; for (@{$hunk[$ix]{TEXT}}) { - print; + if ($_ =~ /^-/) { + $color = "old"; + } elsif ($_ =~ /^\+/) { + $color = "new"; + } elsif ($_ =~ /^@@/) { + $color = "frag"; + } else { + $color = "plain"; + } + print $DIFF_COLOR{$color} . $_ . $DIFF_COLOR{reset}; } print "Stage this hunk [y/n/a/d$other/?]? "; my $line = ; @@ -749,8 +780,14 @@ sub diff_cmd { HEADER => $status_head, }, @mods); return if (!@them); - system(qw(git diff-index -p --cached HEAD --), - map { $_->{VALUE} } @them); + if ($use_color) { + system(qw(git diff-index -p --color --cached HEAD --), + map { $_->{VALUE} } @them); + } + else { + system(qw(git diff-index -p --cached HEAD --), + map { $_->{VALUE} } @them); + } } sub quit_cmd { @@ -798,6 +835,7 @@ sub main_loop { my @z; +parse_color(); refresh(); status_cmd(); main_loop();