0

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:

  1. if I start typing i and then hit twice tab is there a way to still make it propose IF and IF_ELSE ?
  2. if I type IF EL can I tell him on real time to replace the space by _ so the completion works?
martineau
  • 119,623
  • 25
  • 170
  • 301
supe345
  • 131
  • 6

1 Answers1

0

you could always convert your text to upper and replace spaces with "_"

    def complete(self,text,state):
        text = tex.upper().replace(" ","_")
        results =  [x for x in self.wordList if x.startswith(text)] + [None]
        return results[state]
heczaco
  • 198
  • 1
  • 13