想用 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
,但是不知道如何重构代码。
闭关修行中......