Coverage for src/prints/boxes.py: 100.00%
34 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-10-18 15:02 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-10-18 15:02 +0000
1#!/usr/bin/env python3
3# Standard libraries
4from shutil import get_terminal_size
5from typing import List
7# Components
8from ..system.platform import Platform
9from ..types.strings import Strings
10from .colors import Colors
12# Boxes class
13class Boxes:
15 # Constants
16 __TOP_LEFT: str = '╭' if Platform.IS_TTY_UTF8 else '-'
17 __TOP_LINE: str = '─' if Platform.IS_TTY_UTF8 else '-'
18 __TOP_RIGHT: str = '╮' if Platform.IS_TTY_UTF8 else '-'
19 __MIDDLE_LEFT: str = '│' if Platform.IS_TTY_UTF8 else '|'
20 __MIDDLE_RIGHT: str = '│' if Platform.IS_TTY_UTF8 else '|'
21 __BOTTOM_LEFT: str = '╰' if Platform.IS_TTY_UTF8 else '-'
22 __BOTTOM_LINE: str = '─' if Platform.IS_TTY_UTF8 else '-'
23 __BOTTOM_RIGHT: str = '╯' if Platform.IS_TTY_UTF8 else '-'
24 __OFFSET_LINE: int = 2
25 __PADDING_LINE: int = 2
27 # Members
28 __lines: List[str]
30 # Constructor
31 def __init__(self) -> None:
33 # Initialize members
34 self.__lines = []
36 # Adder
37 def add(self, line: str) -> None:
39 # Add line
40 self.__lines += [line]
42 # Printer
43 def print(self) -> None:
45 # Evaluate lines length
46 length = max(len(Colors.strip(line)) for line in self.__lines)
48 # Acquire terminal width
49 columns, _ = get_terminal_size()
51 # Limit line length
52 limit = columns - Boxes.__OFFSET_LINE - len(
53 Boxes.__MIDDLE_LEFT) - 2 * Boxes.__PADDING_LINE - len(Boxes.__MIDDLE_RIGHT)
54 limit = max(limit, 1)
55 length = min(length, limit)
57 # Header
58 print(' ')
60 # Print header line
61 print(f"{' ' * Boxes.__OFFSET_LINE}{Colors.YELLOW}{Boxes.__TOP_LEFT}" \
62 f'{Boxes.__TOP_LINE * (length + 2 * Boxes.__PADDING_LINE)}{Boxes.__TOP_RIGHT}')
64 # Print content lines
65 for line in self.__lines:
66 for part in Strings.wrap(line, length=length):
67 print(
68 f"{' ' * Boxes.__OFFSET_LINE}{Colors.YELLOW}{Boxes.__MIDDLE_LEFT}" \
69 f"{' ' * Boxes.__PADDING_LINE}{Strings.center(part, length)}" \
70 f"{' ' * Boxes.__PADDING_LINE}{Colors.YELLOW}{Boxes.__MIDDLE_RIGHT}"
71 )
73 # Print bottom line
74 print(
75 f"{' ' * Boxes.__OFFSET_LINE}{Colors.YELLOW}{Boxes.__BOTTOM_LEFT}" \
76 f'{Boxes.__BOTTOM_LINE * (length + 2 * Boxes.__PADDING_LINE)}' \
77 f'{Boxes.__BOTTOM_RIGHT}{Colors.RESET}'
78 )
80 # Footer
81 print(' ')
82 Platform.flush()