如何禁止Term :: ReadLine的默认文件名完成?

时间:2015-07-15 15:35:36

标签: perl tab-completion

如何禁用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( "> " ) ) {
    ...
}

1 个答案:

答案 0 :(得分: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";
}
相关问题