@@ -306,17 +306,27 @@ def indent(renderable: RenderableType, level: int) -> Padding:
306
306
def prepare_objects_for_rich_print (* objects : Any ) -> tuple [RenderableType , ...]:
307
307
"""Prepare a tuple of objects for printing by Rich's Console.print().
308
308
309
- Converts any non-Rich objects (i.e., not ConsoleRenderable or RichCast)
310
- into rich.Text objects by stringifying them and processing them with
311
- from_ansi(). This ensures Rich correctly interprets any embedded ANSI
312
- escape sequences.
309
+ This function converts any non-Rich object whose string representation contains
310
+ ANSI style codes into a rich.Text object. This ensures correct display width
311
+ calculation, as Rich can then properly parse and account for the non-printing
312
+ ANSI codes. All other objects are left untouched, allowing Rich's native
313
+ renderers to handle them.
313
314
314
315
:param objects: objects to prepare
315
- :return: a tuple containing the processed objects, where non-Rich objects are
316
- converted to rich.Text.
316
+ :return: a tuple containing the processed objects.
317
317
"""
318
318
object_list = list (objects )
319
319
for i , obj in enumerate (object_list ):
320
- if not isinstance (obj , (ConsoleRenderable , RichCast )):
321
- object_list [i ] = string_to_rich_text (str (obj ))
320
+ # If the object is a recognized renderable, we don't need to do anything. Rich will handle it.
321
+ if isinstance (obj , (ConsoleRenderable , RichCast )):
322
+ continue
323
+
324
+ # Check if the object's string representation contains ANSI styles, and if so,
325
+ # replace it with a Rich Text object for correct width calculation.
326
+ obj_str = str (obj )
327
+ obj_text = string_to_rich_text (obj_str )
328
+
329
+ if obj_text .plain != obj_str :
330
+ object_list [i ] = obj_text
331
+
322
332
return tuple (object_list )
0 commit comments