Skip to content

Commit

Permalink
Merge pull request #2870 from LCH-1/master
Browse files Browse the repository at this point in the history
add code_width in traceback
  • Loading branch information
willmcgugan authored Jul 1, 2024
2 parents 2a7384c + 5104bf2 commit 4bd7108
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions rich/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class RichHandler(Handler):
markup (bool, optional): Enable console markup in log messages. Defaults to False.
rich_tracebacks (bool, optional): Enable rich tracebacks with syntax highlighting and formatting. Defaults to False.
tracebacks_width (Optional[int], optional): Number of characters used to render tracebacks, or None for full width. Defaults to None.
tracebacks_code_width (int, optional): Number of code characters used to render tracebacks, or None for full width. Defaults to 88.
tracebacks_extra_lines (int, optional): Additional lines of code to render tracebacks, or None for full width. Defaults to None.
tracebacks_theme (str, optional): Override pygments theme used in traceback.
tracebacks_word_wrap (bool, optional): Enable word wrapping of long tracebacks lines. Defaults to True.
Expand Down Expand Up @@ -74,6 +75,7 @@ def __init__(
markup: bool = False,
rich_tracebacks: bool = False,
tracebacks_width: Optional[int] = None,
tracebacks_code_width: int = 88,
tracebacks_extra_lines: int = 3,
tracebacks_theme: Optional[str] = None,
tracebacks_word_wrap: bool = True,
Expand Down Expand Up @@ -104,6 +106,7 @@ def __init__(
self.tracebacks_word_wrap = tracebacks_word_wrap
self.tracebacks_show_locals = tracebacks_show_locals
self.tracebacks_suppress = tracebacks_suppress
self.tracebacks_code_width = tracebacks_code_width
self.locals_max_length = locals_max_length
self.locals_max_string = locals_max_string
self.keywords = keywords
Expand Down Expand Up @@ -140,6 +143,7 @@ def emit(self, record: LogRecord) -> None:
exc_value,
exc_traceback,
width=self.tracebacks_width,
code_width=self.tracebacks_code_width,
extra_lines=self.tracebacks_extra_lines,
theme=self.tracebacks_theme,
word_wrap=self.tracebacks_word_wrap,
Expand Down
11 changes: 10 additions & 1 deletion rich/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def install(
*,
console: Optional[Console] = None,
width: Optional[int] = 100,
code_width: Optional[int] = 88,
extra_lines: int = 3,
theme: Optional[str] = None,
word_wrap: bool = False,
Expand All @@ -68,6 +69,7 @@ def install(
Args:
console (Optional[Console], optional): Console to write exception to. Default uses internal Console instance.
width (Optional[int], optional): Width (in characters) of traceback. Defaults to 100.
code_width (Optional[int], optional): Code width (in characters) of traceback. Defaults to 88.
extra_lines (int, optional): Extra lines of code. Defaults to 3.
theme (Optional[str], optional): Pygments theme to use in traceback. Defaults to ``None`` which will pick
a theme appropriate for the platform.
Expand Down Expand Up @@ -104,6 +106,7 @@ def excepthook(
value,
traceback,
width=width,
code_width=code_width,
extra_lines=extra_lines,
theme=theme,
word_wrap=word_wrap,
Expand Down Expand Up @@ -214,6 +217,7 @@ class Traceback:
trace (Trace, optional): A `Trace` object produced from `extract`. Defaults to None, which uses
the last exception.
width (Optional[int], optional): Number of characters used to traceback. Defaults to 100.
code_width (Optional[int], optional): Number of code characters used to traceback. Defaults to 88.
extra_lines (int, optional): Additional lines of code to render. Defaults to 3.
theme (str, optional): Override pygments theme used in traceback.
word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False.
Expand Down Expand Up @@ -242,6 +246,7 @@ def __init__(
trace: Optional[Trace] = None,
*,
width: Optional[int] = 100,
code_width: Optional[int] = 88,
extra_lines: int = 3,
theme: Optional[str] = None,
word_wrap: bool = False,
Expand All @@ -265,6 +270,7 @@ def __init__(
)
self.trace = trace
self.width = width
self.code_width = code_width
self.extra_lines = extra_lines
self.theme = Syntax.get_theme(theme or "ansi_dark")
self.word_wrap = word_wrap
Expand Down Expand Up @@ -296,6 +302,7 @@ def from_exception(
traceback: Optional[TracebackType],
*,
width: Optional[int] = 100,
code_width: Optional[int] = 88,
extra_lines: int = 3,
theme: Optional[str] = None,
word_wrap: bool = False,
Expand All @@ -315,6 +322,7 @@ def from_exception(
exc_value (BaseException): Exception value.
traceback (TracebackType): Python Traceback object.
width (Optional[int], optional): Number of characters used to traceback. Defaults to 100.
code_width (Optional[int], optional): Number of code characters used to traceback. Defaults to 88.
extra_lines (int, optional): Additional lines of code to render. Defaults to 3.
theme (str, optional): Override pygments theme used in traceback.
word_wrap (bool, optional): Enable word wrapping of long lines. Defaults to False.
Expand Down Expand Up @@ -345,6 +353,7 @@ def from_exception(
return cls(
rich_traceback,
width=width,
code_width=code_width,
extra_lines=extra_lines,
theme=theme,
word_wrap=word_wrap,
Expand Down Expand Up @@ -694,7 +703,7 @@ def render_locals(frame: Frame) -> Iterable[ConsoleRenderable]:
),
highlight_lines={frame.lineno},
word_wrap=self.word_wrap,
code_width=88,
code_width=self.code_width,
indent_guides=self.indent_guides,
dedent=False,
)
Expand Down

0 comments on commit 4bd7108

Please sign in to comment.