2

As we all known that Ipython is an amazing tool, and when I read the source code of it, I found that it use prompt_toolkit in its completer.
I have a program using the lib readline, and the completer code like below, I wanna change it into prompt_toolkit. How should I do?

class Completer(object):
    def complete(self, text, state):
        buffer = readline.get_line_buffer()
        line = readline.get_line_buffer().split()
        COMMANDS = actions.keys()
        # show all commands
        if not line:
            return [c + ' ' for c in COMMANDS][state]
        # account for last argument ending in a space
        if RE_SPACE.match(buffer):
            line.append('')
        # resolve command to the implementation function
        cmd = line[0].strip()
        if cmd in COMMANDS:
            impl = getattr(actions[cmd], 'complete')
            args = line[1:]
            if args:
                return (impl(args) + [None])[state]
            return [cmd + ' '][state]
        results = [c + ' ' for c in COMMANDS if c.startswith(cmd)] + [None]
        return results[state]



com = Completer()

if(sys.platform == 'darwin'):
    readline.parse_and_bind("bind ^I rl_complete")
else:
    readline.parse_and_bind("tab: complete")

readline.set_completer_delims(' /\t\n;')
readline.set_completer(com.complete)
readline.set_history_length(10000)
caimaoy
  • 1,148
  • 1
  • 11
  • 25
  • can you be more specific on what the problem with `readline()` is? – WhatsThePoint Feb 28 '17 at 08:49
  • I wanna reuse the code written by using readline. `prompt_toolkit ` has its own `Completer `, I don't konw how to write it, and whether I can reuse the code in readline `completer`. – caimaoy Feb 28 '17 at 08:56

1 Answers1

0

After I read the source code, I found the only thing that I have to do is to rewrite the function, Completer.

the function Completer of readline wish you to return a list with [state].

the function Completer of prompt_toolkit.completion is a generator function, we just have to yield from the list of the words. In python3 we can use the keyword yield from.

the code should be just like the below:

from prompt_toolkit.completion import Completer, Completion

class MyCustomCompleter(Completer):
    def get_completions(self, document, complete_event):
        buffer = document.text
        line = document.text.split()
        COMMANDS = actions.keys()
        # show all commands
        if not line:
            for i in [c for c in COMMANDS]:
                yield Completion(i, start_position=-1 * len(document.text), display=i)
        # account for last argument ending in a space
        else:
            if RE_SPACE.match(buffer):
                line.append('')
            # resolve command to the implementation function
            cmd = line[0].strip()
            if cmd in COMMANDS:
                try:
                    impl = getattr(actions[cmd], 'complete')
                except:
                    yield Completion(cmd, start_position=-1 * len(document.text))
                else:
                    args = line[1:]
                    if args:
                        ret = impl(args)
                        if not ret:
                            pass
                        else:
                            for i in ret:
                                ret = cmd + ' ' + ' '.join(args[:-1]) + ' ' + i
                                ret = ' '.join(ret.split())
                                yield Completion(ret, start_position=-1 * (len(document.text)), display=i.split()[-1])
                    else:
                        yield Completion(cmd + ' ', start_position=-1 * len(document.text))
            else:
                results = [c for c in COMMANDS if c.startswith(cmd)]
                for i in results:
                    yield Completion(i, start_position=-1 * len(document.text), display=i)

content = prompt(completer=MyCustomCompleter())
Michael Hoff
  • 6,119
  • 1
  • 14
  • 38
caimaoy
  • 1,148
  • 1
  • 11
  • 25