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

想用 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,但是不知道如何重构代码。

阿神
阿神

闭关修行中......

모든 응답(0)
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!