Subject: [PATCH] Color support added to git-add--interactive.

From: Dan Zwell <[email-masked]>
Date: Fri, 12 Oct 2007 23:13:14 -0500

Recently there was some talk of color for git-add--interactive, but the 
person who said he already had a patch didn't produce it.

-Reads configuration from git-config (using a new key,
  color.add-interactive), respects "auto" if called from a script
-Uses the library Term::ANSIColor, which is included with modern
  versions of perl.

There is one problem--a block is commented out, because adding the 
"--color" option to git-diff-files somehow breaks git-add--interactive, 
and I would love some help from someone who knows a little more about 
the rest of the script than I do. Also, this is the first perl I have 
written, and criticism is welcome. A gzipped patch is attached, in case 
thunderbird mangles the tabs. Feel free to replace the colors that I 
chose with something that better conforms to the "git style", if there 
is such a thing.

diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index be68814..f55d787 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -2,6 +2,18 @@

  use strict;

+my $use_color;
+my $color_config = qx(git config --get color.add-interactive);
+if ($color_config=~"true" || -t STDOUT && $color_config=~"auto") {
+	$use_color = "true";
+	require Term::ANSIColor;
+}
+sub print_ansi_color {
+	if ($use_color) {
+		print Term::ANSIColor::color($_[0]);
+	}
+}
+
  sub run_cmd_pipe {
  	if ($^O eq 'MSWin32') {
  		my @invalid = grep {m/[":*]/} @_;
@@ -175,7 +187,9 @@ sub list_and_choose {
  			if (!$opts->{LIST_FLAT}) {
  				print "     ";
  			}
+			print_ansi_color "bold";
  			print "$opts->{HEADER}\n";
+			print_ansi_color "clear";
  		}
  		for ($i = 0; $i < @stuff; $i++) {
  			my $chosen = $chosen[$i] ? '*' : ' ';
@@ -205,7 +219,9 @@ sub list_and_choose {

  		return if ($opts->{LIST_ONLY});

+		print_ansi_color "bold blue";
  		print $opts->{PROMPT};
+		print_ansi_color "reset";
  		if ($opts->{SINGLETON}) {
  			print "> ";
  		}
@@ -338,6 +354,16 @@ sub add_untracked_cmd {

  sub parse_diff {
  	my ($path) = @_;
+	# FIXME: the following breaks git, and I'm not sure why. When
+	# the following is uncommented, git no longer asks whether we
+	# want to add given hunks.
+	#my @diff;
+	#if ($use_color) {
+	#    #@diff = run_cmd_pipe(qw(git diff-files --color -p --), $path);
+	#}
+	#else {
+	#    #@diff = run_cmd_pipe(qw(git diff-files -p --), $path);
+	#}
  	my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
  	my (@hunk) = { TEXT => [] };

@@ -544,6 +570,7 @@ sub coalesce_overlapping_hunks {
  }

  sub help_patch_cmd {
+	print_ansi_color "blue";
  	print <<\EOF ;
  y - stage this hunk
  n - do not stage this hunk
@@ -555,6 +582,7 @@ k - leave this hunk undecided, see previous 
undecided hunk
  K - leave this hunk undecided, see previous hunk
  s - split the current hunk into smaller hunks
  EOF
+	print_ansi_color "clear";
  }

  sub patch_update_cmd {
@@ -619,7 +647,9 @@ sub patch_update_cmd {
  		for (@{$hunk[$ix]{TEXT}}) {
  			print;
  		}
+		print_ansi_color "bold";
  		print "Stage this hunk [y/n/a/d$other/?]? ";
+		print_ansi_color "reset";
  		my $line = <STDIN>;
  		if ($line) {
  			if ($line =~ /^y/i) {
-- 
1.5.3.4.207.gc0ee

Dan Zwell