Default/editable string to raw_input
David Wahler
dwahler at gmail.com
Wed Mar 22 10:09:41 EST 2006
Paraic Gallagher wrote:
> Hi,
>
> This is my first post to the list, I hope somebody can help me with this
> problem. Apologies if it has been posted before but I have been internet
> searching to no avail.
>
> What I am trying to do is provide a simple method for a user to change a
> config file, for a test suite.
> The config file consists of a number of keys, eg. build number, target
> device, etc.
>
> What I would like to do is give the user a key, and the previous value
> for that key.
> The user can then accept the previous value or edit it on the line.
>
> I have a method where the previous value of a key is presented to the
> user and can be accepted if the user hits return. But as some of the
> values are somewhat long, and the user may only need to change 1 or 2
> characters, it would be a nice feature to offer line editing.
>
> I can get the readline.set_pre_input_hook() function to send text after
> a raw_input prompt, but this text cannot be edited.
With the disclaimer that, as others have said, this may not be the best
user-interface choice:
import readline
readline.set_startup_hook(lambda: readline.insert_text(old_value))
try:
new_value = raw_input()
finally:
readline.set_startup_hook(None)
Note that, among other issues, this isn't threadsafe (but then, you
really shouldn't be doing console I/O from multiple threads anyway).
Hope this helps.
-- David
More information about the Python-list
mailing list