-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
press_any_key_to_continue initial commit
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from typing import Optional | ||
|
||
from prompt_toolkit import PromptSession | ||
from prompt_toolkit.formatted_text import to_formatted_text | ||
from prompt_toolkit.key_binding import KeyBindings | ||
from prompt_toolkit.keys import Keys | ||
from prompt_toolkit.styles import Style | ||
from prompt_toolkit.styles import merge_styles | ||
|
||
from questionary.constants import DEFAULT_STYLE | ||
from questionary.question import Question | ||
|
||
|
||
def press_any_key_to_continue( | ||
message: Optional[str] = None, | ||
style: Optional[Style] = None, | ||
**kwargs, | ||
): | ||
"""Wait until user presses any key to continue. | ||
Example: | ||
>>> import questionary | ||
>>> questionary.press_any_key_to_continue().ask() | ||
Press any key to continue... | ||
None | ||
Args: | ||
message: Question text. Defaults to `"Press any key to continue..."` | ||
style: A custom color and style for the question parts. You can | ||
configure colors as well as font types for different elements. | ||
Returns: | ||
:class:`Question`: Question instance, ready to be prompted (using `.ask()`). | ||
""" | ||
merged_style = merge_styles([DEFAULT_STYLE, style]) | ||
|
||
if message is None: | ||
message = ("Press any key to continue...",) | ||
|
||
def get_prompt_tokens(): | ||
tokens = [] | ||
|
||
tokens.append(("class:question", f" {message} ")) | ||
|
||
return to_formatted_text(tokens) | ||
|
||
def exit_with_result(event): | ||
event.app.exit(result=None) | ||
|
||
bindings = KeyBindings() | ||
|
||
@bindings.add(Keys.Any) | ||
def any_key(event): | ||
exit_with_result(event) | ||
|
||
return Question( | ||
PromptSession( | ||
get_prompt_tokens, key_bindings=bindings, style=merged_style, **kwargs | ||
).app | ||
) |
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,11 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from tests.utils import feed_cli_with_input | ||
|
||
|
||
def test_press_any_key_to_continue_default_message(): | ||
message = None | ||
text = "c" | ||
result, cli = feed_cli_with_input("press_any_key_to_continue", message, text) | ||
|
||
assert result is None |