-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox_printer.py
283 lines (219 loc) · 7.39 KB
/
box_printer.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from __future__ import annotations
import dataclasses
import enum
from contextlib import contextmanager
from dataclasses import dataclass
from functools import singledispatchmethod
from typing import Generator, Iterable, List, Optional, Protocol, Sequence, Tuple
RenderedLine = Tuple[int, str]
RenderedBox = List[RenderedLine]
@dataclass
class Box:
pass
@dataclass
class Alt(Box):
"""
Alternation: either left or right, with bias to left.
"""
left: Box
right: Box
class DecoratorCallback(Protocol):
def __call__(
self, printer: BoxPrinter
) -> Generator[None, RenderedBox, RenderedBox]:
pass
@dataclass
class Decorated(Box):
decorator: DecoratorCallback
box: Box
@dataclass
class Empty(Box):
"""
A box without any content.
"""
@dataclass
class Column(Box):
"""
A column of boxes.
"""
boxes: Sequence[Box]
@dataclass
class Row(Box):
"""
A row of boxes.
"""
boxes: Sequence[Box]
@dataclass
class HSpace(Box):
"""
Horizontal space. Ignored in vertical direction.
"""
space: str
@dataclass
class VSpace(Box):
"""
Vertical space. Ignored in horizontal direction.
"""
space: str
@dataclass
class Text(Box):
"""
Text fragment, without newlines.
"""
text: str
length: Optional[int] = None
def __post_init__(self):
if self.length is None:
self.length = len(self.text)
def _horizontal_concat(boxes: Iterable[RenderedBox]) -> RenderedBox:
(rows, cols) = max_sum((len(lines), lines[0][0]) for lines in boxes if lines)
boxes = [_resize_box(box, rows, box[0][0] if box else 0) for box in boxes]
join_line = lambda parts: (sum(x[0] for x in parts), "".join(x[1] for x in parts))
return [join_line(parts) for parts in zip(*boxes)]
def _vertical_concat(boxes: Iterable[RenderedBox]) -> RenderedBox:
lines = []
for box in boxes:
lines.extend(box)
columns = max(line[0] for line in lines) if lines else 0
return [(columns, line + " " * (columns - length)) for (length, line) in lines]
class Direction(enum.Enum):
HORIZONTAL = _horizontal_concat
VERTICAL = _vertical_concat
class BoxPrinter:
@dataclass
class _State:
max_width: int
direction: Direction
boxes: List[RenderedBox] = dataclasses.field(default_factory=lambda: [[]])
@property
def current_box(self) -> RenderedBox:
return self.boxes[-1]
def push_box(self):
self.boxes.append([])
def pop_box(self):
box = self.boxes.pop()
merged_box = self.direction([self.current_box, box])
if merged_box and merged_box[0][0] > self.max_width:
raise OverflowError()
self.boxes[-1] = merged_box
def __init__(self, box: Box, max_width: int):
self._box = box
self._state = self._State(max_width, Direction.HORIZONTAL)
def render(self) -> str:
self._render_box(self._box)
assert len(self._state.boxes) == 1
return "\n".join(line[1] for line in self._state.current_box)
@singledispatchmethod
def _render_box(self, box: Box):
raise ValueError(f"Not a box: {box!r}")
@_render_box.register
def _render_empty(self, empty: Empty):
pass
@_render_box.register
def _render_text(self, text: Text):
with self._new_box():
assert text.length is not None
self._state.boxes[-1] = [(text.length, text.text)]
@_render_box.register
def _render_alt(self, alt: Alt):
try:
with self._snapshot():
self._render_box(alt.left)
except OverflowError:
self._render_box(alt.right)
@_render_box.register
def _render_decorated(self, decorated: Decorated):
with self._new_box():
decorator = decorated.decorator(self)
next(decorator)
self._render_box(decorated.box)
try:
decorator.send(self._state.current_box)
except StopIteration as e:
self._state.boxes[-1] = e.value
else:
raise RuntimeError(
f"Decorator for {decorated} didn't throw expected "
"StopIteration exception with result"
)
@_render_box.register
def _render_column(self, column: Column):
with self._new_box():
with self._override("direction", Direction.VERTICAL):
for box in column.boxes:
self._render_box(box)
@_render_box.register
def _render_row(self, row: Row):
with self._new_box():
with self._override("direction", Direction.HORIZONTAL):
for box in row.boxes:
self._render_box(box)
@_render_box.register
def _render_hspace(self, hspace: HSpace):
if self._state.direction is Direction.HORIZONTAL:
self._render_box(Text(hspace.space))
@_render_box.register
def _render_vspace(self, vspace: VSpace):
if self._state.direction is Direction.VERTICAL:
self._render_box(Text(vspace.space))
@contextmanager
def _snapshot(self):
saved_state = self._state
# XXX not a deep copy
self._state = dataclasses.replace(saved_state, boxes=list(saved_state.boxes))
try:
yield
except Exception:
self._state = saved_state
raise
@contextmanager
def _override(self, name, value):
saved_value = getattr(self._state, name)
try:
setattr(self._state, name, value)
yield
finally:
setattr(self._state, name, saved_value)
@contextmanager
def _new_box(self):
self._state.push_box()
try:
yield
finally:
self._state.pop_box()
def _resize_box(box: RenderedBox, rows: int, cols: int) -> RenderedBox:
"""
Resize the given box, represented by a list of rendered lines, to the given size.
"""
result = []
for (length, row) in box:
result.append((cols, row + " " * (cols - length)))
for _ in range(rows - len(result)):
result.append((cols, " " * cols))
return result
def max_sum(iterable: Iterable[Tuple[int, int]]) -> Tuple[int, int]:
"""
max() and sum(), but in a single pass.
"""
current_max = 0
current_sum = 0
for (max_value, sum_value) in iterable:
current_max = max(current_max, max_value)
current_sum += sum_value
return (current_max, current_sum)
def group(boxes: Sequence[Box]) -> Box:
return Alt(Row(boxes), Column(boxes))
def frame(title: Optional[str], box: Box) -> Box:
def add_frame(printer: BoxPrinter) -> Generator[None, RenderedBox, RenderedBox]:
with printer._override("max_width", printer._state.max_width - 4):
lines = yield
columns = lines[0][0] if lines else 0
spaced_title = " " + title + " " if title else "──"
if len(spaced_title) > columns:
spaced_title = spaced_title[: max(columns - 1, 0)] + "…"
result = [(columns + 4, "╭─" + spaced_title.ljust(columns + 1, "─") + "╮")]
for line in lines:
result.append((columns + 4, "│ " + line[1] + " │"))
result.append((columns + 4, "╰" + "─" * (columns + 2) + "╯"))
return result
return Decorated(add_frame, box)