index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const term = new Terminal();
  2. var command = "";
  3. var need_more_lines = false;
  4. var stopped = false;
  5. var repl = 0;
  6. var Module = {
  7. 'print': function(text) {
  8. term.write(text + "\r\n");
  9. },
  10. 'printErr': function(text) {
  11. term.write(text + "\r\n");
  12. },
  13. 'onRuntimeInitialized': function(text) {
  14. var vm = Module.ccall('pkpy_new_vm', 'number', ['boolean'], [true]);
  15. repl = Module.ccall('pkpy_new_repl', 'number', ['number'], [vm]);
  16. term.write(need_more_lines ? "... " : ">>> ");
  17. },
  18. 'onAbort': function(text) {
  19. stopped = true;
  20. },
  21. };
  22. function term_init() {
  23. term.open(document.getElementById('terminal'));
  24. term.onData(e => {
  25. if (stopped) return;
  26. switch (e) {
  27. case '\r': // Enter
  28. term.write("\r\n");
  29. need_more_lines = Module.ccall('pkpy_repl_input', 'bool', ['number', 'string'], [repl, command]);
  30. command = '';
  31. term.write(need_more_lines ? "... " : ">>> ");
  32. break;
  33. case '\u007F': // Backspace (DEL)
  34. // Do not delete the prompt
  35. if (term._core.buffer.x > 4) { // '>>> ' or '... '
  36. term.write('\b \b');
  37. if (command.length > 0) {
  38. command = command.substr(0, command.length - 1);
  39. }
  40. }
  41. break;
  42. default: // Print all other characters for demo
  43. if (e >= String.fromCharCode(0x20) && e <= String.fromCharCode(0x7E) || e >= '\u00a0') {
  44. command += e;
  45. term.write(e);
  46. }
  47. }
  48. });
  49. }