git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Dan Zwell <dzwell@zwell.net>
To: Git Mailing List <git@vger.kernel.org>
Cc: Jeff King <peff@peff.net>, Junio C Hamano <gitster@pobox.com>,
	"Shawn O. Pearce" <spearce@spearce.org>,
	Wincent Colaiuta <win@wincent.com>,
	Jonathan del Strother <maillist@steelskies.com>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Frank Lichtenheld <frank@lichtenheld.de>,
	Jakub Narebski <jnareb@gmail.com>
Subject: [PATCH 4/5] Let git-add--interactive read colors from configuration
Date: Thu, 22 Nov 2007 04:56:06 -0600	[thread overview]
Message-ID: <20071122045606.0232fc2d@paradox.zwell.net> (raw)
In-Reply-To: <20071110180109.34febc3f@paradox.zwell.net>

Colors are specified in color.interactive.{prompt,header,help}.
They are specified as git color strings as described in the
documentation. The method color_to_ansi_code() in Git.pm parses
these strings and returns ANSI color codes (using
Term::ANSIColor).

Signed-off-by: Dan Zwell <dzwell@zwell.net>
---
 Documentation/config.txt  |    7 ++++
 git-add--interactive.perl |   20 ++++++++---
 perl/Git.pm               |   80 ++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 100 insertions(+), 7 deletions(-)

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 3712d6a..47c1ab2 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -388,6 +388,13 @@ color.interactive::
 	`auto`, use colors only when the output is to the
 	terminal. Defaults to false.
 
+color.interactive.<slot>::
+	Use customized color for `git add --interactive`
+	output. `<slot>` may be `prompt`, `header`, or `help`, for
+	three distinct types of normal output from interactive
+	programs.  The values of these variables may be specified as
+	in color.branch.<slot>.
+
 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 2b5559f..f76f008 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -6,8 +6,8 @@ use Git;
 my ($use_color, $prompt_color, $header_color, $help_color, $normal_color);
 
 {
-	# set color options:
 	my $repo = Git->repository();
+	# set interactive color options:
 	my $color_config = $repo->config('color.interactive');
 	$use_color = 0;
 	if (!defined $color_config) {
@@ -28,11 +28,19 @@ my ($use_color, $prompt_color, $header_color, $help_color, $normal_color);
 			$use_color = 0;
 		}
 		else { # set up colors
-			# Sane (visible) defaults:
-			$prompt_color = Term::ANSIColor::color('blue bold');
-			$header_color = Term::ANSIColor::color('bold');
-			$help_color   = Term::ANSIColor::color('red bold');
-			$normal_color = Term::ANSIColor::color('reset');
+			# Grab the 3 main colors in git color string format, with sane
+			# (visible) defaults:
+			$prompt_color = Git::color_to_ansi_code(
+				scalar $repo->config_default('color.interactive.prompt',
+					'bold blue'));
+			$header_color = Git::color_to_ansi_code(
+				scalar $repo->config_default('color.interactive.header',
+					'bold'));
+			$help_color = Git::color_to_ansi_code(
+				scalar $repo->config_default('color.interactive.help',
+					'red bold'));
+
+			$normal_color = Git::color_to_ansi_code('normal');
 		}
 	}
 }
diff --git a/perl/Git.pm b/perl/Git.pm
index 7327300..18ef6b4 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -515,7 +515,6 @@ sub config {
 	};
 }
 
-
 =item config_bool ( VARIABLE )
 
 Retrieve the bool configuration C<VARIABLE>. The return value
@@ -578,6 +577,85 @@ sub config_default {
 	}
 }
 
