Improve the formatting of the msgpack content viewer.

This commit is contained in:
Tom Hacohen 2020-07-21 18:27:19 +03:00
parent 5fd702934c
commit 008ba5d726

View File

@ -17,10 +17,25 @@ def parse_msgpack(s: bytes) -> typing.Any:
return PARSE_ERROR 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): def format_msgpack(data):
current_line: base.TViewLine = [] return base.format_text(pretty(data))
current_line.append(("text", pprint.pformat(data, indent=4)))
yield current_line
class ViewMsgPack(base.View): class ViewMsgPack(base.View):