login
Header Space

 
 

[PATCH 1/2] Added basic color support to git add --interactive

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Jeff King <peff@...>
Cc: Shawn O. Pearce <spearce@...>, Wincent Colaiuta <win@...>, Git Mailing List <git@...>, Jonathan del Strother <maillist@...>, Johannes Schindelin <Johannes.Schindelin@...>, Frank Lichtenheld <frank@...>
Date: Friday, November 2, 2007 - 11:41 pm

=46rom 29b34bb32846921c9432bf1b74c93d06a0667a44 Mon Sep 17 00:00:00 2001
From: Dan Zwell <dzwell@zwell.net>
Date: Mon, 22 Oct 2007 15:55:20 -0500
Subject: [PATCH] Added basic color support to git add --interactive

Added function "print_colored" that prints text with a color that
is passed in. Converted many calls to "print" to being calls to
"print_colored".

The prompt, the header, and the help output are the 3 types of
colorized output, and each has its own color.

Colorization is done through Term::ANSIColor, which is included
with modern versions of perl. This is optional, and should not
need to be present if color.interactive is not turned on.

Signed-off-by: Dan Zwell <dzwell@zwell.net>
---
I believe this version takes care of the complaints people had
with this patch. I hope this is helpful.

 Documentation/config.txt  |    6 ++++++
 git-add--interactive.perl |   42 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index edf50cd..2fd783f 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -382,6 +382,12 @@ color.diff.<slot>::
 	whitespace).  The values of these variables may be specified as
 	in color.branch.<slot>.
=20
+color.interactive::
+	When true (or `always`), always use colors in `git add
+	--interactive`.  When false (or `never`), never.  When set to
+	`auto`, use colors only when the output is to the
+	terminal. Defaults to false.
+
 color.pager::
 	A boolean to enable/disable colored output when the pager is in
 	use (default is true).
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index ac598f8..16dc7b0 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -2,6 +2,36 @@
=20
 use strict;
=20
+my ($use_color, $prompt_color, $header_color, $help_color, $normal_color);
+my $color_config =3D qx(git config --get color.interactive);
+if ($color_config=3D~/true|always/ || -t STDOUT && $color_config=3D~/auto/=
) {
+	require Term::ANSIColor;
+
+	$use_color =3D "true";
+	# Sane (visible) defaults:
+	$prompt_color =3D Term::ANSIColor::color("blue bold");
+	$header_color =3D Term::ANSIColor::color("bold");
+	$help_color   =3D Term::ANSIColor::color("red bold");
+	$normal_color =3D Term::ANSIColor::color("reset");
+}
+
+sub print_colored {
+	my $color =3D shift;
+	my $string =3D join("", @_);
+
+	if ($use_color) {
+		# Put a color code at the beginning of each line, a reset at the end
+		# color after newlines that are not at the end of the string
+		$string =3D~ s/(\n+)(.)/$1$color$2/g;
+		# reset before newlines
+		$string =3D~ s/(\n+)/$normal_color$1/g;
+		# codes at beginning and end (if necessary):
+		$string =3D~ s/^/$color/;
+		$string =3D~ s/$/$normal_color/ unless $string =3D~ /\n$/;
+	}
+	print $string;
+}
+
 sub run_cmd_pipe {
 	if ($^O eq 'MSWin32') {
 		my @invalid =3D grep {m/[":*]/} @_;
@@ -175,7 +205,7 @@ sub list_and_choose {
 			if (!$opts->{LIST_FLAT}) {
 				print "     ";
 			}
-			print "$opts->{HEADER}\n";
+			print_colored $header_color, "$opts->{HEADER}\n";
 		}
 		for ($i =3D 0; $i < @stuff; $i++) {
 			my $chosen =3D $chosen[$i] ? '*' : ' ';
@@ -205,7 +235,7 @@ sub list_and_choose {
=20
 		return if ($opts->{LIST_ONLY});
=20
-		print $opts->{PROMPT};
+		print_colored $prompt_color, $opts->{PROMPT};
 		if ($opts->{SINGLETON}) {
 			print "> ";
 		}
@@ -544,7 +574,7 @@ sub coalesce_overlapping_hunks {
 }
=20
 sub help_patch_cmd {
-	print <<\EOF ;
+	print_colored $help_color, <<\EOF ;
 y - stage this hunk
 n - do not stage this hunk
 a - stage this and all the remaining hunks
@@ -619,7 +649,7 @@ sub patch_update_cmd {
 		for (@{$hunk[$ix]{TEXT}}) {
 			print;
 		}
-		print "Stage this hunk [y/n/a/d$other/?]? ";
+		print_colored $prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
 		my $line =3D <STDIN>;
 		if ($line) {
 			if ($line =3D~ /^y/i) {
@@ -673,7 +703,7 @@ sub patch_update_cmd {
 			elsif ($other =3D~ /s/ && $line =3D~ /^s/) {
 				my @split =3D split_hunk($hunk[$ix]{TEXT});
 				if (1 < @split) {
-					print "Split into ",
+					print_colored $header_color, "Split into ",
 					scalar(@split), " hunks.\n";
 				}
 				splice(@hunk, $ix, 1,
@@ -766,7 +796,7 @@ sub quit_cmd {
 }
=20
 sub help_cmd {
-	print <<\EOF ;
+	print_colored $help_color, <<\EOF ;
 status        - show paths with changes
 update        - add working tree state to the staged set of changes
 revert        - revert staged set of changes back to the HEAD version
--=20
1.5.3.5.474.g3e4bb
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 1/2] Added basic color support to git add --interactive, Dan Zwell, (Fri Nov 2, 11:41 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)
speck-geostationary