http://www.perlmonks.org?node_id=1178213

Bad_ptr has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

I want to write a simple program that will do operations on files that match a given pattern.
My trouble is that I also want to show a dynamically changing preview of a list of files that match that pattern(fixed number of lines), updating it while the user stop typing for a second and I have no idea how to implement that.

It must look like this:

$ Please enter a pattern: '*.pl' 1.pl 2.pl 3.pl ...

Where to start? What is the easiest way to achieve that?

Replies are listed 'Best First'.
Re: Terminal. Update text while user types.
by haukex (Archbishop) on Dec 20, 2016 at 15:17 UTC

    Hi Bad_ptr,

    So am I understanding correctly that you want to implement something like a suggestion list (kind of like what browsers do in their address bar)? I think that for something dynamic like that, you may need to look into toolkits for the terminal. I'm not sure if there are existing solutions that do something like what you want, and a quick search hasn't revealed much yet, but as a starting point for implementing your own solution, Curses (or perhaps Curses::UI) comes to mind.

    Update: Actually, I'm only guessing this is supposed to be on the terminal, since you haven't specified that? Oops, yes you did, in the title, nevermind ;-)

    Regards,
    -- Hauke D

Re:Terminal. Update text while user types.
by kschwab (Vicar) on Dec 20, 2016 at 17:16 UTC

    You could try Term::Complete

    #!/usr/bin/perl use Term::Complete; print "Type ap, then hit the <tab> key or <ctrl-d>\n"; my $foo=Complete('>',('application','apple','applicant')); print "You typed [$foo]\n";
    You may need to play around with configuration to meet your requirements, but it's fairly easy to use.
Re: Terminal. Update text while user types. -- glob and Term::ReadLine solution
by Discipulus (Canon) on Dec 21, 2016 at 08:38 UTC
    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.