Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions questionary/prompts/confirm.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def confirm(
style: Optional[Style] = None,
auto_enter: bool = True,
instruction: Optional[str] = None,
placeholder: Optional[str] = None,
**kwargs: Any,
) -> Question:
"""A yes or no question. The user can either confirm or deny.
Expand Down Expand Up @@ -61,6 +62,10 @@ def confirm(

instruction: A message describing how to proceed through the
confirmation prompt.

placeholder: Optional placeholder text shown when no answer is selected.
The placeholder text will disappear once the user presses a key.
Comment on lines +66 to +67

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
placeholder: Optional placeholder text shown when no answer is selected.
The placeholder text will disappear once the user presses a key.
placeholder: Optional placeholder text shown when no answer is selected.
The placeholder text will disappear once the user presses a key.


Returns:
:class:`Question`: Question instance, ready to be prompted (using `.ask()`).
"""
Expand All @@ -83,6 +88,8 @@ def get_prompt_tokens():
if status["answer"] is not None:
answer = YES if status["answer"] else NO
tokens.append(("class:answer", answer))
elif placeholder is not None and not status["complete"]:
tokens.append(("class:placeholder", placeholder))

return to_formatted_text(tokens)

Expand Down
22 changes: 22 additions & 0 deletions tests/prompts/test_confirm.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,25 @@ def test_confirm_instruction():
"confirm", message, text, instruction="Foo instruction"
)
assert result is True


def test_confirm_placeholder():
message = "Foo message"
text = "Y" + "\r"
placeholder = "This is a placeholder"

result, cli = feed_cli_with_input(
"confirm", message, text, placeholder=placeholder
)
assert result is True


def test_confirm_placeholder_disappears_on_input():
message = "Foo message"
text = "n" + KeyInputs.ENTER + "\r"
placeholder = "This should disappear"

result, cli = feed_cli_with_input(
"confirm", message, text, auto_enter=False, placeholder=placeholder
)
assert result is False
Comment on lines +106 to +125

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests don't exercise the placeholder logic. Both only assert the y/n result, which passes regardless of whether the placeholder renders or clears.

Could you assert on what's displayed: that the class:placeholder token is present before any key is pressed, and gone once an answer is given?