Following Aiden Bell's prompt, I tried Readline in Perl. Solution 1 using Term :: Complete (also using CPAN, I think):
use Term::Complete; my $F; open($F,"<","bin/phonebook.csv"); my @terms = <$F>; chomp(@terms); close($F); my $input; while (!defined $input) { $input = Complete("Enter a name or number: ",@terms); my ($name,$number) = split(/\t/,$input); print("Sending SMS to $name ($number).\n"); system("sudo gammu --sendsms TEXT $number"); }
Press \ to finish, press Ctrl-D to see all the features.
Solution 2: Ctrl-D is a single keystroke, so using the standard Term :: Readline allows you to complete and display possible completion options using only \.
use Term::ReadLine; my $F; open($F,"<","bin/phonebook.csv"); my @terms = <$F>; chomp(@terms); close($F); my $term = new Term::ReadLine; $term->Attribs->{completion_function} = sub { return @terms; }; my $prompt = "Enter name or number >> "; my $OUT = $term->OUT || \*STDOUT; while ( defined (my $input = $term->readline($prompt)) ) { my ($name,$number) = split(/\t/,$input); print("Sending SMS to $name ($number).\n"); system("sudo gammu --sendsms TEXT $number"); }
This decision still needs to be completed.
Edit: The final decision of Modifying Term :: Complete ( http://search.cpan.org/~jesse/perl-5.12.0/lib/Term/Complete.pm ) gives me a quick conclusion.
Source code: http://search.cpan.org/CPAN/authors/id/J/JE/JESSE/perl-5.12.0.tar.gz Solution No. 1 works with this modification. I will put the whole sample in another place, if it can be used by someone
Modifications of Completion.pm (just reusing code for Control-D and \ for each character):
170c172,189
my $redo=0; @match = grep(/^\Q$return/, @cmp_lst); unless ($#match < 0) { $l = length($test = shift(@match)); foreach $cmp (@match) { until (substr($cmp, 0, $l) eq substr($test, 0, $l)) { $l--; } } print("\a"); print($test = substr($test, $r, $l - $r)); $redo = $l - $r == 0; if ($redo) { print(join("\r\n", '', grep(/^\Q$return/, @cmp_lst)), "\r\n"); } $r = length($return .= $test); } if ($redo) { redo LOOP; } else { last CASE; }
code>