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
|
from __future__ import annotations
from blessed import Terminal
def boxed(
term: Terminal,
content: str,
*,
border: bool = True,
border_color: str = "white",
center: bool = False,
width: int | None = None,
) -> str:
if border:
border_width = term.length(content) + 2
border_formatter = getattr(term, border_color)
lines = [
border_formatter("┌" + "─" * border_width + "┐"),
" ".join(
[border_formatter("│") + term.normal, content, border_formatter("│")]
),
border_formatter("└" + "─" * border_width + "┘") + term.normal,
]
else:
# border is disabled in UI tests.
lines = ["", content, ""]
if center:
if width is None:
width = term.width
lines = [term.center(line, width=width) for line in lines]
return "\n".join(lines)
|