Skip to content

Commit 0752ff0

Browse files
authored
Merge pull request #3953 from Textualize/zwj-fix
Fix ZWJ and edge cases
2 parents 1d402e0 + 54ae0cf commit 0752ff0

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [14.3.2] - 2026-02-01
9+
10+
### Fixed
11+
12+
- Fixed solo ZWJ crash https://github.com/Textualize/rich/pull/3953
13+
- Fixed control codes reporting width of 1 https://github.com/Textualize/rich/pull/3953
14+
815
## [14.3.1] - 2026-01-24
916

1017
### Fixed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "rich"
33
homepage = "https://github.com/Textualize/rich"
44
documentation = "https://rich.readthedocs.io/en/latest/"
5-
version = "14.3.1"
5+
version = "14.3.2"
66
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
77
authors = ["Will McGugan <[email protected]>"]
88
license = "MIT"

rich/cells.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,26 @@ def get_character_cell_size(character: str, unicode_version: str = "auto") -> in
5555
int: Number of cells (0, 1 or 2) occupied by that character.
5656
"""
5757
codepoint = ord(character)
58+
if codepoint and codepoint < 32 or 0x07F <= codepoint < 0x0A0:
59+
return 0
5860
table = load_cell_table(unicode_version).widths
59-
if codepoint > table[-1][1]:
61+
62+
last_entry = table[-1]
63+
if codepoint > last_entry[1]:
6064
return 1
65+
6166
lower_bound = 0
6267
upper_bound = len(table) - 1
63-
index = (lower_bound + upper_bound) // 2
64-
while True:
68+
69+
while lower_bound <= upper_bound:
70+
index = (lower_bound + upper_bound) >> 1
6571
start, end, width = table[index]
6672
if codepoint < start:
6773
upper_bound = index - 1
6874
elif codepoint > end:
6975
lower_bound = index + 1
7076
else:
71-
return 0 if width == -1 else width
72-
if upper_bound < lower_bound:
73-
break
74-
index = (lower_bound + upper_bound) // 2
77+
return width
7578
return 1
7679

7780

@@ -135,19 +138,22 @@ def _cell_len(text: str, unicode_version: str) -> int:
135138

136139
SPECIAL = {"\u200d", "\ufe0f"}
137140

138-
iter_characters = iter(text)
141+
index = 0
142+
character_count = len(text)
139143

140-
for character in iter_characters:
144+
while index < character_count:
145+
character = text[index]
141146
if character in SPECIAL:
142147
if character == "\u200d":
143-
next(iter_characters)
148+
index += 1
144149
elif last_measured_character:
145150
total_width += last_measured_character in cell_table.narrow_to_wide
146151
last_measured_character = None
147152
else:
148153
if character_width := get_character_cell_size(character, unicode_version):
149154
last_measured_character = character
150155
total_width += character_width
156+
index += 1
151157

152158
return total_width
153159

tests/test_cells.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,20 @@ def test_nerd_font():
187187
"""Regression test for https://github.com/Textualize/rich/issues/3943"""
188188
# Not allocated by unicode, but used by nerd fonts
189189
assert cell_len("\U000f024d") == 1
190+
191+
192+
def test_zwj():
193+
"""Test special case of zero width joiners"""
194+
assert cell_len("") == 0
195+
assert cell_len("\u200d") == 0
196+
assert cell_len("1\u200d") == 1
197+
# This sequence should really produce 2, but it aligns with with wcwidth
198+
# What gets written to the terminal is anybody's guess, I've seen multiple variations
199+
assert cell_len("1\u200d2") == 1
200+
201+
202+
def test_non_printable():
203+
"""Non printable characters should report a width of 0."""
204+
for ordinal in range(31):
205+
character = chr(ordinal)
206+
assert cell_len(character) == 0

0 commit comments

Comments
 (0)