tty-prompt
tty-prompt copied to clipboard
Filtering a list where the items contain spaces when using select
Describe the problem
I'm trying to find a way to not use the space character as selection when filtering a list using select. I'm using select for a list of names that have spaces in them (e.g ["Some Team", "Some Team: Subteam"]
).
When the user types "Some " the prompt selects the first instance and I want it to continue filtering using the space.
I tried using prompt.select('Select a team name:', team_names, filter: true, keys: [:return])
but that didn't work.
Steps to reproduce the problem
prompt = TTY::Prompt.new(active_color: :green)
team_names = ['Some Team', 'Some Team: Subteam']
prompt.select('Select a team name:', team_names, filter: true)
Actual behaviour
When the user types "Some " the prompt selects the first instance (i.e. Some Team
)
Expected behaviour
When the user types "Some " the prompt continues filtering using the space.
Describe your environment
- OS version: macOS 11.2.3
- Ruby version: ruby 2.6.3
- TTY::Prompt version: 0.23.0
Hi Francisco 👋
Thanks for submitting the issue.
I had a quick look at implementing this for the select
prompt and I can make it work fine. However, the space character in the multi_select
prompt is used for selecting choices and I'm not sure how to solve this conundrum. These two prompts work in tandem so I would need to find a solution that works for both of them. Do you have any suggestions?
Interesting! I was thinking that we'd keep the default as is (i.e. space is selection) unless explicitly stated otherwise -- something similar to what I tried above:
prompt.select('Select a team name:', team_names, filter: true, keys: [:return])
Then if the user explicitly does this for multi_select
we can require it to also pass another key for multiple selection?
Does that make sense?
Yup, makes sense. I wanted to avoid the need to specify a different key for selection in the multi_select
prompt but this may be the most straightforward way. Having said that, there is inherent complexity in going down this route. Choosing keys has an impact on displayed hints as well as the regex used for filtering. Not to mention a need then to dynamically register key events for a new selection key. Ultimately this will lead to greater flexibility. Do you have time to submit PR?
I can. I'm not super familiar with the structure of the codebase so if you can guide me a little bit where I could add a failing test + where I'd go to work to fix it that'd be a great start!
Sure thing. The logic responsible for the select
is in list.rb and for the multi_select
in mulit_list.rb. The test files match the prompt names.
@piotrmurach Any chance you could help me to get a start here? I've downloaded the project but I'm not entirely sure how I'd start tackling this since I'm fairly unfamiliar with the implementation.
Even if you can put a branch up with what you had would probably be super helpful!
Hey all! I stumbled upon the same problem, needing my filters to include spaces.
I believe I have implemented what @piotrmurach described in the PR above, including the check for clashing keys in mutli-select.