python - 正确使用 prompt_toolkit 替换原有的 readline 程序的方法是什么?
阿神
阿神 2017-04-18 10:22:46
0
0
836

想用 prompt_toolkit 库替换原有的 readline 程序如何重构?
下面已经有了原有的补全方法,是否可以复用原有的方法?

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)

想做出 Ipython 那样酷炫的效果,然后去查看源码,发现他使用了 prompt_toolkit,但是不知道如何重构代码。

阿神
阿神

闭关修行中......

Antworte allen(0)
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!