Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Autocomplete in perl console application

by shmem (Chancellor)
on Aug 01, 2007 at 13:24 UTC ( [id://630047]=note: print w/replies, xml ) Need Help??


in reply to Autocomplete in perl console application

I am not clear how to get the uncompleted command from the console.

The following minimal shell might help. You get the current word as the first parameter to the complete() function:

use Term::ReadLine; my $term = Term::ReadLine->new('console_app'); my $attr = $term->Attribs; $attr->{attempted_completion_function} = \&complete; our @keywords; chop and push @keywords, split while <DATA>; while (1) { my $line = $term->readline("perl >"); eval $line; warn $@ if $@; }; sub complete { my ($text, $line, $start, $end) = @_; # See if current word matches a package symbol my @matches = grep { /^\Q$text/ } keys %::; # current package if (@matches) { return @matches; } # nope? then check for builtins. return $term->completion_matches($text,\&keyword); }; { my $i; sub keyword { my ($text, $state) = @_; return unless $text; if($state) { $i++; } else { # first call $i = 0; } for (; $i<=$#keywords; $i++) { return $keywords[$i] if $keywords[$i] =~ /^\Q$text/; }; return undef; } }; # builtins (extracted from perlfunc) __DATA__ chomp chop chr crypt hex index lc lcfirst length oct ord pack reverse rindex sprintf substr uc ucfirst pos quotemeta split study abs atan2 cos exp hex int log oct rand sin sqrt srand pop push shift splice unshift grep join map reverse sort unpack delete each exists keys values binmode close closedir dbmclose dbmopen die eof fileno flock format getc print printf read readdir rewinddir seek seekdir select syscall sysread sysseek syswrite tell telldir truncate warn write pack read syscall sysread syswrite unpack vec chdir chmod chown chroot fcntl glob ioctl link lstat mkdir open opendir readlink rename rmdir stat symlink sysopen umask unlink utime caller continue die do dump eval exit goto last next redo return sub wantarray caller import local my our package use defined dump eval formline local my our reset scalar undef wantarray alarm exec fork getpgrp getppid getpriority kill pipe setpgrp setpriority sleep system times wait waitpid do import no package require use bless dbmclose dbmopen package ref tie tied untie use accept bind connect getpeername getsockname listen recv send setsockopt shutdown socket socketpair msgctl msgget msgrcv msgsnd semctl semget semop shmctl shmget shmread shmwrite endgrent endhostent endnetent endpwent getgrent getgrgid getgrnam getlogin getpwent getpwnam setgrent setpwent endprotoent endservent gethostbyaddr gethostbyname gethostent getnetbyaddr getnetbyname getnetent getprotobynumber getprotoent getservbyname getservbyport getservent sethostent setnetent setservent gmtime localtime time times abs bless chomp chr exists formline glob import lc lcfirst map my no our prototype qx qw readline readpipe ref sysopen tie tied uc ucfirst untie use sub

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Autocomplete in perl console application
by Anonymous Monk on Aug 01, 2007 at 15:55 UTC
    Thanks a lot. This is what I was trying to implement. I will add this Idea in the App I am trying to develop. Cheers.. Raghu

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found