1 回答
TA贡献1845条经验 获得超8个赞
您可以创建自定义键绑定以在用户按 Enter 时执行完成。例如:
from prompt_toolkit import prompt
from prompt_toolkit.completion import FuzzyWordCompleter
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.filters import (
has_completions,
completion_is_selected,
)
# Override enter key to automatically perform first completion.
key_bindings = KeyBindings()
filter = has_completions & ~completion_is_selected
@key_bindings.add("enter", filter=filter)
def _(event):
event.current_buffer.go_to_completion(0)
event.current_buffer.validate_and_handle()
completer = FuzzyWordCompleter([
"Using", "Testing", "Building", "Designing", "Documenting"
])
result = prompt("$ ",
completer=completer,
complete_while_typing=True,
key_bindings=key_bindings,
)
if result:
print(result)
将被覆盖的键绑定当前位于shortcuts/prompt.py
和中key_binding/bindings/basic.py
。
如果您的提示应用程序更复杂(例如多行输入或多个缓冲区),那么您可能需要额外的逻辑来处理这些情况。 prompt_toolkit.filters.is_multiline可能有用。
添加回答
举报