forked from tmbo/questionary
-
Notifications
You must be signed in to change notification settings - Fork 0
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
This commit is heavily inspired by [gbataille's](https://github.com/gbataille) [PR](tmbo#42)
- Loading branch information
mario-dg
committed
May 29, 2024
1 parent
c894eef
commit 2e7f4cc
Showing
8 changed files
with
398 additions
and
3 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_prefix_filter_search=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_prefix_filter_search=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.