1

I have a sort-of 'hacked' CLI I'm trying to build. I have a global input in a while loop, and the user can perpetually enter 'commands' to produce various results. I'm wondering, how can I let the user press the Up-Arrow (as you would in a basic terminal) to put the contents of the latest entered command? The code will improve context.

while True:
    arg = input("(user) ~ ")
    if arg == "joke":
        print("There's a band called 1023MB. They haven't had any gigs yet.")
    elif arg == "hi":
        print("Hello.")
    
    # If arg == up-arrow: getLatestCommand()
    # I'm not quite sure how to implement this...

Example scenario:

(user) ~ joke
There's a band called 1023MB. They haven't had any gigs yet.
(user) ~ UP-ARROW PRESS
(user) ~ joke

I attempted to use the keyboard module in Python, but I was unable to get rid of this ugly thing: whenever I press the up arrow on mac in a Python input, it forces output of the following, "^[[A" which is annoying.

LVCode
  • 11
  • 3
  • 2
    You will need to save the commands entered by the user and bind up arrow to a function which recalls commands. – isAif Jun 22 '20 at 04:40
  • 1
    Does this answer your question? [detect key press in python?](https://stackoverflow.com/questions/24072790/detect-key-press-in-python) – felipe Jun 22 '20 at 04:40
  • Thanks for you input, however, I attempted that route, but I was unable to get rid of this ugly thing: whenever I press the up arrow on mac in a python input, it forces output of the following, "^[[A" which is annoying. – LVCode Jun 22 '20 at 04:45
  • 1
    For one "up-arrow" command you can use a global variable but for more than one command you should use a list as a stack and for each command perform pop operation on that list. – ClassHacker Jun 22 '20 at 04:47

0 Answers0