@@ -178,6 +178,10 @@ def rich_text_to_string(text: Text) -> str:
178178 return capture .get ()
179179
180180
181+ # If True, Rich still has the bug addressed in string_to_rich_text().
182+ _from_ansi_has_newline_bug = Text .from_ansi ("\n " ).plain == ""
183+
184+
181185def string_to_rich_text (text : str ) -> Text :
182186 r"""Create a Text object from a string which can contain ANSI escape codes.
183187
@@ -192,24 +196,25 @@ def string_to_rich_text(text: str) -> Text:
192196 """
193197 result = Text .from_ansi (text )
194198
195- # If the original string ends with a recognized line break character,
196- # then restore the missing newline. We use "\n" because Text.from_ansi()
197- # converts all line breaks into newlines.
198- # Source: https://docs.python.org/3/library/stdtypes.html#str.splitlines
199- line_break_chars = {
200- "\n " , # Line Feed
201- "\r " , # Carriage Return
202- "\v " , # Vertical Tab
203- "\f " , # Form Feed
204- "\x1c " , # File Separator
205- "\x1d " , # Group Separator
206- "\x1e " , # Record Separator
207- "\x85 " , # Next Line (NEL)
208- "\u2028 " , # Line Separator
209- "\u2029 " , # Paragraph Separator
210- }
211- if text and text [- 1 ] in line_break_chars :
212- result .append ("\n " )
199+ if _from_ansi_has_newline_bug :
200+ # If the original string ends with a recognized line break character,
201+ # then restore the missing newline. We use "\n" because Text.from_ansi()
202+ # converts all line breaks into newlines.
203+ # Source: https://docs.python.org/3/library/stdtypes.html#str.splitlines
204+ line_break_chars = {
205+ "\n " , # Line Feed
206+ "\r " , # Carriage Return
207+ "\v " , # Vertical Tab
208+ "\f " , # Form Feed
209+ "\x1c " , # File Separator
210+ "\x1d " , # Group Separator
211+ "\x1e " , # Record Separator
212+ "\x85 " , # Next Line (NEL)
213+ "\u2028 " , # Line Separator
214+ "\u2029 " , # Paragraph Separator
215+ }
216+ if text and text [- 1 ] in line_break_chars :
217+ result .append ("\n " )
213218
214219 return result
215220
0 commit comments