How can I implement incremental search (type search) on the command line?

I would like to write small scripts that contain an incremental search (find-as-you-type) on the command line.

Use case: I have my mobile phone connected via USB using gammu --sendsms TEXT I can write text messages. I have a phone book like CSV and I want a search type to be found on it.

What is the easiest / best way to do this? It can be in bash / zsh / Perl / Python or any other scripting language.

Edit: Modifying Term :: Complete did what I want. Below is the answer.

+4
source share
2 answers

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>

+4
source

I get the impression GNU Readline supports such things. Although, I did not use it myself. Here is an example of C ++ custom autocomplete, which can easily be done in C. There is also a Python API for readline.

fooobar.com/questions/76726 / ... gives examples in Python , one of which ...

 import readline def completer(text, state): options = [x in addrs where x.startswith(text)] if state < options.length: return options[state] else return None readline.set_completer(completer) 

this bash article can help autocomplete. This article also provides examples of programming the Bash auto complete function.

+4
source

Source: https://habr.com/ru/post/1307660/


All Articles