Example #1
0
def print_formatted_text(text: str,
                         style: Optional[str] = None,
                         **kwargs: Any) -> None:
    """Print formatted text.

    Sometimes you want to spice up your printed messages a bit,
    :meth:`questionary.print` is a helper to do just that.

    Example:

        >>> import questionary
        >>> questionary.print("Hello World 🦄", style="bold italic fg:darkred")
        Hello World 🦄

    .. image:: ../images/print.gif

    Args:
        text: Text to be printed.
        style: Style used for printing. The style argument uses the
            prompt :ref:`toolkit style strings <prompt_toolkit:styling>`.
    """
    from prompt_toolkit import print_formatted_text as pt_print
    from prompt_toolkit.formatted_text import FormattedText as FText

    if style is not None:
        text_style = Style([("text", style)])
    else:
        text_style = DEFAULT_STYLE

    pt_print(FText([("class:text", text)]), style=text_style, **kwargs)
def test_with_style():
    """
    Text `print_formatted_text` with `HTML` wrapped in `to_formatted_text`.
    """
    f = _Capture()

    html = HTML('<ansigreen>hello</ansigreen> <b>world</b>')
    formatted_text = to_formatted_text(html, style='class:myhtml')
    pt_print(formatted_text, file=f)

    assert f.data == \
        b'\x1b[0m\x1b[?7h\x1b[0;32mhello\x1b[0m \x1b[0;1mworld\x1b[0m\r\n\x1b[0m'
def test_with_style():
    """
    Text `print_formatted_text` with `HTML` wrapped in `to_formatted_text`.
    """
    f = _Capture()

    html = HTML('<ansigreen>hello</ansigreen> <b>world</b>')
    formatted_text = to_formatted_text(html, style='class:myhtml')
    pt_print(formatted_text, file=f)

    assert f.data == \
        b'\x1b[0m\x1b[?7h\x1b[0;32mhello\x1b[0m \x1b[0;1mworld\x1b[0m\r\n\x1b[0m'
Example #4
0
def test_with_style():
    f = _Capture()
    style = Style.from_dict({
        'hello': '#ff0066',
        'world': '#44ff44 italic',
    })
    tokens = FormattedText([
        ('class:hello', 'Hello '),
        ('class:world', 'world'),
    ])
    pt_print(tokens, style=style, file=f)
    assert b'\x1b[0;38;5;197mHello' in f.data
    assert b'\x1b[0;38;5;83;3mworld' in f.data
Example #5
0
def test_with_style():
    f = _Capture()
    style = Style.from_dict({
        "hello": "#ff0066",
        "world": "#44ff44 italic",
    })
    tokens = FormattedText([
        ("class:hello", "Hello "),
        ("class:world", "world"),
    ])
    pt_print(tokens, style=style, file=f)
    assert b"\x1b[0;38;5;197mHello" in f.data
    assert b"\x1b[0;38;5;83;3mworld" in f.data
def test_with_style():
    f = _Capture()
    style = Style.from_dict({
        'hello': '#ff0066',
        'world': '#44ff44 italic',
    })
    tokens = FormattedText([
        ('class:hello', 'Hello '),
        ('class:world', 'world'),
    ])
    pt_print(tokens, style=style, file=f)
    assert b'\x1b[0;38;5;197mHello' in f.data
    assert b'\x1b[0;38;5;83;3mworld' in f.data
def test_html_with_style():
    """
    Text `print_formatted_text` with `HTML` wrapped in `to_formatted_text`.
    """
    f = _Capture()

    html = HTML("<ansigreen>hello</ansigreen> <b>world</b>")
    formatted_text = to_formatted_text(html, style="class:myhtml")
    pt_print(formatted_text, file=f, color_depth=ColorDepth.DEFAULT)

    assert (
        f.data ==
        b"\x1b[0m\x1b[?7h\x1b[0;32mhello\x1b[0m \x1b[0;1mworld\x1b[0m\r\n\x1b[0m"
    )
def test_formatted_text_with_style():
    f = _Capture()
    style = Style.from_dict({
        "hello": "#ff0066",
        "world": "#44ff44 italic",
    })
    tokens = FormattedText([
        ("class:hello", "Hello "),
        ("class:world", "world"),
    ])

    # NOTE: We pass the default (8bit) color depth, so that the unit tests
    #       don't start failing when environment variables change.
    pt_print(tokens, style=style, file=f, color_depth=ColorDepth.DEFAULT)
    assert b"\x1b[0;38;5;197mHello" in f.data
    assert b"\x1b[0;38;5;83;3mworld" in f.data
Example #9
0
def print_formatted_text(text: str,
                         style: Optional[str] = None,
                         **kwargs: Any) -> None:
    """Print formatted text.

    Args:
        text: text to be printed
        style: style used for printing. uses as prompt toolkit style string, details at
            https://python-prompt-toolkit.readthedocs.io/en/master/pages/advanced_topics/styling.html#style-strings

    Example:
        print_formatted_text("Hello World!", style="bold italic fg:darkred")"""
    from prompt_toolkit import print_formatted_text as pt_print
    from prompt_toolkit.formatted_text import FormattedText as FText

    if style is not None:
        text_style = Style([("text", style)])
    else:
        text_style = DEFAULT_STYLE

    pt_print(FText([("class:text", text)]), style=text_style, **kwargs)
Example #10
0
def test_print_formatted_text():
    f = _Capture()
    pt_print([('', 'hello'), ('', 'world')], file=f)
    assert b'hello' in f.data
    assert b'world' in f.data
def test_print_formatted_text_backslash_r():
    f = _Capture()
    pt_print("hello\r\n", file=f)
    assert b"hello" in f.data
def test_print_formatted_text():
    f = _Capture()
    pt_print([("", "hello"), ("", "world")], file=f)
    assert b"hello" in f.data
    assert b"world" in f.data
def test_print_formatted_text():
    f = _Capture()
    pt_print([('', 'hello'), ('', 'world')], file=f)
    assert b'hello' in f.data
    assert b'world' in f.data
def test_print_formatted_text_backslash_r():
    f = _Capture()
    pt_print('hello\r\n', file=f)
    assert b'hello' in f.data