-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotnetconsole.worker.js
384 lines (295 loc) · 10.2 KB
/
dotnetconsole.worker.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
importScripts('bridge.js');
Console = (() => {
// Helper objects
let consoleKeysForCode = {
Backspace: ConsoleKey.backspace,
Tab: ConsoleKey.tab,
Enter: ConsoleKey.enter,
NumpadEnter: ConsoleKey.enter,
Pause: ConsoleKey.pause,
Escape: ConsoleKey.escape,
Space: ConsoleKey.spacebar,
PageUp: ConsoleKey.pageUp,
PageDown: ConsoleKey.pageDown,
End: ConsoleKey.end,
Home: ConsoleKey.home,
ArrowLeft: ConsoleKey.leftArrow,
ArrowUp: ConsoleKey.upArrow,
ArrowRight: ConsoleKey.rightArrow,
ArrowDown: ConsoleKey.downArrow,
Select: ConsoleKey.select,
PrintScreen: ConsoleKey.printScreen,
Insert: ConsoleKey.insert,
Delete: ConsoleKey.delete,
Help: ConsoleKey.help,
MetaLeft: ConsoleKey.leftWindows,
MetaRight: ConsoleKey.rightWindows,
ContextMenu: ConsoleKey.applications,
Minus: ConsoleKey.subtract,
};
for (let charCode = 65; charCode <= 90; charCode++) {
let char = String.fromCharCode(charCode);
consoleKeysForCode[`Key${char}`] = ConsoleKey[char.toLowerCase()];
}
for (let digit = 0; digit <= 9; digit++) {
consoleKeysForCode[`Digit${digit}`] = ConsoleKey[`d${digit}`];
}
// Properties' backing variables
let backgroundColor = -1;
let bufferHeight = 24;
let bufferWidth = 80;
let cursorLeft = 0;
let cursorTop = 0;
let cursorVisible = true;
let foregroundColor = -1;
let windowHeight = 24;
let windowWidth = 80;
// Private variables
let readKeyConsoleKeyInfo = null;
let readKeyCallback = null;
let readLineValue = "";
let readLineCallback = null;
let lastWritePositionWasBottomRight = false;
// Operations
function keydown({code, key, altKey, ctrlKey, shiftKey}) {
let consoleKey = consoleKeysForCode[code] || 0;
// We assume we have a printable key character if the key is of length one (special keys have longer names).
let keyChar = key.length === 1 ? key : '';
if (readLineCallback) {
// Ignore presses while control is down.
if (ctrlKey) return;
// Read line command ends when enter is pressed.
if (consoleKey === ConsoleKey.enter) {
newLine();
let value = readLineValue;
let callback = readLineCallback
readLineValue = "";
readLineCallback = null;
callback(value);
return;
}
// Backspace moves back.
if (consoleKey === ConsoleKey.backspace) {
// Nothing happens if we're at the start again.
if (readLineValue.length === 0) return;
readLineValue = readLineValue.substring(0, readLineValue.length - 1);
// Move cursor one spot back.
cursorLeft--;
if (cursorLeft < 0) {
cursorLeft = bufferWidth - 1;
cursorTop--;
}
postMessage({
operation: 'setCursorPosition',
cursorLeft,
cursorTop
});
// Delete character at cursor.
postMessage({
operation: 'writeCharacter',
x: cursorLeft,
y: cursorTop,
character: ' '
});
return;
}
// Keys that don't have a character don't do anything.
if (keyChar.length !== 1) return;
// Add the character to the string and write it out.
readLineValue += keyChar;
write(keyChar);
} else {
// Store console key information so we can return it in subsequent read key calls.
readKeyConsoleKeyInfo = new ConsoleKeyInfo.$ctor1(keyChar, consoleKey, shiftKey, altKey, ctrlKey);
// If the console is already waiting for a press, return it.
if (readKeyCallback) {
let callback = readKeyCallback;
readKeyCallback = null;
callback(getReadKeyConsoleKeyInfo());
}
}
}
function getReadKeyConsoleKeyInfo()
{
let consoleKeyInfo = readKeyConsoleKeyInfo;
readKeyConsoleKeyInfo = null;
return consoleKeyInfo;
}
let operations = {keydown};
onmessage = messageEvent => {
operations[messageEvent.data.operation](messageEvent.data);
}
// Private methods
function scroll() {
postMessage({operation: 'scroll'});
}
function setColors(foreground, background) {
foregroundColor = foreground;
backgroundColor = background;
postMessage({
operation: 'setColors',
foregroundColor,
backgroundColor
});
}
function newLine() {
// Move to a new line.
cursorLeft = 0;
cursorTop++;
// If we're out of bounds, scroll.
if (cursorTop >= bufferHeight) {
scroll();
cursorTop = bufferHeight - 1;
}
postMessage({
operation: 'setCursorPosition',
cursorLeft,
cursorTop
});
lastWritePositionWasBottomRight = false;
}
function write(value, mustEndWithNewLine = false) {
if (!value) return;
let wentToNewLineLast = false;
for (let i = 0; i < value.length; i++) {
let character = value[i];
wentToNewLineLast = false;
switch (character) {
case '\n':
newLine();
continue;
default:
// If the last character written was in the bottom-right and we try to write in bottom-left, scroll first.
if (lastWritePositionWasBottomRight && cursorLeft === 0 && cursorTop === bufferHeight - 1) {
scroll();
}
// Update UI on main thread.
postMessage({
operation: 'writeCharacter',
x: cursorLeft,
y: cursorTop,
character
});
}
// See if we wrote to the bottom-right.
if (cursorLeft === bufferWidth - 1 && cursorTop === bufferHeight - 1) {
lastWritePositionWasBottomRight = true;
}
// Move cursor to the right.
cursorLeft++;
// Displayed cursor must stay in the same line and within the bounds.
let cursorLeftDisplayed = Math.min(cursorLeft, bufferWidth - 1);
postMessage({
operation: 'setCursorPosition',
cursorLeft: cursorLeftDisplayed,
cursorTop
});
// If we're at the end, move one down.
if (cursorLeft >= bufferWidth) {
cursorLeft = 0;
cursorTop++;
wentToNewLineLast = true;
}
// Make sure we stay within bottom bounds.
if (cursorTop >= bufferHeight) {
cursorTop = bufferHeight - 1;
// Don't scroll if this is the last character and we don't need an end line.
if (i !== value.length - 1 || mustEndWithNewLine) scroll();
}
}
// If we must end with new line, the cursor needs to appear at the end.
if (mustEndWithNewLine) {
// If we already went one line down, just reposition the cursor.
if (wentToNewLineLast) {
postMessage({
operation: 'setCursorPosition',
cursorLeft,
cursorTop
});
} else {
// We didn't go down yet, so we just do a new line.
newLine();
}
}
}
return class Console {
// Properties
static get backgroundColor() { return backgroundColor; }
static set backgroundColor(value) { setColors(foregroundColor, value); }
static get bufferHeight() { return bufferHeight; }
static set bufferHeight(value) { bufferHeight = value; }
static get bufferWidth() { return bufferWidth; }
static set bufferWidth(value) { bufferWidth = value; }
static get cursorLeft() { return cursorLeft; }
static set cursorLeft(value) { this.setCursorPosition(value, cursorTop) }
static get cursorTop() { return cursorTop; }
static set cursorTop(value) { this.setCursorPosition(cursorLeft, value); }
static get cursorVisible() { return cursorVisible; }
static set cursorVisible(value) { cursorVisible = value; }
static get foregroundColor() { return foregroundColor; }
static set foregroundColor(value) { setColors(value, backgroundColor); }
static get windowHeight() { return windowHeight; }
static set windowHeight(value) { windowHeight = value; }
static get windowWidth() { return windowWidth; }
static set windowWidth(value) { windowWidth = value; }
// Methods
static clear() {
postMessage({operation:'clear'});
this.setCursorPosition(0, 0);
}
static readKey(intercept = false) {
let taskCompletionSource = new System.Threading.Tasks.TaskCompletionSource();
let completeReadKey = (consoleKeyInfo) => {
taskCompletionSource.setResult(consoleKeyInfo);
if (!intercept) {
write(consoleKeyInfo.keyChar);
}
}
// See if we already have a key waiting for us.
let consoleKeyInfo = getReadKeyConsoleKeyInfo();
if (consoleKeyInfo) {
// Immediately fulfill the task.
completeReadKey(consoleKeyInfo);
} else {
// Register a callback to continue execution.
readKeyCallback = completeReadKey;
}
return taskCompletionSource.task;
}
static readLine() {
let taskCompletionSource = new System.Threading.Tasks.TaskCompletionSource();
readLineCallback = value => {
taskCompletionSource.setResult(value);
}
return taskCompletionSource.task;
}
static resetColor() {
setColors(-1, -1);
}
static setCursorPosition(left, top) {
if (left < 0 || left >= bufferWidth) {
throw new Error("The value must be greater than or equal to zero and less than the console's buffer size in that dimension. (Parameter: 'left')")
}
if (top < 0 || top >= bufferHeight) {
throw new Error("The value must be greater than or equal to zero and less than the console's buffer size in that dimension. (Parameter: 'top')")
}
cursorLeft = left;
cursorTop = top;
postMessage({
operation: 'setCursorPosition',
cursorLeft,
cursorTop
});
}
static writeLine(value) {
if (value) {
write(value, true);
} else {
newLine();
}
}
static write(value) {
write(value);
}
}
})();