>From ad3c6a2f015197a72d85039783a63a24c19f7017 Mon Sep 17 00:00:00 2001 From: Dan Zwell <dzwell@xxxxxxxxx> Date: Mon, 22 Oct 2007 16:08:01 -0500 Subject: [PATCH] Let git-add--interactive read colors from .gitconfig 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_string() in Git.pm parses these strings and returns ANSI color codes (using Term::ANSIColor). Signed-off-by: Dan Zwell <dzwell@xxxxxxxxx> --- 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. Documentation/config.txt | 7 +++++ git-add--interactive.perl | 22 ++++++++++++----- perl/Git.pm | 56 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/Documentation/config.txt b/Documentation/config.txt index 2fd783f..ade3399 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 16dc7b0..2bce5a1 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -1,18 +1,26 @@ #!/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/) { - require Term::ANSIColor; - $use_color = "true"; - # 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: + 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"); } sub print_colored { diff --git a/perl/Git.pm b/perl/Git.pm index 3f4080c..9100e0b 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 @@ -550,6 +549,61 @@ sub config_bool { } +=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"); + +=cut + +sub color_to_ansi_code { + my ($git_string) = @_; + my @ansi_words; + my %attrib_mappings = ( + "bold" => "bold", + "ul" => "underline", + "blink" => "blink", + # not supported: + #"dim" => "", + "reverse" => "reverse" + ); + my ($fg_done, $word); + + foreach $word (split /\s+/, $git_string) { + if ($word =~ /normal/) { + $fg_done = "true"; + } + elsif ($word =~ /black|red|green|yellow/ || + $word =~ /blue|magenta|cyan|white/) { + # is a color. + if ($fg_done) { + # this is the background + push @ansi_words, "on_" . $word; + } + else { + # this is foreground + $fg_done = "true"; + push @ansi_words, $word; + } + } + else { + # this is an attribute, not a color. + if ($attrib_mappings{$word}) { + push(@ansi_words, + $attrib_mappings{$word}); + } + } + } + require Term::ANSIColor; + return Term::ANSIColor::color(join(" ", @ansi_words)||"reset"); +} + =item ident ( TYPE | IDENTSTR ) =item ident_person ( TYPE | IDENTSTR | IDENTARRAY ) -- 1.5.3.5.474.g3e4bb - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html