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

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Git Mailing List <git@...>
Cc: Jeff King <peff@...>, Jonathan del Strother <maillist@...>, Johannes Schindelin Wincent Colaiuta <win@...>
Date: Saturday, October 13, 2007 - 12:13 am

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
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH] Color support added to git-add--interactive., Dan Zwell, (Sat Oct 13, 12:13 am)
Re: [PATCH] Color support added to git-add--interactive., Johannes Schindelin, (Sat Oct 13, 8:25 am)
Re: [PATCH] Color support added to git-add--interactive., Frank Lichtenheld, (Sat Oct 13, 8:49 am)
Re: [PATCH] Color support added to git-add--interactive., Wincent Colaiuta, (Sat Oct 13, 10:45 am)
Re: [PATCH] Color support added to git-add--interactive., Johannes Schindelin, (Sat Oct 13, 12:38 pm)
Re: [PATCH] Color support added to git-add--interactive., Jean-Luc Herren, (Sat Oct 13, 12:38 pm)
Re: [PATCH] Color support added to git-add--interactive., Wincent Colaiuta, (Sat Oct 13, 1:14 pm)
Re: [PATCH] Color support added to git-add--interactive., Andreas Ericsson, (Sat Oct 13, 2:31 pm)
Re: [PATCH] Color support added to git-add--interactive., Wincent Colaiuta, (Sat Oct 13, 4:36 pm)
Re: [PATCH] Color support added to git-add--interactive., Jean-Luc Herren, (Sat Oct 13, 6:23 pm)
[PATCH 0/3] Adding colors to git-add--interactive, Dan Zwell, (Sat Nov 10, 10:21 pm)
[PATCH 0/3] Adding colors to git-add--interactive, Dan Zwell, (Sat Nov 10, 8:01 pm)
[PATCH 0/5] Colors for git-add--interactive, Dan Zwell, (Thu Nov 22, 6:54 am)
Re: [PATCH 0/5] Colors for git-add--interactive, Junio C Hamano, (Thu Nov 22, 3:20 pm)
Re: [PATCH 0/5] Colors for git-add--interactive, Jeff King, (Thu Nov 22, 7:57 am)
Re: [PATCH 0/3] Adding colors to git-add--interactive, Junio C Hamano, (Sun Nov 11, 4:23 am)