Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering a list where the items contain spaces when using select #161

Open
fdiaz opened this issue Apr 1, 2021 · 8 comments
Open

Filtering a list where the items contain spaces when using select #161

fdiaz opened this issue Apr 1, 2021 · 8 comments

Comments

@fdiaz
Copy link

fdiaz commented Apr 1, 2021

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
@piotrmurach piotrmurach added the bug label Apr 1, 2021
@piotrmurach
Copy link
Owner

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?

@fdiaz
Copy link
Author

fdiaz commented Apr 5, 2021

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?

@piotrmurach
Copy link
Owner

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?

@fdiaz
Copy link
Author

fdiaz commented Apr 7, 2021

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!

@piotrmurach
Copy link
Owner

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.

@fdiaz
Copy link
Author

fdiaz commented Apr 21, 2021

@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!

@Geekfish
Copy link

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.

@milouse
Copy link

milouse commented May 1, 2024

As I just encountered the same issue, I work around it with a specific new class and a little monkey patch:

class SpaceList < TTY::Prompt::List
  def keyspace(event)
    @filter << event.value
    @active = 1
  end
end

module TTY
  class Prompt
    def select_with_space(question, *args, &block)
      invoke_select(SpaceList, question, *args, &block)
    end
  end
end

prompt = TTY::Prompt.new(interrupt: :exit)
answer = prompt.select_with_space('Select a sentence: ', sentences, filter: true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants