|
| 1 | +"""TextFrame - ASCII terminal frame simulator. |
| 2 | +
|
| 3 | +This module provides a fixed-size ASCII frame for visualizing terminal content |
| 4 | +with overflow detection and diagnostic rendering. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import typing as t |
| 10 | +from dataclasses import dataclass, field |
| 11 | + |
| 12 | +if t.TYPE_CHECKING: |
| 13 | + from collections.abc import Sequence |
| 14 | + |
| 15 | + |
| 16 | +class ContentOverflowError(ValueError): |
| 17 | + """Raised when content does not fit into the configured frame dimensions. |
| 18 | +
|
| 19 | + Attributes |
| 20 | + ---------- |
| 21 | + overflow_visual : str |
| 22 | + A diagnostic ASCII visualization showing the content and a mask |
| 23 | + of the valid/invalid areas. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__(self, message: str, overflow_visual: str) -> None: |
| 27 | + super().__init__(message) |
| 28 | + self.overflow_visual = overflow_visual |
| 29 | + |
| 30 | + |
| 31 | +@dataclass(slots=True) |
| 32 | +class TextFrame: |
| 33 | + """A fixed-size ASCII terminal frame simulator. |
| 34 | +
|
| 35 | + Attributes |
| 36 | + ---------- |
| 37 | + content_width : int |
| 38 | + Width of the inner content area. |
| 39 | + content_height : int |
| 40 | + Height of the inner content area. |
| 41 | + fill_char : str |
| 42 | + Character to pad empty space. Defaults to space. |
| 43 | + content : list[str] |
| 44 | + The current content lines. |
| 45 | +
|
| 46 | + Examples |
| 47 | + -------- |
| 48 | + >>> frame = TextFrame(content_width=10, content_height=2) |
| 49 | + >>> frame.set_content(["hello", "world"]) |
| 50 | + >>> print(frame.render()) |
| 51 | + +----------+ |
| 52 | + |hello | |
| 53 | + |world | |
| 54 | + +----------+ |
| 55 | + """ |
| 56 | + |
| 57 | + content_width: int |
| 58 | + content_height: int |
| 59 | + fill_char: str = " " |
| 60 | + content: list[str] = field(default_factory=list) |
| 61 | + |
| 62 | + def set_content(self, lines: Sequence[str]) -> None: |
| 63 | + """Set content, applying validation logic. |
| 64 | +
|
| 65 | + Parameters |
| 66 | + ---------- |
| 67 | + lines : Sequence[str] |
| 68 | + Lines of content to set. |
| 69 | +
|
| 70 | + Raises |
| 71 | + ------ |
| 72 | + ContentOverflowError |
| 73 | + If content exceeds frame dimensions. |
| 74 | + """ |
| 75 | + input_lines = list(lines) |
| 76 | + |
| 77 | + # Calculate dimensions |
| 78 | + max_w = max((len(line) for line in input_lines), default=0) |
| 79 | + max_h = len(input_lines) |
| 80 | + |
| 81 | + is_overflow = max_w > self.content_width or max_h > self.content_height |
| 82 | + |
| 83 | + if is_overflow: |
| 84 | + visual = self._render_overflow(input_lines, max_w, max_h) |
| 85 | + raise ContentOverflowError( |
| 86 | + f"Content ({max_w}x{max_h}) exceeds frame " |
| 87 | + f"({self.content_width}x{self.content_height})", |
| 88 | + overflow_visual=visual, |
| 89 | + ) |
| 90 | + |
| 91 | + self.content = input_lines |
| 92 | + |
| 93 | + def render(self) -> str: |
| 94 | + """Render the frame as ASCII art. |
| 95 | +
|
| 96 | + Returns |
| 97 | + ------- |
| 98 | + str |
| 99 | + The rendered frame with borders. |
| 100 | + """ |
| 101 | + return self._draw_frame(self.content, self.content_width, self.content_height) |
| 102 | + |
| 103 | + def _render_overflow(self, lines: list[str], max_w: int, max_h: int) -> str: |
| 104 | + """Render the diagnostic overflow view (Reality vs Mask). |
| 105 | +
|
| 106 | + Parameters |
| 107 | + ---------- |
| 108 | + lines : list[str] |
| 109 | + The overflow content lines. |
| 110 | + max_w : int |
| 111 | + Maximum width of content. |
| 112 | + max_h : int |
| 113 | + Maximum height of content. |
| 114 | +
|
| 115 | + Returns |
| 116 | + ------- |
| 117 | + str |
| 118 | + A visualization showing content frame and valid/invalid mask. |
| 119 | + """ |
| 120 | + display_w = max(self.content_width, max_w) |
| 121 | + display_h = max(self.content_height, max_h) |
| 122 | + |
| 123 | + # 1. Reality Frame - shows actual content |
| 124 | + reality = self._draw_frame(lines, display_w, display_h) |
| 125 | + |
| 126 | + # 2. Mask Frame - shows valid vs invalid areas |
| 127 | + mask_lines = [] |
| 128 | + for r in range(display_h): |
| 129 | + row = [] |
| 130 | + for c in range(display_w): |
| 131 | + is_valid = r < self.content_height and c < self.content_width |
| 132 | + row.append(" " if is_valid else ".") |
| 133 | + mask_lines.append("".join(row)) |
| 134 | + |
| 135 | + mask = self._draw_frame(mask_lines, display_w, display_h) |
| 136 | + return f"{reality}\n{mask}" |
| 137 | + |
| 138 | + def _draw_frame(self, lines: list[str], w: int, h: int) -> str: |
| 139 | + """Draw a bordered frame around content. |
| 140 | +
|
| 141 | + Parameters |
| 142 | + ---------- |
| 143 | + lines : list[str] |
| 144 | + Content lines to frame. |
| 145 | + w : int |
| 146 | + Frame width (excluding borders). |
| 147 | + h : int |
| 148 | + Frame height (excluding borders). |
| 149 | +
|
| 150 | + Returns |
| 151 | + ------- |
| 152 | + str |
| 153 | + Bordered ASCII frame. |
| 154 | + """ |
| 155 | + border = f"+{'-' * w}+" |
| 156 | + body = [] |
| 157 | + for r in range(h): |
| 158 | + line = lines[r] if r < len(lines) else "" |
| 159 | + body.append(f"|{line.ljust(w, self.fill_char)}|") |
| 160 | + return "\n".join([border, *body, border]) |
0 commit comments