index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. term.write("Aborted\r\n");
  19. },
  20. };
  21. function term_init() {
  22. term.open(document.getElementById('terminal'));
  23. term.onData(e => {
  24. if (stopped) return;
  25. switch (e) {
  26. case '\r': // Enter
  27. term.write("\r\n");
  28. if(command == 'exit()'){
  29. stopped = true;
  30. term.write("Bye!\r\n");
  31. break;
  32. }
  33. need_more_lines = Module.ccall('repl_input', Boolean, ['string'], [command]);
  34. command = '';
  35. term.write(need_more_lines ? "... " : ">>> ");
  36. break;
  37. case '\u007F': // Backspace (DEL)
  38. // Do not delete the prompt
  39. if (term._core.buffer.x > 4) { // '>>> ' or '... '
  40. term.write('\b \b');
  41. if (command.length > 0) {
  42. command = command.substr(0, command.length - 1);
  43. }
  44. }
  45. break;
  46. default: // Print all other characters for demo
  47. if (e >= String.fromCharCode(0x20) && e <= String.fromCharCode(0x7E) || e >= '\u00a0') {
  48. command += e;
  49. term.write(e);
  50. }
  51. }
  52. });
  53. }