+=item color_to_ansi_code ( COLOR )
+
+Converts a git-style color string, like "underline blue white" to
+an ANSI color code. The code is generated by Term::ANSIColor,
+after the string is parsed into the format that is accepted by
+that module. Used as follows:
+
+	print color_to_ansi_code("underline blue white");
+	print "some text";
+	print color_to_ansi_code("normal");
+
+color_to_ansi_code('') returns the empty string, and should do
+nothing when printed.
+
+=cut
+
+sub color_to_ansi_code {
+	my ($git_string) = @_;
+	my @ansi_words;
+	my %git_to_perl_color = (
+		'bold'    => 'bold',
+		'ul'      => 'underline',
+		'blink'   => 'blink',
+		'reverse' => 'reverse'
+		# not supported by Term::ANSIColor:
+		#'dim'     => ''
+	);
+	my %valid_color = map { $_ => 1 } qw(black red green yellow
+					    blue magenta cyan white);
+
+	my ($fg_done, $token);
+	foreach $token (split /\s+/, $git_string) {
+		$token = lc($token);
+
+		if ($token eq 'normal') {
+			$fg_done = 1;
+		}
+		elsif (exists $valid_color{$token}) {
+			# is a color.
+			if ($fg_done) {
+				# this is the background
+				push @ansi_words, 'on_' . $token;
+			}
+			else {
+				# this is foreground
+				$fg_done = 1;
+				push @ansi_words, $token;
+			}
+		}
+		else {
+			# this is an attribute, not a color.
+			if ($git_to_perl_color{$token}) {
+				push(@ansi_words,
+					 $git_to_perl_color{$token});
+			}
+			else {
+				print STDERR 'Warning: bad color or attribute: ';
+				print STDERR "\"$token\". Check git configuration.\n";
+			}
+		}
+	}
+
+	# decide what to return--return color codes, 'clear' code, or
+	# the empty string, depending on the input we were passed /
+	# what we have processed:
+	if (@ansi_words) {
+		return Term::ANSIColor::color(join(' ', @ansi_words));
+	}
+	else {
+		if ($fg_done) {
+			# the git attrib 'normal' was processed
+			return Term::ANSIColor::color('clear');
+		}
+		else {
+			return '';
+		}
+	}
+}
+
 =item ident ( TYPE | IDENTSTR )
 
 =item ident_person ( TYPE | IDENTSTR | IDENTARRAY )
