Skip to content

Commit b4c61ed

Browse files
committed
Color support for "git-add -i"
This is mostly lifted from earlier series by Dan Zwell, but updated to use "git config --get-color" and "git config --get-colorbool" to make it simpler and more consistent with commands written in C. A new configuration color.interactive variable is like color.diff and color.status, and controls if "git-add -i" uses color. A set of configuration variables, color.interactive.<slot>, are used to define what color is used for the prompt, header, and help text. For perl scripts, Git.pm provides $repo->get_color() method, which takes the slot name and the default color, and returns the terminal escape sequence to color the output text. $repo->get_colorbool() method can be used to check if color is set to be used for a given operation. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 0f6f5a4 commit b4c61ed

File tree

3 files changed

+146
-20
lines changed

3 files changed

+146
-20
lines changed

Documentation/config.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,18 @@ color.diff.<slot>::
391391
whitespace). The values of these variables may be specified as
392392
in color.branch.<slot>.
393393

394+
color.interactive::
395+
When set to `always`, always use colors in `git add --interactive`.
396+
When false (or `never`), never. When set to `true` or `auto`, use
397+
colors only when the output is to the terminal. Defaults to false.
398+
399+
color.interactive.<slot>::
400+
Use customized color for `git add --interactive`
401+
output. `<slot>` may be `prompt`, `header`, or `help`, for
402+
three distinct types of normal output from interactive
403+
programs. The values of these variables may be specified as
404+
in color.branch.<slot>.
405+
394406
color.pager::
395407
A boolean to enable/disable colored output when the pager is in
396408
use (default is true).

git-add--interactive.perl

Lines changed: 99 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
11
#!/usr/bin/perl -w
22

33
use strict;
4+
use Git;
5+
6+
# Prompt colors:
7+
my ($prompt_color, $header_color, $help_color, $normal_color);
8+
# Diff colors:
9+
my ($new_color, $old_color, $fraginfo_color, $metainfo_color, $whitespace_color);
10+
11+
my ($use_color, $diff_use_color);
12+
my $repo = Git->repository();
13+
14+
$use_color = $repo->get_colorbool('color.interactive');
15+
16+
if ($use_color) {
17+
# Set interactive colors:
18+
19+
# Grab the 3 main colors in git color string format, with sane
20+
# (visible) defaults:
21+
$prompt_color = $repo->get_color("color.interactive.prompt", "bold blue");
22+
$header_color = $repo->get_color("color.interactive.header", "bold");
23+
$help_color = $repo->get_color("color.interactive.help", "red bold");
24+
$normal_color = $repo->get_color("", "reset");
25+
26+
# Do we also set diff colors?
27+
$diff_use_color = $repo->get_colorbool('color.diff');
28+
if ($diff_use_color) {
29+
$new_color = $repo->get_color("color.diff.new", "green");
30+
$old_color = $repo->get_color("color.diff.old", "red");
31+
$fraginfo_color = $repo->get_color("color.diff.frag", "cyan");
32+
$metainfo_color = $repo->get_color("color.diff.meta", "bold");
33+
$whitespace_color = $repo->get_color("color.diff.whitespace", "normal red");
34+
}
35+
}
36+
37+
sub colored {
38+
my $color = shift;
39+
my $string = join("", @_);
40+
41+
if ($use_color) {
42+
# Put a color code at the beginning of each line, a reset at the end
43+
# color after newlines that are not at the end of the string
44+
$string =~ s/(\n+)(.)/$1$color$2/g;
45+
# reset before newlines
46+
$string =~ s/(\n+)/$normal_color$1/g;
47+
# codes at beginning and end (if necessary):
48+
$string =~ s/^/$color/;
49+
$string =~ s/$/$normal_color/ unless $string =~ /\n$/;
50+
}
51+
return $string;
52+
}
453

