readline的set_completion_display_matches_hook需在显示提示前按回车键

4 投票
1 回答
839 浏览
提问于 2025-04-17 17:31

背景

你好,我正在尝试为我的tab补全输出写一个自定义的显示方式。这是我的 display hook 函数-

代码

def match_display_hook(self, substitution, matches, longest_match_length):
    print ''
    for match in matches:
        print match
    readline.redisplay()

问题

但问题是,我必须按回车键才能得到提示,而不像默认的tab补全输出那样可以立即看到提示。我看到有人在另一个讨论中提到过 rl 模块,但难道就没有办法通过readline本身来实现吗?

1 个回答

3

好的,我找到了一种方法,不确定这是不是解决问题的正确方式。不过我在 match_display_hook 的最后打印了提示信息和 readline 缓冲区,结果一切看起来都不错。以下是我新的 match_display_hook:

def match_display_hook(self, substitution, matches, longest_match_length):
    print ''
    for match in matches:
        print match
    print self.prompt.rstrip(),
    print readline.get_line_buffer(),
    readline.redisplay()

这个方法效果很好。

撰写回答