-
Notifications
You must be signed in to change notification settings - Fork 46
/
console.js
277 lines (230 loc) · 8.72 KB
/
console.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/***********/
/* CONSOLE */
/***********/
GW.console = {
outputBuffer: newDocument(),
flushBuffer: () => {
if (GW.console.view == false)
return;
GW.console.view.contentView.append(GW.console.outputBuffer);
GW.console.updateHeight();
GW.console.scrollToBottom();
},
scrollToBottom: () => {
GW.console.view.scrollView.scrollTop = GW.console.view.contentView.clientHeight
- GW.console.view.scrollView.clientHeight;
},
clearOutput: () => {
GW.console.view.contentView.replaceChildren();
GW.console.updateHeight();
GW.console.scrollToBottom();
},
print: (entity, flush = true) => {
let style = "";
if ( entity == undefined
|| entity == null)
style = "color: #777;"
if (entity instanceof Error)
style = "color: #f00;"
let output;
if (entity instanceof Error) {
output = entity.stack;
console.error(entity);
} else if (typeof entity == "string") {
output = entity.replace(/</g, "<").replace(/>/g, ">");
console.log(entity);
} else if (entity instanceof Element) {
output = entity.outerHTML.replace(/</g, "<").replace(/>/g, ">");
console.log(entity);
} else {
if (entity) {
let jsonString = JSON.stringify(entity, null, "\n"
).replace(/\\n/g, "\n"
).replace(/\\t/g, "\t"
).replace(/\\"/g, "\"")
console.log(jsonString);
jsonString = JSON.stringify(entity, null, "\n"
).replace(/\\t/g, " "
).replace(/\\"/g, """
).replace(/</g, "<"
).replace(/>/g, ">"
).replace(/\n+/g, "\n"
).replace(/\\n/g, "<br />")
output = jsonString;
} else {
console.log(entity);
output = entity;
}
}
GW.console.outputBuffer.appendChild(newElement("P", { style: style }, { innerHTML: output }));
if (flush)
GW.console.flushBuffer();
},
setInputCursorPosition: (pos) => {
GW.console.view.input.setSelectionRange(pos, pos);
},
show: () => {
GW.console.scrollToBottom();
GW.console.view.classList.toggle("hidden", false);
GW.console.view.input.focus();
},
hide: () => {
GW.console.view.input.blur();
GW.console.view.classList.toggle("hidden", true);
},
isVisible: () => {
return (GW.console.view.classList.contains("hidden") == false);
},
updateHeight: () => {
GW.console.view.style.setProperty("--GW-console-view-height", GW.console.view.offsetHeight + "px");
},
setPrompt: (string) => {
GW.console.view.prompt.innerHTML = string;
},
clearCommandLine: () => {
GW.console.view.input.value = "";
},
keyDown: (event) => {
if (GW.console.isVisible() == false)
return;
let allowedKeys = [ "Enter", "ArrowUp", "ArrowDown" ];
if (allowedKeys.includes(event.key) == false)
return;
if (document.activeElement != GW.console.view.input)
return;
switch (event.key) {
case "Enter":
GW.console.commandLineCommandReceived();
break;
case "ArrowUp":
event.preventDefault();
if (GW.console.commandLog_pointer == GW.console.commandLog.length)
GW.console.commandLog_currentCommandLine = GW.console.view.input.value;
let prevLine = GW.console.commandLog_prevEntry();
if (prevLine != null) {
GW.console.view.input.value = prevLine;
GW.console.setInputCursorPosition(GW.console.view.input.value.length);
}
break;
case "ArrowDown":
event.preventDefault();
let nextLine = GW.console.commandLog_nextEntry();
if (nextLine != null) {
GW.console.view.input.value = nextLine;
GW.console.setInputCursorPosition(GW.console.view.input.value.length);
}
break;
}
},
keyUp: (event) => {
let allowedKeys = [ "`", "Esc", "Escape" ];
if (allowedKeys.includes(event.key) == false)
return;
switch (event.key) {
case "`":
if (GW.console.isVisible() == false)
GW.console.show();
break;
case "Esc":
case "Escape":
if (GW.console.isVisible() == true)
GW.console.hide();
break;
};
},
commandLineInputReceived: (event) => {
// Nothing… yet.
},
commandLog: [ ],
commandLog_currentCommandLine: "",
commandLog_pointer: 0,
commandLog_prevEntry: () => {
if (GW.console.commandLog_pointer == 0)
return null;
return GW.console.commandLog_entryAtIndex(--(GW.console.commandLog_pointer));
},
commandLog_nextEntry: () => {
if (GW.console.commandLog_pointer == GW.console.commandLog.length)
return null;
return GW.console.commandLog_entryAtIndex(++(GW.console.commandLog_pointer));
},
commandLog_entryAtIndex: (index) => {
return (index == GW.console.commandLog.length
? GW.console.commandLog_currentCommandLine
: GW.console.commandLog[index]);
},
commandLineCommandReceived: () => {
let inputLine = event.target.value;
GW.console.print("> " + inputLine);
GW.console.clearCommandLine();
GW.console.commandLog.push(inputLine);
GW.console.commandLog_currentCommandLine = "";
GW.console.commandLog_pointer = GW.console.commandLog.length;
if (/^`.*`$/.test(inputLine))
GW.console.jsExecLine(inputLine.slice(1, -1));
else
GW.console.execLine(inputLine);
},
execLine: (line) => {
let command = line;
switch (command.toLowerCase()) {
case "clear":
GW.console.clearOutput();
break;
default:
GW.console.print(`gwrnsh: ${line}: command not found.`);
break;
}
},
jsExecLine: (line) => {
$(line);
}
};
// Dump temporary buffer.
if (GW.consoleTempBuffer > "") {
GW.consoleTempBuffer.split("\n").forEach(line => {
GW.console.print(line, false);
});
GW.consoleTempBuffer = null;
}
doWhenBodyExists(() => {
// Construct views.
GW.console.view = addUIElement(`<div id="console" class="hidden">
<div class="console-scroll-view">
<div class="console-content-view"></div>
</div>
<div class="console-command-line">
<div class="console-command-line-prompt">
<span></span>
</div>
<div class="console-command-line-entry-field">
<input name="console-command" title="JS REPL" type="text" autocomplete="off"></input>
</div>
</div>
</div>`);
// Convenience references.
GW.console.view.scrollView = GW.console.view.querySelector(".console-scroll-view");
GW.console.view.contentView = GW.console.view.querySelector(".console-content-view");
GW.console.view.prompt = GW.console.view.querySelector(".console-command-line-prompt span");
GW.console.view.input = GW.console.view.querySelector(".console-command-line-entry-field input");
// Set prompt.
GW.console.setPrompt(location.pathname);
// Flush output buffer.
GW.console.flushBuffer();
// Update height.
GW.console.updateHeight();
// Add event listeners, if console enabled.
if ( getQueryVariable("console") == "1"
|| getQueryVariable("console") == "2"
|| localStorage.getItem("console-enabled") == "true") {
// Add show/hide key event listener.
document.addEventListener("keyup", GW.console.keyUp);
// Add command line “Enter” key event listener.
document.addEventListener("keydown", GW.console.keyDown);
// Add command line input (text entry) event listener.
GW.console.view.input.addEventListener("input", GW.console.commandLineInputReceived);
}
// Show console, if auto-show enabled.
if (getQueryVariable("console") == "2")
GW.console.show();
});