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

FuzzySelect returns wrong index if items has duplicate entries #286

Open
mike-lloyd03 opened this issue Oct 3, 2023 · 3 comments
Open
Labels
p-fuzzy-select Prompt: Fuzzy Select t-bug Type: Bug fix

Comments

@mike-lloyd03
Copy link

If a slice with duplicate values is passed to the items() method on a new FuzzySelect, the index of the first item is returned if the user selects the second item.

use dialoguer::{console::Term, FuzzySelect};

fn main() {
    let items = ["item0", "item1", "item1", "item3", "item4"];

    let selection = FuzzySelect::new()
        .items(&items)
        .interact_on_opt(&Term::stderr())
        .unwrap();

    if let Some(index) = selection {
        println!("Selection: {index}");
    }
}

Selecting the first item returns the correct index.

|
  item0
> item1
  item1
  item3
  item4

Selection: 1

Selecting the second item returns the incorrect index.

|
  item0
  item1
> item1
  item3
  item4

Selection: 1
@mike-lloyd03
Copy link
Author

I think the issue originates here where the selected index is determined from the string of the item that was chosen.

let sel_string = filtered_list[sel].0;
let sel_string_pos_in_items =
self.items.iter().position(|item| item.eq(sel_string));

I'm not seeing an easy way to determine the index that is selected instead of the string itself.

@pksunkara pksunkara added t-bug Type: Bug fix p-fuzzy-select Prompt: Fuzzy Select labels Oct 28, 2023
@zopadan
Copy link

zopadan commented Mar 6, 2024

Perhaps, as an added benefit to the user, duplicate items in the list can be determined and renamed as such? E.g.
|
item0
item1
item1 (Duplicate 1)
item3
item4

Can have any N number of duplicates. This would generate unique names for each item in the list.

@zopadan
Copy link

zopadan commented Mar 6, 2024

Perhaps, as an added benefit to the user, duplicate items in the list can be determined and renamed as such? E.g. | item0 item1 item1 (Duplicate 1) item3 item4

Can have any N number of duplicates. This would generate unique names for each item in the list.

This is of course assuming that any of the select menus can and should support duplicate entries. I can't really think of a reason that would be needed (how could the 2 similar selections actually result in different outcomes?), so the simple solution would be to always return a set of only unique entries in the items list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p-fuzzy-select Prompt: Fuzzy Select t-bug Type: Bug fix
Projects
None yet
Development

No branches or pull requests

3 participants