This is an experiment with the autocomplete functionality in the Rust crate inquire.
In particular, suppose I want the user to pick some tags. I already have some tags they might want to use, or they can add new ones.
I want a Text
input where I can:
- enter multiple tags, separated by spaces
- get suggestions and autocomplete for already-known tags
- create brand new tags
This is what it looks like:
Here's what I'm doing:
-
Initially I'm shown all five known tags (
adventure
,action
,mystery
,romance
andscifi
). -
I type
adv
and then presstab
. This autocompletes to the known tagadventure
. -
I type
s
, which shows me the two tags that contains
(mystery
andscifi
). I use the arrow keys to selectscifi
. -
I type
a
and then presstab
. This autocompletes to the known tagaction
.Notice that
adventure
isn't offered as a suggestion: it knows I already have this tag. -
I start typing
ro
. It suggests the known tagromance
, but I keep typing the new tagrobots
. -
I press
enter
to complete my typing, and I see the list of tags I've given (adventure
,scifi
,action
androbots
).
I already know one project where I want to use it, but I can imagine this is the sort of component I might want to use in a bunch of places. I've pulled it into a standalone repo so it's easier to find and maintain as a reusable snippet.
Copy the code from main.rs
into your project.
Play around with it.
Tweak it.
Fiddle with the details.
The behaviour probably isn't quite right for what you need, but hopefully it's a starting point.