如何让python自动补全显示匹配?

发布于 2024-10-15 08:43:10 字数 1448 浏览 4 评论 0原文

我有一个带有自动完成功能的完成类。简单版本:

class Completer:
    def __init__(self):
        self.words = ["mkdir","mktbl", "help"]
        self.prefix = None

    def complete(self, prefix, index):
        if prefix != self.prefix:
            self.matching_words = [w for w in self.words if w.startswith(prefix)]
            self.prefix = prefix
        else:
            pass                
        try:
            return self.matching_words[index]
        except IndexError:
            return None

并执行类似这样的操作以使用 readline 获得自动完成:

import readline
readline.parse_and_bind("tab: complete")

completer = Completer()
readline.set_completer(completer.complete)
user_input =raw_input("> ")

因此,自动完成有 3 个单词 ["help", "mkdir"," mktbl"] 在示例中。

如果用户执行:
<代码>>他
用户得到:
<代码>> help

但如果用户执行
<代码>> mk
没有发生任何事情,因为没有单个匹配项(mkdir 和 mktbl)

如果存在多个匹配项,如何显示选项?就像 Bash 那样自动完成文件名吗?

因此用户会得到类似的结果:
<代码>> mk
mktbl mkdir
<代码>> mk


PS 我尝试过
_readline.insert_text(...)_

打印...
进入完成函数,但它会阻止插入,因此用户会得到如下所示的内容:
<代码>> mk
<代码>> mkmktbl mkdir

PPS 我需要一个 Linux 解决方案。

I have kind of a completer class with an autocompletion function. Simple version:

class Completer:
    def __init__(self):
        self.words = ["mkdir","mktbl", "help"]
        self.prefix = None

    def complete(self, prefix, index):
        if prefix != self.prefix:
            self.matching_words = [w for w in self.words if w.startswith(prefix)]
            self.prefix = prefix
        else:
            pass                
        try:
            return self.matching_words[index]
        except IndexError:
            return None

And execute something like this to get auto-completion with readline:

import readline
readline.parse_and_bind("tab: complete")

completer = Completer()
readline.set_completer(completer.complete)
user_input =raw_input("> ")

So, there are 3 words for auto-completion ["help", "mkdir","mktbl"] in the example.

if a user executes:
> he<tab>
the user gets:
> help

but if the user executes
> mk<tab>
nothing is happening because there are not a single match (mkdir and mktbl)

How to display options in case there are several matches? Like the Bash do with a file names autocompletion?

Thus user whold get something like:
> mk<tab>
mktbl mkdir
> mk<cursor>


P.S. I have tried to put
_readline.insert_text(...)_
and
print ...
into completer function but it brakes the insertion, so a user gets something like this:
> mk<tab>
> mkmktbl mkdir <cursor>

P.P.S I need a linux solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月隐月明月朦胧 2024-10-22 08:43:10

请设置 readline 选项

set show-all-if-ambiguous on

如果您希望在第一个 之后完成, 。否则只需点击 两次即可。

参考:http://caliban.org/bash/,阅读行提示和技巧部分 >

PS。在 OS X 和 Linux 上测试了你的代码,它运行良好(在我的机器上;)

Set the readline option

set show-all-if-ambiguous on

if you want completions after the first <tab>. Otherwise just hit <tab> twice.

Reference: http://caliban.org/bash/, Section readline Tips and Tricks

PS. Tested your code on OS X and Linux, it works well (on my machines ;)

北斗星光 2024-10-22 08:43:10

有人建议我一个解决方案来完成答案。它允许组织自动完成选项的完成输出。

对于linux readline 有函数
readline.set_completion_display_matches_hook
http://docs.python.org/library/readline。 html?highlight=readline#readline.set_completion_display_matches_hook

因此,对于上面列出的示例,

def print_suggestions(self, substitution, matches, longest_match_length) :
    print "useless text to be displayed"
    print substitution
    print " ".join[match for match in matches]
    print longest_match_length

readline.set_completion_display_matches_hook(print_suggestions)

此代码将产生:
<代码>> mk

useless text to be displayed
mk
mkdir mktbl
5  

> mk

对于 Windows readline,堆栈溢出有一个答案:
如何让 IPython 通过以下方式组织选项卡完成可能性类?

不知道它在 mac 上如何工作。

I was suggested a solution that complete the answer. It allows to organize completion output of autocompletion options.

For linux readline there are function
readline.set_completion_display_matches_hook
http://docs.python.org/library/readline.html?highlight=readline#readline.set_completion_display_matches_hook

So, for the example listed above this code

def print_suggestions(self, substitution, matches, longest_match_length) :
    print "useless text to be displayed"
    print substitution
    print " ".join[match for match in matches]
    print longest_match_length

readline.set_completion_display_matches_hook(print_suggestions)

this will produse:
> mk<tab>

useless text to be displayed
mk
mkdir mktbl
5  

> mk<cursor>

For windows readline there is an answer at stack overflow:
How do I make IPython organize tab completion possibilities by class?

Don't know how it works for mac.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文