-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPrettyPrint.cs
156 lines (146 loc) · 4.43 KB
/
PrettyPrint.cs
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
//-----------------------------------------------------------------
// Adapted from repl.cs:
// https://github.com/mono/mono/tree/master/mcs/tools/csharp
//
//-----------------------------------------------------------------
using System;
using System.Collections;
using System.Text;
using Mono.CSharp;
using UnityEngine;
public class PrettyPrint {
static string EscapeString(string s) {
return s.Replace("\"", "\\\"");
}
static void EscapeChar(StringBuilder output, char c) {
if(c == '\'')
output.Append("'\\''");
else if(c > 32)
output.AppendFormat("'{0}'", c);
else {
switch(c) {
case '\a':
output.Append("'\\a'");
break;
case '\b':
output.Append("'\\b'");
break;
case '\n':
output.Append("'\\n'");
break;
case '\v':
output.Append("'\\v'");
break;
case '\r':
output.Append("'\\r'");
break;
case '\f':
output.Append("'\\f'");
break;
case '\t':
output.Append("'\\t");
break;
default:
output.AppendFormat("'\\x{0:x}", (int)c);
break;
}
}
}
private static int _depth = 0;
private static void OpenInline(StringBuilder output, int listLength) {
output.Append(listLength < 10 ? "{ " : "{\n\t");
_depth++;
}
private static void CloseInline(StringBuilder output, int listLength) {
output.Append(listLength < 10 ? " }" : "\n}");
_depth--;
}
private static void NextItem(StringBuilder output, int listLength) {
output.Append(listLength < 10 ? ", " : ",\n\t");
}
private static void Open(StringBuilder output) {
output.Append("{");
_depth++;
}
private static void Close(StringBuilder output) {
output.Append("}");
_depth--;
}
public static void PP(StringBuilder output, object result, bool expandTypes = false) {
_depth = 0;
InternalPP(output, result, expandTypes);
}
protected static void InternalPP(StringBuilder output, object result, bool expandTypes = false) {
if(result == null)
output.Append("null");
else {
if(result is REPLMessage) {
// Raw, no escaping or quoting.
output.Append(((REPLMessage)result).msg);
} else if(result is Component) {
string n;
try {
n = ((Component)result).name;
} catch(MissingReferenceException) {
n = "<destroyed>";
}
output.Append(n);
} else if(result is GameObject) {
string n;
try {
n = ((GameObject)result).name;
} catch(MissingReferenceException) {
n = "<destroyed>";
}
output.Append(n);
} else if(result is Array) {
Array a = (Array)result;
int top = a.GetUpperBound(0), bottom = a.GetLowerBound(0);
OpenInline(output, top - bottom);
for(int i = bottom; i <= top; i++) {
InternalPP(output, a.GetValue(i));
if(i != top)
NextItem(output, top - bottom);
}
CloseInline(output, top - bottom);
} else if(result is bool)
output.Append(((bool)result) ? "true" : "false");
else if(result is string)
output.Append('"').Append(EscapeString((string)result)).Append('"');
else if(result is IDictionary) {
IDictionary dict = (IDictionary)result;
int top = dict.Count, count = 0;
Open(output);
foreach(DictionaryEntry entry in dict) {
count++;
InternalPP(output, entry.Key);
output.Append(": ");
InternalPP(output, entry.Value);
if(count != top)
NextItem(output, 0);
}
Close(output);
} else if(result is IEnumerable) {
int i = 0;
ArrayList tmp = new ArrayList();
foreach(object item in(IEnumerable)result)
tmp.Add(item);
OpenInline(output, tmp.Count);
foreach(object item in tmp) {
if(i++ != 0)
NextItem(output, tmp.Count);
InternalPP(output, item);
}
CloseInline(output, tmp.Count);
} else if(result is char)
EscapeChar(output, (char)result);
else if(result is Type || result.GetType().Name == "MonoType") {
if(_depth > 0 || !expandTypes)
output.Append("typeof(" + ((Type)result).Namespace + "." + ((Type)result).Name + ")");
else
output.Append(InteractiveBase.Describe(result));
} else
output.Append(result.ToString());
}
}
}