-
Notifications
You must be signed in to change notification settings - Fork 25
Misc
This page is deprecated, documentation moved to: https://inquirerpy.readthedocs.io/
You can use a Separator to effectively group choices visually in the following prompts which
involves choices:
class Separator:
def __init__(self, line: str = 15 * "-") -> None:"""
? Select regions: █
Sydney
❯ Singapore
---------------
us-east-1
us-east-2
"""
from InquirerPy import inquirer
from InquirerPy.separator import Separator
region = inquirer.select(
message="Select regions:",
choices=[
{"name": "Sydney", "value": "ap-southeast-2"},
{"name": "Singapore", "value": "ap-southeast-1"},
Separator(),
"us-east-1",
"us-east-2",
],
multiselect=True,
transformer=lambda result: "%s region%s selected"
% (len(result), "s" if len(result) > 1 else ""),
).execute()Any string value that you want to display as the separator.
InquirerPy will raise the exception KeyboardInterrupt when user hit ctrl-c by default.
If you prefer to not raise this exception and simply just skip the question, you can do the following.
from InquirerPy import prompt, inquirer
# Classic Syntax (PyInquirer)
result = prompt({"type": "input", "message": "Name:"}, raise_keyboard_interrupt=False)
# Alternate Syntax
name = inquirer.text(message="Name:").execute(raise_keyboard_interrupt=False)InquirerPy provides a helper function called patched_print which can help
printing string to the terminal while the prompt is still running.
Its quite limiting at the moment as it can only print values above the current prompt which is only useful when doing debugging. In the future, the goal is to allow values to be printed above an entire prompt session.
The following example will print "Hello World" above the prompt when hitting alt-b.
from InquirerPy.utils import patched_print
from InquirerPy import inquirer
prompt = inquirer.text(message="Name:")
@prompt.register_kb("alt-b")
def _(_):
patched_print("Hello World")
name = prompt.execute()InquirerPy provides a helper function called color_print which can help print colored
messages both when the prompt is running and not running.
It automatically detects if the current terminal window has a prompt running or not. If the prompt is running, the colored text will be printed above the prompt. Otherwise if there's no prompt running, the colored text will simply be outputted to the terminal window.
from InquirerPy.utils import color_print
from InquirerPy import inquirer
prompt = inquirer.text(message="Name:")
@prompt.register_kb("alt-b")
def _(_):
color_print([("#e5c07b", "Hello"), ("#ffffff", "World")])
name = prompt.execute()
color_print([("class:aaa", "fooboo")], style={"aaa": "#000000"})The first parameter consists of an array of tuple.
- 0: the first element of the tuple can be either a color (reference Style) or a class (
class:bbb). When specifying class, thestyleparameter is required. - 1: the second element of the tuple is the value to display
A dictionary containing the mapping of class and color. You don't need to provide this value if the formatted_text contains color.
If you make calls to InquirerPy multiples times,
instead of passing the same parameters repeatedly, you can customized some of the common options via ENV variables.
from InquirerPy import prompt
from InquirerPy import inquirer
from InquirerPy import get_style
# before
result = prompt(questions=[{"type": "confirm", "message": "Confirm?"}], style={"questionmark": "#ffffff"})
result = inquirer.confirm(message="Confirm?", style=get_style({"questionmark": "#ffffff"})).execute()
# after
import os
os.environ["INQUIRERPY_STYLE_QUESTIONMARK"] = "#ffffff"
result = prompt(questions=[{"type": "confirm", "message": "Confirm?"}])
result = inquirer.confirm(message="Confirm?").execute()| style class | ENV |
|---|---|
| questionmark | INQUIRERPY_STYLE_QUESTIONMARK |
| answer | INQUIRERPY_STYLE_ANSWER |
| input | INQUIRERPY_STYLE_INPUT |
| question | INQUIRERPY_STYLE_QUESTION |
| instruction | INQUIRERPY_STYLE_INSTRUCTION |
| pointer | INQUIRERPY_STYLE_POINTER |
| checkbox | INQUIRERPY_STYLE_CHECKBOX |
| separator | INQUIRERPY_STYLE_SEPARATOR |
| skipped | INQUIRERPY_STYLE_SKIPPED |
| validator | INQUIRERPY_STYLE_VALIDATOR |
| marker | INQUIRERPY_STYLE_MARKER |
| fuzzy_prompt | INQUIRERPY_STYLE_FUZZY_PROMPT |
| fuzzy_info | INQUIRERPY_STYLE_FUZZY_INFO |
| fuzzy_border | INQUIRERPY_STYLE_FUZZY_BORDER |
| fuzzy_match | INQUIRERPY_STYLE_FUZZY_MATCH |
The ENV variable takes the lease priority when resolving styles, meaning if you provide a style it will always override the ENV variable if same style class is styled.
style parameter -> ENV -> default styleThe value in INQUIRERPY_VI_MODE does not matter, as long as its a string longer than 0, InquirerPy will
be set to vi_mode.
from InquirerPy import prompt
from InquirerPy import inquirer
# before
result = prompt(questions=[{"type": "input", "message": "Name:"}], vi_mode=True)
result = inquirer.text(message="Name:", vi_mode=True).execute()
# after
import os
os.environ["INQUIRERPY_VI_MODE"] = "true"
result = prompt(questions=[{"type": "input", "message": "Name:"}])
result = inquirer.text(message="Name").execute()| editing mode | ENV |
|---|---|
vi_mode=True |
INQUIRERPY_VI_MODE |
Different than style, since vi_mode has a default value of False, the ENV variable takes higher priority,
meaning if INQUIRERPY_VI_MODE is present, setting vi_mode=False won't take effect anymore. You'll need
to del os.environ["INQUIRERPY_VI_MODE"] in this case.
ENV -> param vi_modeThe value in INQUIRERPY_NO_RAISE_KBI does not matter, as long as its a string longer than 0, InquirerPy will
raise not raise when user hit ctrl-c.
from InquirerPy import prompt
from InquirerPy import inquirer
# before
result = prompt(questions=[{"type": "secret", "message": "Password:"}], raise_keyboard_interrupt=False)
result = inquirer.text(message="Name:", vi_mode=True).execute(raise_keyboard_interrupt=False)
# after
import os
os.environ["INQUIRERPY_NO_RAISE_KBI"] = "true"
result = prompt(questions=[{"type": "secret", "message": "Password:"}])
result = inquirer.text(message="Name").execute()| editing mode | ENV |
|---|---|
raise_keyboard_interrupt=False |
INQUIRERPY_NO_RAISE_KBI |
Different than style, since raise_keyboard_interrupt has a default value of True, the ENV variable takes higher priority,
meaning if INQUIRERPY_NO_RAISE_KBI is present, setting raise_keyboard_interrupt=True won't take effect anymore. You'll need
to del os.environ["INQUIRERPY_NO_RAISE_KBI"] in this case.
ENV -> param raise_keyboard_interruptPrompts
