git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Dan Zwell <dzwell@zwell.net>
Cc: Jeff King <peff@peff.net>,
	"Shawn O. Pearce" <spearce@spearce.org>,
	Wincent Colaiuta <win@wincent.com>,
	Git Mailing List <git@vger.kernel.org>,
	Jonathan del Strother <maillist@steelskies.com>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Frank Lichtenheld <frank@lichtenheld.de>
Subject: Re: *[PATCH 2/2] Let git-add--interactive read colors from .gitconfig
Date: Fri, 02 Nov 2007 22:06:08 -0700	[thread overview]
Message-ID: <7vy7dfyl33.fsf_-_@gitster.siamese.dyndns.org> (raw)
In-Reply-To: <20071102224111.7f7e165c@paradox.zwell.net> (Dan Zwell's message of "Fri, 2 Nov 2007 22:41:11 -0500")

Dan Zwell <dzwell@zwell.net> writes:

> One thought is that is seems a bit sloppy to call "require Term::ANSIColor"
> within color_to_ansi_code(), but I can't really see a better way. After all,
> that is where the methods from that library are really needed. And I don't
> know why Git.pm should need to know whether color will end up being used.

How big is Term::ANSIColor, and how universally available is it?
Implementing the ANSI "ESC [ %d m" arithmetic color.c in Perl
ourselves does not feel too much effort, compared to the
potential hassle of dealing with extra dependencies and
potential drift between scripts and C implementation.

We may later want to update the C side to take colors from
terminfo, but that is a separate topic ;-)

Since your 2/2 updates on your 1/2, the diff is difficult to
comment on, so I'll comment on the combined effects.

diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index ac598f8..2bce5a1 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -1,6 +1,44 @@
 #!/usr/bin/perl -w
 
 use strict;
+use Git;
+
+my ($use_color, $prompt_color, $header_color, $help_color, $normal_color);
+my $color_config = qx(git config --get color.interactive);
+if ($color_config=~/true|always/ || -t STDOUT && $color_config=~/auto/) {
+	$use_color = "true";
+	# Grab the 3 main colors in git color string format, with sane
+	# (visible) defaults:
+	my $repo = Git->repository();
+	my $git_prompt_color =
+		Git::config($repo, "color.interactive.prompt")||"bold blue";
+	my $git_header_color =
+		Git::config($repo, "color.interactive.header")||"bold";
+	my $git_help_color =
+		Git::config($repo, "color.interactive.help")||"red bold";
+
+	$prompt_color = Git::color_to_ansi_code($git_prompt_color);
+	$header_color = Git::color_to_ansi_code($git_header_color);
+	$help_color   = Git::color_to_ansi_code($git_help_color);
+	$normal_color = Git::color_to_ansi_code("normal");
+}

If we are to still use Term::ANSIColor, then we might want to
protect ourselves from a broken installation:

        if ($color_config =~ /true|always/ ||
            -t STDOUT && $color_config =~ /auto/) {
                eval { require Term::ANSIColor; };
                if (!$@) {
                        $use_color = 1;
                        ... set up the colors ...
                }
                else {
                        $use_color = 0;
                }
        }

Then you can remove the require from Git::color_to_ansi_code().
Your current calling convention is to require the calling site
to be sure the module is availble; the suggested change merely
makes it responsible to also make sure the module is loaded.

Hmm?

By the way, coloring the diff text itself may be just the matter
of doing something like this (except that you now need to snarf
OLD, NEW, METAINFO and FRAGINFO colors for diff configuration as
well.

In addition to a small matter of testing, a more practical issue
would be to add PAGER support there, I think.

---

 git-add--interactive.perl |   32 ++++++++++++++++++++++++--------
 1 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 2bce5a1..1063a34 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -388,6 +388,27 @@ sub parse_diff {
 	return @hunk;
 }
 
+sub print_diff_hunk {
+	my ($text) = @_;
+	for (@$text) {
+		if (!$use_color) {
+			print;
+			next;
+		}
+		if (/^\+/) {
+			print_colored $new_color, $_;
+		} elsif (/^\-/) {
+			print_colored $old_color, $_;
+		} elsif (/^\@/) {
+			print_colored $fraginfo_color, $_;
+		} elsif (/^ /) {
+			print_colored $normal_color, $_;
+		} else {
+			print_colored $metainfo_color, $_;
+		}
+	}
+}
+
 sub hunk_splittable {
 	my ($text) = @_;
 
@@ -610,9 +631,7 @@ sub patch_update_cmd {
 	my ($ix, $num);
 	my $path = $it->{VALUE};
 	my ($head, @hunk) = parse_diff($path);
-	for (@{$head->{TEXT}}) {
-		print;
-	}
+	print_diff_hunk($head->{TEXT});
 	$num = scalar @hunk;
 	$ix = 0;
 
@@ -654,9 +673,7 @@ sub patch_update_cmd {
 		if (hunk_splittable($hunk[$ix]{TEXT})) {
 			$other .= '/s';
 		}
-		for (@{$hunk[$ix]{TEXT}}) {
-			print;
-		}
+		print_diff_hunk($hunk[$ix]{TEXT});
 		print_colored $prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
 		my $line = <STDIN>;
 		if ($line) {
@@ -794,8 +811,7 @@ sub diff_cmd {
 				     HEADER => $status_head, },
 				   @mods);
 	return if (!@them);
-	system(qw(git diff-index -p --cached HEAD --),
-	       map { $_->{VALUE} } @them);
+	system(qw(git diff -p --cached HEAD --), map { $_->{VALUE} } @them);
 }
 
 sub quit_cmd {

  reply	other threads:[~2007-11-03  5:06 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                               ` [PATCH 4/5] Let git-add--interactive read colors from configuration Dan Zwell
2007-11-22 12:18                                 ` 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                       ` Junio C Hamano [this message]
2007-11-03  7:26                         ` *[PATCH " 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=7vy7dfyl33.fsf_-_@gitster.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=dzwell@zwell.net \
    --cc=frank@lichtenheld.de \
    --cc=git@vger.kernel.org \
    --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).