|
20 | 20 | margin: 0 auto; |
21 | 21 | } |
22 | 22 | #editor { |
23 | | - padding:5px; |
| 23 | + padding: 5px; |
24 | 24 | border: 1px solid black; |
25 | 25 | width: 100%; |
26 | 26 | height: 300px; |
|
139 | 139 |
|
140 | 140 | class WasmTerminal { |
141 | 141 | constructor() { |
142 | | - this.reset() |
| 142 | + this.reset(); |
143 | 143 |
|
144 | 144 | this.xterm = new Terminal({ |
145 | 145 | scrollback: 10000, |
|
158 | 158 | this.xterm.onData(this.handleTermData); |
159 | 159 | } |
160 | 160 |
|
161 | | - reset(){ |
| 161 | + reset() { |
162 | 162 | this.inputBuffer = new BufferQueue(); |
163 | 163 | this.input = ""; |
164 | 164 | this.resolveInput = null; |
|
203 | 203 | if (!(ord === 0x1b || ord == 0x7f || ord < 32)) { |
204 | 204 | this.inputBuffer.addData(data); |
205 | 205 | } |
206 | | - // TODO: Handle more escape sequences? |
| 206 | + // TODO: Handle more escape sequences? |
207 | 207 | } else if (ord === 0x1b) { |
208 | 208 | // Handle special characters |
209 | | - switch(data.slice(1)){ |
210 | | - case '[A': // up |
| 209 | + switch (data.slice(1)) { |
| 210 | + case "[A": // up |
211 | 211 | this.historyBack(); |
212 | 212 | break; |
213 | | - case '[B': // down |
| 213 | + case "[B": // down |
214 | 214 | this.historyForward(); |
215 | 215 | break; |
216 | | - case '[C': // right |
| 216 | + case "[C": // right |
217 | 217 | this.cursorRight(); |
218 | 218 | break; |
219 | | - case '[D': // left |
| 219 | + case "[D": // left |
220 | 220 | this.cursorLeft(); |
221 | 221 | break; |
222 | | - case '[H': // home key |
| 222 | + case "[H": // home key |
223 | 223 | this.cursorHome(true); |
224 | 224 | break; |
225 | | - case '[F': // end key |
| 225 | + case "[F": // end key |
226 | 226 | this.cursorEnd(true); |
227 | 227 | break; |
228 | | - case '[3~': // delete key |
| 228 | + case "[3~": // delete key |
229 | 229 | this.deleteAtCursor(); |
230 | 230 | break; |
231 | 231 | default: |
|
248 | 248 | break; |
249 | 249 | case "\x03": // CTRL+C |
250 | 250 | this.input = ""; |
251 | | - this.xterm.write("\n") |
252 | | - this.xterm.write('\x1b[' + (this.cursorPosition + 4) + 'D'); |
| 251 | + this.xterm.write("\n"); |
| 252 | + this.xterm.write( |
| 253 | + "\x1b[" + (this.cursorPosition + 4) + "D", |
| 254 | + ); |
253 | 255 | this.cursorPosition = 0; |
254 | | - this.resolveInput("" + '\n'); |
| 256 | + this.resolveInput("" + "\n"); |
255 | 257 | break; |
256 | 258 | case "\x09": // TAB |
257 | 259 | this.handleTab(); |
|
274 | 276 | } |
275 | 277 | }; |
276 | 278 |
|
277 | | - clearLine(){ |
278 | | - this.xterm.write('\x1b[K') |
| 279 | + clearLine() { |
| 280 | + this.xterm.write("\x1b[K"); |
279 | 281 | } |
280 | 282 |
|
281 | 283 | writeLine(line) { |
|
286 | 288 |
|
287 | 289 | handleCursorInsert(data) { |
288 | 290 | const trailing = this.input.slice(this.cursorPosition); |
289 | | - this.input = this.input.slice(0, this.cursorPosition) + data + trailing; |
| 291 | + this.input = |
| 292 | + this.input.slice(0, this.cursorPosition) + |
| 293 | + data + |
| 294 | + trailing; |
290 | 295 | this.cursorPosition += data.length; |
291 | 296 | this.xterm.write(data); |
292 | | - if (trailing.length !== 0){ |
| 297 | + if (trailing.length !== 0) { |
293 | 298 | this.xterm.write(trailing); |
294 | | - this.xterm.write('\x1b[' + trailing.length + 'D'); |
| 299 | + this.xterm.write("\x1b[" + trailing.length + "D"); |
295 | 300 | } |
296 | 301 | this.updateHistory(); |
297 | 302 | } |
|
303 | 308 | const suffix = this.input.slice(this.cursorPosition); |
304 | 309 | const count = 4 - (this.cursorPosition % 4); |
305 | 310 | const toAdd = " ".repeat(count); |
306 | | - this.input = prefix + toAdd + suffix |
| 311 | + this.input = prefix + toAdd + suffix; |
307 | 312 | this.cursorHome(false); |
308 | 313 | this.clearLine(); |
309 | 314 | this.xterm.write(this.input); |
310 | | - if (suffix){ |
311 | | - this.xterm.write('\x1b[' + suffix.length + 'D'); |
| 315 | + if (suffix) { |
| 316 | + this.xterm.write("\x1b[" + suffix.length + "D"); |
312 | 317 | } |
313 | 318 | this.cursorPosition += count; |
314 | 319 | this.updateHistory(false); |
|
323 | 328 | return; |
324 | 329 | } |
325 | 330 | const trailing = this.input.slice(this.cursorPosition); |
326 | | - this.input = this.input.slice(0, this.cursorPosition - 1) + trailing; |
| 331 | + this.input = |
| 332 | + this.input.slice(0, this.cursorPosition - 1) + trailing; |
327 | 333 | this.cursorLeft(); |
328 | 334 | this.clearLine(); |
329 | | - if (trailing.length !== 0){ |
| 335 | + if (trailing.length !== 0) { |
330 | 336 | this.xterm.write(trailing); |
331 | | - this.xterm.write('\x1b[' + trailing.length + 'D'); |
| 337 | + this.xterm.write("\x1b[" + trailing.length + "D"); |
332 | 338 | } |
333 | 339 | this.updateHistory(); |
334 | 340 | } |
335 | 341 |
|
336 | | - deleteAtCursor(){ |
337 | | - if (this.cursorPosition < this.input.length){ |
338 | | - const trailing = this.input.slice(this.cursorPosition + 1); |
339 | | - this.input = this.input.slice(0, this.cursorPosition) + trailing; |
| 342 | + deleteAtCursor() { |
| 343 | + if (this.cursorPosition < this.input.length) { |
| 344 | + const trailing = this.input.slice( |
| 345 | + this.cursorPosition + 1, |
| 346 | + ); |
| 347 | + this.input = |
| 348 | + this.input.slice(0, this.cursorPosition) + trailing; |
340 | 349 | this.clearLine(); |
341 | | - if (trailing.length !== 0){ |
| 350 | + if (trailing.length !== 0) { |
342 | 351 | this.xterm.write(trailing); |
343 | | - this.xterm.write('\x1b[' + trailing.length + 'D'); |
| 352 | + this.xterm.write("\x1b[" + trailing.length + "D"); |
344 | 353 | } |
345 | 354 | this.updateHistory(); |
346 | 355 | } |
347 | 356 | } |
348 | 357 |
|
349 | | - cursorRight(){ |
350 | | - if (this.cursorPosition < this.input.length){ |
351 | | - this.cursorPosition += 1; |
352 | | - this.xterm.write('\x1b[C'); |
353 | | - } |
| 358 | + cursorRight() { |
| 359 | + if (this.cursorPosition < this.input.length) { |
| 360 | + this.cursorPosition += 1; |
| 361 | + this.xterm.write("\x1b[C"); |
| 362 | + } |
354 | 363 | } |
355 | 364 |
|
356 | | - cursorLeft(){ |
357 | | - if (this.cursorPosition > 0){ |
| 365 | + cursorLeft() { |
| 366 | + if (this.cursorPosition > 0) { |
358 | 367 | this.cursorPosition -= 1; |
359 | | - this.xterm.write('\x1b[D'); |
| 368 | + this.xterm.write("\x1b[D"); |
360 | 369 | } |
361 | 370 | } |
362 | 371 |
|
363 | 372 | cursorHome(updatePosition) { |
364 | | - if (this.cursorPosition > 0){ |
365 | | - this.xterm.write('\x1b[' + this.cursorPosition + 'D'); |
| 373 | + if (this.cursorPosition > 0) { |
| 374 | + this.xterm.write("\x1b[" + this.cursorPosition + "D"); |
366 | 375 | if (updatePosition) { |
367 | 376 | this.cursorPosition = 0; |
368 | 377 | } |
369 | 378 | } |
370 | 379 | } |
371 | 380 |
|
372 | 381 | cursorEnd() { |
373 | | - if (this.cursorPosition < this.input.length){ |
374 | | - this.xterm.write('\x1b[' + (this.input.length - this.cursorPosition) + 'C'); |
| 382 | + if (this.cursorPosition < this.input.length) { |
| 383 | + this.xterm.write( |
| 384 | + "\x1b[" + |
| 385 | + (this.input.length - this.cursorPosition) + |
| 386 | + "C", |
| 387 | + ); |
375 | 388 | this.cursorPosition = this.input.length; |
376 | 389 | } |
377 | 390 | } |
378 | 391 |
|
379 | | - updateHistory(){ |
380 | | - if (this.historyIndex !== -1){ |
| 392 | + updateHistory() { |
| 393 | + if (this.historyIndex !== -1) { |
381 | 394 | this.historyBuffer[this.historyIndex] = this.input; |
382 | | - }else{ |
| 395 | + } else { |
383 | 396 | this.beforeHistoryNav = this.input; |
384 | 397 | } |
385 | 398 | } |
386 | 399 |
|
387 | | - historyBack(){ |
388 | | - if (this.history.length === 0){ |
| 400 | + historyBack() { |
| 401 | + if (this.history.length === 0) { |
389 | 402 | return; |
390 | | - }else if (this.historyIndex === -1){ |
| 403 | + } else if (this.historyIndex === -1) { |
391 | 404 | // we're not currently navigating the history; store |
392 | 405 | // the current command and then look at the end of our |
393 | 406 | // history buffer |
394 | 407 | this.beforeHistoryNav = this.input; |
395 | 408 | this.historyIndex = this.history.length - 1; |
396 | | - }else if (this.historyIndex > 0){ |
| 409 | + } else if (this.historyIndex > 0) { |
397 | 410 | this.historyIndex -= 1; |
398 | 411 | } |
399 | 412 | this.input = this.historyBuffer[this.historyIndex]; |
|
403 | 416 | this.cursorPosition = this.input.length; |
404 | 417 | } |
405 | 418 |
|
406 | | - historyForward(){ |
407 | | - if (this.history.length === 0 || this.historyIndex === -1){ |
| 419 | + historyForward() { |
| 420 | + if (this.history.length === 0 || this.historyIndex === -1) { |
408 | 421 | // we're not currently navigating the history; NOP. |
409 | 422 | return; |
410 | | - }else if (this.historyIndex < this.history.length - 1){ |
| 423 | + } else if (this.historyIndex < this.history.length - 1) { |
411 | 424 | this.historyIndex += 1; |
412 | 425 | this.input = this.historyBuffer[this.historyIndex]; |
413 | | - }else if (this.historyIndex == this.history.length - 1){ |
| 426 | + } else if (this.historyIndex == this.history.length - 1) { |
414 | 427 | // we're coming back from the last history value; reset |
415 | 428 | // the input to whatever it was when we started going |
416 | 429 | // through the history |
|
449 | 462 | } |
450 | 463 | return new Promise((resolve, reject) => { |
451 | 464 | this.resolveInput = (value) => { |
452 | | - if (value.replace(/\s/g, '').length != 0){ |
453 | | - if (this.historyIndex !== -1){ |
454 | | - this.historyBuffer[this.historyIndex] = this.history[this.historyIndex]; |
| 465 | + if (value.replace(/\s/g, "").length != 0) { |
| 466 | + if (this.historyIndex !== -1) { |
| 467 | + this.historyBuffer[this.historyIndex] = |
| 468 | + this.history[this.historyIndex]; |
455 | 469 | } |
456 | 470 | this.history.push(value.slice(0, -1)); |
457 | 471 | this.historyBuffer.push(value.slice(0, -1)); |
|
594 | 608 | finishedCallback, |
595 | 609 | ); |
596 | 610 | }; |
597 | | - var editor; |
598 | | - document.addEventListener('DOMContentLoaded', () => { |
599 | | - editor = ace.edit("editor"); |
600 | | - editor.session.setMode("ace/mode/python"); |
601 | | - }); |
| 611 | + var editor; |
| 612 | + document.addEventListener("DOMContentLoaded", () => { |
| 613 | + editor = ace.edit("editor"); |
| 614 | + editor.session.setMode("ace/mode/python"); |
| 615 | + }); |
602 | 616 | </script> |
603 | 617 | </head> |
604 | 618 | <body> |
|
0 commit comments