User input with a default value that can be modified

E

Etienne Hilson

Hello the list :)

I do a little program that permit the user to manage list of sentences.
This program runs into a linux shell.
The user can add, modify and delete the sentences.

What I want to do is :

When the user want to modify one sentence, I would like to do this :

Modify your sentence : The_sentence_appear_here_followed_by_a_cursor

And the user can go back with the cursor, like in the bash shell,
delete, modify, and when pressing enter, the new value (or the same if
not modified) is put in my variable.

Of course, the first think I did as a newbie was :

new_sentence = raw_input("Modify your sentence : "old_sentence)

But OF COURSE, stupid am I, the user cannot put the cursor back into
the old sentence !

I think about playing with some sophisticated keyboard exercise where
I could program a new input command with a value already appearing as
answer, but I am pretty sure that it exists already.

What do you think about it ?

Actually, it is quite difficult to find anything on it, because the
keywords are not very obvious (input, default answer, ...)

Thank you for your help.

Etienne
 
H

half.italian

Hello the list :)

I do a little program that permit the user to manage list of sentences.
This program runs into a linux shell.
The user can add, modify and delete the sentences.

What I want to do is :

When the user want to modify one sentence, I would like to do this :

Modify your sentence : The_sentence_appear_here_followed_by_a_cursor

And the user can go back with the cursor, like in the bash shell,
delete, modify, and when pressing enter, the new value (or the same if
not modified) is put in my variable.

Of course, the first think I did as a newbie was :

new_sentence = raw_input("Modify your sentence : "old_sentence)

But OF COURSE, stupid am I, the user cannot put the cursor back into
the old sentence !

I think about playing with some sophisticated keyboard exercise where
I could program a new input command with a value already appearing as
answer, but I am pretty sure that it exists already.

What do you think about it ?

Actually, it is quite difficult to find anything on it, because the
keywords are not very obvious (input, default answer, ...)

Thank you for your help.

Etienne

Check into the readline module. This is what I came up with. A
second thread injects the text into the open readline instance.
Hopefully the experts will show the _right_ way to do it.

import readline, threading
import time

class write(threading.Thread):
def __init__ (self, s):
threading.Thread.__init__(self)
self.s = s

def run(self):
time.sleep(.01)
readline.insert_text(self.s)
readline.redisplay()

write("Edit this sentence").start()
s = raw_input("prompt:")

print s

~Sean
 
H

half.italian

Hello the list :)

I do a little program that permit the user to manage list of sentences.
This program runs into a linux shell.
The user can add, modify and delete the sentences.

What I want to do is :

When the user want to modify one sentence, I would like to do this :

Modify your sentence : The_sentence_appear_here_followed_by_a_cursor

And the user can go back with the cursor, like in the bash shell,
delete, modify, and when pressing enter, the new value (or the same if
not modified) is put in my variable.

Of course, the first think I did as a newbie was :

new_sentence = raw_input("Modify your sentence : "old_sentence)

But OF COURSE, stupid am I, the user cannot put the cursor back into
the old sentence !

I think about playing with some sophisticated keyboard exercise where
I could program a new input command with a value already appearing as
answer, but I am pretty sure that it exists already.

What do you think about it ?

Actually, it is quite difficult to find anything on it, because the
keywords are not very obvious (input, default answer, ...)

Thank you for your help.

Etienne

Check into the readline module. This is what I came up with. A
second thread injects the text into the open readline instance.
Hopefully the experts will show the _right_ way to do it.

import readline, threading
import time

class write(threading.Thread):
def __init__ (self, s):
threading.Thread.__init__(self)
self.s = s

def run(self):
time.sleep(.01)
readline.insert_text(self.s)
readline.redisplay()

write("Edit this sentence").start()
s = raw_input("prompt:")

print s

~Sean
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top