@@ -124,6 +124,8 @@ def format( # pylint: disable=redefined-builtin
124
124
include_keys : Optional [Set [str ]] = None ,
125
125
exclude_keys : Optional [Set [str ]] = None ,
126
126
markdown : bool = False ,
127
+ max_str_len : int | None = None ,
128
+ max_bytes_len : int | None = None ,
127
129
** kwargs ,
128
130
) -> str :
129
131
"""Formats a (maybe) hierarchical value with flags.
@@ -143,6 +145,10 @@ def format( # pylint: disable=redefined-builtin
143
145
exclude_keys: A set of keys to exclude from the top-level dict or object.
144
146
Applicable only when `include_keys` is set to None.
145
147
markdown: If True, use markdown notion to quote the formatted object.
148
+ max_str_len: The max length of the string to be formatted. If the string is
149
+ longer than this length, it will be truncated.
150
+ max_bytes_len: The max length of the bytes to be formatted. If the bytes is
151
+ longer than this length, it will be truncated.
146
152
**kwargs: Keyword arguments that will be passed through unto child
147
153
``Formattable`` objects.
148
154
@@ -210,6 +216,12 @@ def _format_child(v):
210
216
s .append (_indent ('}' , root_indent ))
211
217
else :
212
218
if isinstance (value , str ):
219
+ if max_str_len is not None and len (value ) > max_str_len :
220
+ value = value [:max_str_len ] + '...'
221
+ s = [repr (value )]
222
+ elif isinstance (value , bytes ):
223
+ if max_bytes_len is not None and len (value ) > max_bytes_len :
224
+ value = value [:max_bytes_len ] + b'...'
213
225
s = [repr (value )]
214
226
else :
215
227
s = [repr (value ) if compact else str (value )]
0 commit comments