File: widgets.py

package info (click to toggle)
pg-activity 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,144 kB
  • sloc: python: 3,902; sql: 1,067; sh: 5; makefile: 2
file content (32 lines) | stat: -rw-r--r-- 923 bytes parent folder | download | duplicates (2)
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)