newbie Q on sdtin word completion

B

Bernd Fischer

Hi,

I'm on a Linux env and try to get
word completion form sdtin done,
like Perl's
$stdin = Complete( "\t: ", @choices );

What I have so far shows me the directory listing
on the second hit on TAB and not the list of
choices on the first like I wanted to have.

Any help appreciated,
Bernd


#! /usr/bin/python

import readline
import string
import readline


##
## completer function
##

def comp( text, state ):
global compList
matches = [ ]
text = readline.get_line_buffer( )
n = len( text )
for word in compList:
if word[ :n ] == text:
matches.append( word )
return matches


##
## chosser function
##

def chooser( inList, msgString ):

stdin = None
compList = inList

while stdin not in inList:
print "\nSelect a %s <>:\n" % msgString
for elem in inList:
print "\t", elem
print "" # equivalent to newline /n
stdin = string.rstrip( raw_input( "\t: " ) )
if stdin not in inList:
print "\nWrong %s, select again!" % msgString
return sdtin



readline.parse_and_bind( "tab: complete" )
readline.set_completer( comp )

choices = [ "appel", "pear" ]

chooser( choices , "fruit" )
 
F

Fredrik Lundh

Bernd said:
I'm on a Linux env and try to get
word completion form sdtin done,
like Perl's
$stdin = Complete( "\t: ", @choices );

What I have so far shows me the directory listing
on the second hit on TAB and not the list of
choices on the first like I wanted to have.

your completer function is called with a prefix and an index. for each
completion request, readline will call this function repeatedly, with in-
creasing indices. for each call, you should return the corresponding
match, or None if there are no more matches.

the second example on this page might help:

http://effbot.org/librarybook/readline.htm

</F>
 
B

Bernd Fischer

Thanks, exactly what I was looking for.
I should go and buy you book ;-)

Bernd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top