@innovatorved/code-lab 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/ARCHITECTURE.md +4 -2
  2. package/CHANGELOG.md +38 -0
  3. package/README.md +15 -5
  4. package/dist/code-lab.css +52 -2
  5. package/dist/code-lab.d.ts +1 -1
  6. package/dist/code-lab.esm.js +1533 -396
  7. package/dist/code-lab.js +1532 -395
  8. package/dist/diagnostics.d.ts +5 -0
  9. package/dist/extensions/cpp.d.ts +8 -0
  10. package/dist/extensions/cpp.esm.js +333 -10
  11. package/dist/extensions/cpp.js +333 -10
  12. package/dist/extensions/guided-lab.esm.js +1 -1
  13. package/dist/extensions/guided-lab.js +1 -1
  14. package/dist/extensions/python.esm.js +117 -104
  15. package/dist/extensions/python.js +117 -104
  16. package/dist/index.d.ts +1 -1
  17. package/dist/preview-sw.js +34 -17
  18. package/dist/runtime.esm.js +38 -31
  19. package/dist/runtime.js +38 -31
  20. package/dist/schemas.d.ts +4 -4
  21. package/dist/schemas.esm.js +1 -1
  22. package/dist/schemas.js +1 -1
  23. package/dist/scm.d.ts +13 -2
  24. package/dist/search.d.ts +5 -1
  25. package/dist/shell.esm.js +72 -16
  26. package/dist/shell.js +72 -16
  27. package/dist/terminal.esm.js +183 -54
  28. package/dist/terminal.js +183 -54
  29. package/dist/workbench/index.d.ts +1 -1
  30. package/dist/workbench/styles.d.ts +1 -1
  31. package/dist/zip.d.ts +4 -3
  32. package/package.json +1 -1
  33. package/src/code-lab.css +52 -2
  34. package/src/csp.ts +7 -10
  35. package/src/diagnostics.ts +42 -3
  36. package/src/extensions/cpp.ts +303 -9
  37. package/src/extensions/python.ts +128 -116
  38. package/src/index.ts +1 -1
  39. package/src/preview-sw.js +25 -5
  40. package/src/runtime.ts +36 -29
  41. package/src/schemas.ts +2 -2
  42. package/src/scm.ts +106 -19
  43. package/src/search.ts +71 -27
  44. package/src/shell.ts +70 -14
  45. package/src/terminal.ts +181 -52
  46. package/src/workbench/diff-editor.ts +41 -13
  47. package/src/workbench/drawer.ts +17 -8
  48. package/src/workbench/guided-pane.ts +12 -2
  49. package/src/workbench/share.ts +37 -8
  50. package/src/workbench/styles.ts +2 -1
  51. package/src/workbench/toolbar.ts +15 -4
  52. package/src/workbench.ts +484 -91
  53. package/src/zip.ts +29 -7
  54. package/dist/runtime/module-loader.d.ts +0 -11
  55. package/dist/runtime/vfs.d.ts +0 -31
  56. package/dist/runtime/wasi-host.d.ts +0 -33
  57. package/dist/shell/lexer-parser.d.ts +0 -26
