-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-145217: Add colour to pprint output
#145218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
0906a8d
71739fe
1aac260
e11465b
7208828
fede255
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,18 +39,38 @@ | |
| import types as _types | ||
| from io import StringIO as _StringIO | ||
|
|
||
| lazy import _colorize | ||
| lazy from _pyrepl.utils import disp_str, gen_colors | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a blocker? Or if that move later happens, we can then update this code in a similar way?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", | ||
| "PrettyPrinter", "pp"] | ||
|
|
||
|
|
||
| def pprint(object, stream=None, indent=1, width=80, depth=None, *, | ||
| compact=False, expand=False, sort_dicts=True, | ||
| underscore_numbers=False): | ||
| def pprint( | ||
| object, | ||
| stream=None, | ||
| indent=1, | ||
| width=80, | ||
| depth=None, | ||
| *, | ||
| color=True, | ||
| compact=False, | ||
| expand=False, | ||
| sort_dicts=True, | ||
| underscore_numbers=False, | ||
| ): | ||
| """Pretty-print a Python object to a stream [default is sys.stdout].""" | ||
| printer = PrettyPrinter( | ||
| stream=stream, indent=indent, width=width, depth=depth, | ||
| compact=compact, expand=expand, sort_dicts=sort_dicts, | ||
| underscore_numbers=underscore_numbers) | ||
| stream=stream, | ||
| indent=indent, | ||
| width=width, | ||
| depth=depth, | ||
| color=color, | ||
| compact=compact, | ||
| expand=expand, | ||
| sort_dicts=sort_dicts, | ||
| underscore_numbers=underscore_numbers, | ||
| ) | ||
| printer.pprint(object) | ||
|
|
||
|
|
||
|
|
@@ -111,10 +131,27 @@ def _safe_tuple(t): | |
| return _safe_key(t[0]), _safe_key(t[1]) | ||
|
|
||
|
|
||
| def _colorize_output(text): | ||
| """Apply syntax highlighting.""" | ||
| colors = list(gen_colors(text)) | ||
| chars, _ = disp_str(text, colors=colors, force_color=True, escape=False) | ||
| return "".join(chars) | ||
|
|
||
|
|
||
| class PrettyPrinter: | ||
| def __init__(self, indent=1, width=80, depth=None, stream=None, *, | ||
| compact=False, expand=False, sort_dicts=True, | ||
| underscore_numbers=False): | ||
| def __init__( | ||
| self, | ||
| indent=1, | ||
| width=80, | ||
| depth=None, | ||
| stream=None, | ||
| *, | ||
| color=True, | ||
| compact=False, | ||
| expand=False, | ||
| sort_dicts=True, | ||
| underscore_numbers=False, | ||
| ): | ||
| """Handle pretty printing operations onto a stream using a set of | ||
| configured parameters. | ||
|
|
||
|
|
@@ -131,6 +168,11 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *, | |
| The desired output stream. If omitted (or false), the standard | ||
| output stream available at construction will be used. | ||
|
|
||
| color | ||
| If true (the default), syntax highlighting is enabled for pprint | ||
| when the stream and environment variables permit. | ||
| If false, colored output is always disabled. | ||
|
|
||
| compact | ||
| If true, several items will be combined in one line. | ||
| Incompatible with expand mode. | ||
|
|
@@ -168,10 +210,16 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *, | |
| self._expand = bool(expand) | ||
| self._sort_dicts = sort_dicts | ||
| self._underscore_numbers = underscore_numbers | ||
| self._color = color | ||
|
|
||
| def pprint(self, object): | ||
| if self._stream is not None: | ||
| self._format(object, self._stream, 0, 0, {}, 0) | ||
| if self._color and _colorize.can_colorize(file=self._stream): | ||
| sio = _StringIO() | ||
| self._format(object, sio, 0, 0, {}, 0) | ||
| self._stream.write(_colorize_output(sio.getvalue())) | ||
| else: | ||
| self._format(object, self._stream, 0, 0, {}, 0) | ||
| self._stream.write("\n") | ||
|
|
||
| def pformat(self, object): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add colour to :mod:`pprint` output. Patch by Hugo van Kemenade. |
Uh oh!
There was an error while loading. Please reload this page.