File: press_any_key_to_continue.py

package info (click to toggle)
python-questionary 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 960 kB
  • sloc: python: 3,917; makefile: 66
file content (61 lines) | stat: -rw-r--r-- 1,662 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from typing import Any
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 questionary.question import Question
from questionary.styles import merge_styles_default


def press_any_key_to_continue(
    message: Optional[str] = None,
    style: Optional[Style] = None,
    **kwargs: Any,
):
    """Wait until user presses any key to continue.

    Example:
        >>> import questionary
        >>> questionary.press_any_key_to_continue().ask()
         Press any key to continue...
        ''

    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])

    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
    )