@@ -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]
0 commit comments