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