开发者

Python readline module: Set 2 character delimiter

开发者 https://www.devze.com 2023-04-02 13:50 出处:网络
I\'m using the readline module to autocomplete names (firstname lastname). I want to set a 2 character delimiter, but by setting readline.set_completer_delims(\', \') it accepts both the comma and th

I'm using the readline module to autocomplete names (firstname lastname).

I want to set a 2 character delimiter, but by setting readline.set_completer_delims(', ') it accepts both the comma and the whitespace as a delimiter. But I only want the combination.

The problem is, now I enter a first name that exists several times, with different last names. Instead of suggesting all possible last names in the completion, readline thinks that the whitespace character is a delimiter and starts suggesting all names over again.

How can I solve this problem?


Further information: I am already using a custom completion function:

# Configure and enable tab completion
def completer(text, state):
    """Contacts completer function for readline module"""
    options = [x[2].strip() for x in contacts
               if x[2].lower().startswith(text.strip().lower())]
    try:
        return options[state] + ', '
    except IndexError:
        return None

readline.set_completer(completer)

The problem is not that 开发者_JAVA百科the function works incorrectly. I debugged it, and when completing a word that ends with a whitespace (like "simon "), the text value passed to the completer is " " instead of "simon ".


You probably will have to use your own function for this, with

readline.set_completer([function])

From python doc :

"The completer function is called as function(text, state), for state in 0, 1, 2, ..., until it returns a non-string value. It should return the next possible completion starting with text."

0

精彩评论

暂无评论...
验证码 换一张
取 消