@@ -1,11 +1,11 @@
1
1
  /**
2
2
  * ============================================================
3
- * @innovatorved/code-lab v0.3.0 — Standalone Browser Code IDE & Virtual Runtime
3
+ * @innovatorved/code-lab v0.3.2 — Standalone Browser Code IDE & Virtual Runtime
4
4
  * ============================================================
5
5
  * Features:
6
6
  * - Sandboxed Web Worker Execution Engine with Multi-Instance Isolation & Watchdog Timer
7
7
  * - First-Class Node.js and Bun Runtimes (Bun.serve, bun:test, bun:sqlite, Bun.*)
8
- * - Full POSIX Shell AST & 55+ UNIX Coreutils (pipes, redirects, globs, env vars, git & git clone)
8
+ * - Full POSIX Shell AST & 71 UNIX Coreutils (pipes, redirects, globs, env vars, git & git clone)
9
9
  * - Interactive PTY Terminal Emulator with inline prompt, Tab completion & ANSI colors
10
10
  * - Persistent HTTP/TCP Servers, CLI curl & Live Port/HTTP Preview
11
11
  * - Modular VS Code Workbench: File Tree, Tabs, Command Palette, Diff Viewer
@@ -422,6 +422,14 @@ var CodeLabLogger = (() => {
422
422
 
423
423
  const parseAnsiToHtml = ansiToHtml;
424
424
 
425
+ const MAX_SCROLLBACK = 2000;
426
+ const VALID_LOG_TYPES = new Set(['stdout', 'stderr', 'system', 'info', 'warn', 'error', 'success']);
427
+
428
+ function sanitizeLogType(type) {
429
+ const t = String(type || 'stdout').trim();
430
+ return VALID_LOG_TYPES.has(t) ? t : 'stdout';
431
+ }
432
+
425
433
  class PTYTerminalEngine {
426
434
  constructor(container, options = {}) {
427
435
  if (
@@ -436,6 +444,9 @@ var CodeLabLogger = (() => {
436
444
  }
437
445
  this.container = container || null;
438
446
  this.options = options || {};
447
+ this.maxScrollback = typeof this.options.maxScrollback === 'number' && this.options.maxScrollback > 0
448
+ ? this.options.maxScrollback
449
+ : MAX_SCROLLBACK;
439
450
  this.logs = Array.isArray(this.options.logs) ? this.options.logs : [];
440
451
  this.cwd = this.options.cwd || '/workspace';
441
452
  this.replMode = Boolean(this.options.replMode);
@@ -449,6 +460,7 @@ var CodeLabLogger = (() => {
449
460
  this.searchQuery = '';
450
461
  this.searchMatchIdx = -1;
451
462
  this._readLineQueue = [];
463
+ this.isBusy = false;
452
464
  this.onCommand = typeof this.options.onCommand === 'function' ? this.options.onCommand : null;
453
465
  this.getCompletions = typeof this.options.getCompletions === 'function' ? this.options.getCompletions : null;
454
466
  this.onKey = typeof this.options.onKey === 'function' ? this.options.onKey : null;
@@ -599,13 +611,40 @@ var CodeLabLogger = (() => {
599
611
  this.writeStreamChunk(String(data) + '\n', type);
600
612
  }
601
613
 
614
+ isCommandRunning() {
615
+ return Boolean(this.isBusy && (!this._readLineQueue || this._readLineQueue.length === 0));
616
+ }
617
+
618
+ _pushLog(entry) {
619
+ const normalized = {
620
+ type: sanitizeLogType(entry && entry.type),
621
+ text: String(entry && entry.text != null ? entry.text : '')
622
+ };
623
+ this.logs.push(normalized);
624
+ if (this.logs.length > this.maxScrollback) {
625
+ const excess = this.logs.length - this.maxScrollback;
626
+ this.logs.splice(0, excess);
627
+ if (this._targetLineIdx >= 0) {
628
+ this._targetLineIdx = Math.max(0, this._targetLineIdx - excess);
629
+ }
630
+ if (this.container && typeof this.container.querySelector === 'function') {
631
+ const logEl = this.container.querySelector('#cl-terminal-log');
632
+ if (logEl && logEl.children && logEl.children.length > this.maxScrollback) {
633
+ while (logEl.children.length > this.maxScrollback && typeof logEl.removeChild === 'function' && logEl.firstChild) {
634
+ logEl.removeChild(logEl.firstChild);
635
+ }
636
+ }
637
+ }
638
+ }
639
+ return normalized;
640
+ }
641
+
602
642
  _getActiveLine(type) {
603
643
  if (this._targetLineIdx >= 0 && this._targetLineIdx < this.logs.length) {
604
644
  return this.logs[this._targetLineIdx];
605
645
  }
606
646
  if (this._activeLineCommitted || this.logs.length === 0) {
607
- const newLine = { text: '', type };
608
- this.logs.push(newLine);
647
+ const newLine = this._pushLog({ text: '', type });
609
648
  this._activeLineCommitted = false;
610
649
  this._cursorCol = 0;
611
650
  this._targetLineIdx = this.logs.length - 1;
@@ -622,7 +661,7 @@ var CodeLabLogger = (() => {
622
661
  const logEl = this.container.querySelector('#cl-terminal-log');
623
662
  if (!logEl) return;
624
663
  const lineEl = document.createElement('div');
625
- lineEl.className = 'codelab-log-line ' + (line.type || 'stdout');
664
+ lineEl.className = 'codelab-log-line ' + sanitizeLogType(line.type);
626
665
  lineEl.innerHTML = ansiToHtml(line.text);
627
666
  logEl.appendChild(lineEl);
628
667
  this._activeLineEl = lineEl;
@@ -645,7 +684,7 @@ var CodeLabLogger = (() => {
645
684
  }
646
685
  }
647
686
  if (lineEl) {
648
- lineEl.className = 'codelab-log-line ' + (line.type || 'stdout');
687
+ lineEl.className = 'codelab-log-line ' + sanitizeLogType(line.type);
649
688
  lineEl.innerHTML = ansiToHtml(line.text);
650
689
  }
651
690
  }
@@ -684,17 +723,29 @@ var CodeLabLogger = (() => {
684
723
  return [...this.history];
685
724
  }
686
725
 
687
- readLine(promptText = '') {
688
- const prevPrompt = this.promptOverride;
689
- if (promptText) {
690
- this.setPrompt(promptText);
726
+ _activateNextReadLine() {
727
+ if (!this._readLineQueue || this._readLineQueue.length === 0) return;
728
+ const head = this._readLineQueue[0];
729
+ if (head && typeof head === 'object' && !head._activated) {
730
+ head._activated = true;
731
+ head.prevPrompt = this.promptOverride;
732
+ if (head.promptText) {
733
+ this.promptOverride = head.promptText;
734
+ }
691
735
  }
736
+ }
737
+
738
+ readLine(promptText = '') {
692
739
  return new Promise((resolve) => {
693
- this._readLineQueue.push((line) => {
694
- this.promptOverride = prevPrompt;
695
- this.updateActiveLine();
696
- resolve(line);
697
- });
740
+ const item = {
741
+ promptText: String(promptText || ''),
742
+ prevPrompt: null,
743
+ _activated: false,
744
+ resolve
745
+ };
746
+ this._readLineQueue.push(item);
747
+ this._activateNextReadLine();
748
+ this.updateActiveLine();
698
749
  });
699
750
  }
700
751
 
@@ -764,6 +815,10 @@ var CodeLabLogger = (() => {
764
815
  if (input) {
765
816
  this._addListener(input, 'keydown', (e) => this.handleKeyDown(e));
766
817
  this._addListener(input, 'input', () => {
818
+ if (this.isCommandRunning()) {
819
+ input.value = '';
820
+ return;
821
+ }
767
822
  if (input.value !== this.buffer) {
768
823
  this.buffer = input.value;
769
824
  this.cursor = typeof input.selectionStart === 'number' ? input.selectionStart : this.buffer.length;
@@ -805,6 +860,35 @@ var CodeLabLogger = (() => {
805
860
  const key = e.key;
806
861
  const isCtrl = Boolean(e.ctrlKey || e.metaKey);
807
862
 
863
+ if (isCtrl && (key === 'l' || key === 'L')) {
864
+ if (e.preventDefault) e.preventDefault();
865
+ this.clear();
866
+ return;
867
+ }
868
+
869
+ if (isCtrl && (key === 'c' || key === 'C')) {
870
+ if (e.preventDefault) e.preventDefault();
871
+ const wasBusy = this.isBusy;
872
+ this.isBusy = false;
873
+ this._pushLog({ type: 'system', text: wasBusy ? '^C' : `${this.getPrompt()}${this.buffer}^C` });
874
+ this.buffer = '';
875
+ this.cursor = 0;
876
+ this.historyIdx = -1;
877
+ if (this.replMode) {
878
+ this.replMode = false;
879
+ }
880
+ if (this.onInterrupt) {
881
+ try { this.onInterrupt(); } catch (err) { _terminalLogger.warn('Terminal onInterrupt callback threw.', err); }
882
+ }
883
+ this.render();
884
+ return;
885
+ }
886
+
887
+ if (this.isCommandRunning()) {
888
+ if (e.preventDefault) e.preventDefault();
889
+ return;
890
+ }
891
+
808
892
  if (isCtrl && (key === 'r' || key === 'R')) {
809
893
  if (e.preventDefault) e.preventDefault();
810
894
  if (!this.searchMode) {
@@ -936,33 +1020,11 @@ var CodeLabLogger = (() => {
936
1020
  return;
937
1021
  }
938
1022
 
939
- if (isCtrl && (key === 'l' || key === 'L')) {
940
- if (e.preventDefault) e.preventDefault();
941
- this.clear();
942
- return;
943
- }
944
-
945
- if (isCtrl && (key === 'c' || key === 'C')) {
946
- if (e.preventDefault) e.preventDefault();
947
- this.logs.push({ type: 'system', text: `${this.getPrompt()}${this.buffer}^C` });
948
- this.buffer = '';
949
- this.cursor = 0;
950
- this.historyIdx = -1;
951
- if (this.replMode) {
952
- this.replMode = false;
953
- }
954
- if (this.onInterrupt) {
955
- try { this.onInterrupt(); } catch (err) { _terminalLogger.warn('Terminal onInterrupt callback threw.', err); }
956
- }
957
- this.render();
958
- return;
959
- }
960
-
961
1023
  if (isCtrl && (key === 'd' || key === 'D')) {
962
1024
  if (this.buffer === '') {
963
1025
  if (e.preventDefault) e.preventDefault();
964
1026
  if (this.replMode) {
965
- this.logs.push({ type: 'system', text: `${this.getPrompt()}.exit` });
1027
+ this._pushLog({ type: 'system', text: `${this.getPrompt()}.exit` });
966
1028
  this.replMode = false;
967
1029
  this.render();
968
1030
  return;
@@ -1056,14 +1118,41 @@ var CodeLabLogger = (() => {
1056
1118
  }
1057
1119
 
1058
1120
  // Print candidate matches
1059
- this.logs.push({ type: 'system', text: `${this.getPrompt()}${this.buffer}` });
1060
- this.logs.push({ type: 'stdout', text: matches.join(' ') });
1121
+ this._pushLog({ type: 'system', text: `${this.getPrompt()}${this.buffer}` });
1122
+ this._pushLog({ type: 'stdout', text: matches.join(' ') });
1061
1123
  this.render();
1062
1124
  }
1063
1125
  }
1064
1126
 
1065
1127
  submitLine() {
1128
+ if (this._readLineQueue && this._readLineQueue.length > 0) {
1129
+ const line = this.buffer;
1130
+ this.buffer = '';
1131
+ this.cursor = 0;
1132
+ this.historyIdx = -1;
1133
+ this.searchMode = false;
1134
+ this.searchQuery = '';
1135
+ this.searchMatchIdx = -1;
1136
+
1137
+ const entry = this._readLineQueue.shift();
1138
+ if (typeof entry === 'function') {
1139
+ this.updateActiveLine();
1140
+ entry(line);
1141
+ } else if (entry && typeof entry.resolve === 'function') {
1142
+ this.promptOverride = entry.prevPrompt;
1143
+ this._activateNextReadLine();
1144
+ this.updateActiveLine();
1145
+ entry.resolve(line);
1146
+ }
1147
+ return;
1148
+ }
1149
+
1150
+ if (this.isBusy) {
1151
+ return;
1152
+ }
1153
+
1066
1154
  const line = this.buffer;
1155
+ const promptSnap = this.getPrompt();
1067
1156
  this.buffer = '';
1068
1157
  this.cursor = 0;
1069
1158
  this.historyIdx = -1;
@@ -1071,22 +1160,49 @@ var CodeLabLogger = (() => {
1071
1160
  this.searchQuery = '';
1072
1161
  this.searchMatchIdx = -1;
1073
1162
 
1163
+ const echoed = this._pushLog({ type: 'system', text: `${promptSnap}${line}` });
1164
+ this._activeLineCommitted = true;
1165
+ this._cursorCol = 0;
1166
+ this._targetLineIdx = -1;
1167
+ this._needsRewrite = false;
1168
+ this._appendDomLine(echoed);
1169
+ this._activeLineEl = null;
1170
+
1074
1171
  if (line.trim()) {
1075
1172
  if (this.history[this.history.length - 1] !== line.trim()) {
1076
1173
  this.history.push(line.trim());
1077
1174
  }
1078
1175
  }
1079
1176
 
1080
- if (this._readLineQueue && this._readLineQueue.length > 0) {
1081
- const resolver = this._readLineQueue.shift();
1177
+ if (!this.onCommand) {
1082
1178
  this.updateActiveLine();
1083
- resolver(line);
1084
1179
  return;
1085
1180
  }
1086
1181
 
1182
+ this.isBusy = true;
1087
1183
  this.updateActiveLine();
1088
- if (this.onCommand) {
1089
- this.onCommand(line);
1184
+
1185
+ let cmdResult;
1186
+ try {
1187
+ cmdResult = this.onCommand(line);
1188
+ } catch (err) {
1189
+ this.isBusy = false;
1190
+ this.updateActiveLine();
1191
+ throw err;
1192
+ }
1193
+
1194
+ if (cmdResult && typeof cmdResult.then === 'function') {
1195
+ Promise.resolve(cmdResult)
1196
+ .catch((err) => {
1197
+ _terminalLogger.debug('Terminal onCommand promise rejected.', err);
1198
+ })
1199
+ .finally(() => {
1200
+ this.isBusy = false;
1201
+ this.updateActiveLine();
1202
+ });
1203
+ } else {
1204
+ this.isBusy = false;
1205
+ this.updateActiveLine();
1090
1206
  }
1091
1207
  }
1092
1208
 
@@ -1099,6 +1215,16 @@ var CodeLabLogger = (() => {
1099
1215
  const lineEl = this.container.querySelector('#cl-pty-active-line');
1100
1216
  if (!lineEl) return;
1101
1217
 
1218
+ if (this.isCommandRunning()) {
1219
+ if (typeof lineEl.setAttribute === 'function') lineEl.setAttribute('data-busy', 'true');
1220
+ if (lineEl.style) lineEl.style.display = 'none';
1221
+ lineEl.innerHTML = `<span class="codelab-pty__prompt"></span><span class="codelab-pty__before"></span><span class="codelab-pty__cursor"></span><span class="codelab-pty__after"></span>`;
1222
+ return;
1223
+ }
1224
+
1225
+ if (typeof lineEl.removeAttribute === 'function') lineEl.removeAttribute('data-busy');
1226
+ if (lineEl.style) lineEl.style.display = '';
1227
+
1102
1228
  const prompt = this.searchMode
1103
1229
  ? `(reverse-i-search)\`${this.searchQuery}\`: `
1104
1230
  : this.getPrompt();
@@ -1119,21 +1245,24 @@ var CodeLabLogger = (() => {
1119
1245
  );
1120
1246
 
1121
1247
  const logsHtml = this.logs
1122
- .map(item => `<div class="codelab-log-line ${escapeHtml(item.type || '')}">${ansiToHtml(item.text)}</div>`)
1248
+ .map(item => `<div class="codelab-log-line ${sanitizeLogType(item.type)}">${ansiToHtml(item.text)}</div>`)
1123
1249
  .join('');
1124
1250
 
1125
- const prompt = this.searchMode
1126
- ? `(reverse-i-search)\`${this.searchQuery}\`: `
1127
- : this.getPrompt();
1128
- const before = this.buffer.slice(0, this.cursor);
1129
- const cursorChar = this.cursor < this.buffer.length ? this.buffer[this.cursor] : ' ';
1130
- const after = this.cursor < this.buffer.length ? this.buffer.slice(this.cursor + 1) : '';
1251
+ const busy = this.isCommandRunning();
1252
+ const prompt = busy
1253
+ ? ''
1254
+ : (this.searchMode ? `(reverse-i-search)\`${this.searchQuery}\`: ` : this.getPrompt());
1255
+ const before = busy ? '' : this.buffer.slice(0, this.cursor);
1256
+ const cursorChar = busy ? '' : (this.cursor < this.buffer.length ? this.buffer[this.cursor] : ' ');
1257
+ const after = busy ? '' : (this.cursor < this.buffer.length ? this.buffer.slice(this.cursor + 1) : '');
1258
+ const busyAttr = busy ? ' data-busy="true" style="display:none;"' : '';
1259
+ const cursorClass = busy ? 'codelab-pty__cursor' : 'codelab-pty__cursor blink';
1131
1260
 
1132
1261
  this.container.innerHTML = `
1133
1262
  <div class="codelab-terminal" id="cl-terminal-surface" tabindex="0">
1134
1263
  <div class="codelab-terminal__log" id="cl-terminal-log">${logsHtml}</div>
1135
- <div class="codelab-pty__active-line" id="cl-pty-active-line">
1136
- <span class="codelab-pty__prompt">${escapeHtml(prompt)}</span><span class="codelab-pty__before">${escapeHtml(before)}</span><span class="codelab-pty__cursor blink">${escapeHtml(cursorChar)}</span><span class="codelab-pty__after">${escapeHtml(after)}</span>
1264
+ <div class="codelab-pty__active-line" id="cl-pty-active-line"${busyAttr}>
1265
+ <span class="codelab-pty__prompt">${escapeHtml(prompt)}</span><span class="codelab-pty__before">${escapeHtml(before)}</span><span class="${cursorClass}">${escapeHtml(cursorChar)}</span><span class="codelab-pty__after">${escapeHtml(after)}</span>
1137
1266
  </div>
1138
1267
  <form class="codelab-repl" id="cl-repl-form" style="position:absolute;opacity:0;pointer-events:none;height:0;overflow:hidden;">
1139
1268
  <input type="text" class="codelab-repl__input" id="cl-repl-input" autocomplete="off" spellcheck="false" aria-label="Terminal command input" value="${escapeHtml(this.buffer)}">