Replies: 2 comments 1 reply
|
You can do this by combining a Example: from prompt_toolkit import prompt
from prompt_toolkit.completion import WordCompleter
choices = [
"Google",
"Cloudflare",
"Quad9",
"AdGuard",
"OpenDNS",
]
completer = WordCompleter(
choices,
ignore_case=True,
)
result = prompt(
"Pick a provider: ",
completer=completer,
)
print(result)For a more custom UI with live filtering and keyboard navigation, the usual approach is:
Also, |
|
There are two different UI models here, and prompt_toolkit currently provides the building blocks for both but not a single choice(..., searchable=True) shortcut. If you are happy with completion-menu behavior, the smallest solution is a normal prompt with FuzzyWordCompleter. prompt_toolkit already implements fuzzy scoring/highlighting there, so you don't need to maintain your own filtering algorithm. If you specifically need the choice shortcut's persistent list/navigation UI plus a search field, I would compose it rather than force a Completer into ChoiceInput: use a Buffer/BufferControl for the query and a RadioList (or FormattedTextControl for more control) for the filtered choices. Recompute the visible values when the query buffer changes, preserve the selected value when it is still present, and use shared key bindings/focus management for Up/Down/Enter. The existing answer's WordCompleter example is prefix completion, not fuzzy choice filtering. Current prompt_toolkit actually ships FuzzyCompleter and FuzzyWordCompleter, so for a command-palette-like input where the completion menu is acceptable, use those directly. For a VS Code Ctrl+P-style full-screen selector, the custom two-control composition remains the appropriate route: one editable Buffer owns the search text; the list owns selection. Keeping those states separate also avoids making the selected choice overwrite the query text. |
Uh oh!
There was an error while loading. Please reload this page.
How to achieve the same behavior of
prompt_toolkit.shortcuts.choicebut with an extra text input field that allows user to filter/search choices. Is there a library based on prompt-toolkit that support that already?Thanks
All reactions