-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtiny_actor_renderer.py
More file actions
68 lines (54 loc) · 1.93 KB
/
Copy pathtiny_actor_renderer.py
File metadata and controls
68 lines (54 loc) · 1.93 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Width-safe Tiny Actor card renderer (#1269).
Renders a compact 2-3 line card (face, label, optional quote) using
display-width-safe helpers from ``buddy_renderer``. Every output line
is guaranteed to have exactly ``card_width`` display columns.
"""
from __future__ import annotations
from typing import List
from tiny_actor_card import TinyActorCard
from buddy_renderer import (
display_width,
pad_to_display_width,
truncate_to_display_width,
)
def _center_to_width(text: str, width: int) -> str:
"""Center *text* within *width* display columns.
Left-pads with spaces so the visible content sits roughly in the
middle, then right-pads to exactly *width*.
"""
text_w = display_width(text)
if text_w >= width:
return pad_to_display_width(truncate_to_display_width(text, width), width)
left_pad = (width - text_w) // 2
return pad_to_display_width((" " * left_pad) + text, width)
def render_card(
card: TinyActorCard,
*,
card_width: int = 14,
show_quote: bool = True,
) -> List[str]:
"""Render a :class:`TinyActorCard` as a list of display-width-safe lines.
Parameters
----------
card:
The actor card to render.
card_width:
Target display width for every line (default 14).
show_quote:
When ``False``, the quote line is always omitted even if the
card has a quote.
Returns
-------
list[str]
2 or 3 lines, each exactly *card_width* display columns wide.
"""
lines: List[str] = []
# Line 1: face (centered)
lines.append(_center_to_width(card.face, card_width))
# Line 2: label (centered, truncated if too wide)
lines.append(_center_to_width(card.label, card_width))
# Line 3 (optional): quote
if show_quote and card.quote is not None:
truncated = truncate_to_display_width(card.quote, card_width)
lines.append(pad_to_display_width(truncated, card_width))
return lines