-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtiny_actor_grid.py
More file actions
67 lines (53 loc) · 1.93 KB
/
Copy pathtiny_actor_grid.py
File metadata and controls
67 lines (53 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
"""Responsive Tiny Actor Grid layout engine with column fallback (#1268).
Accepts pre-rendered card lines and assembles them into a responsive grid
that adapts to the available terminal width.
"""
from __future__ import annotations
from itertools import zip_longest
from buddy_renderer import display_width, pad_to_display_width
def layout_grid(
cards: list[list[str]],
*,
available_width: int = 80,
card_width: int = 14,
gap: int = 2,
) -> list[str]:
"""Arrange pre-rendered cards into a responsive grid.
Parameters
----------
cards:
Each card is a list of strings (one per visual line).
available_width:
Maximum terminal columns for the output.
card_width:
Display width of a single card.
gap:
Number of space characters between adjacent columns.
Returns
-------
list[str]
Assembled lines ready for terminal output.
"""
if not cards:
return []
max_columns = max(1, (available_width + gap) // (card_width + gap))
rows: list[list[list[str]]] = []
for i in range(0, len(cards), max_columns):
rows.append(cards[i : i + max_columns])
separator = " " * gap
row_width = max_columns * card_width + (max_columns - 1) * gap
output: list[str] = []
for row in rows:
tallest = max(len(card) for card in row)
padded_cards: list[list[str]] = []
for card in row:
padded = list(card) + [" " * card_width] * (tallest - len(card))
padded_cards.append(padded)
for line_parts in zip_longest(*padded_cards, fillvalue=" " * card_width):
parts = [pad_to_display_width(part, card_width) for part in line_parts]
# Pad row to full width when fewer cards than max_columns
while len(parts) < max_columns:
parts.append(" " * card_width)
line = separator.join(parts)
output.append(line)
return output