magmax / python-inquirer

A collection of common interactive command line user interfaces, based on Inquirer.js (https://github.com/SBoudrias/Inquirer.js/)
MIT License
992 stars 98 forks source link

Added a search feature for List() #524

Open fullfox opened 6 months ago

fullfox commented 6 months ago

Hello,

This adds two features to the List():

matcher = lambda entry, search: entry.lower().startswith(search.lower())

questions = [
    inquirer.List(
        "size", message="What size do you need?", choices=["Jumbo", "Large", "Standard"], search=True, matcher=matcher
    ),
]
answers = inquirer.prompt(questions)

image

If the choices array contains tuples, it is the responsability of the user to provide a matcher function that handles tuple instead on string.

I've provided unit test and example in the commit. Hope this is clean enough. Please let me know if anything is unclear or if my implementation can be improved. If no matcher function is provided, the code behave exactly as before my commit.

Cube707 commented 6 months ago

Thank you very much for this. This feature has been on the ToDo list for quite some time.

However in the current example there is not way to correct or go back on the typed stuff. I think it would make sens if BACKSAPCE would work or something similar, in case someone makes a type.

Also documentation would be nice, even if its not beeing deployed at the moment

Cube707 commented 6 months ago

also some pre-commit stuff fails

fullfox commented 6 months ago

Hello,

Actually I've implemented the backspace deletion. Did you test and didn't it work ?

I'll check why nox fails tomorrow.

Cube707 commented 6 months ago

Yes I tested the provided example on windows and it didn't seem to work.

The its the flake8 pre-commit hook that fails. It just wants some formating corrections

fullfox commented 6 months ago

Okay I fixed the backspace handling for windows, and i made it flake8 compliant.

fullfox commented 6 months ago

Hello, could you check my pr ?

Cube707 commented 6 months ago

Sorry for the delay but I am on holiday until 24. Of February.

But its on the todo list for when I am back

Cube707 commented 6 months ago

also sidenote for the future: Rebasing onto the new state of main doesn't produce a bunch of merge-commits

fullfox commented 6 months ago

Sure that would cover more use cases. I'll try to implement that this week.

fullfox commented 5 months ago

Okay i changed the matcher signature to matcher(choices: list, pressedKey: str, searchString: str) -> (int, str) where searchString acts like a memory to handle scenario where we want to keep track of previous keys (a search for example). And the function returns a tuple (index, searchString) with the choosed index and the new searchString.