I am trying to implement an arbitrary autocomplete on windows for a command-line user interface I am writing. Inspired by the first answer to that question, I tried to just run the script written there, before realizing that I was on Windows and needed to use pyreadline
instead of readline
. After some trials I ended up with the script below, which is basically a copy-paste, but with pyreader initialization:
from pyreadline import Readline
readline = Readline()
class MyCompleter(object): # Custom completer
def __init__(self, options):
self.options = sorted(options)
def complete(self, text, state):
if state == 0: # on first trigger, build possible matches
if text: # cache matches (entries that start with entered text)
self.matches = [s for s in self.options
if s and s.startswith(text)]
else: # no text entered, all matches possible
self.matches = self.options[:]
# return match indexed by state
try:
return self.matches[state]
except IndexError:
return None
completer = MyCompleter(["hello", "hi", "how are you", "goodbye", "great"])
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')
input = raw_input("Input: ")
print "You entered", input
The problem is however when I try to run that script, <TAB>
does not lead to autocomplete.
How do I get <TAB>
to perform the autocompletion behavior?
Initially I though I messed up the completer setting and binding initialization that would be different for pyreadeline
compared to readline
, but from module code and examples in pyreadline
docs it looks they are identical.
I am trying to execute it on 2.7 Anaconda Python distribution in Windows 10, if this is of any use.