554
# command line options
655
my $patch_mode;
@@ -246,10 +295,20 @@ sub is_valid_prefix {
246295
sub highlight_prefix {
247296
my $prefix = shift;
248297
my $remainder = shift;
249-
return $remainder unless defined $prefix;
250-
return is_valid_prefix($prefix) ?
251-
"[$prefix]$remainder" :
252-
"$prefix$remainder";
298+
299+
if (!defined $prefix) {
300+
return $remainder;
301+
}
302+
303+
if (!is_valid_prefix($prefix)) {
304+
return "$prefix$remainder";
305+
}
306+
307+
if (!$use_color) {
308+
return "[$prefix]$remainder";
309+
}
310+
311+
return "$prompt_color$prefix$normal_color$remainder";
253312
}
254313

255314
sub list_and_choose {
@@ -266,7 +325,7 @@ sub list_and_choose {
266325
if (!$opts->{LIST_FLAT}) {
267326
print " ";
268327
}
269-
print "$opts->{HEADER}\n";
328+
print colored $header_color, "$opts->{HEADER}\n";
270329
}
271330
for ($i = 0; $i < @stuff; $i++) {
272331
my $chosen = $chosen[$i] ? '*' : ' ';
@@ -304,7 +363,7 @@ sub list_and_choose {
304363

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

307-
print $opts->{PROMPT};
366+
print colored $prompt_color, $opts->{PROMPT};
308367
if ($opts->{SINGLETON}) {
309368
print "> ";
310369
}
@@ -371,7 +430,7 @@ sub list_and_choose {
371430
}
372431

373432
sub singleton_prompt_help_cmd {
374-
print <<\EOF ;
433+
print colored $help_color, <<\EOF ;
375434
Prompt help:
376435
1 - select a numbered item
377436
foo - select item based on unique prefix
@@ -380,7 +439,7 @@ sub singleton_prompt_help_cmd {
380439
}
381440

382441
sub prompt_help_cmd {
383-
print <<\EOF ;
442+
print colored $help_color, <<\EOF ;
384443
Prompt help:
385444
1 - select a single item
386445
3-5 - select a range of items
@@ -477,6 +536,31 @@ sub parse_diff {
477536
return @hunk;
478537
}
479538

539+
sub colored_diff_hunk {
540+
my ($text) = @_;
541+
# return the text, so that it can be passed to print()
542+
my @ret;
543+
for (@$text) {
544+
if (!$diff_use_color) {
545+
push @ret, $_;
546+
next;
547+
}
548+
549+
if (/^\+/) {
550+
push @ret, colored($new_color, $_);
551+
} elsif (/^\-/) {
552+
push @ret, colored($old_color, $_);
553+
} elsif (/^\@/) {
554+
push @ret, colored($fraginfo_color, $_);
555+
} elsif (/^ /) {
556+
push @ret, colored($normal_color, $_);
557+
} else {
558+
push @ret, colored($metainfo_color, $_);
559+
}
560+
}
561+
return @ret;
562+
}
563+
480564
sub hunk_splittable {
481565
my ($text) = @_;
482566

@@ -671,7 +755,7 @@ sub coalesce_overlapping_hunks {
671755
}
672756

673757
sub help_patch_cmd {
674-
print <<\EOF ;
758+
print colored $help_color, <<\EOF ;
675759
y - stage this hunk
676760
n - do not stage this hunk
677761
a - stage this and all the remaining hunks in the file
@@ -710,9 +794,7 @@ sub patch_update_file {
710794
my ($ix, $num);
711795
my $path = shift;
712796
my ($head, @hunk) = parse_diff($path);
713-
for (@{$head->{TEXT}}) {
714-
print;
715-
}
797+
print colored_diff_hunk($head->{TEXT});
716798
$num = scalar @hunk;
717799
$ix = 0;
718800

@@ -754,10 +836,8 @@ sub patch_update_file {
754836
if (hunk_splittable($hunk[$ix]{TEXT})) {
755837
$other .= '/s';
756838
}
757-
for (@{$hunk[$ix]{TEXT}}) {
758-
print;
759-
}
760-
print "Stage this hunk [y/n/a/d$other/?]? ";
839+
print colored_diff_hunk($hunk[$ix]{TEXT});
840+
print colored $prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
761841
my $line = <STDIN>;
762842
if ($line) {
763843
if ($line =~ /^y/i) {
@@ -811,7 +891,7 @@ sub patch_update_file {
811891
elsif ($other =~ /s/ && $line =~ /^s/) {
812892
my @split = split_hunk($hunk[$ix]{TEXT});
813893
if (1 < @split) {
814-
print "Split into ",
894+
print colored $header_color, "Split into ",
815895
scalar(@split), " hunks.\n";
816896
}
817897
splice(@hunk, $ix, 1,
@@ -894,8 +974,7 @@ sub diff_cmd {
894974
HEADER => $status_head, },
895975
@mods);
896976
return if (!@them);
897-
system(qw(git diff-index -p --cached HEAD --),
898-
map { $_->{VALUE} } @them);
977+
system(qw(git diff -p --cached HEAD --), map { $_->{VALUE} } @them);
899978
}
900979

901980
sub quit_cmd {
@@ -904,7 +983,7 @@ sub quit_cmd {
904983
}
905984

906985
sub help_cmd {
907-
print <<\EOF ;
986+
print colored $help_color, <<\EOF ;
908987
status - show paths with changes
909988
update - add working tree state to the staged set of changes
910989
revert - revert staged set of changes back to the HEAD version

perl/Git.pm

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,41 @@ sub config_int {
581581
};
582582
}
583583

584+
=item get_colorbool ( NAME )
585+
586+
Finds if color should be used for NAMEd operation from the configuration,
587+
and returns boolean (true for "use color", false for "do not use color").
588+
589+
=cut
590+
591+
sub get_colorbool {
592+
my ($self, $var) = @_;
593+
my $stdout_to_tty = (-t STDOUT) ? "true" : "false";
594+
my $use_color = $self->command_oneline('config', '--get-colorbool',
595+
$var, $stdout_to_tty);
596+
return ($use_color eq 'true');
597+
}
598+
599+
=item get_color ( SLOT, COLOR )
600+
601+
Finds color for SLOT from the configuration, while defaulting to COLOR,
602+
and returns the ANSI color escape sequence:
603+
604+
print $repo->get_color("color.interactive.prompt", "underline blue white");
605+
print "some text";
606+
print $repo->get_color("", "normal");
607+
608+
=cut
609+
610+
sub get_color {
611+
my ($self, $slot, $default) = @_;
612+
my $color = $self->command_oneline('config', '--get-color', $slot, $default);
613+
if (!defined $color) {
614+
$color = "";
615+
}
616+
return $color;
617+
}
618+
584619
=item ident ( TYPE | IDENTSTR )
585620
586621
=item ident_person ( TYPE | IDENTSTR | IDENTARRAY )

0 commit comments

Comments
 (0)