简体   繁体   English

如何使python自动完成显示匹配?

[英]How to make python autocompletion display matches?

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: 并执行类似这样的操作以使用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. 因此,在示例中有3个单词用于自动完成[“ help ”,“ mkdir ”,“ mktbl ”]。

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) 没有任何事情发生,因为没有一个匹配(mkdir和mktbl)

How to display options in case there are several matches? 如果有多个匹配项,如何显示选项? Like the Bash do with a file names autocompletion? 像Bash一样使用文件名自动完成?

Thus user whold get something like: 因此,用户可以获得如下内容:
> mk<tab>
mktbl mkdir
> mk<cursor>


PS I have tried to put PS我试过试试
_readline.insert_text(...)_ _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>

PPS I need a linux solution. PPS我需要一个linux解决方案。

Set the readline option 设置readline选项

set show-all-if-ambiguous on

if you want completions after the first <tab> . 如果你想在第一个<tab>之后完成。 Otherwise just hit <tab> twice. 否则只需按两次<tab>

Reference: http://caliban.org/bash/ , Section readline Tips and Tricks 参考: http//caliban.org/bash/,Section readline提示和技巧

PS. PS。 Tested your code on OS X and Linux, it works well (on my machines ;) 在OS X和Linux上测试了你的代码,它运行良好(在我的机器上;)

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 对于linux readline,有函数readline.set_completion_display_matches_hook
http://docs.python.org/library/readline.html?highlight=readline#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: 对于Windows readline,堆栈溢出有一个答案:
How do I make IPython organize tab completion possibilities by class? 如何让IPython按类组织标签完成的可能性?

Don't know how it works for mac. 不知道它对mac的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM