pyreadline / pyreadline

pyreadline repository
100 stars 85 forks source link

No behavior for arbitrary input autocomplete set-up #45

Open chiffa opened 8 years ago

chiffa commented 8 years ago

Hello there!

I am experiencing some problems with using pyreadline module for arbitrary input autcomplete. The summary of my problem is available here.

To sum up the problem, 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 StackOverflow question, I copied it and adapted to run with pyreadline instead of readline, since I am no windows.

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, but instead appends a tab to the input.

I am trying to execute it on 2.7 Anaconda Python distribution in Windows 10, if this is of any use.

TheFriendlyCoder commented 7 years ago

I too am experiencing the same problem. Based a very brief review of the pyreadline implementation, I don't believe the library implements the correct hooks for the native raw_input / input methods to trap the key presses you mentioned. I'm guessing, just base on an equally brief review of the project homepage, that this functionality is only currently supported under IronPython - the .NET variation of the Python runtime. I suspect running under there the tab completion will work (untested of course).

I'd be curious if my assumptions here are correct / accurate and if there is any interest / incentive by the project maintainers to add support for this under the native CPython interpreter.