index.js 1.5 KB

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