@@ -188,10 +188,8 @@ def rich_text_to_string(text: Text) -> str:
188
188
def string_to_rich_text (text : str ) -> Text :
189
189
r"""Create a Text object from a string which can contain ANSI escape codes.
190
190
191
- This wraps rich.Text.from_ansi() to handle a discarded newline issue.
192
-
193
- Text.from_ansi() currently removes the ending line break from string.
194
- e.g. "Hello\n" becomes "Hello"
191
+ This wraps rich.Text.from_ansi() to handle an issue where it removes the
192
+ trailing line break from a string (e.g. "Hello\n" becomes "Hello").
195
193
196
194
There is currently a pull request to fix this.
197
195
https://github.com/Textualize/rich/pull/3793
@@ -201,8 +199,9 @@ def string_to_rich_text(text: str) -> Text:
201
199
"""
202
200
result = Text .from_ansi (text )
203
201
204
- # If 'text' ends with a line break character, restore the missing newline to 'result'.
205
- # Note: '\r\n' is handled as its last character is '\n'.
202
+ # If the original string ends with a recognized line break character,
203
+ # then restore the missing newline. We use "\n" because Text.from_ansi()
204
+ # converts all line breaks into newlines.
206
205
# Source: https://docs.python.org/3/library/stdtypes.html#str.splitlines
207
206
line_break_chars = {
208
207
"\n " , # Line Feed
@@ -217,7 +216,6 @@ def string_to_rich_text(text: str) -> Text:
217
216
"\u2029 " , # Paragraph Separator
218
217
}
219
218
if text and text [- 1 ] in line_break_chars :
220
- # We use "\n" because Text.from_ansi() converts all line breaks chars into newlines.
221
219
result .append ("\n " )
222
220
223
221
return result
0 commit comments