本文介绍了如何在命令行上实现增量(按您类型查找)搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在命令行上编写具有增量搜索(即按类型查找)功能的小脚本.

用例:我的手机通过 USB 连接,使用 gammu --sendsms TEXT 我可以写短信.我有 CSV 格式的电话簿,并想在其上按 i 类型搜索.

最简单/最好的方法是什么?它可能是 bash/zsh/Perl/Python 或任何其他脚本语言.

解决方案:修改 Term::Complete 做了我想要的.答案见下文.

解决方案

按照 Aiden Bell 的提示,我在 Perl 中尝试了 Readline.使用 Term::Complete 的解决方案 1(我认为也被 CPAN 使用):

使用 Term::Complete;我的$F;open($F,"<","bin/phonebook.csv");我的@terms = <$F>;chomp(@terms);关闭($F);我的 $input;while (!defined $input) {$input = Complete("请输入姓名或号码:",@terms);我的 ($name,$number) = split(/\t/,$input);print("发送短信到$name ($number).\n");system("sudo gammu --sendsms TEXT $number");}

按 \ 完成,按 Ctrl-D 查看所有可能性.

解决方案 2:Ctrl-D 是一次按键操作,因此使用标准的 Term::Readline 允许完成并仅使用 \ 来显示可能的完成.

使用 Term::ReadLine;我的$F;open($F,"<","bin/phonebook.csv");我的@terms = <$F>;chomp(@terms);关闭($F);我的 $term = new Term::ReadLine;$term->Attribs->{completion_function} = sub { return @terms;};my $prompt = "输入姓名或号码>>";我的 $OUT = $term->OUT ||\*标准输出;while ( 已定义 (我的 $input = $term->readline($prompt)) ) {我的 ($name,$number) = split(/\t/,$input);print("发送短信到$name ($number).\n");system("sudo gammu --sendsms TEXT $number");}

这个解决方案还需要一个来完成.

最终解决方案修改 Term::Complete (http://search.cpan.org/~jesse/perl-5.12.0/lib/Term/Complete.pm) 确实让我即时完成.

源代码:http://search.cpan.org/CPAN/authors/id/J/JE/JESSE/perl-5.12.0.tar.gz解决方案 1 与此修改一起使用.如果有人可以使用,我会将整个样本放在其他地方

Completion.pm 的修改(只是将它的代码重用于 Control-D 和 \ 用于每个字符):

170c172,189

 我的 $redo=0;@match = grep(/^\Q$return/, @cmp_lst);除非 ($#match 

I'd like to write small scripts which feature 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 the phonebook as CSV, and want to search-as-i-type on that.

What's the easiest/best way to do it? It might be in bash/zsh/Perl/Python or any other scripting language.

Edit: Solution: Modifying Term::Complete did what I want. See below for the answer.

解决方案

Following Aiden Bell's hint, I tried Readline in Perl. Solution 1 using Term::Complete (also used by 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 complete, press Ctrl-D to see all possibilities.

Solution 2: Ctrl-D is one keystroke to much, so using standard Term::Readline allows completion and the display off possible completions 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 solution still needs a for completion.

Edit: Final SolutionModifying Term::Complete (http://search.cpan.org/~jesse/perl-5.12.0/lib/Term/Complete.pm) does give me on the fly completion.

Source code: http://search.cpan.org/CPAN/authors/id/J/JE/JESSE/perl-5.12.0.tar.gzSolution number 1 works with this modification. I will put the whole sample online somewhere else if this can be used by somebody

Modifications of Completion.pm (just reusing it's code for Control-D and \ for each character):

170c172,189

这篇关于如何在命令行上实现增量(按您类型查找)搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:37