@matheusbsbs / matheus-bagliano / commits / ca6ac7f135
Fork 0

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>

Thomas Rast committed Feb 5, 2009 at 09:28 UTC ca6ac7f1357df352fec247dc98a3e6784e6c3fea
2 files changed +48 -4
Documentation/config.txt
+7
@@ -1000,6 +1000,13 @@ instaweb.port::
1000 The port number to bind the gitweb httpd to. See
1001 linkgit:git-instaweb[1].
1002
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 +
1010 log.date::
1011 Set default date-time mode for the log command. Setting log.date
1012 value is similar to using 'git-log'\'s --date option. The value is one of the
git-add--interactive.perl
+41 -4
@@ -33,6 +33,14 @@ my ($diff_new_color) =
33
34 my $normal_color = $repo->get_color("", "reset");
35
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 +
44 sub colored {
45 my $color = shift;
46 my $string = join("", @_);
@@ -758,11 +766,32 @@ sub diff_applies {
766 return close $fh;
767 }
768
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 +
790 sub prompt_yesno {
791 my ($prompt) = @_;
792 while (1) {
793 print colored $prompt_color, $prompt;
765 - my $line = <STDIN>;
794 + my $line = prompt_single_character;
795 return 0 if $line =~ /^n/i;
796 return 1 if $line =~ /^y/i;
797 }
@@ -893,7 +922,7 @@ sub patch_update_file {
922 print @{$mode->{DISPLAY}};
923 print colored $prompt_color,
924 "Stage mode change [y/n/a/d/?]? ";
896 - my $line = <STDIN>;
925 + my $line = prompt_single_character;
926 if ($line =~ /^y/i) {
927 $mode->{USE} = 1;
928 last;
@@ -966,7 +995,7 @@ sub patch_update_file {
995 print;
996 }
997 print colored $prompt_color, "Stage this hunk [y,n,a,d,/$other,?]? ";
969 - my $line = <STDIN>;
998 + my $line = prompt_single_character;
999 if ($line) {
1000 if ($line =~ /^y/i) {
1001 $hunk[$ix]{USE} = 1;
@@ -1018,9 +1047,17 @@ sub patch_update_file {
1047 next;
1048 }
1049 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 + }
1058 my $search_string;
1059 eval {
1023 - $search_string = qr{$1}m;
1060 + $search_string = qr{$regex}m;
1061 };
1062 if ($@) {
1063 my ($err,$exp) = ($@, $1);