Skip to content

Instantly share code, notes, and snippets.

@crowsonkb
Created January 6, 2019 05:50
Show Gist options
  • Save crowsonkb/91dd5f3f1c138c17822e2f1f7b898eec to your computer and use it in GitHub Desktop.
Save crowsonkb/91dd5f3f1c138c17822e2f1f7b898eec to your computer and use it in GitHub Desktop.
The prompt-toolkit utility function format_fragments().
"""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