Term::Readline::Gnu - 如何使用 complete_internal(如果可能..)?

Term::Readline::Gnu - how to use complete_internal (if possible..)?

我知道如何使用 Term::Readline::Gnu (Perl) 的自定义完成函数,例如

str     list_completion_function(str text, int state)

http://search.cpan.org/dist/Term-ReadLine-Gnu/Gnu.pm#Custom_Completion https://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC47

$attribs->{attempted_completion_function } = sub {
  my ($text, $line, $start, $end) = @_;
  my @cmds = ('one', 'two', 'three');
  $attribs->{completion_word} = \@cmds;
  return $term->completion_matches($text, $attribs->{'list_completion_function'} );
};

..但我完全不知道如何使用 complete_internal:

int     rl_complete_internal(int what_to_do = TAB)

http://search.cpan.org/dist/Term-ReadLine-Gnu/Gnu.pm#Custom_Completion

来自 GNU Readline 文档:

值为?' means list the possible completions.TAB'表示进行标准补全。 *' means insert all of the possible completions.!'表示显示所有可能的补全 (...)

https://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC47

这听起来像是 gnu-readline 有一个 "cisco-like"/router-cli 模式 - 但也许我在这里完全错了?如果有这样的事情;如何使用 Term::Readline::Gnu?

将自定义完成数据传递给它

我搜索了 SO、GitHub 代码、Google 等 pp,我几乎肯定会错过(理解)某些东西。如果你能点亮我就太好了。

这里是一个如何使用rl_complete_internal的例子:

use feature qw(say);
use strict;
use utf8;
use open qw( :std :utf8 );
use warnings;
use Term::ReadLine;

my $term   = Term::ReadLine->new('Test', \*STDIN, \*STDOUT);
$term->ornaments( 0 );
my $attribs = $term->Attribs;
$attribs->{completion_word} = [qw(one two three)];
$attribs->{completion_entry_function} =
    $attribs->{list_completion_function};
$term->add_defun('custom-action', \&my_bind_key_func );
$term->bind_key(ord "\cy", 'custom-action');
my $answer = $term->readline( 'Enter input: ' );
say "You entered: '$answer'";

sub my_bind_key_func {
    my($count, $key) = @_; 
    $term->complete_internal( ord '?' );
    return 0;
}

如果您在提示符下键入 t,然后按 CTRL-y,它将显示两个完成候选,即 twothree。这是因为根据 GNU Readline Library documentation, section 2.6:

int rl_complete_internal (int what_to_do)

Complete the word at or before point. what_to_do says what to do with the completion. A value of ? means list the possible completions. TAB means do standard completion. * means insert all of the possible completions. ! means to display all of the possible completions, if there is more than one, as well as performing partial completion. @ is similar to !, but possible completions are not listed if the possible completions share a common prefix.