Skip to content

Commit 7bf2f4a

Browse files
authored
Merge pull request #32 from cknd/line_wrap_docs
Configurable line wrap
2 parents 16154c6 + e5f93a8 commit 7bf2f4a

File tree

6 files changed

+27
-7
lines changed

6 files changed

+27
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Added
44
- Option to disable verbose formatting for given types of exceptions (generating a standard python-like traceback instead)
5+
- Line wrap for printed variables can be adjusted or turned off
56

67
## Changed
78
- Disabled verbose formatting for KeyboardInterrupts by default. Call `format(..., suppressed_exceptions=None`) to enforce verbose printing even on a keyboard interrupt.

demo_complex.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66
Hovercraft().eels()
77
except:
88
# raise
9-
stackprinter.show(style='darkbg2', reverse=False, suppressed_paths=[r"lib/python.*/site-packages/numpy"])
9+
stackprinter.show(style='darkbg2',
10+
line_wrap=40,
11+
reverse=False,
12+
suppressed_paths=[r"lib/python.*/site-packages/numpy"])

stackprinter/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ def format(thing=None, **kwargs):
106106
Maximum number of characters to be used for each variable value.
107107
Default: 500
108108
109+
line_wrap: int (default 60)
110+
Limit how many columns are available to print each variable
111+
(excluding its name). Set to 0 or False to disable wrapping.
112+
109113
suppressed_paths: list of regex patterns
110114
Set less verbose formatting for frames whose code lives in certain paths
111115
(e.g. library code). Files whose path matches any of the given regex

stackprinter/formatting.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def format_summary(frames, style='plaintext', source_lines=1, reverse=False,
3939

4040
def format_stack(frames, style='plaintext', source_lines=5,
4141
show_signature=True, show_vals='like_source',
42-
truncate_vals=500, reverse=False, suppressed_paths=None):
42+
truncate_vals=500, line_wrap=60, reverse=False, suppressed_paths=None):
4343
"""
4444
Render a list of frames (or FrameInfo tuples)
4545
@@ -59,13 +59,15 @@ def format_stack(frames, style='plaintext', source_lines=5,
5959
show_signature=show_signature,
6060
show_vals=show_vals,
6161
truncate_vals=truncate_vals,
62+
line_wrap=line_wrap,
6263
suppressed_paths=suppressed_paths)
6364

6465
verbose_formatter = get_formatter(style=style,
6566
source_lines=source_lines,
6667
show_signature=show_signature,
6768
show_vals=show_vals,
6869
truncate_vals=truncate_vals,
70+
line_wrap=line_wrap,
6971
suppressed_paths=suppressed_paths)
7072

7173
frame_msgs = []

stackprinter/frame_formatting.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class FrameFormatter():
2222
val_tpl = ' ' * var_indent + "%s = %s\n"
2323

2424
def __init__(self, source_lines=5, source_lines_after=1,
25-
show_signature=True, show_vals='like_source', truncate_vals=500,
25+
show_signature=True, show_vals='like_source',
26+
truncate_vals=500, line_wrap: int = 60,
2627
suppressed_paths=None):
2728
"""
2829
Formatter for single frames.
@@ -55,6 +56,10 @@ def __init__(self, source_lines=5, source_lines_after=1,
5556
truncate_vals: int (default 500)
5657
Maximum number of characters to be used for each variable value
5758
59+
line_wrap: int (default 60)
60+
insert linebreaks after this nr of characters, use 0 to never insert
61+
a linebreak
62+
5863
suppressed_paths: list of regex patterns
5964
Set less verbose formatting for frames whose code lives in certain paths
6065
(e.g. library code). Files whose path matches any of the given regex
@@ -82,7 +87,8 @@ def __init__(self, source_lines=5, source_lines_after=1,
8287
self.show_signature = show_signature
8388
self.show_vals = show_vals
8489
self.truncate_vals = truncate_vals
85-
self.suppressed_paths = suppressed_paths # already compile regexes and make a `match` callable?
90+
self.line_wrap = line_wrap
91+
self.suppressed_paths = suppressed_paths
8692

8793
def __call__(self, frame, lineno=None):
8894
"""
@@ -174,7 +180,8 @@ def _format_assignments(self, assignments):
174180
for name, value in assignments.items():
175181
val_str = format_value(value,
176182
indent=len(name) + self.var_indent + 3,
177-
truncation=self.truncate_vals)
183+
truncation=self.truncate_vals,
184+
wrap=self.line_wrap)
178185
assign_str = self.val_tpl % (name, val_str)
179186
msgs.append(assign_str)
180187
if len(msgs) > 0:
@@ -329,7 +336,8 @@ def _format_assignments(self, assignments, colormap):
329336
for name, value in assignments.items():
330337
val_str = format_value(value,
331338
indent=len(name) + self.var_indent + 3,
332-
truncation=self.truncate_vals)
339+
truncation=self.truncate_vals,
340+
wrap=self.line_wrap)
333341
assign_str = self.val_tpl % (name, val_str)
334342
hue, sat, val, bold = colormap.get(name, self.colors['var_invisible'])
335343
clr_str = get_ansi_tpl(hue, sat, val, bold) % assign_str

stackprinter/prettyprinting.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def format_value(value, indent=0, truncation=None, wrap=60,
3434
cut after this nr of characters
3535
3636
wrap: int
37-
insert linebreaks after this nr of characters
37+
insert linebreaks after this nr of characters, use 0 to never add a linebreak
3838
3939
max_depth: int
4040
max repeated calls to this function when formatting container types
@@ -241,6 +241,8 @@ def truncate(string, n):
241241

242242

243243
def wrap_lines(string, max_width=80):
244+
if not max_width or max_width <= 0:
245+
return string
244246

245247
def wrap(lines):
246248
for l in lines:

0 commit comments

Comments
 (0)