-- 
1.5.3.5.565.gf0b83-dirty

  parent reply	other threads:[~2007-11-22 11:04 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-13  4:13 [PATCH] Color support added to git-add--interactive Dan Zwell
2007-10-13  8:12 ` Jeff King
2007-10-13 12:49   ` Frank Lichtenheld
2007-10-13 12:25 ` Johannes Schindelin
2007-10-13 14:45 ` Wincent Colaiuta
2007-10-13 16:38   ` Jean-Luc Herren
2007-10-13 17:14     ` Wincent Colaiuta
2007-10-13 18:31     ` Andreas Ericsson
2007-10-13 16:38   ` Johannes Schindelin
2007-10-13 17:27   ` Jeff King
2007-10-13 17:51     ` Jeff King
2007-10-13 20:03       ` Dan Zwell
2007-10-13 20:36         ` Wincent Colaiuta
2007-10-13 21:50           ` Dan Z
2007-10-13 22:23             ` Jean-Luc Herren
2007-10-15  3:43         ` Jeff King
2007-10-17  0:47           ` revised: " Dan Zwell
2007-10-17  1:51             ` Shawn O. Pearce
2007-10-17  7:57               ` Dan Zwell
2007-10-17  8:11                 ` Shawn O. Pearce
2007-10-22 21:32               ` [PATCH 1/2] Added basic color support to git add --interactive Dan Zwell
2007-10-23  2:11                 ` [PATCH] resend of git-add--interactive color patch against spearce/pu Dan Zwell
2007-10-23  2:19                 ` [PATCH] Let git-add--interactive read "git colors" from git-config Dan Zwell
2007-10-23  4:29                   ` Jeff King
2007-10-23  4:40                     ` Shawn O. Pearce
2007-10-23  4:03                 ` [PATCH 1/2] Added basic color support to git add --interactive Jeff King
2007-10-23  6:28                   ` Wincent Colaiuta
2007-10-23  6:41                     ` Jeff King
2007-10-23  7:44                       ` Wincent Colaiuta
2007-10-22 21:40               ` [PATCH 2/2] Let git-add--interactive read colors from git-config Dan Zwell
2007-10-23  4:27                 ` Jeff King
2007-10-23  8:52                   ` Dan Zwell
2007-11-03  3:41                     ` [PATCH 1/2] Added basic color support to git add --interactive Dan Zwell
2007-11-04  4:57                       ` Jeff King
2007-11-04  5:36                         ` Junio C Hamano
2007-11-04  5:43                           ` Jeff King
2007-11-11  0:01                             ` [PATCH 0/3] Adding colors to git-add--interactive Dan Zwell
2007-11-11  7:54                               ` Jeff King
2007-11-11  8:23                                 ` Junio C Hamano
2007-11-11  8:39                                   ` Dan Zwell
2007-11-22 10:54                               ` [PATCH 0/5] Colors for git-add--interactive Dan Zwell
2007-11-22 11:57                                 ` Jeff King
2007-11-22 19:20                                 ` Junio C Hamano
2007-11-22 10:54                               ` [PATCH 1/5] Added basic color support to git add --interactive Dan Zwell
2007-11-22 10:55                               ` [PATCH 2/5] Don't return 'undef' in case called in a vector context Dan Zwell
2007-11-22 12:06                                 ` Jeff King
2007-11-22 21:17                                 ` Junio C Hamano
2007-11-23  4:15                                   ` Dan Zwell
2007-11-22 10:55                               ` [PATCH 3/5] Added config_default($key, $default) to Git.pm Dan Zwell
2007-11-22 12:14                                 ` Jeff King
2007-11-22 10:56                               ` Dan Zwell [this message]
2007-11-22 12:18                                 ` [PATCH 4/5] Let git-add--interactive read colors from configuration Jeff King
2007-11-22 21:28                                   ` Junio C Hamano
2007-11-22 22:30                                     ` Jeff King
2007-11-23  5:32                                       ` Dan Zwell
2007-11-23  9:09                                         ` Jeff King
2007-11-23  9:17                                           ` Junio C Hamano
2007-11-22 10:56                               ` [PATCH 5/5] Added diff hunk coloring to git-add--interactive Dan Zwell
2007-11-22 12:25                                 ` Jeff King
2007-11-22 21:37                                   ` Junio C Hamano
2007-11-23 10:21                                     ` Jeff King
2007-11-22 22:35                                   ` Junio C Hamano
2007-11-11  0:01                             ` [PATCH 1/3] Added basic color support to git add --interactive Dan Zwell
2007-11-11  0:02                             ` [PATCH 2/3] Let git-add--interactive read colors from .gitconfig Dan Zwell
2007-11-11  0:03                             ` [PATCH 3/3] Added diff hunk coloring to git-add--interactive Dan Zwell
2007-11-11 10:00                               ` Junio C Hamano
2007-11-11  2:21                             ` [PATCH 0/3] Adding colors " Dan Zwell
2007-11-11  2:23                             ` Subject: [PATCH 1/3] Added basic color support to git add --interactive Dan Zwell
2007-11-11 19:56                               ` Junio C Hamano
2007-11-11  2:23                             ` Subject: [PATCH 2/3] Let git-add--interactive read colors from .gitconfig Dan Zwell
2007-11-11  9:53                               ` Junio C Hamano
2007-11-11 10:34                                 ` Junio C Hamano
2007-11-13  1:39                                 ` Dan Zwell
2007-11-13  2:32                                   ` Junio C Hamano
2007-11-13  2:55                                     ` Dan Zwell
2007-11-13  7:26                                       ` Jeff King
2007-11-13  7:29                                       ` Junio C Hamano
2007-11-13  8:25                                         ` Dan Zwell
2007-11-13  9:46                                           ` Jakub Narebski
2007-11-03  3:41                     ` [PATCH 2/2] " Dan Zwell
2007-11-03  5:06                       ` *[PATCH " Junio C Hamano
2007-11-03  7:26                         ` Dan Zwell
2007-11-03 18:11                           ` Junio C Hamano
2007-10-15  4:12   ` [PATCH] Color support added to git-add--interactive Jeff King
2007-10-13 20:21 ` Tom Tobin
2007-10-13 20:26   ` Tom Tobin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20071122045606.0232fc2d@paradox.zwell.net \
    --to=dzwell@zwell.net \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=frank@lichtenheld.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jnareb@gmail.com \
    --cc=maillist@steelskies.com \
    --cc=peff@peff.net \
    --cc=spearce@spearce.org \
    --cc=win@wincent.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).