-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add search functionality to select and checkbox prompt, based on …
…#42 (#374) * feat: add search functionality to select and checkbox prompt This commit is heavily inspired by [gbataille's](https://github.com/gbataille) [PR](#42) * chore: bump minor version, because of new feature * chore: bump version in pyproject.toml * feat: changed prefix filter to search filter, allow all/invert with ctrl Instead of a prefix filter, the search filter is now searched within all entries. This seems to be more common than a prefix search. Using the search functionality disabled the ability to select all options or invert the selection in the checkbox control. If the search filter is enabled, these two functionalities can now be used with the key modifier ctrl. Updated the displayed instructions to match the changes made. * fix: reverted version bump --------- Co-authored-by: mario-dg <[email protected]>
- Loading branch information
Showing
8 changed files
with
401 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import questionary | ||
from examples import custom_style_dope | ||
|
||
zoo_animals = [ | ||
"Lion", | ||
"Tiger", | ||
"Elephant", | ||
"Giraffe", | ||
"Zebra", | ||
"Panda", | ||
"Kangaroo", | ||
"Gorilla", | ||
"Chimpanzee", | ||
"Orangutan", | ||
"Hippopotamus", | ||
"Rhinoceros", | ||
"Leopard", | ||
"Cheetah", | ||
"Polar Bear", | ||
"Grizzly Bear", | ||
"Penguin", | ||
"Flamingo", | ||
"Peacock", | ||
"Ostrich", | ||
"Emu", | ||
"Koala", | ||
"Sloth", | ||
"Armadillo", | ||
"Meerkat", | ||
"Lemur", | ||
"Red Panda", | ||
"Wolf", | ||
"Fox", | ||
"Otter", | ||
"Sea Lion", | ||
"Walrus", | ||
"Seal", | ||
"Crocodile", | ||
"Alligator", | ||
"Python", | ||
"Boa Constrictor", | ||
"Iguana", | ||
"Komodo Dragon", | ||
"Tortoise", | ||
"Turtle", | ||
"Parrot", | ||
"Toucan", | ||
"Macaw", | ||
"Hyena", | ||
"Jaguar", | ||
"Anteater", | ||
"Capybara", | ||
"Bison", | ||
"Moose", | ||
] | ||
|
||
|
||
if __name__ == "__main__": | ||
toppings = ( | ||
questionary.checkbox( | ||
"Select animals for your zoo", | ||
choices=zoo_animals, | ||
validate=lambda a: ( | ||
True if len(a) > 0 else "You must select at least one zoo animal" | ||
), | ||
style=custom_style_dope, | ||
use_jk_keys=False, | ||
use_search_filter=True, | ||
).ask() | ||
or [] | ||
) | ||
|
||
print( | ||
f"Alright let's create our zoo with following animals: {', '.join(toppings)}." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Example for a select question type with search enabled. | ||
Run example by typing `python -m examples.select_search` in your console.""" | ||
from pprint import pprint | ||
|
||
import questionary | ||
from examples import custom_style_dope | ||
from questionary import Choice | ||
from questionary import Separator | ||
from questionary import prompt | ||
|
||
|
||
def ask_pystyle(**kwargs): | ||
# create the question object | ||
question = questionary.select( | ||
"What do you want to do?", | ||
qmark="😃", | ||
choices=[ | ||
"Order a pizza", | ||
"Make a reservation", | ||
"Cancel a reservation", | ||
"Modify your order", | ||
Separator(), | ||
"Ask for opening hours", | ||
Choice("Contact support", disabled="Unavailable at this time"), | ||
"Talk to the receptionist", | ||
], | ||
style=custom_style_dope, | ||
use_jk_keys=False, | ||
use_search_filter=True, | ||
**kwargs, | ||
) | ||
|
||
# prompt the user for an answer | ||
return question.ask() | ||
|
||
|
||
def ask_dictstyle(**kwargs): | ||
questions = [ | ||
{ | ||
"type": "select", | ||
"name": "theme", | ||
"message": "What do you want to do?", | ||
"choices": [ | ||
"Order a pizza", | ||
"Make a reservation", | ||
"Cancel a reservation", | ||
"Modify your order", | ||
Separator(), | ||
"Ask for opening hours", | ||
{"name": "Contact support", "disabled": "Unavailable at this time"}, | ||
"Talk to the receptionist", | ||
], | ||
} | ||
] | ||
|
||
return prompt(questions, style=custom_style_dope, **kwargs) | ||
|
||
|
||
if __name__ == "__main__": | ||
pprint(ask_pystyle()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.