20
20
RenderableType ,
21
21
)
22
22
from rich .padding import Padding
23
+ from rich .pretty import is_expandable
23
24
from rich .protocol import rich_cast
24
25
from rich .segment import Segment
25
26
from rich .style import StyleType
@@ -289,11 +290,14 @@ def indent(renderable: RenderableType, level: int) -> Padding:
289
290
def prepare_objects_for_rendering (* objects : Any ) -> tuple [Any , ...]:
290
291
"""Prepare a tuple of objects for printing by Rich's Console.print().
291
292
292
- This function converts any non-Rich object whose string representation contains
293
- ANSI style sequences into a Rich Text object. This ensures correct display width
294
- calculation, as Rich can then properly parse and account for these non-printing
295
- codes. All other objects are left untouched, allowing Rich's native
296
- renderers to handle them.
293
+ This function processes objects to ensure they are rendered correctly by Rich.
294
+ It inspects each object and, if its string representation contains ANSI style
295
+ sequences, it converts the object to a Rich Text object. This ensures Rich can
296
+ properly parse the non-printing codes for accurate display width calculation.
297
+
298
+ Objects that already implement the Rich console protocol or are expandable
299
+ by its pretty printer are left untouched, as they can be handled directly by
300
+ Rich's native renderers.
297
301
298
302
:param objects: objects to prepare
299
303
:return: a tuple containing the processed objects.
@@ -305,13 +309,12 @@ def prepare_objects_for_rendering(*objects: Any) -> tuple[Any, ...]:
305
309
# with a __rich__ method that might return a string.
306
310
renderable = rich_cast (obj )
307
311
308
- # This object implements the Rich console protocol, so no preprocessing is needed .
309
- if isinstance (renderable , ConsoleRenderable ):
312
+ # No preprocessing is needed for Rich-compatible or expandable objects .
313
+ if isinstance (renderable , ConsoleRenderable ) or is_expandable ( renderable ) :
310
314
continue
311
315
316
+ # Check for ANSI style sequences in its string representation.
312
317
renderable_as_str = str (renderable )
313
-
314
- # Check for any ANSI style sequences in the string.
315
318
if ANSI_STYLE_SEQUENCE_RE .search (renderable_as_str ):
316
319
object_list [i ] = Text .from_ansi (renderable_as_str )
317
320
0 commit comments