Skip to main content
 首页 » 编程设计

python之readline set_completion_display_matches_hook 在显示提示之前需要回车键

2025年05月04日7落叶无声

背景

您好,我正在尝试为 readline 中的制表符完成输出编写自定义显示。这是我的 display hook 函数-

代码

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

问题

但问题是我必须按回车键才能获得提示,这与默认的 Tab 完成输出不同,我可以立即获得提示。我看到有人在另一个线程中建议了 rl 模块,但是没有办法通过 readline 本身完成它吗?

请您参考如下方法:

好的,我找到了一种方法,不确定这是否是修复它的正确方法。但是我在 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() 

这很有效。