Created
January 6, 2019 05:50
-
-
Save crowsonkb/91dd5f3f1c138c17822e2f1f7b898eec to your computer and use it in GitHub Desktop.
The prompt-toolkit utility function format_fragments().
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
"""The prompt-toolkit utility function :func:`format_fragments`.""" | |
from prompt_toolkit.formatted_text import FormattedText | |
def format_fragments(fragments, *args, **kwargs): | |
"""Applies :meth:`str.format` to each text fragment in `fragments`. | |
Args: | |
fragments (FormattedText): The input list of text fragments. | |
*args: The positional arguments to supply to :meth:`str.format`. | |
**kwargs: The keyword arguments to supply to :meth:`str.format`. | |
Returns: | |
FormattedText: The resulting list of text fragments. | |
""" | |
out = FormattedText() | |
for style, text, *rest in fragments: | |
out.append((style, text.format(*args, **kwargs), *rest)) | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment