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/str_util.py
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
# Urwid unicode character processing tables
|
|
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 re
|
|
24
|
+
import typing
|
|
25
|
+
import warnings
|
|
26
|
+
|
|
27
|
+
import wcwidth
|
|
28
|
+
|
|
29
|
+
if typing.TYPE_CHECKING:
|
|
30
|
+
from typing_extensions import Literal
|
|
31
|
+
|
|
32
|
+
UNICODE_VERSION = "15.1.0" # Use explicit version
|
|
33
|
+
SAFE_ASCII_RE = re.compile("^[ -~]*$")
|
|
34
|
+
SAFE_ASCII_BYTES_RE = re.compile(b"^[ -~]*$")
|
|
35
|
+
|
|
36
|
+
_byte_encoding: Literal["utf8", "narrow", "wide"] = "narrow"
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def get_char_width(char: str) -> Literal[0, 1, 2]:
|
|
40
|
+
width = wcwidth.wcwidth(char, UNICODE_VERSION)
|
|
41
|
+
if width < 0:
|
|
42
|
+
return 0
|
|
43
|
+
return width
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def get_width(o: int) -> Literal[0, 1, 2]:
|
|
47
|
+
"""Return the screen column width for unicode ordinal o."""
|
|
48
|
+
return get_char_width(chr(o))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def decode_one(text: bytes | str, pos: int) -> tuple[int, int]:
|
|
52
|
+
"""
|
|
53
|
+
Return (ordinal at pos, next position) for UTF-8 encoded text.
|
|
54
|
+
"""
|
|
55
|
+
lt = len(text) - pos
|
|
56
|
+
|
|
57
|
+
b2 = 0 # Fallback, not changing anything
|
|
58
|
+
b3 = 0 # Fallback, not changing anything
|
|
59
|
+
b4 = 0 # Fallback, not changing anything
|
|
60
|
+
|
|
61
|
+
try:
|
|
62
|
+
if isinstance(text, str):
|
|
63
|
+
b1 = ord(text[pos])
|
|
64
|
+
if lt > 1:
|
|
65
|
+
b2 = ord(text[pos + 1])
|
|
66
|
+
if lt > 2:
|
|
67
|
+
b3 = ord(text[pos + 2])
|
|
68
|
+
if lt > 3:
|
|
69
|
+
b4 = ord(text[pos + 3])
|
|
70
|
+
else:
|
|
71
|
+
b1 = text[pos]
|
|
72
|
+
if lt > 1:
|
|
73
|
+
b2 = text[pos + 1]
|
|
74
|
+
if lt > 2:
|
|
75
|
+
b3 = text[pos + 2]
|
|
76
|
+
if lt > 3:
|
|
77
|
+
b4 = text[pos + 3]
|
|
78
|
+
except Exception as e:
|
|
79
|
+
raise ValueError(f"{e}: text={text!r}, pos={pos!r}, lt={lt!r}").with_traceback(e.__traceback__) from e
|
|
80
|
+
|
|
81
|
+
if not b1 & 0x80:
|
|
82
|
+
return b1, pos + 1
|
|
83
|
+
error = ord("?"), pos + 1
|
|
84
|
+
|
|
85
|
+
if lt < 2:
|
|
86
|
+
return error
|
|
87
|
+
if b1 & 0xE0 == 0xC0:
|
|
88
|
+
if b2 & 0xC0 != 0x80:
|
|
89
|
+
return error
|
|
90
|
+
o = ((b1 & 0x1F) << 6) | (b2 & 0x3F)
|
|
91
|
+
if o < 0x80:
|
|
92
|
+
return error
|
|
93
|
+
return o, pos + 2
|
|
94
|
+
if lt < 3:
|
|
95
|
+
return error
|
|
96
|
+
if b1 & 0xF0 == 0xE0:
|
|
97
|
+
if b2 & 0xC0 != 0x80:
|
|
98
|
+
return error
|
|
99
|
+
if b3 & 0xC0 != 0x80:
|
|
100
|
+
return error
|
|
101
|
+
o = ((b1 & 0x0F) << 12) | ((b2 & 0x3F) << 6) | (b3 & 0x3F)
|
|
102
|
+
if o < 0x800:
|
|
103
|
+
return error
|
|
104
|
+
return o, pos + 3
|
|
105
|
+
if lt < 4:
|
|
106
|
+
return error
|
|
107
|
+
if b1 & 0xF8 == 0xF0:
|
|
108
|
+
if b2 & 0xC0 != 0x80:
|
|
109
|
+
return error
|
|
110
|
+
if b3 & 0xC0 != 0x80:
|
|
111
|
+
return error
|
|
112
|
+
if b4 & 0xC0 != 0x80:
|
|
113
|
+
return error
|
|
114
|
+
o = ((b1 & 0x07) << 18) | ((b2 & 0x3F) << 12) | ((b3 & 0x3F) << 6) | (b4 & 0x3F)
|
|
115
|
+
if o < 0x10000:
|
|
116
|
+
return error
|
|
117
|
+
return o, pos + 4
|
|
118
|
+
return error
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def decode_one_uni(text: str, i: int) -> tuple[int, int]:
|
|
122
|
+
"""
|
|
123
|
+
decode_one implementation for unicode strings
|
|
124
|
+
"""
|
|
125
|
+
return ord(text[i]), i + 1
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def decode_one_right(text: bytes, pos: int) -> tuple[int, int] | None:
|
|
129
|
+
"""
|
|
130
|
+
Return (ordinal at pos, next position) for UTF-8 encoded text.
|
|
131
|
+
pos is assumed to be on the trailing byte of a utf-8 sequence.
|
|
132
|
+
"""
|
|
133
|
+
if not isinstance(text, bytes):
|
|
134
|
+
raise TypeError(text)
|
|
135
|
+
error = ord("?"), pos - 1
|
|
136
|
+
p = pos
|
|
137
|
+
while p >= 0:
|
|
138
|
+
if text[p] & 0xC0 != 0x80:
|
|
139
|
+
o, _next_pos = decode_one(text, p)
|
|
140
|
+
return o, p - 1
|
|
141
|
+
p -= 1
|
|
142
|
+
if p == p - 4:
|
|
143
|
+
return error
|
|
144
|
+
return None
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def set_byte_encoding(enc: Literal["utf8", "narrow", "wide"]) -> None:
|
|
148
|
+
if enc not in {"utf8", "narrow", "wide"}:
|
|
149
|
+
raise ValueError(enc)
|
|
150
|
+
global _byte_encoding # noqa: PLW0603 # pylint: disable=global-statement
|
|
151
|
+
_byte_encoding = enc
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def get_byte_encoding() -> Literal["utf8", "narrow", "wide"]:
|
|
155
|
+
return _byte_encoding
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def calc_string_text_pos(text: str, start_offs: int, end_offs: int, pref_col: int) -> tuple[int, int]:
|
|
159
|
+
"""
|
|
160
|
+
Calculate the closest position to the screen column pref_col in text
|
|
161
|
+
where start_offs is the offset into text assumed to be screen column 0
|
|
162
|
+
and end_offs is the end of the range to search.
|
|
163
|
+
|
|
164
|
+
:param text: string
|
|
165
|
+
:param start_offs: starting text position
|
|
166
|
+
:param end_offs: ending text position
|
|
167
|
+
:param pref_col: target column
|
|
168
|
+
:returns: (position, actual_col)
|
|
169
|
+
|
|
170
|
+
..note:: this method is a simplified version of `wcwidth.wcswidth` and ideally should be in wcwidth package.
|
|
171
|
+
"""
|
|
172
|
+
if start_offs > end_offs:
|
|
173
|
+
raise ValueError((start_offs, end_offs))
|
|
174
|
+
|
|
175
|
+
cols = 0
|
|
176
|
+
for idx in range(start_offs, end_offs):
|
|
177
|
+
width = get_char_width(text[idx])
|
|
178
|
+
if width + cols > pref_col:
|
|
179
|
+
return idx, cols
|
|
180
|
+
cols += width
|
|
181
|
+
|
|
182
|
+
return end_offs, cols
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def calc_text_pos(text: str | bytes, start_offs: int, end_offs: int, pref_col: int) -> tuple[int, int]:
|
|
186
|
+
"""
|
|
187
|
+
Calculate the closest position to the screen column pref_col in text
|
|
188
|
+
where start_offs is the offset into text assumed to be screen column 0
|
|
189
|
+
and end_offs is the end of the range to search.
|
|
190
|
+
|
|
191
|
+
text may be unicode or a byte string in the target _byte_encoding
|
|
192
|
+
|
|
193
|
+
Returns (position, actual_col).
|
|
194
|
+
"""
|
|
195
|
+
if start_offs > end_offs:
|
|
196
|
+
raise ValueError((start_offs, end_offs))
|
|
197
|
+
|
|
198
|
+
if isinstance(text, str):
|
|
199
|
+
return calc_string_text_pos(text, start_offs, end_offs, pref_col)
|
|
200
|
+
|
|
201
|
+
if not isinstance(text, bytes):
|
|
202
|
+
raise TypeError(text)
|
|
203
|
+
|
|
204
|
+
if _byte_encoding == "utf8":
|
|
205
|
+
i = start_offs
|
|
206
|
+
sc = 0
|
|
207
|
+
while i < end_offs:
|
|
208
|
+
o, n = decode_one(text, i)
|
|
209
|
+
w = get_width(o)
|
|
210
|
+
if w + sc > pref_col:
|
|
211
|
+
return i, sc
|
|
212
|
+
i = n
|
|
213
|
+
sc += w
|
|
214
|
+
return i, sc
|
|
215
|
+
|
|
216
|
+
# "wide" and "narrow"
|
|
217
|
+
i = start_offs + pref_col
|
|
218
|
+
if i >= end_offs:
|
|
219
|
+
return end_offs, end_offs - start_offs
|
|
220
|
+
if _byte_encoding == "wide" and within_double_byte(text, start_offs, i) == 2:
|
|
221
|
+
i -= 1
|
|
222
|
+
return i, i - start_offs
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def calc_width(text: str | bytes, start_offs: int, end_offs: int) -> int:
|
|
226
|
+
"""
|
|
227
|
+
Return the screen column width of text between start_offs and end_offs.
|
|
228
|
+
|
|
229
|
+
text may be unicode or a byte string in the target _byte_encoding
|
|
230
|
+
|
|
231
|
+
Some characters are wide (take two columns) and others affect the
|
|
232
|
+
previous character (take zero columns). Use the widths table above
|
|
233
|
+
to calculate the screen column width of text[start_offs:end_offs]
|
|
234
|
+
"""
|
|
235
|
+
|
|
236
|
+
if start_offs > end_offs:
|
|
237
|
+
raise ValueError((start_offs, end_offs))
|
|
238
|
+
|
|
239
|
+
if isinstance(text, str):
|
|
240
|
+
return sum(get_char_width(char) for char in text[start_offs:end_offs])
|
|
241
|
+
|
|
242
|
+
if _byte_encoding == "utf8":
|
|
243
|
+
try:
|
|
244
|
+
return sum(get_char_width(char) for char in text[start_offs:end_offs].decode("utf-8"))
|
|
245
|
+
except UnicodeDecodeError as exc:
|
|
246
|
+
warnings.warn(
|
|
247
|
+
"`calc_width` with text encoded to bytes can produce incorrect results"
|
|
248
|
+
f"due to possible offset in the middle of character: {exc}",
|
|
249
|
+
UnicodeWarning,
|
|
250
|
+
stacklevel=2,
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
i = start_offs
|
|
254
|
+
sc = 0
|
|
255
|
+
while i < end_offs:
|
|
256
|
+
o, i = decode_one(text, i)
|
|
257
|
+
w = get_width(o)
|
|
258
|
+
sc += w
|
|
259
|
+
return sc
|
|
260
|
+
# "wide", "narrow" or all printable ASCII, just return the character count
|
|
261
|
+
return end_offs - start_offs
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def is_wide_char(text: str | bytes, offs: int) -> bool:
|
|
265
|
+
"""
|
|
266
|
+
Test if the character at offs within text is wide.
|
|
267
|
+
|
|
268
|
+
text may be unicode or a byte string in the target _byte_encoding
|
|
269
|
+
"""
|
|
270
|
+
if isinstance(text, str):
|
|
271
|
+
return get_char_width(text[offs]) == 2
|
|
272
|
+
if not isinstance(text, bytes):
|
|
273
|
+
raise TypeError(text)
|
|
274
|
+
if _byte_encoding == "utf8":
|
|
275
|
+
o, _n = decode_one(text, offs)
|
|
276
|
+
return get_width(o) == 2
|
|
277
|
+
if _byte_encoding == "wide":
|
|
278
|
+
return within_double_byte(text, offs, offs) == 1
|
|
279
|
+
return False
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def move_prev_char(text: str | bytes, start_offs: int, end_offs: int) -> int:
|
|
283
|
+
"""
|
|
284
|
+
Return the position of the character before end_offs.
|
|
285
|
+
"""
|
|
286
|
+
if start_offs >= end_offs:
|
|
287
|
+
raise ValueError((start_offs, end_offs))
|
|
288
|
+
if isinstance(text, str):
|
|
289
|
+
return end_offs - 1
|
|
290
|
+
if not isinstance(text, bytes):
|
|
291
|
+
raise TypeError(text)
|
|
292
|
+
if _byte_encoding == "utf8":
|
|
293
|
+
o = end_offs - 1
|
|
294
|
+
while text[o] & 0xC0 == 0x80:
|
|
295
|
+
o -= 1
|
|
296
|
+
return o
|
|
297
|
+
if _byte_encoding == "wide" and within_double_byte(text, start_offs, end_offs - 1) == 2:
|
|
298
|
+
return end_offs - 2
|
|
299
|
+
return end_offs - 1
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def move_next_char(text: str | bytes, start_offs: int, end_offs: int) -> int:
|
|
303
|
+
"""
|
|
304
|
+
Return the position of the character after start_offs.
|
|
305
|
+
"""
|
|
306
|
+
if start_offs >= end_offs:
|
|
307
|
+
raise ValueError((start_offs, end_offs))
|
|
308
|
+
if isinstance(text, str):
|
|
309
|
+
return start_offs + 1
|
|
310
|
+
if not isinstance(text, bytes):
|
|
311
|
+
raise TypeError(text)
|
|
312
|
+
if _byte_encoding == "utf8":
|
|
313
|
+
o = start_offs + 1
|
|
314
|
+
while o < end_offs and text[o] & 0xC0 == 0x80:
|
|
315
|
+
o += 1
|
|
316
|
+
return o
|
|
317
|
+
if _byte_encoding == "wide" and within_double_byte(text, start_offs, start_offs) == 1:
|
|
318
|
+
return start_offs + 2
|
|
319
|
+
return start_offs + 1
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def within_double_byte(text: bytes, line_start: int, pos: int) -> Literal[0, 1, 2]:
|
|
323
|
+
"""Return whether pos is within a double-byte encoded character.
|
|
324
|
+
|
|
325
|
+
text -- byte string in question
|
|
326
|
+
line_start -- offset of beginning of line (< pos)
|
|
327
|
+
pos -- offset in question
|
|
328
|
+
|
|
329
|
+
Return values:
|
|
330
|
+
0 -- not within dbe char, or double_byte_encoding == False
|
|
331
|
+
1 -- pos is on the 1st half of a dbe char
|
|
332
|
+
2 -- pos is on the 2nd half of a dbe char
|
|
333
|
+
"""
|
|
334
|
+
if not isinstance(text, bytes):
|
|
335
|
+
raise TypeError(text)
|
|
336
|
+
v = text[pos]
|
|
337
|
+
|
|
338
|
+
if 0x40 <= v < 0x7F:
|
|
339
|
+
# might be second half of big5, uhc or gbk encoding
|
|
340
|
+
if pos == line_start:
|
|
341
|
+
return 0
|
|
342
|
+
|
|
343
|
+
if text[pos - 1] >= 0x81 and within_double_byte(text, line_start, pos - 1) == 1:
|
|
344
|
+
return 2
|
|
345
|
+
return 0
|
|
346
|
+
|
|
347
|
+
if v < 0x80:
|
|
348
|
+
return 0
|
|
349
|
+
|
|
350
|
+
i = pos - 1
|
|
351
|
+
while i >= line_start:
|
|
352
|
+
if text[i] < 0x80:
|
|
353
|
+
break
|
|
354
|
+
i -= 1
|
|
355
|
+
|
|
356
|
+
if (pos - i) & 1:
|
|
357
|
+
return 1
|
|
358
|
+
return 2
|