Reputation: 10920
Suppose I have this code:
choices = [
'north',
'south',
'east',
'west',
]
direction = input('Direction: ')
When this is run from the bash shell using python3 myprogram.py
, the user will be prompted to type in a direction. There are only four choices, so I would like to show the user an autocomplete suggestion menu similar to this example in vim, except that the menu should appear in the bash shell:
The user should be able to cycle through the available choices by pressing Tab or the up and down arrow keys.
How can I use Python to build an autocomplete suggestion menu that appears in the bash shell?
I tried the curses
module, but it is not suitable because it clears the screen (similar to this problem). Do I have to manually manipulate ANSI escape characters?
Upvotes: 2
Views: 1535
Reputation: 10920
You can try using the Python Prompt Toolkit, a library for building interactive command line applications in Python.
The library makes it easy to add interactive autocompletion menus:
(Image source: pcgli)
Upvotes: 3