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/display/escape.py
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
# Urwid escape sequences common to curses_display and raw_display
|
|
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
|
+
"""
|
|
22
|
+
Terminal Escape Sequences for input and display
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from __future__ import annotations
|
|
26
|
+
|
|
27
|
+
import re
|
|
28
|
+
import sys
|
|
29
|
+
import typing
|
|
30
|
+
from collections.abc import MutableMapping, Sequence
|
|
31
|
+
|
|
32
|
+
from urwid import str_util
|
|
33
|
+
|
|
34
|
+
if typing.TYPE_CHECKING:
|
|
35
|
+
from collections.abc import Collection, Iterable
|
|
36
|
+
|
|
37
|
+
# NOTE: because of circular imports (urwid.util -> urwid.escape -> urwid.util)
|
|
38
|
+
# from urwid.util import is_mouse_event -- will not work here
|
|
39
|
+
import urwid.util # isort: skip # pylint: disable=wrong-import-position
|
|
40
|
+
|
|
41
|
+
IS_WINDOWS = sys.platform == "win32"
|
|
42
|
+
|
|
43
|
+
within_double_byte = str_util.within_double_byte
|
|
44
|
+
|
|
45
|
+
SO = "\x0e"
|
|
46
|
+
SI = "\x0f"
|
|
47
|
+
IBMPC_ON = "\x1b[11m"
|
|
48
|
+
IBMPC_OFF = "\x1b[10m"
|
|
49
|
+
|
|
50
|
+
DEC_TAG = "0"
|
|
51
|
+
DEC_SPECIAL_CHARS = "▮◆▒␉␌␍␊°±␋┘┐┌└┼⎺⎻─⎼⎽├┤┴┬│≤≥π≠£·"
|
|
52
|
+
ALT_DEC_SPECIAL_CHARS = "_`abcdefghijklmnopqrstuvwxyz{|}~"
|
|
53
|
+
|
|
54
|
+
DEC_SPECIAL_CHARMAP = {}
|
|
55
|
+
if len(DEC_SPECIAL_CHARS) != len(ALT_DEC_SPECIAL_CHARS):
|
|
56
|
+
raise RuntimeError(repr((DEC_SPECIAL_CHARS, ALT_DEC_SPECIAL_CHARS)))
|
|
57
|
+
|
|
58
|
+
for c, alt in zip(DEC_SPECIAL_CHARS, ALT_DEC_SPECIAL_CHARS):
|
|
59
|
+
DEC_SPECIAL_CHARMAP[ord(c)] = SO + alt + SI
|
|
60
|
+
|
|
61
|
+
SAFE_ASCII_DEC_SPECIAL_RE = re.compile(f"^[ -~{DEC_SPECIAL_CHARS}]*$")
|
|
62
|
+
DEC_SPECIAL_RE = re.compile(f"[{DEC_SPECIAL_CHARS}]")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
###################
|
|
66
|
+
# Input sequences
|
|
67
|
+
###################
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class MoreInputRequired(Exception):
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def escape_modifier(digit: str) -> str:
|
|
75
|
+
mode = ord(digit) - ord("1")
|
|
76
|
+
return "shift " * (mode & 1) + "meta " * ((mode & 2) // 2) + "ctrl " * ((mode & 4) // 4)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
input_sequences = [
|
|
80
|
+
("[A", "up"),
|
|
81
|
+
("[B", "down"),
|
|
82
|
+
("[C", "right"),
|
|
83
|
+
("[D", "left"),
|
|
84
|
+
("[E", "5"),
|
|
85
|
+
("[F", "end"),
|
|
86
|
+
("[G", "5"),
|
|
87
|
+
("[H", "home"),
|
|
88
|
+
("[1~", "home"),
|
|
89
|
+
("[2~", "insert"),
|
|
90
|
+
("[3~", "delete"),
|
|
91
|
+
("[4~", "end"),
|
|
92
|
+
("[5~", "page up"),
|
|
93
|
+
("[6~", "page down"),
|
|
94
|
+
("[7~", "home"),
|
|
95
|
+
("[8~", "end"),
|
|
96
|
+
("[[A", "f1"),
|
|
97
|
+
("[[B", "f2"),
|
|
98
|
+
("[[C", "f3"),
|
|
99
|
+
("[[D", "f4"),
|
|
100
|
+
("[[E", "f5"),
|
|
101
|
+
("[11~", "f1"),
|
|
102
|
+
("[12~", "f2"),
|
|
103
|
+
("[13~", "f3"),
|
|
104
|
+
("[14~", "f4"),
|
|
105
|
+
("[15~", "f5"),
|
|
106
|
+
("[17~", "f6"),
|
|
107
|
+
("[18~", "f7"),
|
|
108
|
+
("[19~", "f8"),
|
|
109
|
+
("[20~", "f9"),
|
|
110
|
+
("[21~", "f10"),
|
|
111
|
+
("[23~", "f11"),
|
|
112
|
+
("[24~", "f12"),
|
|
113
|
+
("[25~", "f13"),
|
|
114
|
+
("[26~", "f14"),
|
|
115
|
+
("[28~", "f15"),
|
|
116
|
+
("[29~", "f16"),
|
|
117
|
+
("[31~", "f17"),
|
|
118
|
+
("[32~", "f18"),
|
|
119
|
+
("[33~", "f19"),
|
|
120
|
+
("[34~", "f20"),
|
|
121
|
+
("OA", "up"),
|
|
122
|
+
("OB", "down"),
|
|
123
|
+
("OC", "right"),
|
|
124
|
+
("OD", "left"),
|
|
125
|
+
("OH", "home"),
|
|
126
|
+
("OF", "end"),
|
|
127
|
+
("OP", "f1"),
|
|
128
|
+
("OQ", "f2"),
|
|
129
|
+
("OR", "f3"),
|
|
130
|
+
("OS", "f4"),
|
|
131
|
+
("Oo", "/"),
|
|
132
|
+
("Oj", "*"),
|
|
133
|
+
("Om", "-"),
|
|
134
|
+
("Ok", "+"),
|
|
135
|
+
("[Z", "shift tab"),
|
|
136
|
+
("On", "."),
|
|
137
|
+
("[200~", "begin paste"),
|
|
138
|
+
("[201~", "end paste"),
|
|
139
|
+
*(
|
|
140
|
+
(prefix + letter, modifier + key)
|
|
141
|
+
for prefix, modifier in zip("O[", ("meta ", "shift "))
|
|
142
|
+
for letter, key in zip("abcd", ("up", "down", "right", "left"))
|
|
143
|
+
),
|
|
144
|
+
*(
|
|
145
|
+
(f"[{digit}{symbol}", modifier + key)
|
|
146
|
+
for modifier, symbol in zip(("shift ", "meta "), "$^")
|
|
147
|
+
for digit, key in zip("235678", ("insert", "delete", "page up", "page down", "home", "end"))
|
|
148
|
+
),
|
|
149
|
+
*((f"O{ord('p') + n:c}", str(n)) for n in range(10)),
|
|
150
|
+
*(
|
|
151
|
+
# modified cursor keys + home, end, 5 -- [#X and [1;#X forms
|
|
152
|
+
(prefix + digit + letter, escape_modifier(digit) + key)
|
|
153
|
+
for prefix in ("[", "[1;")
|
|
154
|
+
for digit in "12345678"
|
|
155
|
+
for letter, key in zip("ABCDEFGH", ("up", "down", "right", "left", "5", "end", "5", "home"))
|
|
156
|
+
),
|
|
157
|
+
*(
|
|
158
|
+
# modified F1-F4 keys - O#X form and [1;#X form
|
|
159
|
+
(prefix + digit + letter, escape_modifier(digit) + f"f{number}")
|
|
160
|
+
for prefix in ("O", "[1;")
|
|
161
|
+
for digit in "12345678"
|
|
162
|
+
for number, letter in enumerate("PQRS", start=1)
|
|
163
|
+
),
|
|
164
|
+
*(
|
|
165
|
+
# modified F1-F13 keys -- [XX;#~ form
|
|
166
|
+
(f"[{num!s};{digit}~", escape_modifier(digit) + key)
|
|
167
|
+
for digit in "12345678"
|
|
168
|
+
for num, key in zip(
|
|
169
|
+
(3, 5, 6, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 23, 24, 25, 26, 28, 29, 31, 32, 33, 34),
|
|
170
|
+
(
|
|
171
|
+
"delete",
|
|
172
|
+
"page up",
|
|
173
|
+
"page down",
|
|
174
|
+
*(f"f{idx}" for idx in range(1, 21)),
|
|
175
|
+
),
|
|
176
|
+
)
|
|
177
|
+
),
|
|
178
|
+
# mouse reporting (special handling done in KeyqueueTrie)
|
|
179
|
+
("[M", "mouse"),
|
|
180
|
+
# mouse reporting for SGR 1006
|
|
181
|
+
("[<", "sgrmouse"),
|
|
182
|
+
# report status response
|
|
183
|
+
("[0n", "status ok"),
|
|
184
|
+
]
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class KeyqueueTrie:
|
|
188
|
+
__slots__ = ("data",)
|
|
189
|
+
|
|
190
|
+
def __init__(self, sequences: Iterable[tuple[str, str]]) -> None:
|
|
191
|
+
self.data: dict[int, str | dict[int, str | dict[int, str]]] = {}
|
|
192
|
+
for s, result in sequences:
|
|
193
|
+
if isinstance(result, dict):
|
|
194
|
+
raise TypeError(result)
|
|
195
|
+
self.add(self.data, s, result)
|
|
196
|
+
|
|
197
|
+
def add(
|
|
198
|
+
self,
|
|
199
|
+
root: MutableMapping[int, str | MutableMapping[int, str | MutableMapping[int, str]]],
|
|
200
|
+
s: str,
|
|
201
|
+
result: str,
|
|
202
|
+
) -> None:
|
|
203
|
+
if not isinstance(root, MutableMapping) or not s:
|
|
204
|
+
raise RuntimeError("trie conflict detected")
|
|
205
|
+
|
|
206
|
+
if ord(s[0]) in root:
|
|
207
|
+
self.add(root[ord(s[0])], s[1:], result)
|
|
208
|
+
return
|
|
209
|
+
if len(s) > 1:
|
|
210
|
+
d = {}
|
|
211
|
+
root[ord(s[0])] = d
|
|
212
|
+
self.add(d, s[1:], result)
|
|
213
|
+
return
|
|
214
|
+
root[ord(s)] = result
|
|
215
|
+
|
|
216
|
+
def get(self, keys, more_available: bool):
|
|
217
|
+
result = self.get_recurse(self.data, keys, more_available)
|
|
218
|
+
if not result:
|
|
219
|
+
result = self.read_cursor_position(keys, more_available)
|
|
220
|
+
return result
|
|
221
|
+
|
|
222
|
+
def get_recurse(
|
|
223
|
+
self,
|
|
224
|
+
root: (
|
|
225
|
+
MutableMapping[int, str | MutableMapping[int, str | MutableMapping[int, str]]]
|
|
226
|
+
| typing.Literal["mouse", "sgrmouse"]
|
|
227
|
+
),
|
|
228
|
+
keys: Collection[int],
|
|
229
|
+
more_available: bool,
|
|
230
|
+
):
|
|
231
|
+
if not isinstance(root, MutableMapping):
|
|
232
|
+
if root == "mouse":
|
|
233
|
+
return self.read_mouse_info(keys, more_available)
|
|
234
|
+
|
|
235
|
+
if root == "sgrmouse":
|
|
236
|
+
return self.read_sgrmouse_info(keys, more_available)
|
|
237
|
+
|
|
238
|
+
return (root, keys)
|
|
239
|
+
if not keys:
|
|
240
|
+
# get more keys
|
|
241
|
+
if more_available:
|
|
242
|
+
raise MoreInputRequired()
|
|
243
|
+
return None
|
|
244
|
+
if keys[0] not in root:
|
|
245
|
+
return None
|
|
246
|
+
return self.get_recurse(root[keys[0]], keys[1:], more_available)
|
|
247
|
+
|
|
248
|
+
def read_mouse_info(self, keys: Collection[int], more_available: bool):
|
|
249
|
+
if len(keys) < 3:
|
|
250
|
+
if more_available:
|
|
251
|
+
raise MoreInputRequired()
|
|
252
|
+
return None
|
|
253
|
+
|
|
254
|
+
b = keys[0] - 32
|
|
255
|
+
x, y = (keys[1] - 33) % 256, (keys[2] - 33) % 256 # supports 0-255
|
|
256
|
+
|
|
257
|
+
prefixes = []
|
|
258
|
+
if b & 4:
|
|
259
|
+
prefixes.append("shift ")
|
|
260
|
+
if b & 8:
|
|
261
|
+
prefixes.append("meta ")
|
|
262
|
+
if b & 16:
|
|
263
|
+
prefixes.append("ctrl ")
|
|
264
|
+
if (b & MOUSE_MULTIPLE_CLICK_MASK) >> 9 == 1:
|
|
265
|
+
prefixes.append("double ")
|
|
266
|
+
if (b & MOUSE_MULTIPLE_CLICK_MASK) >> 9 == 2:
|
|
267
|
+
prefixes.append("triple ")
|
|
268
|
+
prefix = "".join(prefixes)
|
|
269
|
+
|
|
270
|
+
# 0->1, 1->2, 2->3, 64->4, 65->5
|
|
271
|
+
button = ((b & 64) // 64 * 3) + (b & 3) + 1
|
|
272
|
+
|
|
273
|
+
if b & 3 == 3:
|
|
274
|
+
action = "release"
|
|
275
|
+
button = 0
|
|
276
|
+
elif b & MOUSE_RELEASE_FLAG:
|
|
277
|
+
action = "release"
|
|
278
|
+
elif b & MOUSE_DRAG_FLAG:
|
|
279
|
+
action = "drag"
|
|
280
|
+
elif b & MOUSE_MULTIPLE_CLICK_MASK:
|
|
281
|
+
action = "click"
|
|
282
|
+
else:
|
|
283
|
+
action = "press"
|
|
284
|
+
|
|
285
|
+
return ((f"{prefix}mouse {action}", button, x, y), keys[3:])
|
|
286
|
+
|
|
287
|
+
def read_sgrmouse_info(self, keys: Collection[int], more_available: bool):
|
|
288
|
+
# Helpful links:
|
|
289
|
+
# https://stackoverflow.com/questions/5966903/how-to-get-mousemove-and-mouseclick-in-bash
|
|
290
|
+
# http://invisible-island.net/xterm/ctlseqs/ctlseqs.pdf
|
|
291
|
+
|
|
292
|
+
if not keys:
|
|
293
|
+
if more_available:
|
|
294
|
+
raise MoreInputRequired()
|
|
295
|
+
return None
|
|
296
|
+
|
|
297
|
+
value = ""
|
|
298
|
+
pos_m = 0
|
|
299
|
+
found_m = False
|
|
300
|
+
for k in keys:
|
|
301
|
+
value += chr(k)
|
|
302
|
+
if k in {ord("M"), ord("m")}:
|
|
303
|
+
found_m = True
|
|
304
|
+
break
|
|
305
|
+
pos_m += 1
|
|
306
|
+
if not found_m:
|
|
307
|
+
if more_available:
|
|
308
|
+
raise MoreInputRequired()
|
|
309
|
+
return None
|
|
310
|
+
|
|
311
|
+
(b, x, y) = (int(val) for val in value[:-1].split(";"))
|
|
312
|
+
action = value[-1]
|
|
313
|
+
|
|
314
|
+
# shift, etc. is not communicated on my machine, so I
|
|
315
|
+
# can't and won't be able to add support for it.
|
|
316
|
+
# Double and triple clicks are not supported as well. They can be
|
|
317
|
+
# implemented by using a timer. This timer can check if the last
|
|
318
|
+
# registered click is below a certain threshold. This threshold
|
|
319
|
+
# is normally set in the operating system itself, so setting one
|
|
320
|
+
# here will cause an inconsistent behaviour. I do not plan to use
|
|
321
|
+
# that feature, so I won't implement it.
|
|
322
|
+
|
|
323
|
+
prefixes = []
|
|
324
|
+
if b & 8:
|
|
325
|
+
prefixes.append("meta ")
|
|
326
|
+
if b & 16:
|
|
327
|
+
prefixes.append("ctrl ")
|
|
328
|
+
prefix = "".join(prefixes)
|
|
329
|
+
|
|
330
|
+
wheel_used: typing.Literal[0, 1] = (b & 64) >> 6
|
|
331
|
+
|
|
332
|
+
button = (wheel_used * 3) + (b & 3) + 1
|
|
333
|
+
x -= 1
|
|
334
|
+
y -= 1
|
|
335
|
+
|
|
336
|
+
if action == "M":
|
|
337
|
+
if b & MOUSE_DRAG_FLAG:
|
|
338
|
+
action = "drag"
|
|
339
|
+
else:
|
|
340
|
+
action = "press"
|
|
341
|
+
elif action == "m":
|
|
342
|
+
action = "release"
|
|
343
|
+
else:
|
|
344
|
+
raise ValueError(f"Unknown mouse action: {action!r}")
|
|
345
|
+
|
|
346
|
+
return ((f"{prefix}mouse {action}", button, x, y), keys[pos_m + 1 :])
|
|
347
|
+
|
|
348
|
+
def read_cursor_position(self, keys, more_available: bool):
|
|
349
|
+
"""
|
|
350
|
+
Interpret cursor position information being sent by the
|
|
351
|
+
user's terminal. Returned as ('cursor position', x, y)
|
|
352
|
+
where (x, y) == (0, 0) is the top left of the screen.
|
|
353
|
+
"""
|
|
354
|
+
if not keys:
|
|
355
|
+
if more_available:
|
|
356
|
+
raise MoreInputRequired()
|
|
357
|
+
return None
|
|
358
|
+
if keys[0] != ord("["):
|
|
359
|
+
return None
|
|
360
|
+
# read y value
|
|
361
|
+
y = 0
|
|
362
|
+
i = 1
|
|
363
|
+
for k in keys[i:]:
|
|
364
|
+
i += 1
|
|
365
|
+
if k == ord(";"):
|
|
366
|
+
if not y:
|
|
367
|
+
return None
|
|
368
|
+
break
|
|
369
|
+
if k < ord("0") or k > ord("9"):
|
|
370
|
+
return None
|
|
371
|
+
if not y and k == ord("0"):
|
|
372
|
+
return None
|
|
373
|
+
y = y * 10 + k - ord("0")
|
|
374
|
+
if not keys[i:]:
|
|
375
|
+
if more_available:
|
|
376
|
+
raise MoreInputRequired()
|
|
377
|
+
return None
|
|
378
|
+
# read x value
|
|
379
|
+
x = 0
|
|
380
|
+
for k in keys[i:]:
|
|
381
|
+
i += 1
|
|
382
|
+
if k == ord("R"):
|
|
383
|
+
if not x:
|
|
384
|
+
return None
|
|
385
|
+
return (("cursor position", x - 1, y - 1), keys[i:])
|
|
386
|
+
if k < ord("0") or k > ord("9"):
|
|
387
|
+
return None
|
|
388
|
+
if not x and k == ord("0"):
|
|
389
|
+
return None
|
|
390
|
+
x = x * 10 + k - ord("0")
|
|
391
|
+
if not keys[i:] and more_available:
|
|
392
|
+
raise MoreInputRequired()
|
|
393
|
+
return None
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
# This is added to button value to signal mouse release by curses_display
|
|
397
|
+
# and raw_display when we know which button was released. NON-STANDARD
|
|
398
|
+
MOUSE_RELEASE_FLAG = 2048
|
|
399
|
+
|
|
400
|
+
# This 2-bit mask is used to check if the mouse release from curses or gpm
|
|
401
|
+
# is a double or triple release. 00 means single click, 01 double,
|
|
402
|
+
# 10 triple. NON-STANDARD
|
|
403
|
+
MOUSE_MULTIPLE_CLICK_MASK = 1536
|
|
404
|
+
|
|
405
|
+
# This is added to button value at mouse release to differentiate between
|
|
406
|
+
# single, double and triple press. Double release adds this times one,
|
|
407
|
+
# triple release adds this times two. NON-STANDARD
|
|
408
|
+
MOUSE_MULTIPLE_CLICK_FLAG = 512
|
|
409
|
+
|
|
410
|
+
# xterm adds this to the button value to signal a mouse drag event
|
|
411
|
+
MOUSE_DRAG_FLAG = 32
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
#################################################
|
|
415
|
+
# Build the input trie from input_sequences list
|
|
416
|
+
input_trie = KeyqueueTrie(input_sequences)
|
|
417
|
+
#################################################
|
|
418
|
+
|
|
419
|
+
_keyconv = {
|
|
420
|
+
-1: None,
|
|
421
|
+
8: "backspace",
|
|
422
|
+
9: "tab",
|
|
423
|
+
10: "enter",
|
|
424
|
+
13: "enter",
|
|
425
|
+
127: "backspace",
|
|
426
|
+
# curses-only keycodes follow.. (XXX: are these used anymore?)
|
|
427
|
+
258: "down",
|
|
428
|
+
259: "up",
|
|
429
|
+
260: "left",
|
|
430
|
+
261: "right",
|
|
431
|
+
262: "home",
|
|
432
|
+
263: "backspace",
|
|
433
|
+
265: "f1",
|
|
434
|
+
266: "f2",
|
|
435
|
+
267: "f3",
|
|
436
|
+
268: "f4",
|
|
437
|
+
269: "f5",
|
|
438
|
+
270: "f6",
|
|
439
|
+
271: "f7",
|
|
440
|
+
272: "f8",
|
|
441
|
+
273: "f9",
|
|
442
|
+
274: "f10",
|
|
443
|
+
275: "f11",
|
|
444
|
+
276: "f12",
|
|
445
|
+
277: "shift f1",
|
|
446
|
+
278: "shift f2",
|
|
447
|
+
279: "shift f3",
|
|
448
|
+
280: "shift f4",
|
|
449
|
+
281: "shift f5",
|
|
450
|
+
282: "shift f6",
|
|
451
|
+
283: "shift f7",
|
|
452
|
+
284: "shift f8",
|
|
453
|
+
285: "shift f9",
|
|
454
|
+
286: "shift f10",
|
|
455
|
+
287: "shift f11",
|
|
456
|
+
288: "shift f12",
|
|
457
|
+
330: "delete",
|
|
458
|
+
331: "insert",
|
|
459
|
+
338: "page down",
|
|
460
|
+
339: "page up",
|
|
461
|
+
343: "enter", # on numpad
|
|
462
|
+
350: "5", # on numpad
|
|
463
|
+
360: "end",
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if IS_WINDOWS:
|
|
467
|
+
_keyconv[351] = "shift tab"
|
|
468
|
+
_keyconv[358] = "end"
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
def process_keyqueue(codes: Sequence[int], more_available: bool) -> tuple[list[str], Sequence[int]]:
|
|
472
|
+
"""
|
|
473
|
+
codes -- list of key codes
|
|
474
|
+
more_available -- if True then raise MoreInputRequired when in the
|
|
475
|
+
middle of a character sequence (escape/utf8/wide) and caller
|
|
476
|
+
will attempt to send more key codes on the next call.
|
|
477
|
+
|
|
478
|
+
returns (list of input, list of remaining key codes).
|
|
479
|
+
"""
|
|
480
|
+
code = codes[0]
|
|
481
|
+
if 32 <= code <= 126:
|
|
482
|
+
key = chr(code)
|
|
483
|
+
return [key], codes[1:]
|
|
484
|
+
if code in _keyconv:
|
|
485
|
+
return [_keyconv[code]], codes[1:]
|
|
486
|
+
if 0 < code < 27:
|
|
487
|
+
return [f"ctrl {ord('a') + code - 1:c}"], codes[1:]
|
|
488
|
+
if 27 < code < 32:
|
|
489
|
+
return [f"ctrl {ord('A') + code - 1:c}"], codes[1:]
|
|
490
|
+
|
|
491
|
+
em = str_util.get_byte_encoding()
|
|
492
|
+
|
|
493
|
+
if (
|
|
494
|
+
em == "wide"
|
|
495
|
+
and code < 256
|
|
496
|
+
and within_double_byte(
|
|
497
|
+
code.to_bytes(1, "little"),
|
|
498
|
+
0,
|
|
499
|
+
0,
|
|
500
|
+
)
|
|
501
|
+
):
|
|
502
|
+
if not codes[1:] and more_available:
|
|
503
|
+
raise MoreInputRequired()
|
|
504
|
+
if codes[1:] and codes[1] < 256:
|
|
505
|
+
db = chr(code) + chr(codes[1])
|
|
506
|
+
if within_double_byte(db, 0, 1):
|
|
507
|
+
return [db], codes[2:]
|
|
508
|
+
|
|
509
|
+
if em == "utf8" and 127 < code < 256:
|
|
510
|
+
if code & 0xE0 == 0xC0: # 2-byte form
|
|
511
|
+
need_more = 1
|
|
512
|
+
elif code & 0xF0 == 0xE0: # 3-byte form
|
|
513
|
+
need_more = 2
|
|
514
|
+
elif code & 0xF8 == 0xF0: # 4-byte form
|
|
515
|
+
need_more = 3
|
|
516
|
+
else:
|
|
517
|
+
return [f"<{code:d}>"], codes[1:]
|
|
518
|
+
|
|
519
|
+
for i in range(1, need_more + 1):
|
|
520
|
+
if len(codes) <= i:
|
|
521
|
+
if more_available:
|
|
522
|
+
raise MoreInputRequired()
|
|
523
|
+
|
|
524
|
+
return [f"<{code:d}>"], codes[1:]
|
|
525
|
+
|
|
526
|
+
k = codes[i]
|
|
527
|
+
if k > 256 or k & 0xC0 != 0x80:
|
|
528
|
+
return [f"<{code:d}>"], codes[1:]
|
|
529
|
+
|
|
530
|
+
s = bytes(codes[: need_more + 1])
|
|
531
|
+
|
|
532
|
+
try:
|
|
533
|
+
return [s.decode("utf-8")], codes[need_more + 1 :]
|
|
534
|
+
except UnicodeDecodeError:
|
|
535
|
+
return [f"<{code:d}>"], codes[1:]
|
|
536
|
+
|
|
537
|
+
if 127 < code < 256:
|
|
538
|
+
key = chr(code)
|
|
539
|
+
return [key], codes[1:]
|
|
540
|
+
if code != 27:
|
|
541
|
+
return [f"<{code:d}>"], codes[1:]
|
|
542
|
+
|
|
543
|
+
result = input_trie.get(codes[1:], more_available)
|
|
544
|
+
|
|
545
|
+
if result is not None:
|
|
546
|
+
result, remaining_codes = result
|
|
547
|
+
return [result], remaining_codes
|
|
548
|
+
|
|
549
|
+
if codes[1:]:
|
|
550
|
+
# Meta keys -- ESC+Key form
|
|
551
|
+
run, remaining_codes = process_keyqueue(codes[1:], more_available)
|
|
552
|
+
if urwid.util.is_mouse_event(run[0]):
|
|
553
|
+
return ["esc", *run], remaining_codes
|
|
554
|
+
if run[0] == "esc" or run[0].find("meta ") >= 0:
|
|
555
|
+
return ["esc", *run], remaining_codes
|
|
556
|
+
return [f"meta {run[0]}"] + run[1:], remaining_codes
|
|
557
|
+
|
|
558
|
+
return ["esc"], codes[1:]
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
####################
|
|
562
|
+
# Output sequences
|
|
563
|
+
####################
|
|
564
|
+
|
|
565
|
+
ESC = "\x1b"
|
|
566
|
+
|
|
567
|
+
CURSOR_HOME = f"{ESC}[H"
|
|
568
|
+
CURSOR_HOME_COL = "\r"
|
|
569
|
+
|
|
570
|
+
APP_KEYPAD_MODE = f"{ESC}="
|
|
571
|
+
NUM_KEYPAD_MODE = f"{ESC}>"
|
|
572
|
+
|
|
573
|
+
SWITCH_TO_ALTERNATE_BUFFER = f"{ESC}[?1049h"
|
|
574
|
+
RESTORE_NORMAL_BUFFER = f"{ESC}[?1049l"
|
|
575
|
+
|
|
576
|
+
ENABLE_BRACKETED_PASTE_MODE = f"{ESC}[?2004h"
|
|
577
|
+
DISABLE_BRACKETED_PASTE_MODE = f"{ESC}[?2004l"
|
|
578
|
+
|
|
579
|
+
# RESET_SCROLL_REGION = ESC+"[;r"
|
|
580
|
+
# RESET = ESC+"c"
|
|
581
|
+
|
|
582
|
+
REPORT_STATUS = f"{ESC}[5n"
|
|
583
|
+
REPORT_CURSOR_POSITION = f"{ESC}[6n"
|
|
584
|
+
|
|
585
|
+
INSERT_ON = f"{ESC}[4h"
|
|
586
|
+
INSERT_OFF = f"{ESC}[4l"
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
def set_cursor_position(x: int, y: int) -> str:
|
|
590
|
+
if not isinstance(x, int):
|
|
591
|
+
raise TypeError(x)
|
|
592
|
+
if not isinstance(y, int):
|
|
593
|
+
raise TypeError(y)
|
|
594
|
+
|
|
595
|
+
return ESC + f"[{y + 1:d};{x + 1:d}H"
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
def move_cursor_right(x: int) -> str:
|
|
599
|
+
if x < 1:
|
|
600
|
+
return ""
|
|
601
|
+
return ESC + f"[{x:d}C"
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
def move_cursor_up(x: int) -> str:
|
|
605
|
+
if x < 1:
|
|
606
|
+
return ""
|
|
607
|
+
return ESC + f"[{x:d}A"
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
def move_cursor_down(x: int) -> str:
|
|
611
|
+
if x < 1:
|
|
612
|
+
return ""
|
|
613
|
+
return ESC + f"[{x:d}B"
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
HIDE_CURSOR = f"{ESC}[?25l"
|
|
617
|
+
SHOW_CURSOR = f"{ESC}[?25h"
|
|
618
|
+
|
|
619
|
+
MOUSE_TRACKING_ON = f"{ESC}[?1000h{ESC}[?1002h{ESC}[?1006h"
|
|
620
|
+
MOUSE_TRACKING_OFF = f"{ESC}[?1006l{ESC}[?1002l{ESC}[?1000l"
|
|
621
|
+
|
|
622
|
+
DESIGNATE_G1_SPECIAL = f"{ESC})0"
|
|
623
|
+
|
|
624
|
+
ERASE_IN_LINE_RIGHT = f"{ESC}[K"
|