-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmcslldb_commands.py
57 lines (45 loc) · 1.43 KB
/
mcslldb_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
# encoding: utf-8
import json as json_lib
from os import system
from tempfile import NamedTemporaryFile
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import JavascriptLexer
from mcslldb_helpers import get_value, lldb_command
@lldb_command
def json(debugger, expression):
# get JSON string
value = get_value(debugger, expression)
json_string = value.GetObjectDescription()
# prettify JSON
pretty_json_string = json_lib.dumps(json_lib.loads(json_string),
sort_keys=True, indent=4)
# render HTML with highlighted JSON
formatter = HtmlFormatter(linenos=True)
html = """
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
{style}
</style>
</head>
<body>
{body}
</body>
</html>
""".format(**{
'style': formatter.get_style_defs('.highlight'),
'body': highlight(pretty_json_string, JavascriptLexer(), formatter),
})
# save HTML to a temporary file
with NamedTemporaryFile(delete=False) as f:
f.write(html)
# add ".html" extension
original_path = f.name
path = original_path + '.html'
system('mv "%s" "%s"' % (original_path, path))
# show HTML in Quick Look and delete file at the end
system('qlmanage -p "%s" > /dev/null && rm "%s" &' % (path, path))