Skip to content

Commit ca6ac7f

Browse files
trastgitster
authored andcommitted
add -p: prompt for single characters
Use Term::ReadKey, if available and enabled with interactive.singlekey, to let the user answer add -p's prompts by pressing a single key. We're not doing the same in the main 'add -i' interface because file selection etc. may expect several characters. Two commands take an argument: 'g' can easily cope since it'll just offer a choice of chunks. '/' now (unconditionally, even without readkey) offers a chance to enter a regex if none was given. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 68c02d7 commit ca6ac7f

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

Documentation/config.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,13 @@ instaweb.port::
10001000
The port number to bind the gitweb httpd to. See
10011001
linkgit:git-instaweb[1].
10021002

1003+
interactive.singlekey::
1004+
In interactive programs, allow the user to provide one-letter
1005+
input with a single key (i.e., without hitting enter).
1006+
Currently this is used only by the `\--patch` mode of
1007+
linkgit:git-add[1]. Note that this setting is silently
1008+
ignored if portable keystroke input is not available.
1009+
10031010
log.date::
10041011
Set default date-time mode for the log command. Setting log.date
10051012
value is similar to using 'git-log'\'s --date option. The value is one of the

git-add--interactive.perl

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333

3434
my $normal_color = $repo->get_color("", "reset");
3535

36+
my $use_readkey = 0;
37+
if ($repo->config_bool("interactive.singlekey")) {
38+
eval {
39+
use Term::ReadKey;
40+
$use_readkey = 1;
41+
};
42+
}
43+
3644
sub colored {
3745
my $color = shift;
3846
my $string = join("", @_);
@@ -758,11 +766,32 @@ sub diff_applies {
758766
return close $fh;
759767
}
760768

769+
sub _restore_terminal_and_die {
770+
ReadMode 'restore';
771+
print "\n";
772+
exit 1;
773+
}
774+
775+
sub prompt_single_character {
776+
if ($use_readkey) {
777+
local $SIG{TERM} = \&_restore_terminal_and_die;
778+
local $SIG{INT} = \&_restore_terminal_and_die;
779+
ReadMode 'cbreak';
780+
my $key = ReadKey 0;
781+
ReadMode 'restore';
782+
print "$key" if defined $key;
783+
print "\n";
784+
return $key;
785+
} else {
786+
return <STDIN>;
787+
}
788+
}
789+
761790
sub prompt_yesno {
762791
my ($prompt) = @_;
763792
while (1) {
764793
print colored $prompt_color, $prompt;
765-
my $line = <STDIN>;
794+
my $line = prompt_single_character;
766795
return 0 if $line =~ /^n/i;
767796
return 1 if $line =~ /^y/i;
768797
}
@@ -893,7 +922,7 @@ sub patch_update_file {
893922
print @{$mode->{DISPLAY}};
894923
print colored $prompt_color,
895924
"Stage mode change [y/n/a/d/?]? ";
896-
my $line = <STDIN>;
925+
my $line = prompt_single_character;
897926
if ($line =~ /^y/i) {
898927
$mode->{USE} = 1;
899928
last;
@@ -966,7 +995,7 @@ sub patch_update_file {
966995
print;
967996
}
968997
print colored $prompt_color, "Stage this hunk [y,n,a,d,/$other,?]? ";
969-
my $line = <STDIN>;
998+
my $line = prompt_single_character;
970999
if ($line) {
9711000
if ($line =~ /^y/i) {
9721001
$hunk[$ix]{USE} = 1;
@@ -1018,9 +1047,17 @@ sub patch_update_file {
10181047
next;
10191048
}
10201049
elsif ($line =~ m|^/(.*)|) {
1050+
my $regex = $1;
1051+
if ($1 eq "") {
1052+
print colored $prompt_color, "search for regex? ";
1053+
$regex = <STDIN>;
1054+
if (defined $regex) {
1055+
chomp $regex;
1056+
}
1057+
}
10211058
my $search_string;
10221059
eval {
1023-
$search_string = qr{$1}m;
1060+
$search_string = qr{$regex}m;
10241061
};
10251062
if ($@) {
10261063
my ($err,$exp) = ($@, $1);

0 commit comments

Comments
 (0)