【问题标题】:Term::Readline::Gnu - how to use complete_internal (if possible..)?Term::Readline::Gnu - 如何使用 complete_internal (如果可能的话..)?
【发布时间】:2016-05-13 22:57:08
【问题描述】:

我知道如何通过 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”/router-cli 模式 - 但也许我在这里完全错了?如果有这样的事情;如何使用 Term::Readline::Gnu 将自定义完成数据传递给它?

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

【问题讨论】:

    标签: perl command-line-interface readline cisco


    【解决方案1】:

    这里是一个如何使用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_internalint what_to_do

    完成单词 在点或之前。 what_to_do 说明如何处理完成。一种 ? 的值表示列出可能的完成。 TAB 表示做 标准完成。 * 表示插入所有可能的补全。 ! 表示显示所有可能的完成,如果有更多 比一个,以及执行部分完成。 @ 类似于 !,但如果可能,未列出可能的完成 完成共享一个共同的前缀。

    【讨论】:

    • 非常感谢哈康!我正在度假,远离键盘 - 将在接下来的几天仔细查看您的代码,然后提供反馈。
    猜你喜欢
    • 1970-01-01
    • 2012-10-30
    • 2014-01-22
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 2010-11-06
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多