Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inhibit Term::ReadLine's default filename completion?

How can I disable Term::ReadLine's default completion, or rather, make it stop suggesting filename completions at some point?

For example, what do I need to replace return() with in order to inhibit the default completion from the second word onwards?

Neither of these works:

    $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( "> " ) ) {
    ...
}
like image 870
n.r. Avatar asked Oct 29 '25 16:10

n.r.


2 Answers

Define completion_function instead of attempted_completion_function:

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

And then return undef if completion should stop, and return $term->completion_matches($text, $attribs->{filename_completion_function}) if filename completion is to take over.

In the following example, nothing is suggested for the first parameter, but filenames are for the second parameter.

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";
}
like image 179
n.r. Avatar answered Oct 31 '25 08:10

n.r.


With the Gnu implementation I discovered that I can set attempted_completion_over to avoid filename completions when my attempted_completion_function returns no results:

$attribs->{attempted_completion_over} = 1;

Gnu.pm indicates this variable is as of "GRL 4.2".

Note that you should set this variable every time your attempted_completion_function runs, or at least every time it returns no matches. I don't see that documented anywhere, but libreadline apparently resets the variable to zero after every call.

like image 33
Michael Krebs Avatar answered Oct 31 '25 10:10

Michael Krebs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!