libvt 0.0.1a0__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.
libvt/_ucd.py ADDED
@@ -0,0 +1,460 @@
1
+ """Generated two-stage Unicode 17.0.0 width and UAX #29 table.
2
+
3
+ Regenerate with ``uv run python scripts/generate_ucd.py``.
4
+
5
+ Packed 16-bit entry layout in ``_STAGE2``:
6
+ - bits ``0..1``: cell width (``0``, ``1``, or ``2``)
7
+ - bits ``2..5``: ``Grapheme_Cluster_Break`` property ID (``0..13``)
8
+ - bits ``6..7``: ``Indic_Conjunct_Break`` property ID (``0..3``)
9
+ - bit ``8``: ``Extended_Pictographic``
10
+ - bit ``9``: ``Emoji_Modifier``
11
+ - bit ``10``: ``emoji-variation-sequences.txt`` ``FE0E``/``FE0F`` base
12
+ - bit ``11``: BiDi control / spoofing codepoint
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import array
18
+ import base64
19
+ import enum
20
+ import re
21
+ import sys
22
+ import typing as t
23
+ import zlib
24
+
25
+ if t.TYPE_CHECKING:
26
+ from libvt.config import Profile
27
+
28
+
29
+ class WidthRole(enum.IntEnum):
30
+ """Column occupancy role of a terminal grid cell."""
31
+
32
+ NARROW = 0
33
+ WIDE = 1
34
+ SPACER_TAIL = 2
35
+ SPACER_HEAD = 3
36
+
37
+
38
+ GCB_OTHER = 0
39
+ GCB_CR = 1
40
+ GCB_LF = 2
41
+ GCB_CONTROL = 3
42
+ GCB_EXTEND = 4
43
+ GCB_ZWJ = 5
44
+ GCB_REGIONAL_INDICATOR = 6
45
+ GCB_PREPEND = 7
46
+ GCB_SPACING_MARK = 8
47
+ GCB_L = 9
48
+ GCB_V = 10
49
+ GCB_T = 11
50
+ GCB_LV = 12
51
+ GCB_LVT = 13
52
+
53
+ INCB_NONE = 0
54
+ INCB_LINKER = 1
55
+ INCB_CONSONANT = 2
56
+ INCB_EXTEND = 3
57
+
58
+ EXT_PICT_BIT = 1 << 8
59
+ EMOJI_MOD_BIT = 1 << 9
60
+ EMOJI_VS_BASE_BIT = 1 << 10
61
+ BIDI_SPOOF_BIT = 1 << 11
62
+
63
+ CLUSTER_NONE = 0
64
+ _ACTIVE_BIT = 1
65
+
66
+ UNICODE_VERSION = "17.0.0"
67
+ EXPANDED_UCD_SHA256 = "a6a2bbf94b9ebb25bb22b06a46a605b468b0a398bd9ee64e60dbc717c2484cb3"
68
+
69
+ SAFE_NARROW_BMP_RE: t.Final[re.Pattern[str]] = re.compile(
70
+ r"[ -~\xa0-\xa8\xaa-\xac\xaf-\u02ff\u0370-\u0482\u048a-\u0590\u05be\u05c0"
71
+ r"\u05c3\u05c6\u05c8-\u05ff\u0606-\u060f\u061b\u061d-\u064a\u0660-\u066f\u"
72
+ r"0671-\u06d5\u06de\u06e5\u06e6\u06e9\u06ee-\u070e\u0710\u0712-\u072f\u074"
73
+ r"b-\u07a5\u07b1-\u07ea\u07f4-\u07fc\u07fe-\u0815\u081a\u0824\u0828\u082e-"
74
+ r"\u0858\u085c-\u088f\u0892-\u0896\u08a0-\u08c9\u0904-\u0914\u093d\u0950\u"
75
+ r"0960\u0961\u0964-\u0977\u0980\u0984-\u0994\u09a9\u09b1\u09b3-\u09b5\u09b"
76
+ r"a\u09bb\u09bd\u09c5\u09c6\u09c9\u09ca\u09ce-\u09d6\u09d8-\u09db\u09de\u0"
77
+ r"9e0\u09e1\u09e4-\u09ef\u09f2-\u09fd\u09ff\u0a00\u0a04-\u0a3b\u0a3d\u0a43"
78
+ r"-\u0a46\u0a49\u0a4a\u0a4e-\u0a50\u0a52-\u0a6f\u0a72-\u0a74\u0a76-\u0a80"
79
+ r"\u0a84-\u0a94\u0aa9\u0ab1\u0ab4\u0aba\u0abb\u0abd\u0ac6\u0aca\u0ace-\u0a"
80
+ r"e1\u0ae4-\u0af8\u0b00\u0b04-\u0b14\u0b29\u0b31\u0b34\u0b3a\u0b3b\u0b3d\u"
81
+ r"0b45\u0b46\u0b49\u0b4a\u0b4e-\u0b54\u0b58-\u0b5b\u0b5e\u0b60\u0b61\u0b64"
82
+ r"-\u0b70\u0b72-\u0b81\u0b83-\u0bbd\u0bc3-\u0bc5\u0bc9\u0bce-\u0bd6\u0bd8-"
83
+ r"\u0bff\u0c05-\u0c14\u0c29\u0c3a\u0c3b\u0c3d\u0c45\u0c49\u0c4e-\u0c54\u0c"
84
+ r"57\u0c5b-\u0c61\u0c64-\u0c80\u0c84-\u0cbb\u0cbd\u0cc5\u0cc9\u0cce-\u0cd4"
85
+ r"\u0cd7-\u0ce1\u0ce4-\u0cf2\u0cf4-\u0cff\u0d04-\u0d14\u0d3d\u0d45\u0d49\u"
86
+ r"0d4f-\u0d56\u0d58-\u0d61\u0d64-\u0d80\u0d84-\u0dc9\u0dcb-\u0dce\u0dd5\u0"
87
+ r"dd7\u0de0-\u0df1\u0df4-\u0e30\u0e32\u0e3b-\u0e46\u0e4f-\u0eb0\u0eb2\u0eb"
88
+ r"d-\u0ec7\u0ecf-\u0f17\u0f1a-\u0f34\u0f36\u0f38\u0f3a-\u0f3d\u0f40-\u0f70"
89
+ r"\u0f85\u0f88-\u0f8c\u0f98\u0fbd-\u0fc5\u0fc7-\u0fff\u1040-\u104f\u1090-"
90
+ r"\u1099\u109e-\u10ff\u1200-\u135c\u1360-\u1711\u1716-\u1731\u1735-\u1751"
91
+ r"\u1754-\u1771\u1774-\u177f\u17d4-\u17dc\u17de-\u180a\u1810-\u1884\u1887-"
92
+ r"\u18a8\u18aa-\u191f\u192c-\u192f\u193c-\u1a16\u1a1c-\u1a1f\u1a5f\u1a7d\u"
93
+ r"1a7e\u1a80-\u1aaf\u1ade\u1adf\u1aec-\u1aff\u1b05-\u1b0a\u1b0d-\u1b12\u1b"
94
+ r"4d-\u1b6a\u1b74-\u1b7f\u1bb0-\u1bba\u1bbe-\u1be5\u1bf4-\u1c23\u1c38-\u1c"
95
+ r"cf\u1cd3\u1ce9-\u1cec\u1cee-\u1cf3\u1cf5\u1cf6\u1cfa-\u1dbf\u1e00-\u200a"
96
+ r"\u2010-\u2027\u202f-\u203b\u203d-\u2048\u204a-\u205f\u2070-\u20cf\u20f1-"
97
+ r"\u2121\u2123-\u2138\u213a-\u2193\u219a-\u21a8\u21ab-\u2319\u231c-\u2327"
98
+ r"\u232b-\u23ce\u23d0-\u23e8\u23f4-\u23f7\u23fb-\u24c1\u24c3-\u25a9\u25ac-"
99
+ r"\u25b5\u25b7-\u25bf\u25c1-\u25fa\u25ff\u2605-\u260d\u260f\u2610\u2612\u2"
100
+ r"613\u2616\u2617\u2619-\u261c\u261e\u261f\u2621\u2624\u2625\u2627-\u2629"
101
+ r"\u262b-\u262d\u263b-\u263f\u2641\u2643-\u2647\u2654-\u265e\u2661\u2662\u"
102
+ r"2664\u2667\u2669-\u267a\u267c\u267d\u2680-\u2689\u2690\u2691\u2698\u269a"
103
+ r"\u269d-\u269f\u26a2-\u26a6\u26a8\u26a9\u26ac-\u26af\u26b2-\u26bc\u26bf-"
104
+ r"\u26c3\u26c6\u26c7\u26c9-\u26cd\u26d0\u26d2\u26d5-\u26e8\u26eb-\u26ef\u2"
105
+ r"6f6\u26fb\u26fc\u26fe-\u2701\u2703\u2704\u2706\u2707\u270e\u2710\u2711\u"
106
+ r"2713\u2715\u2717-\u271c\u271e-\u2720\u2722-\u2727\u2729-\u2732\u2735-\u2"
107
+ r"743\u2745\u2746\u2748-\u274b\u274d\u274f-\u2752\u2756\u2758-\u2762\u2765"
108
+ r"-\u2794\u2798-\u27a0\u27a2-\u27af\u27b1-\u27be\u27c0-\u2933\u2936-\u2b04"
109
+ r"\u2b08-\u2b1a\u2b1d-\u2b4f\u2b51-\u2b54\u2b56-\u2cee\u2cf2-\u2d7e\u2d80-"
110
+ r"\u2ddf\u2e00-\u2e7f\u2e9a\u2ef4-\u2eff\u2fd6-\u2fef\u303f\u3040\u3097\u3"
111
+ r"098\u3100-\u3104\u3130\u318f\u31e6-\u31ee\u321f\u3248-\u324f\ua48d-\ua48"
112
+ r"f\ua4c7-\ua66e\ua673\ua67e-\ua69d\ua6a0-\ua6ef\ua6f2-\ua801\ua803-\ua805"
113
+ r"\ua807-\ua80a\ua80c-\ua822\ua828-\ua82b\ua82d-\ua87f\ua882-\ua8b3\ua8c6-"
114
+ r"\ua8df\ua8f2-\ua8fe\ua900-\ua925\ua92e-\ua946\ua954-\ua95f\ua97d-\ua97f"
115
+ r"\ua984-\ua988\ua98c-\ua98e\ua9c1-\ua9df\ua9e6\ua9f0-\ua9f9\ua9ff-\uaa28"
116
+ r"\uaa37-\uaa42\uaa44-\uaa4b\uaa4e-\uaa5f\uaa70\uaa74-\uaa79\uaa80-\uaaaf"
117
+ r"\uaab1\uaab5\uaab6\uaab9-\uaabd\uaac0\uaac2-\uaadf\uaaf0-\uaaf4\uaaf7-\u"
118
+ r"abbf\uabdb-\uabe2\uabeb\uabee-\uabff\ud7a4-\ud7af\ud7c7-\ud7ca\ud7fc-\ud"
119
+ r"7ff\ue000-\uf8ff\ufb00-\ufb1d\ufb1f-\ufdff\ufe1a-\ufe1f\ufe53\ufe67\ufe6"
120
+ r"c-\ufefe\uff00\uff61-\uff9d\uffa0-\uffdf\uffe7-\uffef\ufffc-\uffff]+"
121
+ )
122
+
123
+ _PACKED_B85 = (
124
+ "c-rk<XPe_R5FVQqEU*hl=O7EZ1C9fZcJ$tR3%&Q=d;gSwD0uimIEfugvLws4?6|yspE!xFIx`xLI"
125
+ "vu6tiW0<h3hfZ1Q|UC?rPJvQI+ONjpU$GQ=}_|ioRzDH&ZYC{e7b-xq>Jd{)sw??NpioGW^_Q8(d"
126
+ "Ef+o_vnbQM!Vzq^sx{T}{X7n$hFhB-hs^pA&RF-9R_eO>{Gzq+94#x{YoR_U{h5Q$ti4%hcYr`gJ"
127
+ "$mlics6`{;hf#RIGRgY*zROpnl`^cX!(PtcR}6g^GP(6jU$Jx?#toL;1tykmX2MoRtgO5w??^jdO"
128
+ "%J^8#*o1bseTLIGcb`sJ%^seE}dtUzEPd*>ehxC#1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-6p}5LjHi4+@(KXHz5MHP$31q?X?l8dv3DTv;~rnXFR;d53B*=s2LNr@"
154
+ "MRJQ3R04oLl+ixu2=D`F1IIVt>N<L0=-R7%=iIO`Yp?!NC_i(>u79_ALIlv~mCo3&9+Cr$8MYlXq"
155
+ "M+yZl%=6*`pZKZ$oWA4fEr;mJu7+t_SpJxL!f&2kSZWlPp2$b#Zjj+Nez(_@T3a8KnJvc+dknG{O"
156
+ "+>;x7Y7o#^pC_MAWWphHcTf=4bj^b;a3yV;X+|fZ<6GY*5te;6G#lRG3apBb@w)EDXPWhI|4o^#i"
157
+ "fMA!gTDC}g|JeU-UzaV0DTtEYeXU=geJZcY8wy@-Y~)6#XAsv}<JkJ-aC_M<ZI9@n+YoN_GvCVTi"
158
+ "={88-8v)v~ypDVV5tx)8h_Af0%OoyS=usGT}Sgq7BNnfyy-YDjhS*cf+CMNkd(6C6~)CDWc={lt;"
159
+ "pVDXl#u@DN@p{8N{aUb2SDtT$YD}SKEglDO)ig$Ce9ub!G0)h{^2V&v$8wiTd8iO}3uVz>E9WjYd"
160
+ "GUm$hSxNc!y=Gf=TA~Qz-cRJ?n|r;rnzuiDa`SdDWNfAEZWoHWvMl^-tMvImKjSht3f$ds8@~Qs2"
161
+ "q}O06C><y{$<0hS!qS#B3a&i2XlCc-U|G`;x0~?RTH_wb_0R**^BAZ#Djez5MiIaINnD6c}>-y4C"
162
+ "XOY~Q-{XKCBGEp8~9yUeYR_P4*og$viDFRYK7{2H+3dC>a}AO?>8i9-OR#y$Wz@IN@`B5("
163
+ )
164
+
165
+
166
+ def _unpack_tables() -> tuple[array.array[int], array.array[int]]:
167
+ raw = zlib.decompress(base64.b85decode(_PACKED_B85))
168
+ s1_len = int.from_bytes(raw[:4], "little")
169
+ s1 = array.array("H")
170
+ s1.frombytes(raw[4 : 4 + s1_len])
171
+ s2 = array.array("H")
172
+ s2.frombytes(raw[4 + s1_len :])
173
+ if sys.byteorder != "little":
174
+ s1.byteswap()
175
+ s2.byteswap()
176
+ return s1, s2
177
+
178
+
179
+ _STAGE1, _STAGE2 = _unpack_tables()
180
+
181
+
182
+ def ucd_lookup(cp: int) -> int:
183
+ """Return packed 16-bit width and UAX #29 property word for codepoint *cp*."""
184
+ if 0 <= cp <= 0x10FFFF:
185
+ return _STAGE2[(_STAGE1[cp >> 8] << 8) | (cp & 0xFF)]
186
+ return GCB_CONTROL << 2
187
+
188
+
189
+ def _pack_state(
190
+ gcb: int,
191
+ ri_odd: int,
192
+ ep_state: int,
193
+ incb_state: int,
194
+ vs_base: int,
195
+ has_spacing: int,
196
+ multi_scalar: int,
197
+ ) -> int:
198
+ return (
199
+ _ACTIVE_BIT
200
+ | (gcb << 1)
201
+ | (ri_odd << 5)
202
+ | (ep_state << 6)
203
+ | (incb_state << 8)
204
+ | (vs_base << 10)
205
+ | (has_spacing << 11)
206
+ | (multi_scalar << 12)
207
+ )
208
+
209
+
210
+ def init_cluster_state(cp: int, *, mode_2027: bool = True) -> int:
211
+ """Return initial packed cluster state for base codepoint *cp*."""
212
+ entry = ucd_lookup(cp)
213
+ w = entry & 0b11
214
+ gcb = (entry >> 2) & 0x0F
215
+ incb = (entry >> 6) & 0x03
216
+ ep = 1 if (entry & EXT_PICT_BIT) else 0
217
+ vs_b = 1 if (entry & EMOJI_VS_BASE_BIT) else 0
218
+ ri_odd = 1 if (mode_2027 and gcb == GCB_REGIONAL_INDICATOR) else 0
219
+ incb_st = 1 if incb == INCB_CONSONANT else 0
220
+ has_sp = 1 if w >= 1 else 0
221
+ return _pack_state(gcb, ri_odd, ep, incb_st, vs_b, has_sp, 0)
222
+
223
+
224
+ def char_width(ch: str) -> int:
225
+ """Return default terminal column width (``0``, ``1``, or ``2``) for *ch*."""
226
+ cp = ord(ch)
227
+ if 0x20 <= cp <= 0x7E:
228
+ return 1
229
+ return ucd_lookup(cp) & 0b11
230
+
231
+
232
+ def step_cluster(
233
+ prev_state: int,
234
+ cp: int,
235
+ *,
236
+ mode_2027: bool,
237
+ vs15_narrows: bool = True,
238
+ vs16_always_wide: bool = False,
239
+ ) -> tuple[bool, int, int]:
240
+ """Evaluate UAX #29 continuation between *prev_state* and codepoint *cp*.
241
+
242
+ Returns
243
+ -------
244
+ tuple[bool, int, int]
245
+ ``(extends_previous, next_state, width_override)`` where
246
+ ``width_override`` is ``-1`` (keep current cluster width), ``1``
247
+ (narrow via ``VS15`` on an emoji variation base or first narrow glyph
248
+ after ``Prepend``), or ``2`` (widen via ``VS16``, spacing continuation,
249
+ emoji modifier, ZWJ pictographic, or Regional Indicator pair).
250
+ """
251
+ entry = ucd_lookup(cp)
252
+ gcb = (entry >> 2) & 0x0F
253
+ incb = (entry >> 6) & 0x03
254
+ ep = bool(entry & EXT_PICT_BIT)
255
+ w = entry & 0b11
256
+
257
+ if prev_state & _ACTIVE_BIT:
258
+ prev_g = (prev_state >> 1) & 0x0F
259
+ ri_odd = (prev_state >> 5) & 0x01
260
+ ep_state = (prev_state >> 6) & 0x03
261
+ incb_state = (prev_state >> 8) & 0x03
262
+ vs_base = (prev_state >> 10) & 0x01
263
+ has_spacing = (prev_state >> 11) & 0x01
264
+ multi_scalar = (prev_state >> 12) & 0x01
265
+
266
+ if not mode_2027:
267
+ if gcb == GCB_EXTEND:
268
+ return (
269
+ True,
270
+ _pack_state(gcb, 0, ep_state, incb_state, 0, has_spacing, 1),
271
+ -1,
272
+ )
273
+ if w == 0:
274
+ return False, CLUSTER_NONE, -1
275
+ return False, init_cluster_state(cp, mode_2027=False), -1
276
+
277
+ joined = False
278
+ if prev_g == GCB_CR and gcb == GCB_LF:
279
+ joined = True
280
+ elif prev_g in (GCB_CR, GCB_LF, GCB_CONTROL) or gcb in (
281
+ GCB_CR,
282
+ GCB_LF,
283
+ GCB_CONTROL,
284
+ ):
285
+ joined = False
286
+ elif (
287
+ (prev_g == GCB_L and gcb in (GCB_L, GCB_V, GCB_LV, GCB_LVT))
288
+ or (prev_g in (GCB_LV, GCB_V) and gcb in (GCB_V, GCB_T))
289
+ or (prev_g in (GCB_LVT, GCB_T) and gcb == GCB_T)
290
+ or gcb in (GCB_EXTEND, GCB_ZWJ, GCB_SPACING_MARK)
291
+ or prev_g == GCB_PREPEND
292
+ or (incb_state == 2 and incb == INCB_CONSONANT)
293
+ or (ep_state == 2 and ep)
294
+ or (ri_odd == 1 and gcb == GCB_REGIONAL_INDICATOR)
295
+ ):
296
+ joined = True
297
+
298
+ if joined:
299
+ w_override = -1
300
+ if cp == 0xFE0F and (vs_base or (vs16_always_wide and has_spacing)):
301
+ w_override = 2
302
+ elif cp == 0xFE0E and vs_base and vs15_narrows and not multi_scalar:
303
+ w_override = 1
304
+ elif not has_spacing and w >= 1:
305
+ w_override = w
306
+ elif has_spacing and (
307
+ w >= 1
308
+ or gcb == GCB_SPACING_MARK
309
+ or ((entry & EMOJI_MOD_BIT) and ep_state == 1)
310
+ or (ep_state == 2 and ep)
311
+ or (ri_odd == 1 and gcb == GCB_REGIONAL_INDICATOR)
312
+ ):
313
+ w_override = 2
314
+
315
+ next_ri = (ri_odd ^ 1) if gcb == GCB_REGIONAL_INDICATOR else 0
316
+ if ep_state == 1 and gcb == GCB_EXTEND:
317
+ next_ep = 1
318
+ elif ep_state == 1 and gcb == GCB_ZWJ:
319
+ next_ep = 2
320
+ elif ep:
321
+ next_ep = 1
322
+ else:
323
+ next_ep = 0
324
+
325
+ if incb == INCB_CONSONANT:
326
+ next_incb = 1
327
+ elif incb_state in (1, 2) and incb == INCB_LINKER:
328
+ next_incb = 2
329
+ elif incb_state in (1, 2) and incb == INCB_EXTEND:
330
+ next_incb = incb_state
331
+ else:
332
+ next_incb = 0
333
+
334
+ next_vs = 1 if (entry & EMOJI_VS_BASE_BIT) else 0
335
+ next_sp = 1 if (has_spacing or w >= 1 or gcb == GCB_SPACING_MARK) else 0
336
+ next_multi = 0 if (not has_spacing and w >= 1) else 1
337
+ return (
338
+ True,
339
+ _pack_state(
340
+ gcb,
341
+ next_ri,
342
+ next_ep,
343
+ next_incb,
344
+ next_vs,
345
+ next_sp,
346
+ next_multi,
347
+ ),
348
+ w_override,
349
+ )
350
+
351
+ if w == 0 and gcb in (
352
+ GCB_CR,
353
+ GCB_LF,
354
+ GCB_CONTROL,
355
+ GCB_EXTEND,
356
+ GCB_ZWJ,
357
+ GCB_SPACING_MARK,
358
+ GCB_V,
359
+ GCB_T,
360
+ ):
361
+ return False, CLUSTER_NONE, -1
362
+ return False, init_cluster_state(cp, mode_2027=mode_2027), -1
363
+
364
+
365
+ def segment_graphemes(text: str) -> list[str]:
366
+ """Segment *text* into Unicode 17.0.0 UAX #29 extended grapheme clusters."""
367
+ if not text:
368
+ return []
369
+ clusters: list[str] = []
370
+ buf: list[str] = [text[0]]
371
+ state = init_cluster_state(ord(text[0]), mode_2027=True)
372
+ for ch in text[1:]:
373
+ cp = ord(ch)
374
+ entry = ucd_lookup(cp)
375
+ gcb = (entry >> 2) & 0x0F
376
+ prev_g = (state >> 1) & 0x0F
377
+ if prev_g in (GCB_CR, GCB_LF, GCB_CONTROL) and not (
378
+ prev_g == GCB_CR and gcb == GCB_LF
379
+ ):
380
+ clusters.append("".join(buf))
381
+ buf = [ch]
382
+ state = init_cluster_state(cp, mode_2027=True)
383
+ continue
384
+ extends, next_state, _ = step_cluster(state, cp, mode_2027=True)
385
+ if extends:
386
+ buf.append(ch)
387
+ state = next_state
388
+ else:
389
+ clusters.append("".join(buf))
390
+ buf = [ch]
391
+ state = init_cluster_state(cp, mode_2027=True)
392
+ clusters.append("".join(buf))
393
+ return clusters
394
+
395
+
396
+ def display_width(
397
+ text: str,
398
+ *,
399
+ mode_2027: bool = True,
400
+ profile: Profile | None = None,
401
+ ) -> int:
402
+ """Return rendered terminal column width of *text*."""
403
+ if text.isascii():
404
+ return sum(1 for ch in text if " " <= ch <= "~")
405
+ vs15_narrows = profile.vs15_narrows_wide_emoji if profile is not None else True
406
+ vs16_wide = profile.vs16_always_wide if profile is not None else False
407
+ total = 0
408
+ cur_w = 0
409
+ state = CLUSTER_NONE
410
+ for ch in text:
411
+ cp = ord(ch)
412
+ if 0x20 <= cp <= 0x7E and state == CLUSTER_NONE:
413
+ cur_w = 1
414
+ state = init_cluster_state(cp, mode_2027=mode_2027)
415
+ continue
416
+ extends, state, w_override = step_cluster(
417
+ state,
418
+ cp,
419
+ mode_2027=mode_2027,
420
+ vs15_narrows=vs15_narrows,
421
+ vs16_always_wide=vs16_wide,
422
+ )
423
+ if extends:
424
+ if w_override != -1:
425
+ cur_w = w_override
426
+ else:
427
+ total += cur_w
428
+ cur_w = ucd_lookup(cp) & 0b11
429
+ return total + cur_w
430
+
431
+
432
+ def sanitize_terminal_text(text: str) -> str:
433
+ """Escape C0, C1, ``DEL``, and BiDi override codepoints for safe display."""
434
+ out: list[str] = []
435
+ for ch in text:
436
+ cp = ord(ch)
437
+ if 0x20 <= cp <= 0x7E:
438
+ out.append(ch)
439
+ elif cp < 0x20 or 0x7F <= cp <= 0x9F or (ucd_lookup(cp) & BIDI_SPOOF_BIT):
440
+ out.append(f"\\x{cp:02x}" if cp <= 0xFF else f"\\u{cp:04x}")
441
+ else:
442
+ out.append(ch)
443
+ return "".join(out)
444
+
445
+
446
+ __all__ = [
447
+ "BIDI_SPOOF_BIT",
448
+ "CLUSTER_NONE",
449
+ "EXPANDED_UCD_SHA256",
450
+ "SAFE_NARROW_BMP_RE",
451
+ "UNICODE_VERSION",
452
+ "WidthRole",
453
+ "char_width",
454
+ "display_width",
455
+ "init_cluster_state",
456
+ "sanitize_terminal_text",
457
+ "segment_graphemes",
458
+ "step_cluster",
459
+ "ucd_lookup",
460
+ ]