customizing the readline module

Huaiyu Zhu huaiyu at
Sat Aug 17 02:04:29 CEST 2002

I'm trying to use the readline module to act like a menu system on the
command line.

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

    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.

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.

In addition, I'd also like to see two more improvements:

- Is it possible to use multiword tokens?

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

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


More information about the Python-list mailing list