-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdisplay.py
More file actions
162 lines (157 loc) · 5.98 KB
/
Copy pathdisplay.py
File metadata and controls
162 lines (157 loc) · 5.98 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import re
from colorama import Fore, Style
from function import Function
from type import NModule
from enums import EnumValue
from ncmd import Cmd
from native_types import NMap
unescape = {"\\": "\\", '"': '"', "\n": "n", "\r": "r", "\t": "t"}
class Printable:
def get_display(self, color=True, indent="\t", indent_state="", preferred_max_len=50):
return ""
# https://stackoverflow.com/a/38662876
def remove_color(line):
ansi_escape = re.compile(r"(?:\x1B[@-_]|[\x80-\x9F])[0-?]*[ -/]*[@-~]")
return ansi_escape.sub("", line)
def display_value(
value, color=True, indent="\t", indent_state="", preferred_max_len=50
):
multiline = False
if isinstance(value, NModule):
output = "[module %s]" % value.mod_name
if color:
output = Fore.MAGENTA + output + Style.RESET_ALL
elif isinstance(value, NMap):
# There's currently no special syntax for maps, so this mimics its
# constructor.
start = "<mapFrom"
end = ">"
if color:
start = Fore.MAGENTA + start + Style.RESET_ALL
end = Fore.MAGENTA + end + Style.RESET_ALL
display_list, multiline = display_value(
list(value.items()), color=color, indent=indent, indent_state=indent_state
)
output = start + " " + display_list + end
elif isinstance(value, dict):
if len(value) == 0:
output = "{}"
else:
length = 4
parts = []
inner_indent = indent_state + indent
for key, record_value in value.items():
part, part_multiline = display_value(
record_value,
color=color,
indent=indent,
indent_state=inner_indent,
preferred_max_len=preferred_max_len,
)
part = key + ": " + part
length += len(remove_color(part))
parts.append(part)
if part_multiline:
multiline = True
if multiline or length > preferred_max_len:
multiline = True
output = "{\n"
output += ",\n".join(inner_indent + part for part in parts) + "\n"
output += indent_state + "}"
else:
output = "{ %s }" % ", ".join(parts)
elif isinstance(value, list) or isinstance(value, tuple):
is_list = isinstance(value, list)
if len(value) == 0:
output = "[]" if is_list else "()"
else:
length = 2
parts = []
inner_indent = indent_state + indent
for i, item in enumerate(value):
part, part_multiline = display_value(
item,
color=color,
indent=indent,
indent_state=inner_indent,
preferred_max_len=preferred_max_len,
)
if i != len(value) - 1:
part += ","
length += len(remove_color(part))
parts.append(part)
if part_multiline:
multiline = True
if multiline or length > preferred_max_len:
multiline = True
output = "[\n" if is_list else "(\n"
output += "".join(inner_indent + part + "\n" for part in parts)
output += indent_state + ("]" if is_list else ")")
else:
output = ("[%s]" if is_list else "(%s)") % " ".join(parts)
elif isinstance(value, bool):
output = "true" if value else "false"
if color:
output = Fore.YELLOW + output + Style.RESET_ALL
elif isinstance(value, int) or isinstance(value, float):
output = str(value)
if color:
output = Fore.YELLOW + output + Style.RESET_ALL
elif isinstance(value, str):
output = value
for char, escape in unescape.items():
escape = "\\" + escape
if color:
escape = Fore.CYAN + escape + Fore.GREEN
output = output.replace(char, escape)
output = '"%s"' % output
if color:
output = Fore.GREEN + output + Style.RESET_ALL
elif isinstance(value, EnumValue):
if len(value.values) == 0:
output = (
Fore.MAGENTA + value.variant + Style.RESET_ALL
if color
else value.variant
)
else:
length = len(value.variant) + 3
parts = []
inner_indent = indent_state + indent
for field in value.values:
part, part_multiline = display_value(
field,
color=color,
indent=indent,
indent_state=inner_indent,
preferred_max_len=preferred_max_len,
)
length += len(remove_color(part))
parts.append(part)
if part_multiline:
multiline = True
output = "<" + value.variant
if color:
output = Fore.MAGENTA + output + Style.RESET_ALL
if multiline or length > preferred_max_len:
output += "\n" + "".join(inner_indent + part + "\n" for part in parts)
output += indent_state
else:
output += " " + " ".join(parts)
output += Fore.MAGENTA + ">" + Style.RESET_ALL if color else ">"
elif isinstance(value, Cmd):
output = "[cmd]"
if color:
output = Fore.MAGENTA + output + Style.RESET_ALL
elif isinstance(value, Function):
output = "[function]"
if color:
output = Fore.MAGENTA + output + Style.RESET_ALL
elif isinstance(value, Printable):
output = value.get_display(color)
else:
print("???", value)
output = "[unprintable value]"
if color:
output = Fore.RED + output + Style.RESET_ALL
return output, multiline