urwid 2.6.0.post0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of urwid might be problematic. Click here for more details.
- urwid/__init__.py +333 -0
- urwid/canvas.py +1413 -0
- urwid/command_map.py +137 -0
- urwid/container.py +59 -0
- urwid/decoration.py +65 -0
- urwid/display/__init__.py +97 -0
- urwid/display/_posix_raw_display.py +413 -0
- urwid/display/_raw_display_base.py +914 -0
- urwid/display/_web.css +12 -0
- urwid/display/_web.js +462 -0
- urwid/display/_win32.py +171 -0
- urwid/display/_win32_raw_display.py +269 -0
- urwid/display/common.py +1219 -0
- urwid/display/curses.py +690 -0
- urwid/display/escape.py +624 -0
- urwid/display/html_fragment.py +251 -0
- urwid/display/lcd.py +518 -0
- urwid/display/raw.py +37 -0
- urwid/display/web.py +636 -0
- urwid/event_loop/__init__.py +55 -0
- urwid/event_loop/abstract_loop.py +175 -0
- urwid/event_loop/asyncio_loop.py +231 -0
- urwid/event_loop/glib_loop.py +294 -0
- urwid/event_loop/main_loop.py +721 -0
- urwid/event_loop/select_loop.py +230 -0
- urwid/event_loop/tornado_loop.py +206 -0
- urwid/event_loop/trio_loop.py +302 -0
- urwid/event_loop/twisted_loop.py +269 -0
- urwid/event_loop/zmq_loop.py +275 -0
- urwid/font.py +695 -0
- urwid/graphics.py +96 -0
- urwid/highlight.css +19 -0
- urwid/listbox.py +1899 -0
- urwid/monitored_list.py +522 -0
- urwid/numedit.py +376 -0
- urwid/signals.py +330 -0
- urwid/split_repr.py +130 -0
- urwid/str_util.py +358 -0
- urwid/text_layout.py +632 -0
- urwid/treetools.py +515 -0
- urwid/util.py +557 -0
- urwid/version.py +16 -0
- urwid/vterm.py +1806 -0
- urwid/widget/__init__.py +181 -0
- urwid/widget/attr_map.py +161 -0
- urwid/widget/attr_wrap.py +140 -0
- urwid/widget/bar_graph.py +649 -0
- urwid/widget/big_text.py +77 -0
- urwid/widget/box_adapter.py +126 -0
- urwid/widget/columns.py +1145 -0
- urwid/widget/constants.py +574 -0
- urwid/widget/container.py +227 -0
- urwid/widget/divider.py +110 -0
- urwid/widget/edit.py +718 -0
- urwid/widget/filler.py +403 -0
- urwid/widget/frame.py +539 -0
- urwid/widget/grid_flow.py +539 -0
- urwid/widget/line_box.py +194 -0
- urwid/widget/overlay.py +829 -0
- urwid/widget/padding.py +597 -0
- urwid/widget/pile.py +971 -0
- urwid/widget/popup.py +170 -0
- urwid/widget/progress_bar.py +141 -0
- urwid/widget/scrollable.py +597 -0
- urwid/widget/solid_fill.py +44 -0
- urwid/widget/text.py +354 -0
- urwid/widget/widget.py +852 -0
- urwid/widget/widget_decoration.py +166 -0
- urwid/widget/wimp.py +792 -0
- urwid/wimp.py +23 -0
- urwid-2.6.0.post0.dist-info/COPYING +504 -0
- urwid-2.6.0.post0.dist-info/METADATA +332 -0
- urwid-2.6.0.post0.dist-info/RECORD +75 -0
- urwid-2.6.0.post0.dist-info/WHEEL +5 -0
- urwid-2.6.0.post0.dist-info/top_level.txt +1 -0
urwid/canvas.py
ADDED
|
@@ -0,0 +1,1413 @@
|
|
|
1
|
+
# Urwid canvas class and functions
|
|
2
|
+
# Copyright (C) 2004-2011 Ian Ward
|
|
3
|
+
#
|
|
4
|
+
# This library is free software; you can redistribute it and/or
|
|
5
|
+
# modify it under the terms of the GNU Lesser General Public
|
|
6
|
+
# License as published by the Free Software Foundation; either
|
|
7
|
+
# version 2.1 of the License, or (at your option) any later version.
|
|
8
|
+
#
|
|
9
|
+
# This library is distributed in the hope that it will be useful,
|
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+
# Lesser General Public License for more details.
|
|
13
|
+
#
|
|
14
|
+
# You should have received a copy of the GNU Lesser General Public
|
|
15
|
+
# License along with this library; if not, write to the Free Software
|
|
16
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
17
|
+
#
|
|
18
|
+
# Urwid web site: https://urwid.org/
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import contextlib
|
|
24
|
+
import dataclasses
|
|
25
|
+
import typing
|
|
26
|
+
import warnings
|
|
27
|
+
import weakref
|
|
28
|
+
from contextlib import suppress
|
|
29
|
+
|
|
30
|
+
from urwid.str_util import calc_text_pos, calc_width
|
|
31
|
+
from urwid.text_layout import LayoutSegment, trim_line
|
|
32
|
+
from urwid.util import (
|
|
33
|
+
apply_target_encoding,
|
|
34
|
+
get_encoding,
|
|
35
|
+
rle_append_modify,
|
|
36
|
+
rle_join_modify,
|
|
37
|
+
rle_len,
|
|
38
|
+
rle_product,
|
|
39
|
+
trim_text_attr_cs,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
if typing.TYPE_CHECKING:
|
|
43
|
+
from collections.abc import Hashable, Iterable, Sequence
|
|
44
|
+
|
|
45
|
+
from typing_extensions import Literal
|
|
46
|
+
|
|
47
|
+
from .widget import Widget
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class CanvasCache:
|
|
51
|
+
"""
|
|
52
|
+
Cache for rendered canvases. Automatically populated and
|
|
53
|
+
accessed by Widget render() MetaClass magic, cleared by
|
|
54
|
+
Widget._invalidate().
|
|
55
|
+
|
|
56
|
+
Stores weakrefs to the canvas objects, so an external class
|
|
57
|
+
must maintain a reference for this cache to be effective.
|
|
58
|
+
At present the Screen classes store the last topmost canvas
|
|
59
|
+
after redrawing the screen, keeping the canvases from being
|
|
60
|
+
garbage collected.
|
|
61
|
+
|
|
62
|
+
_widgets[widget] = {(wcls, size, focus): weakref.ref(canvas), ...}
|
|
63
|
+
_refs[weakref.ref(canvas)] = (widget, wcls, size, focus)
|
|
64
|
+
_deps[widget} = [dependent_widget, ...]
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
_widgets: typing.ClassVar[
|
|
68
|
+
dict[
|
|
69
|
+
Widget,
|
|
70
|
+
dict[
|
|
71
|
+
tuple[type[Widget], tuple[int, int] | tuple[int] | tuple[()], bool],
|
|
72
|
+
weakref.ReferenceType,
|
|
73
|
+
],
|
|
74
|
+
]
|
|
75
|
+
] = {}
|
|
76
|
+
_refs: typing.ClassVar[
|
|
77
|
+
dict[
|
|
78
|
+
weakref.ReferenceType,
|
|
79
|
+
tuple[Widget, type[Widget], tuple[int, int] | tuple[int] | tuple[()], bool],
|
|
80
|
+
]
|
|
81
|
+
] = {}
|
|
82
|
+
_deps: typing.ClassVar[dict[Widget, list[Widget]]] = {}
|
|
83
|
+
hits = 0
|
|
84
|
+
fetches = 0
|
|
85
|
+
cleanups = 0
|
|
86
|
+
|
|
87
|
+
@classmethod
|
|
88
|
+
def store(cls, wcls, canvas: Canvas) -> None:
|
|
89
|
+
"""
|
|
90
|
+
Store a weakref to canvas in the cache.
|
|
91
|
+
|
|
92
|
+
wcls -- widget class that contains render() function
|
|
93
|
+
canvas -- rendered canvas with widget_info (widget, size, focus)
|
|
94
|
+
"""
|
|
95
|
+
if not canvas.cacheable:
|
|
96
|
+
return
|
|
97
|
+
|
|
98
|
+
if not canvas.widget_info:
|
|
99
|
+
raise TypeError("Can't store canvas without widget_info")
|
|
100
|
+
widget, size, focus = canvas.widget_info
|
|
101
|
+
|
|
102
|
+
def walk_depends(canv):
|
|
103
|
+
"""
|
|
104
|
+
Collect all child widgets for determining who we
|
|
105
|
+
depend on.
|
|
106
|
+
"""
|
|
107
|
+
# FIXME: is this recursion necessary? The cache invalidating might work with only one level.
|
|
108
|
+
depends = []
|
|
109
|
+
for _x, _y, c, _pos in canv.children:
|
|
110
|
+
if c.widget_info:
|
|
111
|
+
depends.append(c.widget_info[0])
|
|
112
|
+
elif hasattr(c, "children"):
|
|
113
|
+
depends.extend(walk_depends(c))
|
|
114
|
+
return depends
|
|
115
|
+
|
|
116
|
+
# use explicit depends_on if available from the canvas
|
|
117
|
+
depends_on = getattr(canvas, "depends_on", None)
|
|
118
|
+
if depends_on is None and hasattr(canvas, "children"):
|
|
119
|
+
depends_on = walk_depends(canvas)
|
|
120
|
+
if depends_on:
|
|
121
|
+
for w in depends_on:
|
|
122
|
+
if w not in cls._widgets:
|
|
123
|
+
return
|
|
124
|
+
for w in depends_on:
|
|
125
|
+
cls._deps.setdefault(w, []).append(widget)
|
|
126
|
+
|
|
127
|
+
ref = weakref.ref(canvas, cls.cleanup)
|
|
128
|
+
cls._refs[ref] = (widget, wcls, size, focus)
|
|
129
|
+
cls._widgets.setdefault(widget, {})[(wcls, size, focus)] = ref
|
|
130
|
+
|
|
131
|
+
@classmethod
|
|
132
|
+
def fetch(cls, widget, wcls, size, focus) -> Canvas | None:
|
|
133
|
+
"""
|
|
134
|
+
Return the cached canvas or None.
|
|
135
|
+
|
|
136
|
+
widget -- widget object requested
|
|
137
|
+
wcls -- widget class that contains render() function
|
|
138
|
+
size, focus -- render() parameters
|
|
139
|
+
"""
|
|
140
|
+
cls.fetches += 1 # collect stats
|
|
141
|
+
|
|
142
|
+
sizes = cls._widgets.get(widget, None)
|
|
143
|
+
if not sizes:
|
|
144
|
+
return None
|
|
145
|
+
ref = sizes.get((wcls, size, focus), None)
|
|
146
|
+
if not ref:
|
|
147
|
+
return None
|
|
148
|
+
canv = ref()
|
|
149
|
+
if canv:
|
|
150
|
+
cls.hits += 1 # more stats
|
|
151
|
+
return canv
|
|
152
|
+
|
|
153
|
+
@classmethod
|
|
154
|
+
def invalidate(cls, widget):
|
|
155
|
+
"""
|
|
156
|
+
Remove all canvases cached for widget.
|
|
157
|
+
"""
|
|
158
|
+
with contextlib.suppress(KeyError):
|
|
159
|
+
for ref in cls._widgets[widget].values():
|
|
160
|
+
with suppress(KeyError):
|
|
161
|
+
del cls._refs[ref]
|
|
162
|
+
del cls._widgets[widget]
|
|
163
|
+
|
|
164
|
+
if widget not in cls._deps:
|
|
165
|
+
return
|
|
166
|
+
dependants = cls._deps.get(widget, [])
|
|
167
|
+
with suppress(KeyError):
|
|
168
|
+
del cls._deps[widget]
|
|
169
|
+
for w in dependants:
|
|
170
|
+
cls.invalidate(w)
|
|
171
|
+
|
|
172
|
+
@classmethod
|
|
173
|
+
def cleanup(cls, ref: weakref.ReferenceType) -> None:
|
|
174
|
+
cls.cleanups += 1 # collect stats
|
|
175
|
+
|
|
176
|
+
w = cls._refs.get(ref, None)
|
|
177
|
+
del cls._refs[ref]
|
|
178
|
+
if not w:
|
|
179
|
+
return
|
|
180
|
+
widget, wcls, size, focus = w
|
|
181
|
+
sizes = cls._widgets.get(widget, None)
|
|
182
|
+
if not sizes:
|
|
183
|
+
return
|
|
184
|
+
with suppress(KeyError):
|
|
185
|
+
del sizes[(wcls, size, focus)]
|
|
186
|
+
if not sizes:
|
|
187
|
+
with contextlib.suppress(KeyError):
|
|
188
|
+
del cls._widgets[widget]
|
|
189
|
+
del cls._deps[widget]
|
|
190
|
+
|
|
191
|
+
@classmethod
|
|
192
|
+
def clear(cls) -> None:
|
|
193
|
+
"""
|
|
194
|
+
Empty the cache.
|
|
195
|
+
"""
|
|
196
|
+
cls._widgets = {}
|
|
197
|
+
cls._refs = {}
|
|
198
|
+
cls._deps = {}
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class CanvasError(Exception):
|
|
202
|
+
pass
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
class Canvas:
|
|
206
|
+
"""
|
|
207
|
+
base class for canvases
|
|
208
|
+
"""
|
|
209
|
+
|
|
210
|
+
cacheable = True
|
|
211
|
+
|
|
212
|
+
_finalized_error = CanvasError(
|
|
213
|
+
"This canvas has been finalized. Use CompositeCanvas to wrap this canvas if you need to make changes."
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
def __init__(self) -> None:
|
|
217
|
+
"""Base Canvas class"""
|
|
218
|
+
self._widget_info = None
|
|
219
|
+
self.coords: dict[str, tuple[int, int, tuple[Widget, int, int]]] = {}
|
|
220
|
+
self.shortcuts: dict[str, str] = {}
|
|
221
|
+
|
|
222
|
+
def finalize(
|
|
223
|
+
self,
|
|
224
|
+
widget: Widget,
|
|
225
|
+
size: tuple[()] | tuple[int] | tuple[int, int],
|
|
226
|
+
focus: bool,
|
|
227
|
+
) -> None:
|
|
228
|
+
"""
|
|
229
|
+
Mark this canvas as finalized (should not be any future
|
|
230
|
+
changes to its content). This is required before caching
|
|
231
|
+
the canvas. This happens automatically after a widget's
|
|
232
|
+
'render call returns the canvas thanks to some metaclass
|
|
233
|
+
magic.
|
|
234
|
+
|
|
235
|
+
widget -- widget that rendered this canvas
|
|
236
|
+
size -- size parameter passed to widget's render method
|
|
237
|
+
focus -- focus parameter passed to widget's render method
|
|
238
|
+
"""
|
|
239
|
+
if self.widget_info:
|
|
240
|
+
raise self._finalized_error
|
|
241
|
+
self._widget_info = widget, size, focus
|
|
242
|
+
|
|
243
|
+
@property
|
|
244
|
+
def widget_info(self):
|
|
245
|
+
return self._widget_info
|
|
246
|
+
|
|
247
|
+
def _get_widget_info(self):
|
|
248
|
+
warnings.warn(
|
|
249
|
+
f"Method `{self.__class__.__name__}._get_widget_info` is deprecated, "
|
|
250
|
+
f"please use property `{self.__class__.__name__}.widget_info`",
|
|
251
|
+
DeprecationWarning,
|
|
252
|
+
stacklevel=2,
|
|
253
|
+
)
|
|
254
|
+
return self.widget_info
|
|
255
|
+
|
|
256
|
+
@property
|
|
257
|
+
def text(self) -> list[bytes]:
|
|
258
|
+
"""
|
|
259
|
+
Return the text content of the canvas as a list of strings, one for each row.
|
|
260
|
+
"""
|
|
261
|
+
return [b"".join([text for (attr, cs, text) in row]) for row in self.content()]
|
|
262
|
+
|
|
263
|
+
@property
|
|
264
|
+
def decoded_text(self) -> Sequence[str]:
|
|
265
|
+
"""Decoded text content of the canvas as a sequence of strings, one for each row."""
|
|
266
|
+
encoding = get_encoding()
|
|
267
|
+
return tuple(line.decode(encoding) for line in self.text)
|
|
268
|
+
|
|
269
|
+
def _text_content(self):
|
|
270
|
+
warnings.warn(
|
|
271
|
+
f"Method `{self.__class__.__name__}._text_content` is deprecated, "
|
|
272
|
+
f"please use property `{self.__class__.__name__}.text`",
|
|
273
|
+
DeprecationWarning,
|
|
274
|
+
stacklevel=2,
|
|
275
|
+
)
|
|
276
|
+
return self.text
|
|
277
|
+
|
|
278
|
+
def content(
|
|
279
|
+
self,
|
|
280
|
+
trim_left: int = 0,
|
|
281
|
+
trim_top: int = 0,
|
|
282
|
+
cols: int | None = None,
|
|
283
|
+
rows: int | None = None,
|
|
284
|
+
attr=None,
|
|
285
|
+
) -> Iterable[list[tuple[object, Literal["0", "U"] | None, bytes]]]:
|
|
286
|
+
raise NotImplementedError()
|
|
287
|
+
|
|
288
|
+
def cols(self) -> int:
|
|
289
|
+
raise NotImplementedError()
|
|
290
|
+
|
|
291
|
+
def rows(self) -> int:
|
|
292
|
+
raise NotImplementedError()
|
|
293
|
+
|
|
294
|
+
def content_delta(self, other: Canvas):
|
|
295
|
+
raise NotImplementedError()
|
|
296
|
+
|
|
297
|
+
def get_cursor(self) -> tuple[int, int] | None:
|
|
298
|
+
c = self.coords.get("cursor", None)
|
|
299
|
+
if not c:
|
|
300
|
+
return None
|
|
301
|
+
return c[:2] # trim off data part
|
|
302
|
+
|
|
303
|
+
def set_cursor(self, c: tuple[int, int] | None) -> None:
|
|
304
|
+
if self.widget_info and self.cacheable:
|
|
305
|
+
raise self._finalized_error
|
|
306
|
+
if c is None:
|
|
307
|
+
with suppress(KeyError):
|
|
308
|
+
del self.coords["cursor"]
|
|
309
|
+
return
|
|
310
|
+
self.coords["cursor"] = (*c, None) # data part
|
|
311
|
+
|
|
312
|
+
cursor = property(get_cursor, set_cursor)
|
|
313
|
+
|
|
314
|
+
def get_pop_up(self) -> tuple[int, int, tuple[Widget, int, int]] | None:
|
|
315
|
+
c = self.coords.get("pop up", None)
|
|
316
|
+
if not c:
|
|
317
|
+
return None
|
|
318
|
+
return c
|
|
319
|
+
|
|
320
|
+
def set_pop_up(self, w: Widget, left: int, top: int, overlay_width: int, overlay_height: int) -> None:
|
|
321
|
+
"""
|
|
322
|
+
This method adds pop-up information to the canvas. This information
|
|
323
|
+
is intercepted by a PopUpTarget widget higher in the chain to
|
|
324
|
+
display a pop-up at the given (left, top) position relative to the
|
|
325
|
+
current canvas.
|
|
326
|
+
|
|
327
|
+
:param w: widget to use for the pop-up
|
|
328
|
+
:type w: widget
|
|
329
|
+
:param left: x position for left edge of pop-up >= 0
|
|
330
|
+
:type left: int
|
|
331
|
+
:param top: y position for top edge of pop-up >= 0
|
|
332
|
+
:type top: int
|
|
333
|
+
:param overlay_width: width of overlay in screen columns > 0
|
|
334
|
+
:type overlay_width: int
|
|
335
|
+
:param overlay_height: height of overlay in screen rows > 0
|
|
336
|
+
:type overlay_height: int
|
|
337
|
+
"""
|
|
338
|
+
if self.widget_info and self.cacheable:
|
|
339
|
+
raise self._finalized_error
|
|
340
|
+
|
|
341
|
+
self.coords["pop up"] = (left, top, (w, overlay_width, overlay_height))
|
|
342
|
+
|
|
343
|
+
def translate_coords(self, dx: int, dy: int) -> dict[str, tuple[int, int, tuple[Widget, int, int]]]:
|
|
344
|
+
"""
|
|
345
|
+
Return coords shifted by (dx, dy).
|
|
346
|
+
"""
|
|
347
|
+
d = {}
|
|
348
|
+
for name, (x, y, data) in self.coords.items():
|
|
349
|
+
d[name] = (x + dx, y + dy, data)
|
|
350
|
+
return d
|
|
351
|
+
|
|
352
|
+
def __repr__(self) -> str:
|
|
353
|
+
extra = [""]
|
|
354
|
+
with contextlib.suppress(BaseException):
|
|
355
|
+
extra.append(f"cols={self.cols()}")
|
|
356
|
+
|
|
357
|
+
with contextlib.suppress(BaseException):
|
|
358
|
+
extra.append(f"rows={self.rows()}")
|
|
359
|
+
|
|
360
|
+
if self.cursor:
|
|
361
|
+
extra.append(f"cursor={self.cursor}")
|
|
362
|
+
|
|
363
|
+
return f"<{self.__class__.__name__} finalized={bool(self.widget_info)}{' '.join(extra)} at 0x{id(self):X}>"
|
|
364
|
+
|
|
365
|
+
def __str__(self) -> str:
|
|
366
|
+
with contextlib.suppress(BaseException):
|
|
367
|
+
return "\n".join(self.decoded_text)
|
|
368
|
+
|
|
369
|
+
return repr(self)
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
class TextCanvas(Canvas):
|
|
373
|
+
"""
|
|
374
|
+
class for storing rendered text and attributes
|
|
375
|
+
"""
|
|
376
|
+
|
|
377
|
+
def __init__(
|
|
378
|
+
self,
|
|
379
|
+
text: Sequence[bytes] | None = None,
|
|
380
|
+
attr: list[list[tuple[Hashable | None, int]]] | None = None,
|
|
381
|
+
cs: list[list[tuple[Literal["0", "U"] | None, int]]] | None = None,
|
|
382
|
+
cursor: tuple[int, int] | None = None,
|
|
383
|
+
maxcol: int | None = None,
|
|
384
|
+
check_width: bool = True,
|
|
385
|
+
) -> None:
|
|
386
|
+
"""
|
|
387
|
+
text -- list of strings, one for each line
|
|
388
|
+
attr -- list of run length encoded attributes for text
|
|
389
|
+
cs -- list of run length encoded character set for text
|
|
390
|
+
cursor -- (x,y) of cursor or None
|
|
391
|
+
maxcol -- screen columns taken by this canvas
|
|
392
|
+
check_width -- check and fix width of all lines in text
|
|
393
|
+
"""
|
|
394
|
+
super().__init__()
|
|
395
|
+
if text is None:
|
|
396
|
+
text = []
|
|
397
|
+
|
|
398
|
+
if check_width:
|
|
399
|
+
widths = []
|
|
400
|
+
for t in text:
|
|
401
|
+
if not isinstance(t, bytes):
|
|
402
|
+
raise CanvasError(
|
|
403
|
+
"Canvas text must be plain strings encoded in the screen's encoding",
|
|
404
|
+
repr(text),
|
|
405
|
+
)
|
|
406
|
+
widths.append(calc_width(t, 0, len(t)))
|
|
407
|
+
else:
|
|
408
|
+
if not isinstance(maxcol, int):
|
|
409
|
+
raise TypeError(maxcol)
|
|
410
|
+
widths = [maxcol] * len(text)
|
|
411
|
+
|
|
412
|
+
if maxcol is None:
|
|
413
|
+
if widths:
|
|
414
|
+
# find maxcol ourselves
|
|
415
|
+
maxcol = max(widths)
|
|
416
|
+
else:
|
|
417
|
+
maxcol = 0
|
|
418
|
+
|
|
419
|
+
if attr is None:
|
|
420
|
+
attr = [[] for _ in range(len(text))]
|
|
421
|
+
if cs is None:
|
|
422
|
+
cs = [[] for _ in range(len(text))]
|
|
423
|
+
|
|
424
|
+
# pad text and attr to maxcol
|
|
425
|
+
for i in range(len(text)):
|
|
426
|
+
w = widths[i]
|
|
427
|
+
if w > maxcol:
|
|
428
|
+
raise CanvasError(
|
|
429
|
+
f"Canvas text is wider than the maxcol specified:\n"
|
|
430
|
+
f"maxcol={maxcol!r}\n"
|
|
431
|
+
f"widths={widths!r}\n"
|
|
432
|
+
f"text={text!r}\n"
|
|
433
|
+
f"urwid target encoding={get_encoding()}"
|
|
434
|
+
)
|
|
435
|
+
if w < maxcol:
|
|
436
|
+
text[i] += b"".rjust(maxcol - w)
|
|
437
|
+
a_gap = len(text[i]) - rle_len(attr[i])
|
|
438
|
+
if a_gap < 0:
|
|
439
|
+
raise CanvasError(f"Attribute extends beyond text \n{text[i]!r}\n{attr[i]!r}")
|
|
440
|
+
if a_gap:
|
|
441
|
+
rle_append_modify(attr[i], (None, a_gap))
|
|
442
|
+
|
|
443
|
+
cs_gap = len(text[i]) - rle_len(cs[i])
|
|
444
|
+
if cs_gap < 0:
|
|
445
|
+
raise CanvasError(f"Character Set extends beyond text \n{text[i]!r}\n{cs[i]!r}")
|
|
446
|
+
if cs_gap:
|
|
447
|
+
rle_append_modify(cs[i], (None, cs_gap))
|
|
448
|
+
|
|
449
|
+
self._attr = attr
|
|
450
|
+
self._cs = cs
|
|
451
|
+
self.cursor = cursor
|
|
452
|
+
self._text = text
|
|
453
|
+
self._maxcol = maxcol
|
|
454
|
+
|
|
455
|
+
def rows(self) -> int:
|
|
456
|
+
"""Return the number of rows in this canvas."""
|
|
457
|
+
return len(self._text)
|
|
458
|
+
|
|
459
|
+
def cols(self) -> int:
|
|
460
|
+
"""Return the screen column width of this canvas."""
|
|
461
|
+
return self._maxcol
|
|
462
|
+
|
|
463
|
+
def translated_coords(self, dx: int, dy: int) -> tuple[int, int] | None:
|
|
464
|
+
"""
|
|
465
|
+
Return cursor coords shifted by (dx, dy), or None if there
|
|
466
|
+
is no cursor.
|
|
467
|
+
"""
|
|
468
|
+
if self.cursor:
|
|
469
|
+
x, y = self.cursor
|
|
470
|
+
return x + dx, y + dy
|
|
471
|
+
return None
|
|
472
|
+
|
|
473
|
+
def content(
|
|
474
|
+
self,
|
|
475
|
+
trim_left: int = 0,
|
|
476
|
+
trim_top: int = 0,
|
|
477
|
+
cols: int | None = 0,
|
|
478
|
+
rows: int | None = 0,
|
|
479
|
+
attr=None,
|
|
480
|
+
) -> Iterable[tuple[object, Literal["0", "U"] | None, bytes]]:
|
|
481
|
+
"""
|
|
482
|
+
Return the canvas content as a list of rows where each row
|
|
483
|
+
is a list of (attr, cs, text) tuples.
|
|
484
|
+
|
|
485
|
+
trim_left, trim_top, cols, rows may be set by
|
|
486
|
+
CompositeCanvas when rendering a partially obscured
|
|
487
|
+
canvas.
|
|
488
|
+
"""
|
|
489
|
+
maxcol, maxrow = self.cols(), self.rows()
|
|
490
|
+
if not cols:
|
|
491
|
+
cols = maxcol - trim_left
|
|
492
|
+
if not rows:
|
|
493
|
+
rows = maxrow - trim_top
|
|
494
|
+
|
|
495
|
+
if not ((0 <= trim_left < maxcol) and (cols > 0 and trim_left + cols <= maxcol)):
|
|
496
|
+
raise ValueError(trim_left)
|
|
497
|
+
if not ((0 <= trim_top < maxrow) and (rows > 0 and trim_top + rows <= maxrow)):
|
|
498
|
+
raise ValueError(trim_top)
|
|
499
|
+
|
|
500
|
+
if trim_top or rows < maxrow:
|
|
501
|
+
text_attr_cs = zip(
|
|
502
|
+
self._text[trim_top : trim_top + rows],
|
|
503
|
+
self._attr[trim_top : trim_top + rows],
|
|
504
|
+
self._cs[trim_top : trim_top + rows],
|
|
505
|
+
)
|
|
506
|
+
else:
|
|
507
|
+
text_attr_cs = zip(self._text, self._attr, self._cs)
|
|
508
|
+
|
|
509
|
+
for text, a_row, cs_row in text_attr_cs:
|
|
510
|
+
if trim_left or cols < self._maxcol:
|
|
511
|
+
text, a_row, cs_row = trim_text_attr_cs( # noqa: PLW2901
|
|
512
|
+
text,
|
|
513
|
+
a_row,
|
|
514
|
+
cs_row,
|
|
515
|
+
trim_left,
|
|
516
|
+
trim_left + cols,
|
|
517
|
+
)
|
|
518
|
+
attr_cs = rle_product(a_row, cs_row)
|
|
519
|
+
i = 0
|
|
520
|
+
row = []
|
|
521
|
+
for (a, cs), run in attr_cs:
|
|
522
|
+
if attr and a in attr:
|
|
523
|
+
a = attr[a] # noqa: PLW2901
|
|
524
|
+
row.append((a, cs, text[i : i + run]))
|
|
525
|
+
i += run
|
|
526
|
+
yield row
|
|
527
|
+
|
|
528
|
+
def content_delta(self, other: Canvas):
|
|
529
|
+
"""
|
|
530
|
+
Return the differences between other and this canvas.
|
|
531
|
+
|
|
532
|
+
If other is the same object as self this will return no
|
|
533
|
+
differences, otherwise this is the same as calling
|
|
534
|
+
content().
|
|
535
|
+
"""
|
|
536
|
+
if other is self:
|
|
537
|
+
return [self.cols()] * self.rows()
|
|
538
|
+
return self.content()
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
class BlankCanvas(Canvas):
|
|
542
|
+
"""
|
|
543
|
+
a canvas with nothing on it, only works as part of a composite canvas
|
|
544
|
+
since it doesn't know its own size
|
|
545
|
+
"""
|
|
546
|
+
|
|
547
|
+
def content(
|
|
548
|
+
self,
|
|
549
|
+
trim_left: int = 0,
|
|
550
|
+
trim_top: int = 0,
|
|
551
|
+
cols: int | None = 0,
|
|
552
|
+
rows: int | None = 0,
|
|
553
|
+
attr=None,
|
|
554
|
+
) -> Iterable[list[tuple[object, Literal["0", "U"] | None, bytes]]]:
|
|
555
|
+
"""
|
|
556
|
+
return (cols, rows) of spaces with default attributes.
|
|
557
|
+
"""
|
|
558
|
+
def_attr = None
|
|
559
|
+
if attr and None in attr:
|
|
560
|
+
def_attr = attr[None]
|
|
561
|
+
line = [(def_attr, None, b"".rjust(cols))]
|
|
562
|
+
for _ in range(rows):
|
|
563
|
+
yield line
|
|
564
|
+
|
|
565
|
+
def cols(self) -> typing.NoReturn:
|
|
566
|
+
raise NotImplementedError("BlankCanvas doesn't know its own size!")
|
|
567
|
+
|
|
568
|
+
def rows(self) -> typing.NoReturn:
|
|
569
|
+
raise NotImplementedError("BlankCanvas doesn't know its own size!")
|
|
570
|
+
|
|
571
|
+
def content_delta(self, other: Canvas) -> typing.NoReturn:
|
|
572
|
+
raise NotImplementedError("BlankCanvas doesn't know its own size!")
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
blank_canvas = BlankCanvas()
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
class SolidCanvas(Canvas):
|
|
579
|
+
"""
|
|
580
|
+
A canvas filled completely with a single character.
|
|
581
|
+
"""
|
|
582
|
+
|
|
583
|
+
def __init__(self, fill_char: str, cols: int, rows: int) -> None:
|
|
584
|
+
super().__init__()
|
|
585
|
+
end, col = calc_text_pos(fill_char, 0, len(fill_char), 1)
|
|
586
|
+
if col != 1:
|
|
587
|
+
raise ValueError(f"Invalid fill_char: {fill_char!r}")
|
|
588
|
+
self._text, cs = apply_target_encoding(fill_char[:end])
|
|
589
|
+
self._cs = cs[0][0]
|
|
590
|
+
self.size = cols, rows
|
|
591
|
+
self.cursor = None
|
|
592
|
+
|
|
593
|
+
def cols(self) -> int:
|
|
594
|
+
return self.size[0]
|
|
595
|
+
|
|
596
|
+
def rows(self) -> int:
|
|
597
|
+
return self.size[1]
|
|
598
|
+
|
|
599
|
+
def content(
|
|
600
|
+
self,
|
|
601
|
+
trim_left: int = 0,
|
|
602
|
+
trim_top: int = 0,
|
|
603
|
+
cols: int | None = None,
|
|
604
|
+
rows: int | None = None,
|
|
605
|
+
attr=None,
|
|
606
|
+
) -> Iterable[list[tuple[object, Literal["0", "U"] | None, bytes]]]:
|
|
607
|
+
if cols is None:
|
|
608
|
+
cols = self.size[0]
|
|
609
|
+
if rows is None:
|
|
610
|
+
rows = self.size[1]
|
|
611
|
+
def_attr = None
|
|
612
|
+
if attr and None in attr:
|
|
613
|
+
def_attr = attr[None]
|
|
614
|
+
|
|
615
|
+
line = [(def_attr, self._cs, self._text * cols)]
|
|
616
|
+
for _ in range(rows):
|
|
617
|
+
yield line
|
|
618
|
+
|
|
619
|
+
def content_delta(self, other):
|
|
620
|
+
"""
|
|
621
|
+
Return the differences between other and this canvas.
|
|
622
|
+
"""
|
|
623
|
+
if other is self:
|
|
624
|
+
return [self.cols()] * self.rows()
|
|
625
|
+
return self.content()
|
|
626
|
+
|
|
627
|
+
|
|
628
|
+
class CompositeCanvas(Canvas):
|
|
629
|
+
"""
|
|
630
|
+
class for storing a combination of canvases
|
|
631
|
+
"""
|
|
632
|
+
|
|
633
|
+
def __init__(self, canv: Canvas = None) -> None:
|
|
634
|
+
"""
|
|
635
|
+
canv -- a Canvas object to wrap this CompositeCanvas around.
|
|
636
|
+
|
|
637
|
+
if canv is a CompositeCanvas, make a copy of its contents
|
|
638
|
+
"""
|
|
639
|
+
# a "shard" is a (num_rows, list of cviews) tuple, one for
|
|
640
|
+
# each cview starting in this shard
|
|
641
|
+
|
|
642
|
+
# a "cview" is a tuple that defines a view of a canvas:
|
|
643
|
+
# (trim_left, trim_top, cols, rows, attr_map, canv)
|
|
644
|
+
|
|
645
|
+
# a "shard tail" is a list of tuples:
|
|
646
|
+
# (col_gap, done_rows, content_iter, cview)
|
|
647
|
+
|
|
648
|
+
# tuples that define the unfinished cviews that are part of
|
|
649
|
+
# shards following the first shard.
|
|
650
|
+
super().__init__()
|
|
651
|
+
|
|
652
|
+
if canv is None:
|
|
653
|
+
self.shards: list[
|
|
654
|
+
tuple[
|
|
655
|
+
int,
|
|
656
|
+
list[tuple[int, int, int, int, dict[Hashable | None, Hashable] | None, Canvas]],
|
|
657
|
+
]
|
|
658
|
+
] = []
|
|
659
|
+
self.children: list[tuple[int, int, Canvas, typing.Any]] = []
|
|
660
|
+
else:
|
|
661
|
+
if hasattr(canv, "shards"):
|
|
662
|
+
self.shards = canv.shards
|
|
663
|
+
else:
|
|
664
|
+
self.shards = [(canv.rows(), [(0, 0, canv.cols(), canv.rows(), None, canv)])]
|
|
665
|
+
self.children = [(0, 0, canv, None)]
|
|
666
|
+
self.coords.update(canv.coords)
|
|
667
|
+
for shortcut in canv.shortcuts:
|
|
668
|
+
self.shortcuts[shortcut] = "wrap"
|
|
669
|
+
|
|
670
|
+
def __repr__(self) -> str:
|
|
671
|
+
extra = [""]
|
|
672
|
+
with contextlib.suppress(BaseException):
|
|
673
|
+
extra.append(f"cols={self.cols()}")
|
|
674
|
+
|
|
675
|
+
with contextlib.suppress(BaseException):
|
|
676
|
+
extra.append(f"rows={self.rows()}")
|
|
677
|
+
|
|
678
|
+
if self.cursor:
|
|
679
|
+
extra.append(f"cursor={self.cursor}")
|
|
680
|
+
if self.children:
|
|
681
|
+
extra.append(f"children=({', '.join(repr(canv) for _, _, canv, _ in self.children)})")
|
|
682
|
+
|
|
683
|
+
return f"<{self.__class__.__name__} finalized={bool(self.widget_info)}{' '.join(extra)} at 0x{id(self):X}>"
|
|
684
|
+
|
|
685
|
+
def rows(self) -> int:
|
|
686
|
+
for r, cv in self.shards:
|
|
687
|
+
if not isinstance(r, int):
|
|
688
|
+
raise TypeError(r, cv)
|
|
689
|
+
|
|
690
|
+
return sum(r for r, cv in self.shards)
|
|
691
|
+
|
|
692
|
+
def cols(self) -> int:
|
|
693
|
+
if not self.shards:
|
|
694
|
+
return 0
|
|
695
|
+
cols = sum(cv[2] for cv in self.shards[0][1])
|
|
696
|
+
if not isinstance(cols, int):
|
|
697
|
+
raise TypeError(cols)
|
|
698
|
+
return cols
|
|
699
|
+
|
|
700
|
+
def content(
|
|
701
|
+
self,
|
|
702
|
+
trim_left: int = 0,
|
|
703
|
+
trim_top: int = 0,
|
|
704
|
+
cols: int | None = None,
|
|
705
|
+
rows: int | None = None,
|
|
706
|
+
attr=None,
|
|
707
|
+
) -> Iterable[list[tuple[object, Literal["0", "U"] | None, bytes]]]:
|
|
708
|
+
"""
|
|
709
|
+
Return the canvas content as a list of rows where each row
|
|
710
|
+
is a list of (attr, cs, text) tuples.
|
|
711
|
+
"""
|
|
712
|
+
shard_tail = []
|
|
713
|
+
for num_rows, cviews in self.shards:
|
|
714
|
+
# combine shard and shard tail
|
|
715
|
+
sbody = shard_body(cviews, shard_tail)
|
|
716
|
+
|
|
717
|
+
# output rows
|
|
718
|
+
for _ in range(num_rows):
|
|
719
|
+
yield shard_body_row(sbody)
|
|
720
|
+
|
|
721
|
+
# prepare next shard tail
|
|
722
|
+
shard_tail = shard_body_tail(num_rows, sbody)
|
|
723
|
+
|
|
724
|
+
def content_delta(self, other: Canvas):
|
|
725
|
+
"""
|
|
726
|
+
Return the differences between other and this canvas.
|
|
727
|
+
"""
|
|
728
|
+
if not hasattr(other, "shards"):
|
|
729
|
+
for row in self.content():
|
|
730
|
+
yield row
|
|
731
|
+
return
|
|
732
|
+
|
|
733
|
+
shard_tail = []
|
|
734
|
+
for num_rows, cviews in shards_delta(self.shards, other.shards):
|
|
735
|
+
# combine shard and shard tail
|
|
736
|
+
sbody = shard_body(cviews, shard_tail)
|
|
737
|
+
|
|
738
|
+
# output rows
|
|
739
|
+
row = []
|
|
740
|
+
for _ in range(num_rows):
|
|
741
|
+
# if whole shard is unchanged, don't keep
|
|
742
|
+
# calling shard_body_row
|
|
743
|
+
if len(row) != 1 or not isinstance(row[0], int):
|
|
744
|
+
row = shard_body_row(sbody)
|
|
745
|
+
yield row
|
|
746
|
+
|
|
747
|
+
# prepare next shard tail
|
|
748
|
+
shard_tail = shard_body_tail(num_rows, sbody)
|
|
749
|
+
|
|
750
|
+
def trim(self, top: int, count: int | None = None) -> None:
|
|
751
|
+
"""Trim lines from the top and/or bottom of canvas.
|
|
752
|
+
|
|
753
|
+
top -- number of lines to remove from top
|
|
754
|
+
count -- number of lines to keep, or None for all the rest
|
|
755
|
+
"""
|
|
756
|
+
if top < 0:
|
|
757
|
+
raise ValueError(f"invalid trim amount {top:d}!")
|
|
758
|
+
if top >= self.rows():
|
|
759
|
+
raise ValueError(f"cannot trim {top:d} lines from {self.rows():d}!")
|
|
760
|
+
if self.widget_info:
|
|
761
|
+
raise self._finalized_error
|
|
762
|
+
|
|
763
|
+
if top:
|
|
764
|
+
self.shards = shards_trim_top(self.shards, top)
|
|
765
|
+
|
|
766
|
+
if count == 0:
|
|
767
|
+
self.shards = []
|
|
768
|
+
elif count is not None:
|
|
769
|
+
self.shards = shards_trim_rows(self.shards, count)
|
|
770
|
+
|
|
771
|
+
self.coords = self.translate_coords(0, -top)
|
|
772
|
+
|
|
773
|
+
def trim_end(self, end: int) -> None:
|
|
774
|
+
"""Trim lines from the bottom of the canvas.
|
|
775
|
+
|
|
776
|
+
end -- number of lines to remove from the end
|
|
777
|
+
"""
|
|
778
|
+
if end <= 0:
|
|
779
|
+
raise ValueError(f"invalid trim amount {end:d}!")
|
|
780
|
+
if end > self.rows():
|
|
781
|
+
raise ValueError(f"cannot trim {end:d} lines from {self.rows():d}!")
|
|
782
|
+
if self.widget_info:
|
|
783
|
+
raise self._finalized_error
|
|
784
|
+
|
|
785
|
+
self.shards = shards_trim_rows(self.shards, self.rows() - end)
|
|
786
|
+
|
|
787
|
+
def pad_trim_left_right(self, left: int, right: int) -> None:
|
|
788
|
+
"""
|
|
789
|
+
Pad or trim this canvas on the left and right
|
|
790
|
+
|
|
791
|
+
values > 0 indicate screen columns to pad
|
|
792
|
+
values < 0 indicate screen columns to trim
|
|
793
|
+
"""
|
|
794
|
+
if self.widget_info:
|
|
795
|
+
raise self._finalized_error
|
|
796
|
+
shards = self.shards
|
|
797
|
+
if left < 0 or right < 0:
|
|
798
|
+
trim_left = max(0, -left)
|
|
799
|
+
cols = self.cols() - trim_left - max(0, -right)
|
|
800
|
+
shards = shards_trim_sides(shards, trim_left, cols)
|
|
801
|
+
|
|
802
|
+
rows = self.rows()
|
|
803
|
+
if left > 0 or right > 0:
|
|
804
|
+
top_rows, top_cviews = shards[0]
|
|
805
|
+
if left > 0:
|
|
806
|
+
new_top_cviews = [(0, 0, left, rows, None, blank_canvas), *top_cviews]
|
|
807
|
+
else:
|
|
808
|
+
new_top_cviews = top_cviews.copy()
|
|
809
|
+
|
|
810
|
+
if right > 0:
|
|
811
|
+
new_top_cviews.append((0, 0, right, rows, None, blank_canvas))
|
|
812
|
+
shards = [(top_rows, new_top_cviews)] + shards[1:]
|
|
813
|
+
|
|
814
|
+
self.coords = self.translate_coords(left, 0)
|
|
815
|
+
self.shards = shards
|
|
816
|
+
|
|
817
|
+
def pad_trim_top_bottom(self, top: int, bottom: int) -> None:
|
|
818
|
+
"""
|
|
819
|
+
Pad or trim this canvas on the top and bottom.
|
|
820
|
+
"""
|
|
821
|
+
if self.widget_info:
|
|
822
|
+
raise self._finalized_error
|
|
823
|
+
orig_shards = self.shards
|
|
824
|
+
|
|
825
|
+
if top < 0 or bottom < 0:
|
|
826
|
+
trim_top = max(0, -top)
|
|
827
|
+
rows = self.rows() - trim_top - max(0, -bottom)
|
|
828
|
+
self.trim(trim_top, rows)
|
|
829
|
+
|
|
830
|
+
cols = self.cols()
|
|
831
|
+
if top > 0:
|
|
832
|
+
self.shards = [(top, [(0, 0, cols, top, None, blank_canvas)]), *self.shards]
|
|
833
|
+
self.coords = self.translate_coords(0, top)
|
|
834
|
+
|
|
835
|
+
if bottom > 0:
|
|
836
|
+
if orig_shards is self.shards:
|
|
837
|
+
self.shards = self.shards[:]
|
|
838
|
+
self.shards.append((bottom, [(0, 0, cols, bottom, None, blank_canvas)]))
|
|
839
|
+
|
|
840
|
+
def overlay(self, other: CompositeCanvas, left: int, top: int) -> None:
|
|
841
|
+
"""Overlay other onto this canvas."""
|
|
842
|
+
if self.widget_info:
|
|
843
|
+
raise self._finalized_error
|
|
844
|
+
|
|
845
|
+
width = other.cols()
|
|
846
|
+
height = other.rows()
|
|
847
|
+
right = self.cols() - left - width
|
|
848
|
+
bottom = self.rows() - top - height
|
|
849
|
+
|
|
850
|
+
if right < 0:
|
|
851
|
+
raise ValueError(f"top canvas of overlay not the size expected!{(other.cols(), left, right, width)!r}")
|
|
852
|
+
if bottom < 0:
|
|
853
|
+
raise ValueError(f"top canvas of overlay not the size expected!{(other.rows(), top, bottom, height)!r}")
|
|
854
|
+
|
|
855
|
+
shards = self.shards
|
|
856
|
+
top_shards = []
|
|
857
|
+
side_shards = self.shards
|
|
858
|
+
bottom_shards = []
|
|
859
|
+
if top:
|
|
860
|
+
side_shards = shards_trim_top(shards, top)
|
|
861
|
+
top_shards = shards_trim_rows(shards, top)
|
|
862
|
+
if bottom:
|
|
863
|
+
bottom_shards = shards_trim_top(side_shards, height)
|
|
864
|
+
side_shards = shards_trim_rows(side_shards, height)
|
|
865
|
+
|
|
866
|
+
left_shards = []
|
|
867
|
+
right_shards = []
|
|
868
|
+
if left > 0:
|
|
869
|
+
left_shards = [shards_trim_sides(side_shards, 0, left)]
|
|
870
|
+
if right > 0:
|
|
871
|
+
right_shards = [shards_trim_sides(side_shards, max(0, left + width), right)]
|
|
872
|
+
|
|
873
|
+
if not self.rows():
|
|
874
|
+
middle_shards = []
|
|
875
|
+
elif left or right:
|
|
876
|
+
middle_shards = shards_join((*left_shards, other.shards, *right_shards))
|
|
877
|
+
else:
|
|
878
|
+
middle_shards = other.shards
|
|
879
|
+
|
|
880
|
+
self.shards = top_shards + middle_shards + bottom_shards
|
|
881
|
+
|
|
882
|
+
self.coords.update(other.translate_coords(left, top))
|
|
883
|
+
|
|
884
|
+
def fill_attr(self, a: Hashable) -> None:
|
|
885
|
+
"""
|
|
886
|
+
Apply attribute a to all areas of this canvas with default attribute currently set to None,
|
|
887
|
+
leaving other attributes intact.
|
|
888
|
+
"""
|
|
889
|
+
self.fill_attr_apply({None: a})
|
|
890
|
+
|
|
891
|
+
def fill_attr_apply(self, mapping: dict[Hashable | None, Hashable]) -> None:
|
|
892
|
+
"""
|
|
893
|
+
Apply an attribute-mapping dictionary to the canvas.
|
|
894
|
+
|
|
895
|
+
mapping -- dictionary of original-attribute:new-attribute items
|
|
896
|
+
"""
|
|
897
|
+
if self.widget_info:
|
|
898
|
+
raise self._finalized_error
|
|
899
|
+
|
|
900
|
+
shards = []
|
|
901
|
+
for num_rows, original_cviews in self.shards:
|
|
902
|
+
new_cviews = []
|
|
903
|
+
for cv in original_cviews:
|
|
904
|
+
# cv[4] == attr_map
|
|
905
|
+
if cv[4] is None:
|
|
906
|
+
new_cviews.append(cv[:4] + (mapping,) + cv[5:])
|
|
907
|
+
else:
|
|
908
|
+
combined = mapping.copy()
|
|
909
|
+
combined.update([(k, mapping.get(v, v)) for k, v in cv[4].items()])
|
|
910
|
+
new_cviews.append(cv[:4] + (combined,) + cv[5:])
|
|
911
|
+
shards.append((num_rows, new_cviews))
|
|
912
|
+
self.shards = shards
|
|
913
|
+
|
|
914
|
+
def set_depends(self, widget_list: Sequence[Widget]) -> None:
|
|
915
|
+
"""
|
|
916
|
+
Explicitly specify the list of widgets that this canvas
|
|
917
|
+
depends on. If any of these widgets change this canvas
|
|
918
|
+
will have to be updated.
|
|
919
|
+
"""
|
|
920
|
+
if self.widget_info:
|
|
921
|
+
raise self._finalized_error
|
|
922
|
+
|
|
923
|
+
self.depends_on = widget_list
|
|
924
|
+
|
|
925
|
+
|
|
926
|
+
def shard_body_row(sbody):
|
|
927
|
+
"""
|
|
928
|
+
Return one row, advancing the iterators in sbody.
|
|
929
|
+
|
|
930
|
+
** MODIFIES sbody by calling next() on its iterators **
|
|
931
|
+
"""
|
|
932
|
+
row = []
|
|
933
|
+
for _done_rows, content_iter, cview in sbody:
|
|
934
|
+
if content_iter:
|
|
935
|
+
row.extend(next(content_iter))
|
|
936
|
+
else: # noqa: PLR5501 # pylint: disable=else-if-used # readability
|
|
937
|
+
# need to skip this unchanged canvas
|
|
938
|
+
if row and isinstance(row[-1], int):
|
|
939
|
+
row[-1] = row[-1] + cview[2]
|
|
940
|
+
else:
|
|
941
|
+
row.append(cview[2])
|
|
942
|
+
|
|
943
|
+
return row
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
def shard_body_tail(num_rows: int, sbody):
|
|
947
|
+
"""
|
|
948
|
+
Return a new shard tail that follows this shard body.
|
|
949
|
+
"""
|
|
950
|
+
shard_tail = []
|
|
951
|
+
col_gap = 0
|
|
952
|
+
|
|
953
|
+
for done_rows, content_iter, cview in sbody:
|
|
954
|
+
cols, rows = cview[2:4]
|
|
955
|
+
done_rows += num_rows # noqa: PLW2901
|
|
956
|
+
if done_rows == rows:
|
|
957
|
+
col_gap += cols
|
|
958
|
+
continue
|
|
959
|
+
shard_tail.append((col_gap, done_rows, content_iter, cview))
|
|
960
|
+
col_gap = 0
|
|
961
|
+
return shard_tail
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
def shards_delta(shards, other_shards):
|
|
965
|
+
"""
|
|
966
|
+
Yield shards1 with cviews that are the same as shards2 having canv = None.
|
|
967
|
+
"""
|
|
968
|
+
# pylint: disable=stop-iteration-return
|
|
969
|
+
other_shards_iter = iter(other_shards)
|
|
970
|
+
other_num_rows = other_cviews = None
|
|
971
|
+
done = other_done = 0
|
|
972
|
+
for num_rows, cviews in shards:
|
|
973
|
+
if other_num_rows is None:
|
|
974
|
+
other_num_rows, other_cviews = next(other_shards_iter)
|
|
975
|
+
while other_done < done:
|
|
976
|
+
other_done += other_num_rows
|
|
977
|
+
other_num_rows, other_cviews = next(other_shards_iter)
|
|
978
|
+
if other_done > done:
|
|
979
|
+
yield (num_rows, cviews)
|
|
980
|
+
done += num_rows
|
|
981
|
+
continue
|
|
982
|
+
# top-aligned shards, compare each cview
|
|
983
|
+
yield (num_rows, shard_cviews_delta(cviews, other_cviews))
|
|
984
|
+
other_done += other_num_rows
|
|
985
|
+
other_num_rows = None
|
|
986
|
+
done += num_rows
|
|
987
|
+
|
|
988
|
+
|
|
989
|
+
def shard_cviews_delta(cviews, other_cviews):
|
|
990
|
+
# pylint: disable=stop-iteration-return
|
|
991
|
+
other_cviews_iter = iter(other_cviews)
|
|
992
|
+
other_cv = None
|
|
993
|
+
cols = other_cols = 0
|
|
994
|
+
for cv in cviews:
|
|
995
|
+
if other_cv is None:
|
|
996
|
+
other_cv = next(other_cviews_iter)
|
|
997
|
+
while other_cols < cols:
|
|
998
|
+
other_cols += other_cv[2]
|
|
999
|
+
other_cv = next(other_cviews_iter)
|
|
1000
|
+
if other_cols > cols:
|
|
1001
|
+
yield cv
|
|
1002
|
+
cols += cv[2]
|
|
1003
|
+
continue
|
|
1004
|
+
# top-left-aligned cviews, compare them
|
|
1005
|
+
if cv[5] is other_cv[5] and cv[:5] == other_cv[:5]:
|
|
1006
|
+
yield cv[:5] + (None,) + cv[6:]
|
|
1007
|
+
else:
|
|
1008
|
+
yield cv
|
|
1009
|
+
other_cols += other_cv[2]
|
|
1010
|
+
other_cv = None
|
|
1011
|
+
cols += cv[2]
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
def shard_body(cviews, shard_tail, create_iter: bool = True, iter_default=None):
|
|
1015
|
+
"""
|
|
1016
|
+
Return a list of (done_rows, content_iter, cview) tuples for
|
|
1017
|
+
this shard and shard tail.
|
|
1018
|
+
|
|
1019
|
+
If a canvas in cviews is None (eg. when unchanged from
|
|
1020
|
+
shard_cviews_delta()) or if create_iter is False then no
|
|
1021
|
+
iterator is created for content_iter.
|
|
1022
|
+
|
|
1023
|
+
iter_default is the value used for content_iter when no iterator
|
|
1024
|
+
is created.
|
|
1025
|
+
"""
|
|
1026
|
+
col = 0
|
|
1027
|
+
body = [] # build the next shard tail
|
|
1028
|
+
cviews_iter = iter(cviews)
|
|
1029
|
+
for col_gap, done_rows, content_iter, tail_cview in shard_tail:
|
|
1030
|
+
while col_gap:
|
|
1031
|
+
try:
|
|
1032
|
+
cview = next(cviews_iter)
|
|
1033
|
+
except StopIteration:
|
|
1034
|
+
break
|
|
1035
|
+
(trim_left, trim_top, cols, rows, attr_map, canv) = cview[:6]
|
|
1036
|
+
col += cols
|
|
1037
|
+
col_gap -= cols # noqa: PLW2901
|
|
1038
|
+
if col_gap < 0:
|
|
1039
|
+
raise CanvasError("cviews overflow gaps in shard_tail!")
|
|
1040
|
+
if create_iter and canv:
|
|
1041
|
+
new_iter = canv.content(trim_left, trim_top, cols, rows, attr_map)
|
|
1042
|
+
else:
|
|
1043
|
+
new_iter = iter_default
|
|
1044
|
+
body.append((0, new_iter, cview))
|
|
1045
|
+
body.append((done_rows, content_iter, tail_cview))
|
|
1046
|
+
for cview in cviews_iter:
|
|
1047
|
+
(trim_left, trim_top, cols, rows, attr_map, canv) = cview[:6]
|
|
1048
|
+
if create_iter and canv:
|
|
1049
|
+
new_iter = canv.content(trim_left, trim_top, cols, rows, attr_map)
|
|
1050
|
+
else:
|
|
1051
|
+
new_iter = iter_default
|
|
1052
|
+
body.append((0, new_iter, cview))
|
|
1053
|
+
return body
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
def shards_trim_top(shards, top: int):
|
|
1057
|
+
"""
|
|
1058
|
+
Return shards with top rows removed.
|
|
1059
|
+
"""
|
|
1060
|
+
if top <= 0:
|
|
1061
|
+
raise ValueError(top)
|
|
1062
|
+
|
|
1063
|
+
shard_iter = iter(shards)
|
|
1064
|
+
shard_tail = []
|
|
1065
|
+
# skip over shards that are completely removed
|
|
1066
|
+
for num_rows, cviews in shard_iter:
|
|
1067
|
+
if top < num_rows:
|
|
1068
|
+
break
|
|
1069
|
+
sbody = shard_body(cviews, shard_tail, False)
|
|
1070
|
+
shard_tail = shard_body_tail(num_rows, sbody)
|
|
1071
|
+
top -= num_rows
|
|
1072
|
+
else:
|
|
1073
|
+
raise CanvasError("tried to trim shards out of existence")
|
|
1074
|
+
|
|
1075
|
+
sbody = shard_body(cviews, shard_tail, False)
|
|
1076
|
+
shard_tail = shard_body_tail(num_rows, sbody)
|
|
1077
|
+
# trim the top of this shard
|
|
1078
|
+
new_sbody = [(0, content_iter, cview_trim_top(cv, done_rows + top)) for done_rows, content_iter, cv in sbody]
|
|
1079
|
+
|
|
1080
|
+
sbody = new_sbody
|
|
1081
|
+
|
|
1082
|
+
new_shards = [(num_rows - top, [cv for done_rows, content_iter, cv in sbody])]
|
|
1083
|
+
|
|
1084
|
+
# write out the rest of the shards
|
|
1085
|
+
new_shards.extend(shard_iter)
|
|
1086
|
+
|
|
1087
|
+
return new_shards
|
|
1088
|
+
|
|
1089
|
+
|
|
1090
|
+
def shards_trim_rows(shards, keep_rows: int):
|
|
1091
|
+
"""
|
|
1092
|
+
Return the topmost keep_rows rows from shards.
|
|
1093
|
+
"""
|
|
1094
|
+
if keep_rows < 0:
|
|
1095
|
+
raise ValueError(keep_rows)
|
|
1096
|
+
|
|
1097
|
+
new_shards = []
|
|
1098
|
+
done_rows = 0
|
|
1099
|
+
for num_rows, cviews in shards:
|
|
1100
|
+
if done_rows >= keep_rows:
|
|
1101
|
+
break
|
|
1102
|
+
new_cviews = []
|
|
1103
|
+
for cv in cviews:
|
|
1104
|
+
if cv[3] + done_rows > keep_rows:
|
|
1105
|
+
new_cviews.append(cview_trim_rows(cv, keep_rows - done_rows))
|
|
1106
|
+
else:
|
|
1107
|
+
new_cviews.append(cv)
|
|
1108
|
+
|
|
1109
|
+
if num_rows + done_rows > keep_rows:
|
|
1110
|
+
new_shards.append((keep_rows - done_rows, new_cviews))
|
|
1111
|
+
else:
|
|
1112
|
+
new_shards.append((num_rows, new_cviews))
|
|
1113
|
+
done_rows += num_rows
|
|
1114
|
+
|
|
1115
|
+
return new_shards
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
def shards_trim_sides(shards, left: int, cols: int):
|
|
1119
|
+
"""
|
|
1120
|
+
Return shards with starting from column left and cols total width.
|
|
1121
|
+
"""
|
|
1122
|
+
if left < 0:
|
|
1123
|
+
raise ValueError(left)
|
|
1124
|
+
if cols <= 0:
|
|
1125
|
+
raise ValueError(cols)
|
|
1126
|
+
shard_tail = []
|
|
1127
|
+
new_shards = []
|
|
1128
|
+
right = left + cols
|
|
1129
|
+
for num_rows, cviews in shards:
|
|
1130
|
+
sbody = shard_body(cviews, shard_tail, False)
|
|
1131
|
+
shard_tail = shard_body_tail(num_rows, sbody)
|
|
1132
|
+
new_cviews = []
|
|
1133
|
+
col = 0
|
|
1134
|
+
for done_rows, _content_iter, cv in sbody:
|
|
1135
|
+
cv_cols = cv[2]
|
|
1136
|
+
next_col = col + cv_cols
|
|
1137
|
+
if done_rows or next_col <= left or col >= right:
|
|
1138
|
+
col = next_col
|
|
1139
|
+
continue
|
|
1140
|
+
if col < left:
|
|
1141
|
+
cv = cview_trim_left(cv, left - col) # noqa: PLW2901
|
|
1142
|
+
col = left
|
|
1143
|
+
if next_col > right:
|
|
1144
|
+
cv = cview_trim_cols(cv, right - col) # noqa: PLW2901
|
|
1145
|
+
new_cviews.append(cv)
|
|
1146
|
+
col = next_col
|
|
1147
|
+
if not new_cviews:
|
|
1148
|
+
prev_num_rows, prev_cviews = new_shards[-1]
|
|
1149
|
+
new_shards[-1] = (prev_num_rows + num_rows, prev_cviews)
|
|
1150
|
+
else:
|
|
1151
|
+
new_shards.append((num_rows, new_cviews))
|
|
1152
|
+
return new_shards
|
|
1153
|
+
|
|
1154
|
+
|
|
1155
|
+
def shards_join(shard_lists):
|
|
1156
|
+
"""
|
|
1157
|
+
Return the result of joining shard lists horizontally.
|
|
1158
|
+
All shards lists must have the same number of rows.
|
|
1159
|
+
"""
|
|
1160
|
+
shards_iters = [iter(sl) for sl in shard_lists]
|
|
1161
|
+
shards_current = [next(i) for i in shards_iters]
|
|
1162
|
+
|
|
1163
|
+
new_shards = []
|
|
1164
|
+
while True:
|
|
1165
|
+
new_cviews = []
|
|
1166
|
+
num_rows = min(r for r, cv in shards_current)
|
|
1167
|
+
|
|
1168
|
+
shards_next = []
|
|
1169
|
+
for rows, cviews in shards_current:
|
|
1170
|
+
if cviews:
|
|
1171
|
+
new_cviews.extend(cviews)
|
|
1172
|
+
shards_next.append((rows - num_rows, None))
|
|
1173
|
+
|
|
1174
|
+
shards_current = shards_next
|
|
1175
|
+
new_shards.append((num_rows, new_cviews))
|
|
1176
|
+
|
|
1177
|
+
# advance to next shards
|
|
1178
|
+
try:
|
|
1179
|
+
for i in range(len(shards_current)):
|
|
1180
|
+
if shards_current[i][0] > 0:
|
|
1181
|
+
continue
|
|
1182
|
+
shards_current[i] = next(shards_iters[i])
|
|
1183
|
+
except StopIteration:
|
|
1184
|
+
break
|
|
1185
|
+
return new_shards
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
def cview_trim_rows(cv, rows: int):
|
|
1189
|
+
return cv[:3] + (rows,) + cv[4:]
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
def cview_trim_top(cv, trim: int):
|
|
1193
|
+
return (cv[0], trim + cv[1], cv[2], cv[3] - trim) + cv[4:]
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
def cview_trim_left(cv, trim: int):
|
|
1197
|
+
return (cv[0] + trim, cv[1], cv[2] - trim) + cv[3:]
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
def cview_trim_cols(cv, cols: int):
|
|
1201
|
+
return cv[:2] + (cols,) + cv[3:]
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
def CanvasCombine(canvas_info: Iterable[tuple[Canvas, typing.Any, bool]]) -> CompositeCanvas:
|
|
1205
|
+
"""Stack canvases in l vertically and return resulting canvas.
|
|
1206
|
+
|
|
1207
|
+
:param canvas_info: list of (canvas, position, focus) tuples:
|
|
1208
|
+
|
|
1209
|
+
position
|
|
1210
|
+
a value that widget.set_focus will accept or None if not allowed
|
|
1211
|
+
focus
|
|
1212
|
+
True if this canvas is the one that would be in focus if the whole widget is in focus
|
|
1213
|
+
"""
|
|
1214
|
+
clist = [(CompositeCanvas(c), p, f) for c, p, f in canvas_info]
|
|
1215
|
+
|
|
1216
|
+
combined_canvas = CompositeCanvas()
|
|
1217
|
+
shards = []
|
|
1218
|
+
children = []
|
|
1219
|
+
row = 0
|
|
1220
|
+
focus_index = 0
|
|
1221
|
+
|
|
1222
|
+
for n, (canv, pos, focus) in enumerate(clist):
|
|
1223
|
+
if focus:
|
|
1224
|
+
focus_index = n
|
|
1225
|
+
children.append((0, row, canv, pos))
|
|
1226
|
+
shards.extend(canv.shards)
|
|
1227
|
+
combined_canvas.coords.update(canv.translate_coords(0, row))
|
|
1228
|
+
for shortcut in canv.shortcuts:
|
|
1229
|
+
combined_canvas.shortcuts[shortcut] = pos
|
|
1230
|
+
row += canv.rows()
|
|
1231
|
+
|
|
1232
|
+
if focus_index:
|
|
1233
|
+
children = [children[focus_index]] + children[:focus_index] + children[focus_index + 1 :]
|
|
1234
|
+
|
|
1235
|
+
combined_canvas.shards = shards
|
|
1236
|
+
combined_canvas.children = children
|
|
1237
|
+
return combined_canvas
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
def CanvasOverlay(top_c: Canvas, bottom_c: Canvas, left: int, top: int) -> CompositeCanvas:
|
|
1241
|
+
"""
|
|
1242
|
+
Overlay canvas top_c onto bottom_c at position (left, top).
|
|
1243
|
+
"""
|
|
1244
|
+
overlayed_canvas = CompositeCanvas(bottom_c)
|
|
1245
|
+
overlayed_canvas.overlay(top_c, left, top)
|
|
1246
|
+
overlayed_canvas.children = [(left, top, top_c, None), (0, 0, bottom_c, None)]
|
|
1247
|
+
overlayed_canvas.shortcuts = {} # disable background shortcuts
|
|
1248
|
+
for shortcut in top_c.shortcuts:
|
|
1249
|
+
overlayed_canvas.shortcuts[shortcut] = "fg"
|
|
1250
|
+
return overlayed_canvas
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
def CanvasJoin(canvas_info: Iterable[tuple[Canvas, typing.Any, bool, int]]) -> CompositeCanvas:
|
|
1254
|
+
"""
|
|
1255
|
+
Join canvases in l horizontally. Return result.
|
|
1256
|
+
|
|
1257
|
+
:param canvas_info: list of (canvas, position, focus, cols) tuples:
|
|
1258
|
+
|
|
1259
|
+
position
|
|
1260
|
+
value that widget.set_focus will accept or None if not allowed
|
|
1261
|
+
focus
|
|
1262
|
+
True if this canvas is the one that would be in focus if the whole widget is in focus
|
|
1263
|
+
cols
|
|
1264
|
+
is the number of screen columns that this widget will require,
|
|
1265
|
+
if larger than the actual canvas.cols() value then this widget
|
|
1266
|
+
will be padded on the right.
|
|
1267
|
+
"""
|
|
1268
|
+
|
|
1269
|
+
l2 = []
|
|
1270
|
+
focus_item = 0
|
|
1271
|
+
maxrow = 0
|
|
1272
|
+
|
|
1273
|
+
for n, (canv, pos, focus, cols) in enumerate(canvas_info):
|
|
1274
|
+
rows = canv.rows()
|
|
1275
|
+
pad_right = cols - canv.cols()
|
|
1276
|
+
if focus:
|
|
1277
|
+
focus_item = n
|
|
1278
|
+
if rows > maxrow:
|
|
1279
|
+
maxrow = rows
|
|
1280
|
+
l2.append((canv, pos, pad_right, rows))
|
|
1281
|
+
|
|
1282
|
+
shard_lists = []
|
|
1283
|
+
children = []
|
|
1284
|
+
joined_canvas = CompositeCanvas()
|
|
1285
|
+
col = 0
|
|
1286
|
+
for canv, pos, pad_right, rows in l2:
|
|
1287
|
+
composite_canvas = CompositeCanvas(canv)
|
|
1288
|
+
if pad_right:
|
|
1289
|
+
composite_canvas.pad_trim_left_right(0, pad_right)
|
|
1290
|
+
if rows < maxrow:
|
|
1291
|
+
composite_canvas.pad_trim_top_bottom(0, maxrow - rows)
|
|
1292
|
+
joined_canvas.coords.update(composite_canvas.translate_coords(col, 0))
|
|
1293
|
+
for shortcut in composite_canvas.shortcuts:
|
|
1294
|
+
joined_canvas.shortcuts[shortcut] = pos
|
|
1295
|
+
shard_lists.append(composite_canvas.shards)
|
|
1296
|
+
children.append((col, 0, composite_canvas, pos))
|
|
1297
|
+
col += composite_canvas.cols()
|
|
1298
|
+
|
|
1299
|
+
if focus_item:
|
|
1300
|
+
children = [children[focus_item]] + children[:focus_item] + children[focus_item + 1 :]
|
|
1301
|
+
|
|
1302
|
+
joined_canvas.shards = shards_join(shard_lists)
|
|
1303
|
+
joined_canvas.children = children
|
|
1304
|
+
return joined_canvas
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
@dataclasses.dataclass
|
|
1308
|
+
class _AttrWalk:
|
|
1309
|
+
counter: int = 0 # counter for moving through elements of a
|
|
1310
|
+
offset: int = 0 # current offset into text of attr[ak]
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
def apply_text_layout(
|
|
1314
|
+
text: str | bytes,
|
|
1315
|
+
attr: list[tuple[Hashable, int]],
|
|
1316
|
+
ls: list[list[tuple[int, int, int | bytes] | tuple[int, int | None]]],
|
|
1317
|
+
maxcol: int,
|
|
1318
|
+
) -> TextCanvas:
|
|
1319
|
+
t: list[bytes] = []
|
|
1320
|
+
a: list[list[tuple[Hashable | None, int]]] = []
|
|
1321
|
+
c: list[list[tuple[Literal["0", "U"] | None, int]]] = []
|
|
1322
|
+
|
|
1323
|
+
aw = _AttrWalk()
|
|
1324
|
+
|
|
1325
|
+
def arange(start_offs: int, end_offs: int) -> list[tuple[Hashable | None, int]]:
|
|
1326
|
+
"""Return an attribute list for the range of text specified."""
|
|
1327
|
+
if start_offs < aw.offset:
|
|
1328
|
+
aw.counter = 0
|
|
1329
|
+
aw.offset = 0
|
|
1330
|
+
o = []
|
|
1331
|
+
# the loop should run at least once, the '=' part ensures that
|
|
1332
|
+
while aw.offset <= end_offs:
|
|
1333
|
+
if len(attr) <= aw.counter:
|
|
1334
|
+
# run out of attributes
|
|
1335
|
+
o.append((None, end_offs - max(start_offs, aw.offset)))
|
|
1336
|
+
break
|
|
1337
|
+
at, run = attr[aw.counter]
|
|
1338
|
+
if aw.offset + run <= start_offs:
|
|
1339
|
+
# move forward through attr to find start_offs
|
|
1340
|
+
aw.counter += 1
|
|
1341
|
+
aw.offset += run
|
|
1342
|
+
continue
|
|
1343
|
+
if end_offs <= aw.offset + run:
|
|
1344
|
+
o.append((at, end_offs - max(start_offs, aw.offset)))
|
|
1345
|
+
break
|
|
1346
|
+
o.append((at, aw.offset + run - max(start_offs, aw.offset)))
|
|
1347
|
+
aw.counter += 1
|
|
1348
|
+
aw.offset += run
|
|
1349
|
+
return o
|
|
1350
|
+
|
|
1351
|
+
for line_layout in ls:
|
|
1352
|
+
# trim the line to fit within maxcol
|
|
1353
|
+
line_layout = trim_line(line_layout, text, 0, maxcol) # noqa: PLW2901
|
|
1354
|
+
|
|
1355
|
+
line = []
|
|
1356
|
+
linea = []
|
|
1357
|
+
linec = []
|
|
1358
|
+
|
|
1359
|
+
def attrrange(start_offs: int, end_offs: int, destw: int) -> None:
|
|
1360
|
+
"""
|
|
1361
|
+
Add attributes based on attributes between
|
|
1362
|
+
start_offs and end_offs.
|
|
1363
|
+
"""
|
|
1364
|
+
# pylint: disable=cell-var-from-loop
|
|
1365
|
+
if start_offs == end_offs:
|
|
1366
|
+
[(at, run)] = arange(start_offs, end_offs) # pylint: disable=unbalanced-tuple-unpacking
|
|
1367
|
+
rle_append_modify(linea, (at, destw)) # noqa: B023
|
|
1368
|
+
return
|
|
1369
|
+
if destw == end_offs - start_offs:
|
|
1370
|
+
for at, run in arange(start_offs, end_offs):
|
|
1371
|
+
rle_append_modify(linea, (at, run)) # noqa: B023
|
|
1372
|
+
return
|
|
1373
|
+
# encoded version has different width
|
|
1374
|
+
o = start_offs
|
|
1375
|
+
for at, run in arange(start_offs, end_offs):
|
|
1376
|
+
if o + run == end_offs:
|
|
1377
|
+
rle_append_modify(linea, (at, destw)) # noqa: B023
|
|
1378
|
+
return
|
|
1379
|
+
tseg = text[o : o + run]
|
|
1380
|
+
tseg, cs = apply_target_encoding(tseg)
|
|
1381
|
+
segw = rle_len(cs)
|
|
1382
|
+
|
|
1383
|
+
rle_append_modify(linea, (at, segw)) # noqa: B023
|
|
1384
|
+
o += run
|
|
1385
|
+
destw -= segw
|
|
1386
|
+
|
|
1387
|
+
for seg in line_layout:
|
|
1388
|
+
# if seg is None: assert 0, ls
|
|
1389
|
+
s = LayoutSegment(seg)
|
|
1390
|
+
if s.end:
|
|
1391
|
+
tseg, cs = apply_target_encoding(text[s.offs : s.end])
|
|
1392
|
+
line.append(tseg)
|
|
1393
|
+
attrrange(s.offs, s.end, rle_len(cs))
|
|
1394
|
+
rle_join_modify(linec, cs)
|
|
1395
|
+
elif s.text:
|
|
1396
|
+
tseg, cs = apply_target_encoding(s.text)
|
|
1397
|
+
line.append(tseg)
|
|
1398
|
+
attrrange(s.offs, s.offs, len(tseg))
|
|
1399
|
+
rle_join_modify(linec, cs)
|
|
1400
|
+
elif s.offs:
|
|
1401
|
+
if s.sc:
|
|
1402
|
+
line.append(b"".rjust(s.sc))
|
|
1403
|
+
attrrange(s.offs, s.offs, s.sc)
|
|
1404
|
+
else:
|
|
1405
|
+
line.append(b"".rjust(s.sc))
|
|
1406
|
+
linea.append((None, s.sc))
|
|
1407
|
+
linec.append((None, s.sc))
|
|
1408
|
+
|
|
1409
|
+
t.append(b"".join(line))
|
|
1410
|
+
a.append(linea)
|
|
1411
|
+
c.append(linec)
|
|
1412
|
+
|
|
1413
|
+
return TextCanvas(t, a, c, maxcol=maxcol)
|