简体   繁体   English

我应该如何使用python lib,prompt_toolkit替换GNU readline程序?

[英]How should I do to use the python lib, prompt_toolkit to replace a GNU readline program?

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 . 众所周知, Ipython是一个了不起的工具,当我阅读它的源代码时,我发现它在completer使用了prompt_toolkit
I have a program using the lib readline , and the completer code like below, I wanna change it into prompt_toolkit . 我有一个使用lib readline的程序,以及如下的更completer代码,我想将其更改为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)

After I read the source code, I found the only thing that I have to do is to rewrite the function, Completer . 阅读源代码后,我发现唯一要做的就是重写功能Completer

the function Completer of readline wish you to return a list with [state]. readline函数Completer希望您返回带有[state]的列表。

the function Completer of prompt_toolkit.completion is a generator function, we just have to yield from the list of the words. 该功能Completerprompt_toolkit.completion是发电机的功能,我们只需要yield从单词的列表。 In python3 we can use the keyword yield from . 在python3中,我们可以使用关键字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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM