|
248 | 248 | break; |
249 | 249 | case "\x03": // CTRL+C |
250 | 250 | this.input = ""; |
251 | | - this.xterm.write("\n"); |
252 | | - this.xterm.write( |
253 | | - "\x1b[" + (this.cursorPosition + 4) + "D", |
254 | | - ); |
255 | 251 | this.cursorPosition = 0; |
256 | | - this.resolveInput("" + "\n"); |
| 252 | + this.historyIndex = -1; |
| 253 | + this.resolveInput("__WASM_REPL_CTRLC__\n"); |
257 | 254 | break; |
258 | 255 | case "\x09": // TAB |
259 | 256 | this.handleTab(); |
|
456 | 453 | // Hack to ensure cursor input start doesn't end up after user input |
457 | 454 | setTimeout(() => { |
458 | 455 | this.handleCursorInsert( |
459 | | - this.inputBuffer.nextLine(), |
| 456 | + this.inputBuffer.nextLine() |
460 | 457 | ); |
461 | 458 | }, 1); |
462 | 459 | } |
463 | 460 | return new Promise((resolve, reject) => { |
464 | 461 | this.resolveInput = (value) => { |
465 | | - if (value.replace(/\s/g, "").length != 0) { |
| 462 | + if ( |
| 463 | + value.replace(/\s/g, "").length != 0 && |
| 464 | + value != "__WASM_REPL_CTRLC__\n" |
| 465 | + ) { |
466 | 466 | if (this.historyIndex !== -1) { |
467 | 467 | this.historyBuffer[this.historyIndex] = |
468 | 468 | this.history[this.historyIndex]; |
|
575 | 575 | replButton.addEventListener("click", (e) => { |
576 | 576 | terminal.clear(); |
577 | 577 | terminal.reset(); // reset the history |
| 578 | + const REPL = ` |
| 579 | +class WASMREPLKeyboardInterrupt(KeyboardInterrupt): |
| 580 | + pass |
| 581 | +
|
| 582 | +import sys |
| 583 | +import code |
| 584 | +import builtins |
| 585 | +
|
| 586 | +def _interrupt_aware_input(prompt=''): |
| 587 | + line = builtins.input(prompt) |
| 588 | + if line.strip() == '__WASM_REPL_CTRLC__': |
| 589 | + raise KeyboardInterrupt() |
| 590 | + return line |
| 591 | +
|
| 592 | +cprt = 'Type "help", "copyright", "credits" or "license" for more information.' |
| 593 | +banner = f'Python {sys.version} on {sys.platform}\\n{cprt}' |
| 594 | +
|
| 595 | +code.interact(banner=banner, readfunc=_interrupt_aware_input, exitmsg='') |
| 596 | +`; |
578 | 597 | programRunning(true); |
579 | | - // Need to use "-i -" to force interactive mode. |
580 | | - // Looks like isatty always returns false in emscripten |
581 | | - pythonWorkerManager.run({ args: ["-i", "-"], files: {} }); |
| 598 | + pythonWorkerManager.run({ args: ["-c", REPL], files: {} }); |
582 | 599 | }); |
583 | 600 |
|
584 | 601 | stopButton.addEventListener("click", (e) => { |
|
0 commit comments