From 008ba5d726f76b101251c87d779e6677673180d8 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Tue, 21 Jul 2020 18:27:19 +0300 Subject: [PATCH] Improve the formatting of the msgpack content viewer. --- mitmproxy/contentviews/msgpack.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/mitmproxy/contentviews/msgpack.py b/mitmproxy/contentviews/msgpack.py index fc8320fe0..821cccc23 100644 --- a/mitmproxy/contentviews/msgpack.py +++ b/mitmproxy/contentviews/msgpack.py @@ -17,10 +17,25 @@ def parse_msgpack(s: bytes) -> typing.Any: return PARSE_ERROR +def pretty(value, htchar=" ", lfchar="\n", indent=0): + nlch = lfchar + htchar * (indent + 1) + if type(value) is dict: + items = [ + nlch + repr(key) + ": " + pretty(value[key], htchar, lfchar, indent + 1) + for key in value + ] + return "{%s}" % (",".join(items) + lfchar + htchar * indent) + elif type(value) is list: + items = [ + nlch + pretty(item, htchar, lfchar, indent + 1) + for item in value + ] + return "[%s]" % (",".join(items) + lfchar + htchar * indent) + else: + return repr(value) + def format_msgpack(data): - current_line: base.TViewLine = [] - current_line.append(("text", pprint.pformat(data, indent=4))) - yield current_line + return base.format_text(pretty(data)) class ViewMsgPack(base.View):