11
11
12
12
from __future__ import annotations
13
13
14
- import shutil
15
14
import sys
16
15
import warnings
17
16
@@ -348,10 +347,15 @@ class RichTracebackFormatter:
348
347
349
348
See :class:`rich.traceback.Traceback` for details on the arguments.
350
349
351
- If a * width* of -1 is passed , the terminal width is used. If the width
350
+ If `` width`` is ``None`` , the terminal width is used. If the width
352
351
can't be determined, fall back to 80.
353
352
354
353
.. versionadded:: 23.2.0
354
+
355
+ .. versionchanged:: 25.3.0
356
+ Default ``width`` is ``None`` to have full width and reflow support.
357
+ ``-1`` has width is deprecated, use ``None`` instead.
358
+ ``code_width`` is now exposed. ``word_wrap`` is enabled by default.
355
359
"""
356
360
357
361
color_system : Literal [
@@ -360,9 +364,10 @@ class RichTracebackFormatter:
360
364
show_locals : bool = True
361
365
max_frames : int = 100
362
366
theme : str | None = None
363
- word_wrap : bool = False
367
+ word_wrap : bool = True
364
368
extra_lines : int = 3
365
- width : int = 100
369
+ width : int | None = None
370
+ code_width : int | None = 88
366
371
indent_guides : bool = True
367
372
locals_max_length : int = 10
368
373
locals_max_string : int = 80
@@ -372,29 +377,37 @@ class RichTracebackFormatter:
372
377
373
378
def __call__ (self , sio : TextIO , exc_info : ExcInfo ) -> None :
374
379
if self .width == - 1 :
375
- self .width , _ = shutil .get_terminal_size ((80 , 0 ))
380
+ warnings .warn (
381
+ "Use None to use the terminal width instead of -1." ,
382
+ DeprecationWarning ,
383
+ stacklevel = 2 ,
384
+ )
385
+ self .width = None
376
386
377
387
sio .write ("\n " )
378
388
379
- Console (
389
+ console = Console (
380
390
file = sio , color_system = self .color_system , width = self .width
381
- ).print (
382
- Traceback .from_exception (
383
- * exc_info ,
384
- show_locals = self .show_locals ,
385
- max_frames = self .max_frames ,
386
- theme = self .theme ,
387
- word_wrap = self .word_wrap ,
388
- extra_lines = self .extra_lines ,
389
- width = self .width ,
390
- indent_guides = self .indent_guides ,
391
- locals_max_length = self .locals_max_length ,
392
- locals_max_string = self .locals_max_string ,
393
- locals_hide_dunder = self .locals_hide_dunder ,
394
- locals_hide_sunder = self .locals_hide_sunder ,
395
- suppress = self .suppress ,
396
- )
397
391
)
392
+ tb = Traceback .from_exception (
393
+ * exc_info ,
394
+ show_locals = self .show_locals ,
395
+ max_frames = self .max_frames ,
396
+ theme = self .theme ,
397
+ word_wrap = self .word_wrap ,
398
+ extra_lines = self .extra_lines ,
399
+ width = self .width ,
400
+ indent_guides = self .indent_guides ,
401
+ locals_max_length = self .locals_max_length ,
402
+ locals_max_string = self .locals_max_string ,
403
+ locals_hide_dunder = self .locals_hide_dunder ,
404
+ locals_hide_sunder = self .locals_hide_sunder ,
405
+ suppress = self .suppress ,
406
+ )
407
+ if hasattr (tb , "code_width" ):
408
+ # `code_width` requires `rich>=13.8.0`
409
+ tb .code_width = self .code_width
410
+ console .print (tb )
398
411
399
412
400
413
rich_traceback = RichTracebackFormatter ()
0 commit comments