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

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Wincent Colaiuta
Date: Saturday, October 13, 2007 - 7:45 am

El 13/10/2007, a las 6:13, Dan Zwell escribió:


Based on a couple of the suggestions you've received I made a couple  
of changes to your patch and given it a quick try-out. I'm no perl  
hacker so there may be better ways.

- as per Jeff's suggestion, changed your print_ansi_color method,  
modelling it after the print_color_ln and color_vprintf functions  
defined in color.c; accepts a color, a string, and an optional  
trailer (where if there is a newline you pass it as the trailer)

- as Johannes pointed out, "clear" and "reset" are not used  
consistently even though the Term::ANSIColor documentation says that  
they're the same, so settled on "clear"; although in any case, the  
changes to the print_ansi_color function mean that it is now the only  
site where clearing takes place

- changed the regex as suggested by Johannes, and a couple of others  
that are used when splitting hunks

- used more explicit notation for regex as proposed by Frank

Took it for a basic spin here and seems to work. Didn't even think  
about implementing user-settable colors.

Cheers,
Wincent

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

  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($$;$) {
+	my $color = shift;
+	my $string = shift;
+	my $trailer = shift;
+	if ($use_color) {
+		printf '%s%s%s', Term::ANSIColor::color($color), $string,
+		    Term::ANSIColor::color('clear');
+	} else {
+		print $string;
+	}
+	if ($trailer) {
+		print $trailer;
+	}
+}
+
  sub run_cmd_pipe {
  	if ($^O eq 'MSWin32') {
  		my @invalid = grep {m/[":*]/} @_;
@@ -175,7 +197,7 @@ sub list_and_choose {
  			if (!$opts->{LIST_FLAT}) {
  				print "     ";
  			}
-			print "$opts->{HEADER}\n";
+			print_ansi_color "bold", "$opts->{HEADER}", "\n";
  		}
  		for ($i = 0; $i < @stuff; $i++) {
  			my $chosen = $chosen[$i] ? '*' : ' ';
@@ -205,7 +227,7 @@ sub list_and_choose {

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

-		print $opts->{PROMPT};
+		print_ansi_color "bold blue", $opts->{PROMPT};
  		if ($opts->{SINGLETON}) {
  			print "> ";
  		}
@@ -338,11 +360,17 @@ sub add_untracked_cmd {

  sub parse_diff {
  	my ($path) = @_;
-	my @diff = run_cmd_pipe(qw(git diff-files -p --), $path);
+	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 (@hunk) = { TEXT => [] };

  	for (@diff) {
-		if (/^@@ /) {
+		if (/^[^-+ ]*@@ /) {
  			push @hunk, { TEXT => [] };
  		}
  		push @{$hunk[-1]{TEXT}}, $_;
@@ -360,7 +388,7 @@ sub hunk_splittable {
  sub parse_hunk_header {
  	my ($line) = @_;
  	my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
-	    $line =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
+	    $line =~ /^[^-+ ]*@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
  	return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
  }

@@ -426,7 +454,7 @@ sub split_hunk {
  			}
  			push @{$this->{TEXT}}, $line;
  			$this->{ADDDEL}++;
-			if ($line =~ /^-/) {
+			if ($line =~ /^[^-+ ]*-/) {
  				$this->{OCNT}++;
  			}
  			else {
@@ -483,7 +511,7 @@ sub merge_hunk {
  	$o_cnt = $n_cnt = 0;
  	for ($i = 1; $i < @{$prev->{TEXT}}; $i++) {
  		my $line = $prev->{TEXT}[$i];
-		if ($line =~ /^\+/) {
+		if ($line =~ /^[^-+ ]*\+/) {
  			$n_cnt++;
  			push @line, $line;
  			next;
@@ -501,7 +529,7 @@ sub merge_hunk {

  	for ($i = 1; $i < @{$this->{TEXT}}; $i++) {
  		my $line = $this->{TEXT}[$i];
-		if ($line =~ /^\+/) {
+		if ($line =~ /^[^-+ ]*\+/) {
  			$n_cnt++;
  			push @line, $line;
  			next;
@@ -544,7 +572,7 @@ sub coalesce_overlapping_hunks {
  }

  sub help_patch_cmd {
-	print <<\EOF ;
+	my $help = <<\EOF ;
  y - stage this hunk
  n - do not stage this hunk
  a - stage this and all the remaining hunks
@@ -555,6 +583,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 "blue", $_, "\n" foreach (split /[\r\n]/, $help);
  }

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

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

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