Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_to_many_choices_for_shortcut_assignment():
ic = InquirerControl([str(i) for i in range(1, 100)], use_shortcuts=True)
# IC should fail gracefully when running out of shortcuts
assert len(list(filter(lambda x: x.shortcut_key is not None, ic.choices))) == len(
InquirerControl.SHORTCUT_KEYS
)
def test_blank_line_fix():
def get_prompt_tokens():
return [("class:question", "What is your favourite letter?")]
ic = InquirerControl(["a", "b", "c"])
inp = create_pipe_input()
try:
inp.send_text("")
layout = common.create_inquirer_layout(
ic, get_prompt_tokens, input=inp, output=DummyOutput()
)
# usually this would be 2000000000000000000000000000000
# but `common._fix_unecessary_blank_lines` makes sure
# the main window is not as greedy (avoiding blank lines)
assert (
layout.container.preferred_height(100, 200).max
== 1000000000000000000000000000001
)
def test_to_many_choices_for_shortcut_assignment():
ic = InquirerControl([str(i) for i in range(1, 100)], use_shortcuts=True)
# IC should fail gracefully when running out of shortcuts
assert len(list(filter(lambda x: x.shortcut_key is not None, ic.choices))) == len(
InquirerControl.SHORTCUT_KEYS
)
def test_prompt_highlight_coexist():
ic = InquirerControl(["a", "b", "c"])
expected_tokens = [
("class:pointer", " » "),
("[SetCursorPosition]", ""),
("class:text", "○ "),
("class:highlighted", "a"),
("", "\n"),
("class:text", " "),
("class:text", "○ "),
("class:text", "b"),
("", "\n"),
("class:text", " "),
("class:text", "○ "),
("class:text", "c"),
]
assert ic.pointed_at == 0
def select(message: str, choices: Sequence[Choice], **prompt_kwargs: Any) -> Question:
def get_prompt_tokens():
tokens = [('', '- '), ('class:x-question', message)]
if ic.is_answered:
answer = ''.join(t for _, t in ic.get_pointed_at().title)
tokens += [('', ' '), ('class:skipped' if answer == 'skip' else '', answer)]
return tokens
ic = InquirerControl(choices, None, use_indicator=False, use_shortcuts=False, use_pointer=True)
bindings = KeyBindings()
@bindings.add(Keys.ControlQ, eager=True)
@bindings.add(Keys.ControlC, eager=True)
def _(event: Any):
event.app.exit(exception=KeyboardInterrupt, style='class:aborting')
@bindings.add(Keys.Down, eager=True)
@bindings.add('j', eager=True)
def move_cursor_down(event: Any):
ic.select_next()
while not ic.is_selection_valid():
ic.select_next()
@bindings.add(Keys.Up, eager=True)
@bindings.add('k', eager=True)
def get_prompt_tokens():
tokens = [('class:x-question', message)]
if ic.is_answered:
tokens.append(('class:answer', ' done'))
else:
tokens.append(
(
'class:instruction',
' (use arrow keys to move, '
' to select, '
' to view in your browser)',
)
)
return tokens
ic = InquirerControl(choices, None, use_indicator=False, use_shortcuts=False, use_pointer=True)
bindings = KeyBindings()
@bindings.add(Keys.ControlQ, eager=True)
@bindings.add(Keys.ControlC, eager=True)
def _(event: Any):
event.app.exit(exception=KeyboardInterrupt, style='class:aborting')
@bindings.add(' ', eager=True)
def toggle(event: Any):
pointed_choice = ic.get_pointed_at().value
if pointed_choice in ic.selected_options:
ic.selected_options.remove(pointed_choice)
else:
ic.selected_options.append(pointed_choice)
@bindings.add('i', eager=True)
list highlighting the current location of the selection
cursor.
use_shortcuts: Allow the user to select items from the list using
shortcuts. The shortcuts will be displayed in front of
the list items.
use_pointer: Flag to enable the pointer in front of the currently
highlighted element.
Returns:
Question: Question instance, ready to be prompted (using `.ask()`).
"""
if choices is None or len(choices) == 0:
raise ValueError("A list of choices needs to be provided.")
if use_shortcuts and len(choices) > len(InquirerControl.SHORTCUT_KEYS):
raise ValueError(
"A list with shortcuts supports a maximum of {} "
"choices as this is the maximum number "
"of keyboard shortcuts that are available. You"
"provided {} choices!"
"".format(len(InquirerControl.SHORTCUT_KEYS), len(choices))
)
merged_style = merge_styles([DEFAULT_STYLE, style])
ic = InquirerControl(
choices,
default,
use_indicator=use_indicator,
use_shortcuts=use_shortcuts,
use_pointer=use_pointer,
use_pointer: Flag to enable the pointer in front of the currently
highlighted element.
Returns:
Question: Question instance, ready to be prompted (using `.ask()`).
"""
if choices is None or len(choices) == 0:
raise ValueError("A list of choices needs to be provided.")
if use_shortcuts and len(choices) > len(InquirerControl.SHORTCUT_KEYS):
raise ValueError(
"A list with shortcuts supports a maximum of {} "
"choices as this is the maximum number "
"of keyboard shortcuts that are available. You"
"provided {} choices!"
"".format(len(InquirerControl.SHORTCUT_KEYS), len(choices))
)
merged_style = merge_styles([DEFAULT_STYLE, style])
ic = InquirerControl(
choices,
default,
use_indicator=use_indicator,
use_shortcuts=use_shortcuts,
use_pointer=use_pointer,
)
def get_prompt_tokens():
# noinspection PyListCreation
tokens = [("class:qmark", qmark), ("class:question", " {} ".format(message))]
qmark: Question prefix displayed in front of the question.
By default this is a `?`
style: A custom color and style for the question parts. You can
configure colors as well as font types for different elements.
use_pointer: Flag to enable the pointer in front of the currently
highlighted element.
Returns:
Question: Question instance, ready to be prompted (using `.ask()`).
"""
merged_style = merge_styles([DEFAULT_STYLE, style])
ic = InquirerControl(choices, default, use_pointer=use_pointer)
def get_prompt_tokens():
tokens = []
tokens.append(("class:qmark", qmark))
tokens.append(("class:question", " {} ".format(message)))
if ic.is_answered:
nbr_selected = len(ic.selected_options)
if nbr_selected == 0:
tokens.append(("class:answer", " done"))
elif nbr_selected == 1:
if isinstance(ic.get_selected_values()[0].title, list):
tokens.append(
(
"class:answer",
"".join(