Skip to content

Commit aeac8a9

Browse files
committed
Removed Text.from_ansi() monkeypatch since it's no longer needed as of Rich 15.0.0.
1 parent 8a48700 commit aeac8a9

File tree

3 files changed

+1
-98
lines changed

3 files changed

+1
-98
lines changed

cmd2/rich_utils.py

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -477,64 +477,3 @@ def prepare_objects_for_rendering(*objects: Any) -> tuple[Any, ...]:
477477
object_list[i] = Text.from_ansi(renderable_as_str)
478478

479479
return tuple(object_list)
480-
481-
482-
###################################################################################
483-
# Rich Library Monkey Patches
484-
#
485-
# These patches fix specific bugs in the Rich library. They are conditional and
486-
# will only be applied if the bug is detected. When the bugs are fixed in a
487-
# future Rich release, these patches and their corresponding tests should be
488-
# removed.
489-
###################################################################################
490-
491-
###################################################################################
492-
# Text.from_ansi() monkey patch
493-
###################################################################################
494-
495-
# Save original Text.from_ansi() so we can call it in our wrapper
496-
_orig_text_from_ansi = Text.from_ansi
497-
498-
499-
@classmethod # type: ignore[misc]
500-
def _from_ansi_wrapper(cls: type[Text], text: str, *args: Any, **kwargs: Any) -> Text: # noqa: ARG001
501-
r"""Wrap Text.from_ansi() to fix its trailing newline bug.
502-
503-
This wrapper handles an issue where Text.from_ansi() removes the
504-
trailing line break from a string (e.g. "Hello\n" becomes "Hello").
505-
506-
There is currently a pull request on Rich to fix this.
507-
https://github.com/Textualize/rich/pull/3793
508-
"""
509-
result = _orig_text_from_ansi(text, *args, **kwargs)
510-
511-
# If the original string ends with a recognized line break character,
512-
# then restore the missing newline. We use "\n" because Text.from_ansi()
513-
# converts all line breaks into newlines.
514-
# Source: https://docs.python.org/3/library/stdtypes.html#str.splitlines
515-
line_break_chars = {
516-
"\n", # Line Feed
517-
"\r", # Carriage Return
518-
"\v", # Vertical Tab
519-
"\f", # Form Feed
520-
"\x1c", # File Separator
521-
"\x1d", # Group Separator
522-
"\x1e", # Record Separator
523-
"\x85", # Next Line (NEL)
524-
"\u2028", # Line Separator
525-
"\u2029", # Paragraph Separator
526-
}
527-
if text and text[-1] in line_break_chars:
528-
result.append("\n")
529-
530-
return result
531-
532-
533-
def _from_ansi_has_newline_bug() -> bool:
534-
"""Check if Test.from_ansi() strips the trailing line break from a string."""
535-
return Text.from_ansi("\n") == Text.from_ansi("")
536-
537-
538-
# Only apply the monkey patch if the bug is present
539-
if _from_ansi_has_newline_bug():
540-
Text.from_ansi = _from_ansi_wrapper # type: ignore[assignment]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dependencies = [
3333
"gnureadline>=8; platform_system == 'Darwin'",
3434
"pyperclip>=1.8.2",
3535
"pyreadline3>=3.4; platform_system == 'Windows'",
36-
"rich>=14.3.0",
36+
"rich>=15.0.0",
3737
"rich-argparse>=1.7.1",
3838
"typing-extensions; python_version == '3.10'",
3939
]

tests/test_rich_utils.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -110,42 +110,6 @@ def test_set_theme() -> None:
110110
assert ru.APP_THEME.styles[rich_style_key] == theme[rich_style_key]
111111

112112

113-
def test_from_ansi_wrapper() -> None:
114-
# Check if we are still patching Text.from_ansi(). If this check fails, then Rich
115-
# has fixed the bug. Therefore, we can remove this test function and ru._from_ansi_wrapper.
116-
assert Text.from_ansi.__func__ is ru._from_ansi_wrapper.__func__ # type: ignore[attr-defined]
117-
118-
# Line breaks recognized by str.splitlines().
119-
# Source: https://docs.python.org/3/library/stdtypes.html#str.splitlines
120-
line_breaks = {
121-
"\n", # Line Feed
122-
"\r", # Carriage Return
123-
"\r\n", # Carriage Return + Line Feed
124-
"\v", # Vertical Tab
125-
"\f", # Form Feed
126-
"\x1c", # File Separator
127-
"\x1d", # Group Separator
128-
"\x1e", # Record Separator
129-
"\x85", # Next Line (NEL)
130-
"\u2028", # Line Separator
131-
"\u2029", # Paragraph Separator
132-
}
133-
134-
# Test all line breaks
135-
for lb in line_breaks:
136-
input_string = f"Text{lb}"
137-
expected_output = input_string.replace(lb, "\n")
138-
assert Text.from_ansi(input_string).plain == expected_output
139-
140-
# Test string without trailing line break
141-
input_string = "No trailing\nline break"
142-
assert Text.from_ansi(input_string).plain == input_string
143-
144-
# Test empty string
145-
input_string = ""
146-
assert Text.from_ansi(input_string).plain == input_string
147-
148-
149113
@with_ansi_style(ru.AllowStyle.ALWAYS)
150114
def test_cmd2_base_console_print() -> None:
151115
"""Test that Cmd2BaseConsole.print() correctly propagates formatting overrides to structured renderables."""

0 commit comments

Comments
 (0)