Skip to content

Commit

Permalink
feat: Add dynamic instruction message for checkbox (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
baturayo committed May 25, 2023
1 parent 0243343 commit acf9391
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
21 changes: 13 additions & 8 deletions questionary/prompts/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def checkbox(
use_arrow_keys: bool = True,
use_jk_keys: bool = True,
use_emacs_keys: bool = True,
instruction: Optional[str] = None,
**kwargs: Any,
) -> Question:
"""Ask the user to select from a list of items.
Expand Down Expand Up @@ -103,6 +104,7 @@ def checkbox(
use_emacs_keys: Allow the user to select items from the list using
`Ctrl+N` (down) and `Ctrl+P` (up) keys.
instruction: A message describing how to navigate the menu.
Returns:
:class:`Question`: Question instance, ready to be prompted (using ``.ask()``).
Expand Down Expand Up @@ -162,15 +164,18 @@ def get_prompt_tokens() -> List[Tuple[str, str]]:
("class:answer", "done ({} selections)".format(nbr_selected))
)
else:
tokens.append(
(
"class:instruction",
"(Use arrow keys to move, "
"<space> to select, "
"<a> to toggle, "
"<i> to invert)",
if instruction is not None:
tokens.append(("class:instruction", instruction))
else:
tokens.append(
(
"class:instruction",
"(Use arrow keys to move, "
"<space> to select, "
"<a> to toggle, "
"<i> to invert)",
)
)
)
return tokens

def get_selected_values() -> List[Any]:
Expand Down
9 changes: 9 additions & 0 deletions tests/prompts/test_checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ def test_select_first_choice():
assert result == ["foo"]


def test_select_with_instruction():
message = "Foo message"
kwargs = {"choices": ["foo", "bar", "bazz"], "instruction": "sample instruction"}
text = KeyInputs.SPACE + KeyInputs.ENTER + "\r"

result, cli = feed_cli_with_input("checkbox", message, text, **kwargs)
assert result == ["foo"]


def test_select_first_choice_with_token_title():
message = "Foo message"
kwargs = {
Expand Down

0 comments on commit acf9391

Please sign in to comment.