{"thread":{"id":"17489","subject":"[RFC PATCH] add -p: prompt for single characters","startedAt":"2009-02-01T20:35:11Z","lastAt":"2009-02-02T13:19:00Z","messageCount":5,"participants":["Thomas Rast","Suraj Kurapati","Junio C Hamano","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"102772","messageId":"1233520511-13061-1-git-send-email-trast@student.ethz.ch","threadId":"17489","inReplyTo":null,"subject":"[RFC PATCH] add -p: prompt for single characters","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2009-02-01T20:35:11Z","receivedAt":"2009-02-01T20:35:11Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Use Term::ReadKey, if available, to let the user answer add -p's\nprompts by pressing a single key.  The 'g' command is the only one\nthat takes an argument, but can easily cope since it'll just offer a\nchoice of chunks.  We're not doing the same in the main 'add -i'\ninterface because file selection etc. may expect several characters.\n\nSigned-off-by: Thomas Rast <trast@student.ethz.ch>\n---\n\nI wrote this on the train today, but it turns out a similar idea was\nalready around earlier:\n\n  http://thread.gmane.org/gmane.comp.version-control.git/100146\n\nI can't find the v4 promised there, so I assume Suraj dropped it.\n\nIt would indeed be nice if we could apply the same to the main 'add\n-i' loop, and I played with the code to do so for a while.  Most of\nthe mechanisms required seem to be in place; it already computes\nshortest unique prefixes for the inputs given, so we could just grab\nthe one-character prefixes from there.\n\nHowever, what to do when the user entered a letter that is known to be\npart of several prefixes?  If we offer to enter the rest of the line,\ne.g., 'return $key.<STDIN>', the user can't backspace away the\n\"existing\" input if he decides to do something else instead.  Perhaps\nreadline could prime the input line with the letter, but I can't find\nany such feature in the Term::ReadLine docs.  Implementation ideas are\nobviously very welcome.\n\n\n git-add--interactive.perl |   35 ++++++++++++++++++++++++++++++++---\n 1 files changed, 32 insertions(+), 3 deletions(-)\n\ndiff --git a/git-add--interactive.perl b/git-add--interactive.perl\nindex ca60356..0633eca 100755\n--- a/git-add--interactive.perl\n+++ b/git-add--interactive.perl\n@@ -33,6 +33,14 @@ my ($diff_new_color) =\n \n my $normal_color = $repo->get_color(\"\", \"reset\");\n \n+my $use_readkey = 0;\n+my %control_keys;\n+eval {\n+\tuse Term::ReadKey;\n+\t$use_readkey = 1;\n+\t%control_keys = GetControlChars;\n+};\n+\n sub colored {\n \tmy $color = shift;\n \tmy $string = join(\"\", @_);\n@@ -758,11 +766,32 @@ sub diff_applies {\n \treturn close $fh;\n }\n \n+sub _restore_terminal_and_die {\n+\tReadMode 'restore';\n+\tprint \"\\n\";\n+\texit 1;\n+}\n+\n+sub prompt_single_character {\n+\tif ($use_readkey) {\n+\t\tlocal $SIG{TERM} = \\&_restore_terminal_and_die;\n+\t\tlocal $SIG{INT} = \\&_restore_terminal_and_die;\n+\t\tReadMode 'cbreak';\n+\t\tmy $key = ReadKey 0;\n+\t\tReadMode 'restore';\n+\t\tprint \"$key\" if defined $key;\n+\t\tprint \"\\n\";\n+\t\treturn $key;\n+\t} else {\n+\t\treturn <STDIN>;\n+\t}\n+}\n+\n sub prompt_yesno {\n \tmy ($prompt) = @_;\n \twhile (1) {\n \t\tprint colored $prompt_color, $prompt;\n-\t\tmy $line = <STDIN>;\n+\t\tmy $line = prompt_single_character;\n \t\treturn 0 if $line =~ /^n/i;\n \t\treturn 1 if $line =~ /^y/i;\n \t}\n@@ -892,7 +921,7 @@ sub patch_update_file {\n \t\t\tprint @{$mode->{DISPLAY}};\n \t\t\tprint colored $prompt_color,\n \t\t\t\t\"Stage mode change [y/n/a/d/?]? \";\n-\t\t\tmy $line = <STDIN>;\n+\t\t\tmy $line = prompt_single_character;\n \t\t\tif ($line =~ /^y/i) {\n \t\t\t\t$mode->{USE} = 1;\n \t\t\t\tlast;\n@@ -965,7 +994,7 @@ sub patch_update_file {\n \t\t\tprint;\n \t\t}\n \t\tprint colored $prompt_color, \"Stage this hunk [y/n/a/d$other/?]? \";\n-\t\tmy $line = <STDIN>;\n+\t\tmy $line = prompt_single_character;\n \t\tif ($line) {\n \t\t\tif ($line =~ /^y/i) {\n \t\t\t\t$hunk[$ix]{USE} = 1;\n-- \ntg: (a34a9db..) t/add-p-readkey (depends on: origin/master)\n"},{"id":"102813","messageId":"cfbcd2f00902011931h48ef51a4n9badbed9051879ca@mail.gmail.com","threadId":"17489","inReplyTo":"1233520511-13061-1-git-send-email-trast@student.ethz.ch","subject":"Re: [RFC PATCH] add -p: prompt for single characters","fromName":"Suraj Kurapati","fromEmail":"sunaku@gmail.com","sentAt":"2009-02-02T03:31:53Z","receivedAt":"2009-02-02T03:31:53Z","isPatch":true,"sender":{"key":"sunaku@gmail.com","avatar":"https://gravatar.com/avatar/30b9a72c51e7281e513a6cbb3591675166b9655a12454b8641a21194155d42ca?d=mp&s=160"},"body":"On Sun, Feb 1, 2009 at 12:35 PM, Thomas Rast <trast@student.ethz.ch> wrote:\n>  http://thread.gmane.org/gmane.comp.version-control.git/100146\n>\n> I can't find the v4 promised there, so I assume Suraj dropped it.\n\nYes, I lost the motivation to develop the patch any further.  Sorry.\n\nBut I am glad to see you carrying the torch forward.  Good luck! :)\n"},{"id":"102843","messageId":"200902020934.06128.trast@student.ethz.ch","threadId":"17489","inReplyTo":"cfbcd2f00902011931h48ef51a4n9badbed9051879ca@mail.gmail.com","subject":"Re: [RFC PATCH] add -p: prompt for single characters","fromName":"Thomas Rast","fromEmail":"trast@student.ethz.ch","sentAt":"2009-02-02T08:34:02Z","receivedAt":"2009-02-02T08:34:02Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Suraj Kurapati wrote:\n> On Sun, Feb 1, 2009 at 12:35 PM, Thomas Rast <trast@student.ethz.ch> wrote:\n> >  http://thread.gmane.org/gmane.comp.version-control.git/100146\n> >\n> > I can't find the v4 promised there, so I assume Suraj dropped it.\n> \n> Yes, I lost the motivation to develop the patch any further.  Sorry.\n\nOk.\n\nI see Junio has already merged wp/add-patch-find, so I'll rebuild on\ntop of that and see what needs to be changed.\n\n-- \nThomas Rast\ntrast@{inf,student}.ethz.ch\n"},{"id":"102847","messageId":"7vljspjk8n.fsf@gitster.siamese.dyndns.org","threadId":"17489","inReplyTo":"200902020934.06128.trast@student.ethz.ch","subject":"Re: [RFC PATCH] add -p: prompt for single characters","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-02-02T08:50:16Z","receivedAt":"2009-02-02T08:50:16Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thomas Rast <trast@student.ethz.ch> writes:\n\n> Suraj Kurapati wrote:\n>> On Sun, Feb 1, 2009 at 12:35 PM, Thomas Rast <trast@student.ethz.ch> wrote:\n>> >  http://thread.gmane.org/gmane.comp.version-control.git/100146\n>> >\n>> > I can't find the v4 promised there, so I assume Suraj dropped it.\n>> \n>> Yes, I lost the motivation to develop the patch any further.  Sorry.\n>\n> Ok.\n>\n> I see Junio has already merged wp/add-patch-find, so I'll rebuild on\n> top of that and see what needs to be changed.\n\nThis is not a very high priority request, but I personally find the prompt\nsitting there until I type Enter somewhat assuring, because I often think,\ntype \"y\", think again, and then finally type \"Enter\".\n\nI'd appreciate if you can allow this \"single stroke\" mode to be optionally\nturned off by the end user.\n"},{"id":"102856","messageId":"20090202131900.GC8487@sigio.peff.net","threadId":"17489","inReplyTo":"1233520511-13061-1-git-send-email-trast@student.ethz.ch","subject":"Re: [RFC PATCH] add -p: prompt for single characters","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-02-02T13:19:00Z","receivedAt":"2009-02-02T13:19:00Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sun, Feb 01, 2009 at 09:35:11PM +0100, Thomas Rast wrote:\n\n> I wrote this on the train today, but it turns out a similar idea was\n> already around earlier:\n> \n>   http://thread.gmane.org/gmane.comp.version-control.git/100146\n> \n> I can't find the v4 promised there, so I assume Suraj dropped it.\n\nIt looks like you addressed the concerns I had with the original. I do\nthink, as Junio mentioned, that it makes sense for this to be\nconfigurable. Because it's interactive by defintion, I don't know if we\nhave the same compatibility concerns that we usually do. So I'm not sure\nif such a config option should default to off or on.\n\n-Peff\n"}]}