customizing the readline module

holger krekel pyth at devel.trillke.net
Sat Aug 17 05:31:45 EDT 2002


Huaiyu Zhu wrote:
> I'm trying to use the readline module to act like a menu system on the
> command line.

very tough.

> Is it possible to customize readline so that the prompt at each stage can be
> modified depending on what have been chosen?

don't understand.  you want to modify the *prompt*?  The one that is
usually set by the environment variable PS1? 

> Example:
>     Give a data structure
>     
>         {a:{b:1, c:1, d:1}, e:{f:1, g:{h:1,i:1}}}
>     
>     The first tab should prompt choices (a, e).  The second tab should prompt
>     (b, c, d) if a is chosen, or (f, g) if b is chosen.
> 
> My failed attempts appear to boil down to the following situation.  As far
> as I can figure out, the readline module uses a completor c by calling c(x,
> n) repeatedly, where x is the partial word and n is the n-th time it is
> called.  This does not appear to provide an adequate means to disambiguate
> between [e] and [e,g] if e and g happen to be the same word.

Again i am lost.  Note that the multiple calls to the completer function
are only a protocol to pass a list of strings. 

If all strings have a common prefix readline puts this prefix on the
cmdline. 

> Given the above structure, it is easy a write a function 
>     [] -> (a,b)
>     [a] -> (b,c,d)
>     [e] -> (f,g)
>     [e,g] -> (h,i)
> 
> Is there a way for readline to accept mapping from a history to a list of
> choices?  The completer object could cache the history, of course, as long
> as readline can somehow indicate that it has advanced one word.

Please try to come up with real-life examples and please, don't bring
in so much terminology.  What do you mean with history?  And what
is a mapping from 'a history to a list of choices'?

> In addition, I'd also like to see two more improvements:
> 
> - Is it possible to use multiword tokens?

you can pass back strings that contain spaces.  You might
have to set_completer_delims to contain fewer characters. 

> - Is it possible to avoid prompting the files in the current directory when
>   nothing else is appropriate?

ASFAIK no. you can pass back two strings with no common prefix to prevent 
this behaviour. 

> Thanks for any pointer.  I'd like to avoid the curses module, if possible.

though understandable i think you might be better off trying to use curses. 

In any case, you might want to check out my current work on a new 
command line completer which handles many of the above readline 
stuff.  It provides documentation for functions among other stuff.

    http://home.trillke.net/~hpk/rlcompleter2.py

regards,

    holger




More information about the Python-list mailing list