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