Using the library readline I managed to provide tab completion when using input(). My code until now looks like this:
import readline
class TabComplete:
def __init__(self, wordList):
self.wordList = wordList
def complete(self,text,state):
results = [x for x in self.wordList if x.startswith(text)] + [None]
return results[state]
readline.parse_and_bind("tab: complete")
tabComplete = ["IF", "IF_ELSE", "FOR", "WHILE"]
completer = TabComplete(tabComplete)
readline.set_completer(completer.complete)
userTyped = input("prompt > ")
If I start typing I and then hit twice on tab it will propose me IF and IF_ELSE as expected.
What I am searching now is:
- if I start typing
iand then hit twicetabis there a way to still make it proposeIFandIF_ELSE? - if I type
IF ELcan I tell him on real time to replace the space by_so the completion works?
you could always convert your text to upper and replace spaces with "_"