@@ -307,9 +307,14 @@ def iter_display_chars(
307307 buffer : str ,
308308 colors : list [ColorSpan ] | None = None ,
309309 start_index : int = 0 ,
310+ * ,
311+ escape : bool = True ,
310312) -> Iterator [StyledChar ]:
311313 """Yield visible display characters with widths and semantic color tags.
312314
315+ With ``escape=True`` (default) ASCII control chars are rewritten to caret
316+ notation (``\\ n`` -> ``^J``); pass ``escape=False`` to keep them verbatim.
317+
313318 Note: ``colors`` is consumed in place as spans are processed -- callers
314319 that split a buffer across multiple calls rely on this mutation to track
315320 which spans have already been handled.
@@ -331,7 +336,7 @@ def iter_display_chars(
331336 if colors and color_idx < len (colors ) and colors [color_idx ].span .start == i :
332337 active_tag = colors [color_idx ].tag
333338
334- if control := _ascii_control_repr (c ):
339+ if escape and ( control := _ascii_control_repr (c ) ):
335340 text = control
336341 width = len (control )
337342 elif ord (c ) < 128 :
@@ -363,6 +368,8 @@ def disp_str(
363368 colors : list [ColorSpan ] | None = None ,
364369 start_index : int = 0 ,
365370 force_color : bool = False ,
371+ * ,
372+ escape : bool = True ,
366373) -> tuple [CharBuffer , CharWidths ]:
367374 r"""Decompose the input buffer into a printable variant with applied colors.
368375
@@ -374,6 +381,9 @@ def disp_str(
374381 - the second list is the visible width of each character in the input
375382 buffer.
376383
384+ With ``escape=True`` (default) ASCII control chars are rewritten to caret
385+ notation (``\\n`` -> ``^J``); pass ``escape=False`` to keep them verbatim.
386+
377387 Note on colors:
378388 - The `colors` list, if provided, is partially consumed within. We're using
379389 a list and not a generator since we need to hold onto the current
@@ -393,7 +403,9 @@ def disp_str(
393403 (['\x1b[1;34mw', 'h', 'i', 'l', 'e\x1b[0m', ' ', '1', ':'], [1, 1, 1, 1, 1, 1, 1, 1])
394404
395405 """
396- styled_chars = list (iter_display_chars (buffer , colors , start_index ))
406+ styled_chars = list (
407+ iter_display_chars (buffer , colors , start_index , escape = escape )
408+ )
397409 chars : CharBuffer = []
398410 char_widths : CharWidths = []
399411 theme = THEME (force_color = force_color )
0 commit comments