testConsole.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { DOMConsole } from "../io/domConsole";
  2. import { getInstance } from "../services/localizedStringsService";
  3. const LocalizedStrings = getInstance();
  4. export class TestConsole {
  5. constructor (inputList) {
  6. this.index = 0;
  7. this.inputList = inputList;
  8. this.list = [];
  9. this.currentLine = null;
  10. }
  11. writeRawHTML (text) {
  12. this._appendTextLn(text);
  13. }
  14. write (text, newLine = false) {
  15. this._appendText(text, DOMConsole.USER, newLine);
  16. }
  17. info (text) {
  18. this._appendTextLn(text, DOMConsole.INFO);
  19. }
  20. err (text) {
  21. this._appendTextLn(text, DOMConsole.ERR);
  22. }
  23. _appendText (text, _type, newLine = false) {
  24. if (this.currentLine == null) {
  25. this.currentLine = this.list.push('') - 1;
  26. }
  27. this.list[this.currentLine] += text;
  28. if (newLine) {
  29. this.currentLine = null;
  30. }
  31. }
  32. _appendTextLn (text) {
  33. this.list.push(text);
  34. this.currentLine = null;
  35. }
  36. getClassForType (type) {
  37. switch (type) {
  38. case DOMConsole.USER:
  39. return "ivprog-term-userText";
  40. case DOMConsole.INFO:
  41. return "ivprog-term-info";
  42. case DOMConsole.ERR:
  43. return "ivprog-term-error";
  44. }
  45. }
  46. requestInput () {
  47. const promise = new Promise( (resolve, reject) => {
  48. if(this.index < this.inputList.length) {
  49. resolve(this.inputList[this.index]);
  50. this.index++;
  51. } else {
  52. reject(new Error(LocalizedStrings.getError("exceeded_input_request")));
  53. }
  54. });
  55. return promise
  56. }
  57. sendOutput (text) {
  58. let output = '' + text;
  59. if (output.indexOf('\n') !== -1) {
  60. const outputList = output.split('\n');
  61. let last = outputList.pop();
  62. outputList.forEach( t => {
  63. t = t.replace(/\t/g, '&#x0020;&#x0020;');
  64. t = t.replace(/\s/g, "&#x0020;");
  65. if (t.length == 0)
  66. this.currentLine = null;
  67. else
  68. this.write(t, true);
  69. });
  70. last = last.replace(/\t/g, '&#x0020;&#x0020;');
  71. last = last.replace(/\s/g, "&#x0020;");
  72. if (last.length != 0)
  73. this.write(last);
  74. } else {
  75. output = output.replace(/\t/g, '&#x0020;&#x0020;');
  76. output = output.replace(/\s/g, "&#x0020;");
  77. this.write(output);
  78. }
  79. }
  80. }