Skip to content

Instantly share code, notes, and snippets.

@tsibley
Created September 7, 2022 22:18
Show Gist options
  • Select an option

  • Save tsibley/a01a7b4eee58554447a1f72f639d8d9c to your computer and use it in GitHub Desktop.

Select an option

Save tsibley/a01a7b4eee58554447a1f72f639d8d9c to your computer and use it in GitHub Desktop.
import itertools
from rich.console import Console as RichConsole
from rich.text import Text as RichText
from typing import Union
flatten = itertools.chain.from_iterable
def heading(console: RichConsole, title: str) -> Union[str, RichText]:
"""
XXX FIXME
"""
left_edge, right_edge = RichText(" ║", "reverse"), RichText("║ ", "reverse")
# Wrap title into lines if necessary, leaving minimal space required for
# our decorations.
min_text_width = 20
min_decoration_width = len(f"{left_edge} {title} {right_edge}") - len(title)
text_width = console.width - min_decoration_width
# Console's not wide enough for our decorations, just skip 'em.
if text_width < min_text_width:
return title
# Let's spruce up things around here. Start by wrapping into lines since
# our decoration involves surrounding the title and its appearance suffers
# when the decoration itself wraps.
lines = RichText(title).wrap(console, text_width)
# Pad out each wrapped line to the same width so the right edge decorations
# are aligned.
for line in lines:
line.rstrip()
max_line_width = max(map(len, lines))
for line in lines:
line.pad_right(max_line_width - len(line))
# Pad out right edge decoration, up to a point.
right_edge.pad_right(min(60, console.width - min_decoration_width - max_line_width))
# Assemble decorated lines
return RichText.assemble(
*flatten(
(left_edge, " ", line, " ", right_edge, "\n")
for line in lines))
if __name__ == "__main__":
cons = RichConsole(markup = False)
cons.print(heading(cons, "Checking for newer Nextstrain CLI version…"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment