3939import types as _types
4040from io import StringIO as _StringIO
4141
42+ lazy import _colorize
43+ lazy from _pyrepl .utils import disp_str , gen_colors
44+
4245__all__ = ["pprint" ,"pformat" ,"isreadable" ,"isrecursive" ,"saferepr" ,
4346 "PrettyPrinter" , "pp" ]
4447
4548
46- def pprint (object , stream = None , indent = 1 , width = 80 , depth = None , * ,
47- compact = False , sort_dicts = True , underscore_numbers = False ):
49+ def pprint (
50+ object ,
51+ stream = None ,
52+ indent = 1 ,
53+ width = 80 ,
54+ depth = None ,
55+ * ,
56+ color = True ,
57+ compact = False ,
58+ sort_dicts = True ,
59+ underscore_numbers = False ,
60+ ):
4861 """Pretty-print a Python object to a stream [default is sys.stdout]."""
4962 printer = PrettyPrinter (
50- stream = stream , indent = indent , width = width , depth = depth ,
51- compact = compact , sort_dicts = sort_dicts ,
52- underscore_numbers = underscore_numbers )
63+ stream = stream ,
64+ indent = indent ,
65+ width = width ,
66+ depth = depth ,
67+ color = color ,
68+ compact = compact ,
69+ sort_dicts = sort_dicts ,
70+ underscore_numbers = underscore_numbers ,
71+ )
5372 printer .pprint (object )
5473
5574
@@ -109,9 +128,26 @@ def _safe_tuple(t):
109128 return _safe_key (t [0 ]), _safe_key (t [1 ])
110129
111130
131+ def _colorize_output (text ):
132+ """Apply syntax highlighting."""
133+ colors = list (gen_colors (text ))
134+ chars , _ = disp_str (text , colors = colors , force_color = True )
135+ return "" .join (chars )
136+
137+
112138class PrettyPrinter :
113- def __init__ (self , indent = 1 , width = 80 , depth = None , stream = None , * ,
114- compact = False , sort_dicts = True , underscore_numbers = False ):
139+ def __init__ (
140+ self ,
141+ indent = 1 ,
142+ width = 80 ,
143+ depth = None ,
144+ stream = None ,
145+ * ,
146+ color = True ,
147+ compact = False ,
148+ sort_dicts = True ,
149+ underscore_numbers = False ,
150+ ):
115151 """Handle pretty printing operations onto a stream using a set of
116152 configured parameters.
117153
@@ -128,6 +164,11 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
128164 The desired output stream. If omitted (or false), the standard
129165 output stream available at construction will be used.
130166
167+ color
168+ If true (the default), syntax highlighting is enabled for pprint
169+ when the stream and environment variables permit.
170+ If false, colored output is always disabled.
171+
131172 compact
132173 If true, several items will be combined in one line.
133174
@@ -156,10 +197,16 @@ def __init__(self, indent=1, width=80, depth=None, stream=None, *,
156197 self ._compact = bool (compact )
157198 self ._sort_dicts = sort_dicts
158199 self ._underscore_numbers = underscore_numbers
200+ self ._color = color
159201
160202 def pprint (self , object ):
161203 if self ._stream is not None :
162- self ._format (object , self ._stream , 0 , 0 , {}, 0 )
204+ if self ._color and _colorize .can_colorize (file = self ._stream ):
205+ sio = _StringIO ()
206+ self ._format (object , sio , 0 , 0 , {}, 0 )
207+ self ._stream .write (_colorize_output (sio .getvalue ()))
208+ else :
209+ self ._format (object , self ._stream , 0 , 0 , {}, 0 )
163210 self ._stream .write ("\n " )
164211
165212 def pformat (self , object ):
0 commit comments