首页 > 解决方案 > prompt-toolkit 在一列中以 READLINE_LIKE 样式完成

问题描述

如何在提示工具包中以 READLINE_LIKE 样式完成一列中的自定义单词?我正在尝试添加 '\n' 来创建一个新行,但它会自动在下一行之前添加制表符。

from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.shortcuts import CompleteStyle, prompt

    class MyCompleter(Completer):
        def get_completions(self, document, complete_event):
            word = document.get_word_before_cursor()
            c = [('one', 'description of One'), ('two', 'description of Two'), ('three', 'description of Three')]
            for complete in c:
                if complete[0].startswith(word):
                    display = complete[0] + '  ->  ' + complete[1] + '\n' 
                    yield Completion(
                        complete[0],
                        start_position=-len(word),
                        display=display,
                    )                
    
    
    prompt(
        "> ",
        completer=MyCompleter(),
        complete_style=CompleteStyle.READLINE_LIKE
    )

以下代码显示如下完成:

> <Tab>
one  ->  description of One
     two  ->  description of Two
     three  ->  description of Three

但我希望它是这样的:

> <Tab>
one  ->  description of One
two  ->  description of Two
three  ->  description of Three

标签: pythonpromptprompt-toolkit

解决方案


推荐阅读