Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Terminal. Update text while user types. -- glob and Term::ReadLine solution

by Discipulus (Abbot)
on Dec 21, 2016 at 08:38 UTC ( #1178278=note: print w/replies, xml ) Need Help??

in reply to Terminal. Update text while user types.

Hello Bad_ptr and welcome to active participation to the monastery!

If i understand your needs, you can accomplish something very similar via Term::ReadLine and glob

Infact that module has autocompletion via TAB key. If you press TAB twice it display all possible valid combinations based on what you entered till now.

I see you use *.pl as pattern; if i understand your needs you can read the pattern enetered at the prompt and use glob to populate an array. You'll use this array to do the autocompletion.

That module is somehow tricky to use correctly and behaves differently depending on the OS.

Here you have a working solution:

use strict; use warnings; use Term::ReadLine; # I need this under windows: $ENV{TERM}=undef; my $term=Term::ReadLine->new("test"); print "Please enter a pattern:"; my $pattern = <STDIN>; chomp $pattern; my @files = glob($pattern); print "there are ",scalar @files," different possibilities.\n"; my $choosen = ''; $term->Attribs->{completion_function} = sub { my ($text, $line, $start) = @_; return grep { /^$text/i } @files ; }; while ( defined ( $_ = $term->readline( 'choose>') ) ) { # a blank bare line is entered? redo the loop next if /^$/; if ($_=~/.*$/){ chomp $_; $choosen = $_; last; } } print "You choosen [$choosen]\n";

If I call this program in the current directory I see (some space and comments added):

prompt>perl Please enter a pattern:*.jpg there are 23 different possibilities. choose> # TAB TAB display all pos +sibilities apod20100415.jpg cat.jpg city.jpg earth.jpg file.jpg file0.jpg file3.jpg file4.jpg file5.jpg file6.jpg file7.jpg file8.jpg flowers.jpg hacking-team-011509362-ff2f66c6-e452-4109-8bfb-6d4499c2bd5e.jpg kawasaki.jpg kid.jpg mandelbrot_set_color_zoom_by_zeno333-d4va5l8.jpg moon.jpg PICT0034.JPG romantic.jpg serfandolweb.jpg sunrise.jpg umbrella.jpg # if i press just ENTER nothing hap +pens choose> choose> choose> choose> choose>file # i press 'f' and after TAB: it dis +play all 'file*' files file.jpg file0.jpg file3.jpg file4.jpg file5.jpg file6.jpg file7.jpg +file8.jpg choose>file4.jpg # i enter '4' and TAB completes int +o the only one matching You choosen [file4.jpg ] # done! the program exit prompt>

I suggest you to read Re^3: about Term::ReadLine and it's imported function The Solution and the whole thread because Term::ReadLine is a bit dificult to manage.



There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1178278]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2020-02-22 07:45 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (98 votes). Check out past polls.