CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
autocompleting directory names

 



hchristopher
Novice

Dec 23, 2008, 1:44 PM

Post #1 of 3 (407 views)
autocompleting directory names Can't Post

I'm writing this Linux command-line app, and in one part of the script the user has to input the name of a directory. It would be stinkin' cool if the user could input part of the name, the hit tab, and the program would autocomplete the closest match, like in a bash shell.

I'm looking at term::readline and term::shell, but haven't had any experience with them. Any suggestions or arcane knowledge to share with me?


joekamphaus
Novice


Dec 25, 2008, 7:45 PM

Post #2 of 3 (399 views)
Re: [hchristopher] autocompleting directory names [In reply to] Can't Post

Yup I had the same problem.

In a bash script you would use the compgen command which is a bash builtin command. The problem I had is that Perl backtics won't work with bash builtin commands. What I ended up doing is writing a small bash script to return the results of compgen. This is not what I would prefer but it is working for now. I would also appreciate any ideas.

Here is the code for compgen.sh


Code
#!/bin/bash 

if [ "$2" = "-c" ]; then
compgen -c -o default $1
else
compgen -o default $1
fi


Oh by the way this one way I get input from the user one character at a time.


Code
 
use POSIX qw(:termios_h);

while (1) {
my $ch = getchar();
# Do some cool stuff;
last if $ch eq "\n";
}


sub getchar {
my $fd_stdin = fileno(STDIN);
my $term = POSIX::Termios->new();
$term->getattr($fd_stdin);
my $oterm = $term->getlflag();
my $echo = ECHO | ECHOK | ICANON;
my $noecho = $oterm & ~$echo;
my $key = '';
$term->setlflag($noecho);
$term->setcc(VTIME, 1);
$term->setattr($fd_stdin, TCSANOW);
sysread(STDIN, $key, 1);
$term->setlflag($oterm);
$term->setcc( VTIME, 0);
$term->setattr($fd_stdin, TCSANOW);
return $key;
}




Joe Kamphaus
Boise Idaho USA
http://www.joekamphaus.net

(This post was edited by joekamphaus on Dec 25, 2008, 8:38 PM)


joekamphaus
Novice


Dec 25, 2008, 8:49 PM

Post #3 of 3 (393 views)
Re: [hchristopher] autocompleting directory names [In reply to] Can't Post

You may also want to use Curses.pm. If you need to control the way text is displayed curses is a pretty good interface. It has a getch() function built in.



Joe Kamphaus
Boise Idaho USA
http://www.joekamphaus.net

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives