mondragonfx
mondragonfx

Reputation: 87

How to trigger a "undo" for user input

I have the following user input statements that are part of a larger function. When prompted for user input it is the case that a user might fat finger the keyboard and continue with the following input prompts. The user input is arbitrary and it really depends on them if they plug in the correct information. However, it could be neat to go back to the previous input statement and continue from there by triggering a keybind (E.g crtl + b lets say) or something similar.

I understand that this could be refined by having explicit invalid inputs with a conditional statement. however, this wouldn't be the use case for my situation. I'm not sure how to go about this. Hoping someone can point me in the right direction.

whichDIMM = input("\nWhich DIMM needs to be replaced?: (ex. A4) \n")
dimmSize = input("\n64G/32G/16G memory?\n")
hostname = input("\nEnter hostname: \n"

Would it be possible to 'Undo' and go back to the previous prompt in case there are spelling mistakes? then continue as expected? otherwise, the user will need to re-run the entire program.

For example:

Which DIMM needs to be replaced?
*mistypes*

64G/32G/16G memory?
(trigger undo)

Which DIMM needs to be replaced?
DIMM_A4

64G/32G/16G memory?
64G

Enter hostname:
myhostname

...
continue with the rest of the script

Upvotes: 0

Views: 295

Answers (1)

Eric Darchis
Eric Darchis

Reputation: 26677

You are basically trying to turn this into a form where you can move between fields and then submit the whole form. This has been written in many ways already so you could just use one such library. A simple one would be npyscreen for example.

Upvotes: 1

Related Questions