I try to build a command line / terminal application using the prompt_toolkit, which should do the following:
- I have a list of (title, id) pairs, which is to big to fit on one screen but small enough to fit into memory. Let's say around 1000 entries.
- To select an item, I start typing. While typing - for example "dog" - the list is filtered to only those entries, having "dog" in the title.
- If the list is small enough (but not empty) I would like to use the arrow keys to select an entry.
- I want to have the id associated with that title.
I'm completely new to prompt_toolkit and approach the problem from both ends:
- Using plain
prompt()
with autocompletion: I tried to misuse a custom completion method to filter my list, but did not managed to display it. - I checked the widget / full screen examples, but the documentation is rather limited. I found for example the
SearchToolbar
but could not really figure out how it interacts with other widgets. - I did not found any example on how to display a list to select an element. There are more complex examples, so I would expect that it's possible, but got lost.
Could somebody point me to an example that solves something like my use case or give me a starting point how to approach this in general?
Here's a quick autocompleter that does what you want I think. This assumes the first field is unique. If it isn't, and the ID is, switch the keys and values in the dictionary.
This checks the first item in the tuple for a match with the input text, and displays both the first item and the ID, then returns the ID when a selection is made.
The display keyword arg lets you show different text in the autocomplete window than what you will return on the completion. The display_meta isn't strictly necessary here, but it does show what will be returned when an item is selected.