【问题标题】:How to inhibit Term::ReadLine's default filename completion?如何禁止 Term::ReadLine 的默认文件名完成?
【发布时间】:2015-07-15 15:35:35
【问题描述】:

如何禁用Term::ReadLine 的默认补全,或者更确切地说,让它在某些时候停止建议文件名补全?

例如,我需要用什么替换 return() 以禁止从第二个单词开始的默认补全?

这些都不起作用:

    $attribs->{'filename_completion_function'}=undef;
    $attribs->{'rl_inhibit_completion'}=1;
use Term::ReadLine;

my $term    = new Term::ReadLine 'sample';
my $attribs = $term->Attribs;
$attribs->{attempted_completion_function} = \&sample_completion;

sub sample_completion {
    my ( $text, $line, $start, $end ) = @_;

    # If first word then username completion, else filename completion
    if ( substr( $line, 0, $start ) =~ /^\s*$/ ) {

        return $term->completion_matches( $text,
            $attribs->{'username_completion_function'} );
    }
    else {
        return ();
    }
}

while ( my $input = $term->readline( "> " ) ) {
    ...
}

【问题讨论】:

  • 试试filename_completion_desired
  • 像这样:$attribs->{filename_completion_desired}=0;?或者$attribs->{rl_filename_completion_desired}=0;?不工作。
  • 我只是从文档中猜测。对于 Gnu 变体,所有变量名称都记录在 here 中。 $attribs 哈希的键已删除前导 rl_。您可能需要 use Term::ReadLine::Gnu 或将环境变量 PERL_RL 设置为 Gnu 才能正常工作。
  • 谢谢。我们不允许直接use Term::ReadLine::Gnu。通过反复试验,似乎要走的路是定义,不是attempted_completion_function,而是completion_function,然后是return $term->completion_matches($text, $attribs->{filename_completion_function}),只要默认文件名完成是接管。
  • 干得好。你应该把它写下来作为答案并接受它。所有这些禁止人们使用模块的官僚作风都是奇怪的!

标签: perl tab-completion


【解决方案1】:

定义completion_function 而不是attempted_completion_function

$attribs->{completion_function} = \&completion;

然后return undef 如果完成应该停止,return $term->completion_matches($text, $attribs->{filename_completion_function}) 如果文件名完成要接管。

在以下示例中,第一个参数不建议任何内容,但第二个参数建议使用文件名。

use Term::ReadLine;

my $term = new Term::ReadLine 'sample';
my $attribs = $term->Attribs;
$attribs->{completion_function} = \&completion;

sub completion {

  my ( $text, $line, $start ) = @_;

  if ( substr( $line, 0, $start ) =~ /^\s*$/) {

    return 

  } else {

    return $term->completion_matches($text, $attribs->{filename_completion_function})

  }
}

while ( my $input = $term->readline ("> ") ) {
  exit 0 if $input eq "q";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    相关资源
    最近更新 更多