testConsole.js 2.1 KB

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