Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by Discipulus (Canon)
on Dec 21, 2016 at 08:38 UTC ( [id://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 term-readline001.pl 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.

HtH

L*

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-23 12:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found