brsh 2.8.0 → 3.0.0

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 (45) hide show
  1. package/.github/workflows/release.yml +3 -1
  2. package/CHANGELOG.md +32 -0
  3. package/bin/brsh-shell.js +16 -131
  4. package/bin/exportFileSystem.js +4 -0
  5. package/bin/zsh-server.js +261 -0
  6. package/dist/brsh.css +1 -1
  7. package/dist/shell.es.js +943 -499
  8. package/dist/shell.es.js.map +1 -1
  9. package/dist/shell.min.js +31 -16
  10. package/dist/shell.min.js.map +1 -1
  11. package/dist/terminal.cjs.js +77 -100
  12. package/dist/terminal.cjs.js.map +1 -1
  13. package/dist/terminal.es.js +1382 -603
  14. package/dist/terminal.es.js.map +1 -1
  15. package/dist/terminal.min.js +77 -100
  16. package/dist/terminal.min.js.map +1 -1
  17. package/dist/wbt.es.js +1863 -752
  18. package/dist/wbt.es.js.map +1 -1
  19. package/dist/wbt.min.js +8 -6
  20. package/dist/wbt.min.js.map +1 -1
  21. package/index.js +472 -0
  22. package/lib/BaseFileSystem.js +40 -0
  23. package/lib/BinaryContainer.js +1 -1
  24. package/lib/Command.js +30 -0
  25. package/lib/FileSystem.js +1 -38
  26. package/lib/InteractiveCommand.js +28 -0
  27. package/lib/NodeFileSystem.js +4 -2
  28. package/lib/ReplCommand.js +15 -0
  29. package/lib/Terminal.js +769 -510
  30. package/lib/TtyStream.js +181 -0
  31. package/lib/WasmCommand.js +168 -18
  32. package/lib/builtins/cat.js +2 -2
  33. package/lib/builtins/grep.js +2 -2
  34. package/lib/local/clear.js +3 -0
  35. package/lib/terminal/DomRenderer.js +325 -0
  36. package/lib/terminal/PtyBridge.js +169 -0
  37. package/lib/terminal/ScreenBuffer.js +594 -0
  38. package/lib/terminal/VtParser.js +728 -0
  39. package/lib/wbt/index.js +270 -14
  40. package/lib/wbt/packages/wllama.js +12 -2
  41. package/lib/wbt/registry.js +11 -2
  42. package/package.json +4 -2
  43. package/styles/terminal.css +33 -4
  44. package/test/terminal.test.js +762 -40
  45. package/test/wbt.test.js +24 -5
@@ -1,650 +1,1429 @@
1
1
  import e from "brsh";
2
2
  //#region \0rolldown/runtime.js
3
- var t = Object.create, n = Object.defineProperty, r = Object.getOwnPropertyDescriptor, i = Object.getOwnPropertyNames, a = Object.getPrototypeOf, o = Object.prototype.hasOwnProperty, s = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), c = (e, t, a, s) => {
4
- if (t && typeof t == "object" || typeof t == "function") for (var c = i(t), l = 0, u = c.length, d; l < u; l++) d = c[l], !o.call(e, d) && d !== a && n(e, d, {
5
- get: ((e) => t[e]).bind(null, d),
6
- enumerable: !(s = r(t, d)) || s.enumerable
7
- });
8
- return e;
9
- }, l = /* @__PURE__ */ ((e, r, i) => (i = e == null ? {} : t(a(e)), c(r || !e || !e.__esModule ? n(i, "default", {
10
- value: e,
11
- enumerable: !0
12
- }) : i, e)))((/* @__PURE__ */ s(((e) => {
13
- (function(t, n) {
14
- if (typeof define == "function" && define.amd) define(["exports"], n);
15
- else if (typeof e == "object" && typeof e.nodeName != "string") n(e);
16
- else {
17
- var r = {};
18
- n(r), t.AnsiUp = r.default;
19
- }
20
- })(e, function(e) {
21
- var t = this && this.__makeTemplateObject || function(e, t) {
22
- return Object.defineProperty ? Object.defineProperty(e, "raw", { value: t }) : e.raw = t, e;
23
- }, n;
24
- (function(e) {
25
- e[e.EOS = 0] = "EOS", e[e.Text = 1] = "Text", e[e.Incomplete = 2] = "Incomplete", e[e.ESC = 3] = "ESC", e[e.Unknown = 4] = "Unknown", e[e.SGR = 5] = "SGR", e[e.OSCURL = 6] = "OSCURL";
26
- })(n ||= {});
27
- var r = function() {
28
- function e() {
29
- this.VERSION = "5.0.1", this.setup_palettes(), this._use_classes = !1, this.bold = !1, this.underline = !1, this.italic = !1, this.fg = this.bg = null, this._buffer = "", this._url_whitelist = {
30
- http: 1,
31
- https: 1
32
- };
3
+ var t = /* @__PURE__ */ ((e) => typeof require < "u" ? require : typeof Proxy < "u" ? new Proxy(e, { get: (e, t) => (typeof require < "u" ? require : e)[t] }) : e)(function(e) {
4
+ if (typeof require < "u") return require.apply(this, arguments);
5
+ throw Error("Calling `require` for \"" + e + "\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.");
6
+ });
7
+ //#endregion
8
+ //#region lib/terminal/ScreenBuffer.js
9
+ function n(e = " ", t = null, n = null, r = 0, i = null) {
10
+ return {
11
+ ch: e,
12
+ fg: t,
13
+ bg: n,
14
+ flags: r,
15
+ link: i
16
+ };
17
+ }
18
+ function r(e) {
19
+ return {
20
+ ch: e.ch,
21
+ fg: e.fg,
22
+ bg: e.bg,
23
+ flags: e.flags,
24
+ link: e.link
25
+ };
26
+ }
27
+ var i = class {
28
+ constructor(e = 80, t = 24, n = {}) {
29
+ this.cols = Math.max(1, e), this.rows = Math.max(1, t), this.maxScrollback = n.maxScrollback ?? 1e3, this.convertEol = n.convertEol !== !1, this.scrollback = [], this.viewportOffset = 0, this.primaryGrid = this._createGrid(this.cols, this.rows), this.altGrid = this._createGrid(this.cols, this.rows), this.isAltScreen = !1, this.cursorRow = 0, this.cursorCol = 0, this.pendingWrap = !1, this.cursorVisible = !0, this.autoWrap = !0, this.originMode = !1, this.scrollTop = 0, this.scrollBottom = this.rows - 1, this.curFg = null, this.curBg = null, this.curFlags = 0, this.curLink = null, this._savedPrimary = null, this._savedCursorPrimary = null, this._savedCursorAlt = null, this.dirtyRows = /* @__PURE__ */ new Set(), this.fullRedrawNeeded = !0, this._markAllDirty();
30
+ }
31
+ get grid() {
32
+ return this.isAltScreen ? this.altGrid : this.primaryGrid;
33
+ }
34
+ _createRow(e = this.cols, t = !1) {
35
+ let r = t ? this.curFg : null, i = t ? this.curBg : null, a = t ? this.curFlags : 0, o = Array(e);
36
+ for (let t = 0; t < e; t++) o[t] = n(" ", r, i, a, null);
37
+ return o;
38
+ }
39
+ _createGrid(e, t) {
40
+ let n = Array(t);
41
+ for (let r = 0; r < t; r++) n[r] = this._createRow(e, !1);
42
+ return n;
43
+ }
44
+ _markRowDirty(e) {
45
+ e >= 0 && e < this.rows && this.dirtyRows.add(e);
46
+ }
47
+ _markRangeDirty(e, t) {
48
+ let n = Math.max(0, e), r = Math.min(this.rows - 1, t);
49
+ for (let e = n; e <= r; e++) this.dirtyRows.add(e);
50
+ }
51
+ _markAllDirty() {
52
+ this.fullRedrawNeeded = !0;
53
+ for (let e = 0; e < this.rows; e++) this.dirtyRows.add(e);
54
+ }
55
+ reset() {
56
+ this.scrollback = [], this.viewportOffset = 0, this.primaryGrid = this._createGrid(this.cols, this.rows), this.altGrid = this._createGrid(this.cols, this.rows), this.isAltScreen = !1, this.cursorRow = 0, this.cursorCol = 0, this.pendingWrap = !1, this.cursorVisible = !0, this.autoWrap = !0, this.originMode = !1, this.scrollTop = 0, this.scrollBottom = this.rows - 1, this.curFg = null, this.curBg = null, this.curFlags = 0, this.curLink = null, this._savedPrimary = null, this._savedCursorPrimary = null, this._savedCursorAlt = null, this._markAllDirty();
57
+ }
58
+ resetAttributes() {
59
+ this.curFg = null, this.curBg = null, this.curFlags = 0;
60
+ }
61
+ saveCursor() {
62
+ let e = {
63
+ row: this.cursorRow,
64
+ col: this.cursorCol,
65
+ pendingWrap: this.pendingWrap,
66
+ fg: this.curFg,
67
+ bg: this.curBg,
68
+ flags: this.curFlags,
69
+ originMode: this.originMode,
70
+ autoWrap: this.autoWrap
71
+ };
72
+ this.isAltScreen ? this._savedCursorAlt = e : this._savedCursorPrimary = e;
73
+ }
74
+ restoreCursor() {
75
+ let e = this.isAltScreen ? this._savedCursorAlt : this._savedCursorPrimary;
76
+ e && (this._markRowDirty(this.cursorRow), this.cursorRow = Math.min(this.rows - 1, Math.max(0, e.row)), this.cursorCol = Math.min(this.cols - 1, Math.max(0, e.col)), this.pendingWrap = !!e.pendingWrap, this.curFg = e.fg, this.curBg = e.bg, this.curFlags = e.flags, this.originMode = e.originMode, this.autoWrap = e.autoWrap, this._markRowDirty(this.cursorRow));
77
+ }
78
+ enterAltScreen(e = !0) {
79
+ this.isAltScreen || (this._savedPrimary = {
80
+ cursorRow: this.cursorRow,
81
+ cursorCol: this.cursorCol,
82
+ pendingWrap: this.pendingWrap,
83
+ fg: this.curFg,
84
+ bg: this.curBg,
85
+ flags: this.curFlags,
86
+ scrollTop: this.scrollTop,
87
+ scrollBottom: this.scrollBottom,
88
+ originMode: this.originMode,
89
+ autoWrap: this.autoWrap
90
+ }, this.isAltScreen = !0, this.viewportOffset = 0, this.scrollTop = 0, this.scrollBottom = this.rows - 1), e && (this.altGrid = this._createGrid(this.cols, this.rows), this.cursorRow = 0, this.cursorCol = 0, this.pendingWrap = !1), this._markAllDirty();
91
+ }
92
+ exitAltScreen() {
93
+ this.isAltScreen && (this.isAltScreen = !1, this._savedPrimary ? (this.cursorRow = Math.min(this.rows - 1, Math.max(0, this._savedPrimary.cursorRow)), this.cursorCol = Math.min(this.cols - 1, Math.max(0, this._savedPrimary.cursorCol)), this.pendingWrap = this._savedPrimary.pendingWrap, this.curFg = this._savedPrimary.fg, this.curBg = this._savedPrimary.bg, this.curFlags = this._savedPrimary.flags, this.scrollTop = Math.min(this.rows - 1, this._savedPrimary.scrollTop), this.scrollBottom = Math.min(this.rows - 1, Math.max(this.scrollTop, this._savedPrimary.scrollBottom)), this.originMode = this._savedPrimary.originMode, this.autoWrap = this._savedPrimary.autoWrap, this._savedPrimary = null) : (this.scrollTop = 0, this.scrollBottom = this.rows - 1), this._markAllDirty());
94
+ }
95
+ setScrollRegion(e, t) {
96
+ let n = Math.max(1, e || 1) - 1, r = t ? Math.min(this.rows, t) - 1 : this.rows - 1;
97
+ n < r && n >= 0 && r < this.rows ? (this.scrollTop = n, this.scrollBottom = r) : (this.scrollTop = 0, this.scrollBottom = this.rows - 1), this.setCursor(0, 0);
98
+ }
99
+ setCursor(e, t) {
100
+ this._markRowDirty(this.cursorRow);
101
+ let n = this.originMode ? this.scrollTop : 0, r = this.originMode ? this.scrollBottom : this.rows - 1, i = (this.originMode ? this.scrollTop : 0) + e;
102
+ this.cursorRow = Math.max(n, Math.min(r, i)), this.cursorCol = Math.max(0, Math.min(this.cols - 1, t)), this.pendingWrap = !1, this._markRowDirty(this.cursorRow);
103
+ }
104
+ moveCursorRelative(e, t) {
105
+ if (this._markRowDirty(this.cursorRow), e !== 0) {
106
+ let t = this.cursorRow >= this.scrollTop && this.cursorRow <= this.scrollBottom && e < 0 ? this.scrollTop : 0, n = this.cursorRow >= this.scrollTop && this.cursorRow <= this.scrollBottom && e > 0 ? this.scrollBottom : this.rows - 1;
107
+ this.cursorRow = Math.max(t, Math.min(n, this.cursorRow + e));
108
+ }
109
+ t !== 0 && (this.cursorCol = Math.max(0, Math.min(this.cols - 1, this.cursorCol + t))), this.pendingWrap = !1, this._markRowDirty(this.cursorRow);
110
+ }
111
+ putChar(e) {
112
+ this.pendingWrap && (this.autoWrap ? (this.cursorCol = 0, this.pendingWrap = !1, this._indexDown()) : this.pendingWrap = !1);
113
+ let t = this.grid[this.cursorRow];
114
+ if (t && this.cursorCol >= 0 && this.cursorCol < this.cols) {
115
+ let n = t[this.cursorCol];
116
+ n.ch = e, n.fg = this.curFg, n.bg = this.curBg, n.flags = this.curFlags, n.link = this.curLink, this._markRowDirty(this.cursorRow);
117
+ }
118
+ this.cursorCol < this.cols - 1 ? this.cursorCol++ : this.pendingWrap = !0;
119
+ }
120
+ carriageReturn() {
121
+ this._markRowDirty(this.cursorRow), this.cursorCol = 0, this.pendingWrap = !1;
122
+ }
123
+ lineFeed() {
124
+ this._markRowDirty(this.cursorRow), this.convertEol && (this.cursorCol = 0), this.pendingWrap = !1, this._indexDown(), this._markRowDirty(this.cursorRow);
125
+ }
126
+ index() {
127
+ this._markRowDirty(this.cursorRow), this.pendingWrap = !1, this._indexDown(), this._markRowDirty(this.cursorRow);
128
+ }
129
+ nextLine() {
130
+ this._markRowDirty(this.cursorRow), this.cursorCol = 0, this.pendingWrap = !1, this._indexDown(), this._markRowDirty(this.cursorRow);
131
+ }
132
+ reverseIndex() {
133
+ this._markRowDirty(this.cursorRow), this.pendingWrap = !1, this.cursorRow === this.scrollTop ? this.scrollDown(1) : this.cursorRow > 0 && this.cursorRow--, this._markRowDirty(this.cursorRow);
134
+ }
135
+ backspace() {
136
+ this._markRowDirty(this.cursorRow), this.pendingWrap ? this.pendingWrap = !1 : this.cursorCol > 0 && this.cursorCol--, this._markRowDirty(this.cursorRow);
137
+ }
138
+ tab() {
139
+ this._markRowDirty(this.cursorRow);
140
+ let e = Math.min(this.cols - 1, (Math.floor(this.cursorCol / 8) + 1) * 8);
141
+ this.cursorCol = e, this.pendingWrap = !1, this._markRowDirty(this.cursorRow);
142
+ }
143
+ _indexDown() {
144
+ this.cursorRow === this.scrollBottom ? this.scrollUp(1) : this.cursorRow < this.rows - 1 && this.cursorRow++;
145
+ }
146
+ scrollUp(e = 1, t = this.scrollTop, n = this.scrollBottom) {
147
+ let i = this.grid, a = Math.min(e, n - t + 1);
148
+ for (let e = 0; e < a; e++) {
149
+ let e = i[t];
150
+ !this.isAltScreen && t === 0 && this.maxScrollback > 0 && (this.scrollback.push(e.map(r)), this.scrollback.length > this.maxScrollback && this.scrollback.shift());
151
+ for (let e = t; e < n; e++) i[e] = i[e + 1];
152
+ i[n] = this._createRow(this.cols, !0);
153
+ }
154
+ this._markRangeDirty(t, n);
155
+ }
156
+ scrollDown(e = 1, t = this.scrollTop, n = this.scrollBottom) {
157
+ let r = this.grid, i = Math.min(e, n - t + 1);
158
+ for (let e = 0; e < i; e++) {
159
+ for (let e = n; e > t; e--) r[e] = r[e - 1];
160
+ r[t] = this._createRow(this.cols, !0);
161
+ }
162
+ this._markRangeDirty(t, n);
163
+ }
164
+ eraseInLine(e = 0) {
165
+ let t = this.grid[this.cursorRow];
166
+ if (!t) return;
167
+ let r = 0, i = this.cols - 1;
168
+ if (e === 0) r = this.cursorCol;
169
+ else if (e === 1) i = this.cursorCol;
170
+ else if (e !== 2) return;
171
+ for (let e = r; e <= i; e++) t[e] = n(" ", this.curFg, this.curBg, this.curFlags, null);
172
+ this.pendingWrap = !1, this._markRowDirty(this.cursorRow);
173
+ }
174
+ eraseInDisplay(e = 0) {
175
+ let t = this.grid;
176
+ if (e === 0) {
177
+ this.eraseInLine(0);
178
+ for (let e = this.cursorRow + 1; e < this.rows; e++) t[e] = this._createRow(this.cols, !0);
179
+ this._markRangeDirty(this.cursorRow, this.rows - 1);
180
+ } else if (e === 1) {
181
+ for (let e = 0; e < this.cursorRow; e++) t[e] = this._createRow(this.cols, !0);
182
+ this.eraseInLine(1), this._markRangeDirty(0, this.cursorRow);
183
+ } else if (e === 2 || e === 3) {
184
+ for (let e = 0; e < this.rows; e++) t[e] = this._createRow(this.cols, !0);
185
+ e === 3 && !this.isAltScreen && (this.scrollback = [], this.viewportOffset = 0), this.pendingWrap = !1, this._markAllDirty();
186
+ }
187
+ }
188
+ eraseChars(e = 1) {
189
+ let t = this.grid[this.cursorRow];
190
+ if (!t) return;
191
+ let r = Math.min(this.cols, this.cursorCol + Math.max(1, e));
192
+ for (let e = this.cursorCol; e < r; e++) t[e] = n(" ", this.curFg, this.curBg, this.curFlags, null);
193
+ this.pendingWrap = !1, this._markRowDirty(this.cursorRow);
194
+ }
195
+ insertChars(e = 1) {
196
+ let t = this.grid[this.cursorRow];
197
+ if (!t) return;
198
+ let r = Math.min(this.cols - this.cursorCol, Math.max(1, e));
199
+ for (let e = this.cols - 1; e >= this.cursorCol + r; e--) t[e] = t[e - r];
200
+ for (let e = this.cursorCol; e < this.cursorCol + r; e++) t[e] = n(" ", this.curFg, this.curBg, this.curFlags, null);
201
+ this.pendingWrap = !1, this._markRowDirty(this.cursorRow);
202
+ }
203
+ deleteChars(e = 1) {
204
+ let t = this.grid[this.cursorRow];
205
+ if (!t) return;
206
+ let r = Math.min(this.cols - this.cursorCol, Math.max(1, e));
207
+ for (let e = this.cursorCol; e < this.cols - r; e++) t[e] = t[e + r];
208
+ for (let e = this.cols - r; e < this.cols; e++) t[e] = n(" ", this.curFg, this.curBg, this.curFlags, null);
209
+ this.pendingWrap = !1, this._markRowDirty(this.cursorRow);
210
+ }
211
+ insertLines(e = 1) {
212
+ this.cursorRow < this.scrollTop || this.cursorRow > this.scrollBottom || (this.scrollDown(Math.max(1, e), this.cursorRow, this.scrollBottom), this.cursorCol = 0, this.pendingWrap = !1);
213
+ }
214
+ deleteLines(e = 1) {
215
+ this.cursorRow < this.scrollTop || this.cursorRow > this.scrollBottom || (this.scrollUp(Math.max(1, e), this.cursorRow, this.scrollBottom), this.cursorCol = 0, this.pendingWrap = !1);
216
+ }
217
+ resize(e, t) {
218
+ let r = Math.max(1, e), i = Math.max(1, t);
219
+ if (r === this.cols && i === this.rows) return;
220
+ let a = (e) => {
221
+ let t = Array(i);
222
+ for (let a = 0; a < i; a++) {
223
+ let i = Array(r), o = a < e.length ? e[a] : null;
224
+ for (let e = 0; e < r; e++) i[e] = o && e < o.length ? o[e] : n(" ", null, null, 0, null);
225
+ t[a] = i;
33
226
  }
34
- return Object.defineProperty(e.prototype, "use_classes", {
35
- get: function() {
36
- return this._use_classes;
37
- },
38
- set: function(e) {
39
- this._use_classes = e;
40
- },
41
- enumerable: !1,
42
- configurable: !0
43
- }), Object.defineProperty(e.prototype, "url_whitelist", {
44
- get: function() {
45
- return this._url_whitelist;
46
- },
47
- set: function(e) {
48
- this._url_whitelist = e;
49
- },
50
- enumerable: !1,
51
- configurable: !0
52
- }), e.prototype.setup_palettes = function() {
53
- var e = this;
54
- this.ansi_colors = [[
55
- {
56
- rgb: [
57
- 0,
58
- 0,
59
- 0
60
- ],
61
- class_name: "ansi-black"
62
- },
63
- {
64
- rgb: [
65
- 187,
66
- 0,
67
- 0
68
- ],
69
- class_name: "ansi-red"
70
- },
71
- {
72
- rgb: [
73
- 0,
74
- 187,
75
- 0
76
- ],
77
- class_name: "ansi-green"
78
- },
79
- {
80
- rgb: [
81
- 187,
82
- 187,
83
- 0
84
- ],
85
- class_name: "ansi-yellow"
86
- },
87
- {
88
- rgb: [
89
- 0,
90
- 0,
91
- 187
92
- ],
93
- class_name: "ansi-blue"
94
- },
95
- {
96
- rgb: [
97
- 187,
98
- 0,
99
- 187
100
- ],
101
- class_name: "ansi-magenta"
102
- },
103
- {
104
- rgb: [
105
- 0,
106
- 187,
107
- 187
108
- ],
109
- class_name: "ansi-cyan"
110
- },
111
- {
112
- rgb: [
113
- 255,
114
- 255,
115
- 255
116
- ],
117
- class_name: "ansi-white"
118
- }
119
- ], [
120
- {
121
- rgb: [
122
- 85,
123
- 85,
124
- 85
125
- ],
126
- class_name: "ansi-bright-black"
127
- },
128
- {
129
- rgb: [
130
- 255,
131
- 85,
132
- 85
133
- ],
134
- class_name: "ansi-bright-red"
135
- },
136
- {
137
- rgb: [
138
- 0,
139
- 255,
140
- 0
141
- ],
142
- class_name: "ansi-bright-green"
143
- },
144
- {
145
- rgb: [
146
- 255,
147
- 255,
148
- 85
149
- ],
150
- class_name: "ansi-bright-yellow"
151
- },
152
- {
153
- rgb: [
154
- 85,
155
- 85,
156
- 255
157
- ],
158
- class_name: "ansi-bright-blue"
159
- },
160
- {
161
- rgb: [
162
- 255,
163
- 85,
164
- 255
165
- ],
166
- class_name: "ansi-bright-magenta"
167
- },
168
- {
169
- rgb: [
170
- 85,
171
- 255,
172
- 255
173
- ],
174
- class_name: "ansi-bright-cyan"
175
- },
176
- {
177
- rgb: [
178
- 255,
179
- 255,
180
- 255
181
- ],
182
- class_name: "ansi-bright-white"
227
+ return t;
228
+ };
229
+ this.primaryGrid = a(this.primaryGrid), this.altGrid = a(this.altGrid), this.cols = r, this.rows = i, this.scrollTop = 0, this.scrollBottom = i - 1, this.cursorRow = Math.min(i - 1, this.cursorRow), this.cursorCol = Math.min(r - 1, this.cursorCol), this.pendingWrap = !1, this._markAllDirty();
230
+ }
231
+ scrollViewport(e) {
232
+ if (this.isAltScreen) return;
233
+ let t = this.scrollback.length, n = Math.max(0, Math.min(t, this.viewportOffset + e));
234
+ n !== this.viewportOffset && (this.viewportOffset = n, this._markAllDirty());
235
+ }
236
+ getVisibleRow(e) {
237
+ if (this.isAltScreen || this.viewportOffset === 0) return this.grid[e];
238
+ let t = this.scrollback.length, n = t - this.viewportOffset + e;
239
+ return n < t ? this.scrollback[n] : this.grid[n - t];
240
+ }
241
+ getLineText(e, t = !0) {
242
+ let n = this.grid[e];
243
+ if (!n) return "";
244
+ let r = "";
245
+ for (let e = 0; e < n.length; e++) r += n[e].ch;
246
+ return t ? r.replace(/\s+$/, "") : r;
247
+ }
248
+ getScreenText(e = {}) {
249
+ let t = e.trimRight !== !1, n = [];
250
+ for (let e = 0; e < this.rows; e++) n.push(this.getLineText(e, t));
251
+ return n.join("\n");
252
+ }
253
+ getScrollbackAndScreenText(e = {}) {
254
+ let t = e.trimRight !== !1, n = [];
255
+ if (!this.isAltScreen) for (let e of this.scrollback) {
256
+ let r = "";
257
+ for (let t = 0; t < e.length; t++) r += e[t].ch;
258
+ n.push(t ? r.replace(/\s+$/, "") : r);
259
+ }
260
+ for (let e = 0; e < this.rows; e++) n.push(this.getLineText(e, t));
261
+ return n.join("\n");
262
+ }
263
+ }, a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = {
264
+ "`": "◆",
265
+ a: "▒",
266
+ f: "°",
267
+ g: "±",
268
+ j: "┘",
269
+ k: "┐",
270
+ l: "┌",
271
+ m: "└",
272
+ n: "┼",
273
+ o: "⎺",
274
+ p: "⎻",
275
+ q: "─",
276
+ r: "⎼",
277
+ s: "⎽",
278
+ t: "├",
279
+ u: "┤",
280
+ v: "┴",
281
+ w: "┬",
282
+ x: "│",
283
+ y: "≤",
284
+ z: "≥",
285
+ "{": "π",
286
+ "|": "≠",
287
+ "}": "£",
288
+ "~": "·"
289
+ }, f = {
290
+ http: !0,
291
+ https: !0,
292
+ tel: !0,
293
+ mailto: !0
294
+ };
295
+ function p(e, t = f) {
296
+ if (!e || typeof e != "string") return null;
297
+ let n = e.trim(), r = n.match(/^([a-zA-Z][a-zA-Z0-9+.-]*):/);
298
+ return !r || !t[r[1].toLowerCase()] ? null : n;
299
+ }
300
+ var m = class {
301
+ constructor(e, t = {}) {
302
+ this.screen = e, this.onResponse = t.onResponse || null, this.onTitle = t.onTitle || null, this.onBell = t.onBell || null, this.urlWhitelist = t.urlWhitelist || f, this.state = a, this._csiPrefix = "", this._csiParams = "", this._csiIntermediate = "", this._escIntermediate = "", this._oscBuffer = "", this._oscEscSeen = !1, this._dcsEscSeen = !1, this._lastPrintedChar = " ", this.charsetG0 = "B", this.charsetG1 = "B", this.activeCharset = 0, this.applicationCursorKeys = !1, this.applicationKeypad = !1, this.bracketedPasteMode = !1, this.mouseTrackingMode = 0, this.mouseProtocol = "x10", this.alternateScrollMode = !1;
303
+ }
304
+ reset() {
305
+ this.state = a, this._csiPrefix = "", this._csiParams = "", this._csiIntermediate = "", this._escIntermediate = "", this._oscBuffer = "", this._oscEscSeen = !1, this._dcsEscSeen = !1, this._lastPrintedChar = " ", this.charsetG0 = "B", this.charsetG1 = "B", this.activeCharset = 0, this.applicationCursorKeys = !1, this.applicationKeypad = !1, this.bracketedPasteMode = !1, this.mouseTrackingMode = 0, this.mouseProtocol = "x10", this.alternateScrollMode = !1, this.screen.reset();
306
+ }
307
+ write(e) {
308
+ if (e == null) return;
309
+ let t = typeof e == "string" ? e : e instanceof Uint8Array || Buffer.isBuffer(e) ? new TextDecoder().decode(e) : String(e);
310
+ for (let e = 0; e < t.length; e++) {
311
+ let n = t[e], r = t.charCodeAt(e);
312
+ switch (this.state) {
313
+ case a:
314
+ this._handleGround(n, r);
315
+ break;
316
+ case o:
317
+ this._handleEsc(n, r);
318
+ break;
319
+ case s:
320
+ this._handleEscIntermediate(n, r);
321
+ break;
322
+ case c:
323
+ this._handleCsi(n, r);
324
+ break;
325
+ case l:
326
+ this._handleOsc(n, r);
327
+ break;
328
+ case u:
329
+ this._handleDcs(n, r);
330
+ break;
331
+ }
332
+ }
333
+ }
334
+ _handleGround(e, t) {
335
+ if (t === 27) {
336
+ this.state = o, this._escIntermediate = "";
337
+ return;
338
+ }
339
+ if (t === 13) {
340
+ this.screen.carriageReturn();
341
+ return;
342
+ }
343
+ if (t === 10 || t === 11 || t === 12) {
344
+ this.screen.lineFeed();
345
+ return;
346
+ }
347
+ if (t === 8) {
348
+ this.screen.backspace();
349
+ return;
350
+ }
351
+ if (t === 9) {
352
+ this.screen.tab();
353
+ return;
354
+ }
355
+ if (t === 7) {
356
+ this.onBell && this.onBell();
357
+ return;
358
+ }
359
+ if (t === 14) {
360
+ this.activeCharset = 1;
361
+ return;
362
+ }
363
+ if (t === 15) {
364
+ this.activeCharset = 0;
365
+ return;
366
+ }
367
+ if (t < 32 || t === 127) return;
368
+ let n = (this.activeCharset === 0 ? this.charsetG0 : this.charsetG1) === "0" && d[e] ? d[e] : e;
369
+ this._lastPrintedChar = n, this.screen.putChar(n);
370
+ }
371
+ _handleEsc(e, t) {
372
+ if (e === "[") {
373
+ this.state = c, this._csiPrefix = "", this._csiParams = "", this._csiIntermediate = "";
374
+ return;
375
+ }
376
+ if (e === "]") {
377
+ this.state = l, this._oscBuffer = "", this._oscEscSeen = !1;
378
+ return;
379
+ }
380
+ if (e === "P" || e === "X" || e === "^" || e === "_") {
381
+ this.state = u, this._dcsEscSeen = !1;
382
+ return;
383
+ }
384
+ if (e === "(" || e === ")" || e === "*" || e === "+" || e === "#") {
385
+ this.state = s, this._escIntermediate = e;
386
+ return;
387
+ }
388
+ if (e === "7") {
389
+ this.screen.saveCursor(), this.state = a;
390
+ return;
391
+ }
392
+ if (e === "8") {
393
+ this.screen.restoreCursor(), this.state = a;
394
+ return;
395
+ }
396
+ if (e === "D") {
397
+ this.screen.index(), this.state = a;
398
+ return;
399
+ }
400
+ if (e === "M") {
401
+ this.screen.reverseIndex(), this.state = a;
402
+ return;
403
+ }
404
+ if (e === "E") {
405
+ this.screen.nextLine(), this.state = a;
406
+ return;
407
+ }
408
+ if (e === "c") {
409
+ this.reset();
410
+ return;
411
+ }
412
+ if (e === "=") {
413
+ this.applicationKeypad = !0, this.state = a;
414
+ return;
415
+ }
416
+ if (e === ">") {
417
+ this.applicationKeypad = !1, this.state = a;
418
+ return;
419
+ }
420
+ t !== 27 && (this.state = a);
421
+ }
422
+ _handleEscIntermediate(e) {
423
+ if (this._escIntermediate === "(") this.charsetG0 = e === "0" ? "0" : "B";
424
+ else if (this._escIntermediate === ")") this.charsetG1 = e === "0" ? "0" : "B";
425
+ else if (this._escIntermediate === "#" && e === "8") {
426
+ for (let e = 0; e < this.screen.rows; e++) for (let t = 0; t < this.screen.cols; t++) this.screen.grid[e][t].ch = "E";
427
+ this.screen._markAllDirty();
428
+ }
429
+ this.state = a;
430
+ }
431
+ _handleCsi(e, t) {
432
+ if (t === 13) {
433
+ this.screen.carriageReturn();
434
+ return;
435
+ }
436
+ if (t === 10 || t === 11 || t === 12) {
437
+ this.screen.lineFeed();
438
+ return;
439
+ }
440
+ if (t === 8) {
441
+ this.screen.backspace();
442
+ return;
443
+ }
444
+ if (t === 27) {
445
+ this.state = o;
446
+ return;
447
+ }
448
+ if (this._csiParams.length === 0 && this._csiPrefix.length === 0 && (e === "?" || e === ">" || e === "<" || e === "=")) {
449
+ this._csiPrefix = e;
450
+ return;
451
+ }
452
+ if (t >= 48 && t <= 57 || e === ";" || e === ":") {
453
+ this._csiParams += e;
454
+ return;
455
+ }
456
+ if (t >= 32 && t <= 47) {
457
+ this._csiIntermediate += e;
458
+ return;
459
+ }
460
+ t >= 64 && t <= 126 && (this._dispatchCsi(this._csiPrefix, this._csiParams, this._csiIntermediate, e), this.state = a);
461
+ }
462
+ _handleOsc(e, t) {
463
+ if (t === 7) {
464
+ this._dispatchOsc(this._oscBuffer), this.state = a;
465
+ return;
466
+ }
467
+ if (this._oscEscSeen) {
468
+ if (this._oscEscSeen = !1, e === "\\") {
469
+ this._dispatchOsc(this._oscBuffer), this.state = a;
470
+ return;
471
+ }
472
+ this._oscBuffer += "\x1B" + e;
473
+ return;
474
+ }
475
+ if (t === 27) {
476
+ this._oscEscSeen = !0;
477
+ return;
478
+ }
479
+ this._oscBuffer += e;
480
+ }
481
+ _handleDcs(e, t) {
482
+ if (t === 7) {
483
+ this.state = a;
484
+ return;
485
+ }
486
+ if (this._dcsEscSeen && (this._dcsEscSeen = !1, e === "\\")) {
487
+ this.state = a;
488
+ return;
489
+ }
490
+ t === 27 && (this._dcsEscSeen = !0);
491
+ }
492
+ _dispatchOsc(e) {
493
+ let t = e.indexOf(";");
494
+ if (t === -1) return;
495
+ let n = e.slice(0, t), r = e.slice(t + 1);
496
+ if (n === "0" || n === "2") this.onTitle && this.onTitle(r);
497
+ else if (n === "8") {
498
+ let e = r.indexOf(";");
499
+ if (e !== -1) {
500
+ let t = r.slice(e + 1);
501
+ this.screen.curLink = t ? p(t, this.urlWhitelist) : null;
502
+ }
503
+ }
504
+ }
505
+ _parseParams(e) {
506
+ return e ? e.split(";").map((e) => e.includes(":") ? e.split(":").map((e) => e === "" ? 0 : parseInt(e, 10) || 0) : e === "" ? 0 : parseInt(e, 10) || 0) : [];
507
+ }
508
+ _dispatchCsi(e, t, n, r) {
509
+ let i = this._parseParams(t), a = typeof i[0] == "number" ? i[0] : 0, o = typeof i[1] == "number" ? i[1] : 0;
510
+ if (e === "?") {
511
+ this._dispatchDecPrivate(i, r);
512
+ return;
513
+ }
514
+ if (e === ">") {
515
+ r === "c" && this.onResponse && this.onResponse("\x1B[>0;276;0c");
516
+ return;
517
+ }
518
+ switch (r) {
519
+ case "A":
520
+ this.screen.moveCursorRelative(-Math.max(1, a), 0);
521
+ break;
522
+ case "B":
523
+ case "e":
524
+ this.screen.moveCursorRelative(Math.max(1, a), 0);
525
+ break;
526
+ case "C":
527
+ case "a":
528
+ this.screen.moveCursorRelative(0, Math.max(1, a));
529
+ break;
530
+ case "D":
531
+ this.screen.moveCursorRelative(0, -Math.max(1, a));
532
+ break;
533
+ case "E":
534
+ this.screen.moveCursorRelative(Math.max(1, a), 0), this.screen.carriageReturn();
535
+ break;
536
+ case "F":
537
+ this.screen.moveCursorRelative(-Math.max(1, a), 0), this.screen.carriageReturn();
538
+ break;
539
+ case "G":
540
+ case "`":
541
+ this.screen.setCursor(this.screen.cursorRow, Math.max(1, a) - 1);
542
+ break;
543
+ case "H":
544
+ case "f":
545
+ this.screen.setCursor(Math.max(1, a) - 1, Math.max(1, o) - 1);
546
+ break;
547
+ case "d":
548
+ this.screen.setCursor(Math.max(1, a) - 1, this.screen.cursorCol);
549
+ break;
550
+ case "J":
551
+ this.screen.eraseInDisplay(a);
552
+ break;
553
+ case "K":
554
+ this.screen.eraseInLine(a);
555
+ break;
556
+ case "X":
557
+ this.screen.eraseChars(Math.max(1, a));
558
+ break;
559
+ case "@":
560
+ this.screen.insertChars(Math.max(1, a));
561
+ break;
562
+ case "P":
563
+ this.screen.deleteChars(Math.max(1, a));
564
+ break;
565
+ case "L":
566
+ this.screen.insertLines(Math.max(1, a));
567
+ break;
568
+ case "M":
569
+ this.screen.deleteLines(Math.max(1, a));
570
+ break;
571
+ case "S":
572
+ this.screen.scrollUp(Math.max(1, a));
573
+ break;
574
+ case "T":
575
+ this.screen.scrollDown(Math.max(1, a));
576
+ break;
577
+ case "r":
578
+ this.screen.setScrollRegion(a || 1, o || this.screen.rows);
579
+ break;
580
+ case "s":
581
+ this.screen.saveCursor();
582
+ break;
583
+ case "u":
584
+ this.screen.restoreCursor();
585
+ break;
586
+ case "b": {
587
+ let e = Math.min(this.screen.cols * this.screen.rows, Math.max(1, a));
588
+ for (let t = 0; t < e; t++) this.screen.putChar(this._lastPrintedChar);
589
+ break;
590
+ }
591
+ case "m":
592
+ this._handleSgr(i);
593
+ break;
594
+ case "h":
595
+ case "l":
596
+ a === 20 && (this.screen.convertEol = r === "h");
597
+ break;
598
+ case "n":
599
+ this.onResponse && (a === 5 ? this.onResponse("\x1B[0n") : a === 6 && this.onResponse(`\x1b[${this.screen.cursorRow + 1};${this.screen.cursorCol + 1}R`));
600
+ break;
601
+ case "c":
602
+ this.onResponse && this.onResponse("\x1B[?1;2c");
603
+ break;
604
+ case "t":
605
+ a === 18 && this.onResponse && this.onResponse(`\x1b[8;${this.screen.rows};${this.screen.cols}t`);
606
+ break;
607
+ }
608
+ }
609
+ _dispatchDecPrivate(e, t) {
610
+ if (t === "c" && this.onResponse) {
611
+ this.onResponse("\x1B[?1;2c");
612
+ return;
613
+ }
614
+ if (t !== "h" && t !== "l") return;
615
+ let n = t === "h";
616
+ for (let t of e) {
617
+ let e = typeof t == "number" ? t : 0;
618
+ switch (e) {
619
+ case 1:
620
+ this.applicationCursorKeys = n;
621
+ break;
622
+ case 6:
623
+ this.screen.originMode = n, this.screen.setCursor(0, 0);
624
+ break;
625
+ case 7:
626
+ this.screen.autoWrap = n;
627
+ break;
628
+ case 25:
629
+ this.screen.cursorVisible = n, this.screen._markRowDirty(this.screen.cursorRow);
630
+ break;
631
+ case 47:
632
+ case 1047:
633
+ n ? this.screen.enterAltScreen(e === 1047) : this.screen.exitAltScreen();
634
+ break;
635
+ case 1048:
636
+ n ? this.screen.saveCursor() : this.screen.restoreCursor();
637
+ break;
638
+ case 1049:
639
+ n ? (this.screen.saveCursor(), this.screen.enterAltScreen(!0)) : (this.screen.exitAltScreen(), this.screen.restoreCursor());
640
+ break;
641
+ case 1e3:
642
+ case 1002:
643
+ case 1003:
644
+ n ? this.mouseTrackingMode = e : this.mouseTrackingMode === e && (this.mouseTrackingMode = 0);
645
+ break;
646
+ case 1005:
647
+ this.mouseProtocol = n ? "utf8" : "x10";
648
+ break;
649
+ case 1006:
650
+ this.mouseProtocol = n ? "sgr" : "x10";
651
+ break;
652
+ case 1007:
653
+ this.alternateScrollMode = n;
654
+ break;
655
+ case 2004:
656
+ this.bracketedPasteMode = n;
657
+ break;
658
+ }
659
+ }
660
+ }
661
+ _handleSgr(e) {
662
+ if (e.length === 0) {
663
+ this.screen.resetAttributes();
664
+ return;
665
+ }
666
+ let t = 0;
667
+ for (; t < e.length;) {
668
+ let n = e[t];
669
+ if (Array.isArray(n)) {
670
+ let e = n[0];
671
+ if (e === 38 || e === 48) {
672
+ let t = e === 38;
673
+ if (n[1] === 5 && n.length >= 3) t ? this.screen.curFg = n[2] : this.screen.curBg = n[2];
674
+ else if (n[1] === 2 && n.length >= 5) {
675
+ let e = n.length >= 6 ? 3 : 2, r = `rgb(${n[e] || 0}, ${n[e + 1] || 0}, ${n[e + 2] || 0})`;
676
+ t ? this.screen.curFg = r : this.screen.curBg = r;
183
677
  }
184
- ]], this.palette_256 = [], this.ansi_colors.forEach(function(t) {
185
- t.forEach(function(t) {
186
- e.palette_256.push(t);
187
- });
188
- });
189
- for (var t = [
190
- 0,
191
- 95,
192
- 135,
193
- 175,
194
- 215,
195
- 255
196
- ], n = 0; n < 6; ++n) for (var r = 0; r < 6; ++r) for (var i = 0; i < 6; ++i) {
197
- var a = {
198
- rgb: [
199
- t[n],
200
- t[r],
201
- t[i]
202
- ],
203
- class_name: "truecolor"
204
- };
205
- this.palette_256.push(a);
206
678
  }
207
- for (var o = 8, s = 0; s < 24; ++s, o += 10) {
208
- var c = {
209
- rgb: [
210
- o,
211
- o,
212
- o
213
- ],
214
- class_name: "truecolor"
215
- };
216
- this.palette_256.push(c);
217
- }
218
- }, e.prototype.escape_txt_for_html = function(e) {
219
- return e.replace(/[&<>"']/gm, function(e) {
220
- if (e === "&") return "&amp;";
221
- if (e === "<") return "&lt;";
222
- if (e === ">") return "&gt;";
223
- if (e === "\"") return "&quot;";
224
- if (e === "'") return "&#x27;";
225
- });
226
- }, e.prototype.append_buffer = function(e) {
227
- var t = this._buffer + e;
228
- this._buffer = t;
229
- }, e.prototype.get_next_packet = function(e) {
230
- e === void 0 && (e = this);
231
- var r = {
232
- kind: n.EOS,
233
- text: "",
234
- url: ""
235
- }, o = e._buffer.length;
236
- if (o == 0) return r;
237
- var s = e._buffer.indexOf("\x1B");
238
- if (s == -1) return r.kind = n.Text, r.text = e._buffer, e._buffer = "", r;
239
- if (s > 0) return r.kind = n.Text, r.text = e._buffer.slice(0, s), e._buffer = e._buffer.slice(s), r;
240
- if (s == 0) {
241
- if (o == 1) return r.kind = n.Incomplete, r;
242
- var c = e._buffer.charAt(1);
243
- if (c != "[" && c != "]") return r.kind = n.ESC, r.text = e._buffer.slice(0, 1), e._buffer = e._buffer.slice(1), r;
244
- if (c == "[") {
245
- this._csi_regex ||= i(t(["\n ^ # beginning of line\n #\n # First attempt\n (?: # legal sequence\n \x1B[ # CSI\n ([<-?]?) # private-mode char\n ([d;]*) # any digits or semicolons\n ([ -/]? # an intermediate modifier\n [@-~]) # the command\n )\n | # alternate (second attempt)\n (?: # illegal sequence\n \x1B[ # CSI\n [ -~]* # anything legal\n ([\0-:]) # anything illegal\n )\n "], ["\n ^ # beginning of line\n #\n # First attempt\n (?: # legal sequence\n \\x1b\\[ # CSI\n ([\\x3c-\\x3f]?) # private-mode char\n ([\\d;]*) # any digits or semicolons\n ([\\x20-\\x2f]? # an intermediate modifier\n [\\x40-\\x7e]) # the command\n )\n | # alternate (second attempt)\n (?: # illegal sequence\n \\x1b\\[ # CSI\n [\\x20-\\x7e]* # anything legal\n ([\\x00-\\x1f:]) # anything illegal\n )\n "]));
246
- var l = e._buffer.match(this._csi_regex);
247
- if (l === null) return r.kind = n.Incomplete, r;
248
- if (l[4]) return r.kind = n.ESC, r.text = e._buffer.slice(0, 1), e._buffer = e._buffer.slice(1), r;
249
- l[1] != "" || l[3] != "m" ? r.kind = n.Unknown : r.kind = n.SGR, r.text = l[2];
250
- var u = l[0].length;
251
- return e._buffer = e._buffer.slice(u), r;
252
- }
253
- if (c == "]") {
254
- if (o < 4) return r.kind = n.Incomplete, r;
255
- if (e._buffer.charAt(2) != "8" || e._buffer.charAt(3) != ";") return r.kind = n.ESC, r.text = e._buffer.slice(0, 1), e._buffer = e._buffer.slice(1), r;
256
- this._osc_st ||= a(t(["\n (?: # legal sequence\n (\x1B\\) # ESC | # alternate\n (\x07) # BEL (what xterm did)\n )\n | # alternate (second attempt)\n ( # illegal sequence\n [\0-] # anything illegal\n | # alternate\n [\b-] # anything illegal\n | # alternate\n [-] # anything illegal\n )\n "], ["\n (?: # legal sequence\n (\\x1b\\\\) # ESC \\\n | # alternate\n (\\x07) # BEL (what xterm did)\n )\n | # alternate (second attempt)\n ( # illegal sequence\n [\\x00-\\x06] # anything illegal\n | # alternate\n [\\x08-\\x1a] # anything illegal\n | # alternate\n [\\x1c-\\x1f] # anything illegal\n )\n "])), this._osc_st.lastIndex = 0;
257
- var d = this._osc_st.exec(e._buffer);
258
- if (d === null) return r.kind = n.Incomplete, r;
259
- if (d[3]) return r.kind = n.ESC, r.text = e._buffer.slice(0, 1), e._buffer = e._buffer.slice(1), r;
260
- var f = this._osc_st.exec(e._buffer);
261
- if (f === null) return r.kind = n.Incomplete, r;
262
- if (f[3]) return r.kind = n.ESC, r.text = e._buffer.slice(0, 1), e._buffer = e._buffer.slice(1), r;
263
- this._osc_regex ||= i(t(["\n ^ # beginning of line\n #\n \x1B]8; # OSC Hyperlink\n [ -:<-~]* # params (excluding ;)\n ; # end of params\n ([!-~]{0,512}) # URL capture\n (?: # ST\n (?:\x1B\\) # ESC | # alternate\n (?:\x07) # BEL (what xterm did)\n )\n ([^\x07]+?) # TEXT capture\n \x1B]8;; # OSC Hyperlink End\n (?: # ST\n (?:\x1B\\) # ESC | # alternate\n (?:\x07) # BEL (what xterm did)\n )\n "], ["\n ^ # beginning of line\n #\n \\x1b\\]8; # OSC Hyperlink\n [\\x20-\\x3a\\x3c-\\x7e]* # params (excluding ;)\n ; # end of params\n ([\\x21-\\x7e]{0,512}) # URL capture\n (?: # ST\n (?:\\x1b\\\\) # ESC \\\n | # alternate\n (?:\\x07) # BEL (what xterm did)\n )\n ([^\\x07]+?) # TEXT capture\n \\x1b\\]8;; # OSC Hyperlink End\n (?: # ST\n (?:\\x1b\\\\) # ESC \\\n | # alternate\n (?:\\x07) # BEL (what xterm did)\n )\n "]));
264
- var l = e._buffer.match(this._osc_regex);
265
- if (l === null) return r.kind = n.ESC, r.text = e._buffer.slice(0, 1), e._buffer = e._buffer.slice(1), r;
266
- r.kind = n.OSCURL, r.url = l[1], r.text = l[2];
267
- var u = l[0].length;
268
- return e._buffer = e._buffer.slice(u), r;
679
+ t++;
680
+ continue;
681
+ }
682
+ switch (n) {
683
+ case 0:
684
+ this.screen.resetAttributes();
685
+ break;
686
+ case 1:
687
+ this.screen.curFlags |= 1;
688
+ break;
689
+ case 2:
690
+ this.screen.curFlags |= 2;
691
+ break;
692
+ case 3:
693
+ this.screen.curFlags |= 4;
694
+ break;
695
+ case 4:
696
+ this.screen.curFlags |= 8;
697
+ break;
698
+ case 5:
699
+ case 6:
700
+ this.screen.curFlags |= 16;
701
+ break;
702
+ case 7:
703
+ this.screen.curFlags |= 32;
704
+ break;
705
+ case 8:
706
+ this.screen.curFlags |= 64;
707
+ break;
708
+ case 9:
709
+ this.screen.curFlags |= 128;
710
+ break;
711
+ case 21:
712
+ case 22:
713
+ this.screen.curFlags &= -4;
714
+ break;
715
+ case 23:
716
+ this.screen.curFlags &= -5;
717
+ break;
718
+ case 24:
719
+ this.screen.curFlags &= -9;
720
+ break;
721
+ case 25:
722
+ this.screen.curFlags &= -17;
723
+ break;
724
+ case 27:
725
+ this.screen.curFlags &= -33;
726
+ break;
727
+ case 28:
728
+ this.screen.curFlags &= -65;
729
+ break;
730
+ case 29:
731
+ this.screen.curFlags &= -129;
732
+ break;
733
+ case 38:
734
+ case 48: {
735
+ let r = n === 38, i = e[t + 1];
736
+ if (i === 5 && t + 2 < e.length) {
737
+ let n = Number(e[t + 2]) || 0;
738
+ r ? this.screen.curFg = n : this.screen.curBg = n, t += 2;
739
+ } else if (i === 2 && t + 4 < e.length) {
740
+ let n = `rgb(${Number(e[t + 2]) || 0}, ${Number(e[t + 3]) || 0}, ${Number(e[t + 4]) || 0})`;
741
+ r ? this.screen.curFg = n : this.screen.curBg = n, t += 4;
269
742
  }
743
+ break;
270
744
  }
271
- }, e.prototype.ansi_to_html = function(e) {
272
- return this.append_buffer(e), this.loop_over_packets();
273
- }, e.prototype.loop_over_packets = function(e) {
274
- e === void 0 && (e = this);
275
- for (var t = [];;) {
276
- var r = this.get_next_packet(e);
277
- if (r.kind == n.EOS || r.kind == n.Incomplete) break;
278
- r.kind == n.ESC || r.kind == n.Unknown || (r.kind == n.Text ? t.push(this.transform_to_html(this.with_state(r))) : r.kind == n.SGR ? this.process_ansi(r) : r.kind == n.OSCURL && t.push(this.process_hyperlink(r)));
279
- }
280
- return t.join("");
281
- }, e.prototype.with_state = function(e) {
282
- return {
283
- bold: this.bold,
284
- underline: this.underline,
285
- italic: this.italic,
286
- fg: this.fg,
287
- bg: this.bg,
288
- text: e.text
289
- };
290
- }, e.prototype.process_ansi = function(e) {
291
- for (var t = e.text.split(";"); t.length > 0;) {
292
- var n = t.shift(), r = parseInt(n, 10);
293
- if (isNaN(r) || r === 0) this.fg = this.bg = null, this.bold = !1, this.italic = !1, this.underline = !1;
294
- else if (r === 1) this.bold = !0;
295
- else if (r === 22) this.bold = !1;
296
- else if (r === 4) this.underline = !0;
297
- else if (r === 24) this.underline = !1;
298
- else if (r === 3) this.italic = !0;
299
- else if (r === 3) this.italic = !1;
300
- else if (r === 39) this.fg = null;
301
- else if (r === 49) this.bg = null;
302
- else if (r >= 30 && r < 38) this.fg = this.ansi_colors[0][r - 30];
303
- else if (r >= 40 && r < 48) this.bg = this.ansi_colors[0][r - 40];
304
- else if (r >= 90 && r < 98) this.fg = this.ansi_colors[1][r - 90];
305
- else if (r >= 100 && r < 108) this.bg = this.ansi_colors[1][r - 100];
306
- else if ((r === 38 || r === 48) && t.length > 0) {
307
- var i = r === 38, a = t.shift();
308
- if (a === "5" && t.length > 0) {
309
- var o = parseInt(t.shift(), 10);
310
- o >= 0 && o <= 255 && (i ? this.fg = this.palette_256[o] : this.bg = this.palette_256[o]);
311
- }
312
- if (a === "2" && t.length > 2) {
313
- var s = parseInt(t.shift(), 10), c = parseInt(t.shift(), 10), l = parseInt(t.shift(), 10);
314
- if (s >= 0 && s <= 255 && c >= 0 && c <= 255 && l >= 0 && l <= 255) {
315
- var u = {
316
- rgb: [
317
- s,
318
- c,
319
- l
320
- ],
321
- class_name: "truecolor"
322
- };
323
- i ? this.fg = u : this.bg = u;
324
- }
325
- }
326
- }
745
+ case 39:
746
+ this.screen.curFg = null;
747
+ break;
748
+ case 49:
749
+ this.screen.curBg = null;
750
+ break;
751
+ default:
752
+ n >= 30 && n <= 37 ? this.screen.curFg = n - 30 : n >= 40 && n <= 47 ? this.screen.curBg = n - 40 : n >= 90 && n <= 97 ? this.screen.curFg = n - 90 + 8 : n >= 100 && n <= 107 && (this.screen.curBg = n - 100 + 8);
753
+ break;
754
+ }
755
+ t++;
756
+ }
757
+ }
758
+ }, h = [
759
+ "ansi-black",
760
+ "ansi-red",
761
+ "ansi-green",
762
+ "ansi-yellow",
763
+ "ansi-blue",
764
+ "ansi-magenta",
765
+ "ansi-cyan",
766
+ "ansi-white",
767
+ "ansi-bright-black",
768
+ "ansi-bright-red",
769
+ "ansi-bright-green",
770
+ "ansi-bright-yellow",
771
+ "ansi-bright-blue",
772
+ "ansi-bright-magenta",
773
+ "ansi-bright-cyan",
774
+ "ansi-bright-white"
775
+ ], g = [
776
+ 0,
777
+ 95,
778
+ 135,
779
+ 175,
780
+ 215,
781
+ 255
782
+ ];
783
+ function _(e) {
784
+ if (typeof e == "string") return e;
785
+ if (typeof e != "number" || e < 16 || e > 255) return null;
786
+ if (e >= 232) {
787
+ let t = 8 + (e - 232) * 10;
788
+ return `rgb(${t}, ${t}, ${t})`;
789
+ }
790
+ let t = e - 16;
791
+ return `rgb(${g[Math.floor(t / 36) % 6]}, ${g[Math.floor(t / 6) % 6]}, ${g[t % 6]})`;
792
+ }
793
+ function v(e) {
794
+ return e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
795
+ }
796
+ var y = /(?:https?:\/\/|mailto:|tel:)[^\s<>"'`]+/g;
797
+ function b(e) {
798
+ let t = e.length;
799
+ for (; t > 0;) {
800
+ let n = e[t - 1];
801
+ if (".!?,;:".includes(n)) t--;
802
+ else if (n === ")") {
803
+ let n = e.slice(0, t), r = (n.match(/\(/g) || []).length;
804
+ if ((n.match(/\)/g) || []).length > r) t--;
805
+ else break;
806
+ } else if (n === "]") {
807
+ let n = e.slice(0, t), r = (n.match(/\[/g) || []).length;
808
+ if ((n.match(/\]/g) || []).length > r) t--;
809
+ else break;
810
+ } else break;
811
+ }
812
+ return e.slice(0, t);
813
+ }
814
+ function x(e, t = f) {
815
+ let n = "";
816
+ for (let t = 0; t < e.length; t++) n += e[t].ch || " ";
817
+ let r = Array(e.length).fill(null);
818
+ for (let t = 0; t < e.length; t++) e[t].link && (r[t] = e[t].link);
819
+ y.lastIndex = 0;
820
+ let i;
821
+ for (; (i = y.exec(n)) !== null;) {
822
+ let e = b(i[0]), n = p(e, t);
823
+ if (!n) continue;
824
+ let a = i.index, o = a + e.length;
825
+ for (let e = a; e < o && e < r.length; e++) r[e] || (r[e] = n);
826
+ }
827
+ return r;
828
+ }
829
+ var S = class {
830
+ constructor(e, t, n = {}) {
831
+ this.container = e, this.screen = t, this.cursorStyle = n.cursor || "none", this.urlWhitelist = n.urlWhitelist || f, this._rowElements = [], this._rafId = null, this._lastCursorRow = -1, this._lastCursorCol = -1, this._lastCursorVisible = !1;
832
+ }
833
+ scheduleRender() {
834
+ this._rafId === null && (typeof requestAnimationFrame == "function" ? this._rafId = requestAnimationFrame(() => {
835
+ this._rafId = null, this.flushRender();
836
+ }) : this.flushRender());
837
+ }
838
+ flushRender() {
839
+ if (this._rafId !== null && typeof cancelAnimationFrame == "function" && (cancelAnimationFrame(this._rafId), this._rafId = null), !this.container || typeof document > "u") return;
840
+ let e = this.screen.rows;
841
+ this._ensureRowElements(e);
842
+ let t = this.cursorStyle !== "none" && this.screen.cursorVisible && this.screen.viewportOffset === 0, n = t ? this.screen.cursorRow : -1, r = t ? this.screen.cursorCol : -1;
843
+ (this._lastCursorRow !== n || this._lastCursorCol !== r || this._lastCursorVisible !== t) && (this._lastCursorRow >= 0 && this._lastCursorRow < e && this.screen.dirtyRows.add(this._lastCursorRow), n >= 0 && n < e && this.screen.dirtyRows.add(n), this._lastCursorRow = n, this._lastCursorCol = r, this._lastCursorVisible = t);
844
+ for (let t of this.screen.dirtyRows) t >= 0 && t < e && this._renderRow(t, t === n ? r : -1);
845
+ this.screen.dirtyRows.clear(), this.screen.fullRedrawNeeded = !1;
846
+ }
847
+ _ensureRowElements(e) {
848
+ for (; this._rowElements.length < e;) {
849
+ let e = document.createElement("li");
850
+ e.classList && (e.classList.add("terminal__line"), e.classList.add("terminal__row")), this.container.appendChild(e), this._rowElements.push(e);
851
+ }
852
+ for (; this._rowElements.length > e;) {
853
+ let e = this._rowElements.pop();
854
+ e && e.parentNode === this.container && this.container.removeChild && this.container.removeChild(e);
855
+ }
856
+ }
857
+ _renderRow(e, t) {
858
+ let n = this._rowElements[e], r = this.screen.getVisibleRow(e);
859
+ if (!n || !r) return;
860
+ let i = x(r, this.urlWhitelist), a = this.screen.isAltScreen ? r.length - 1 : -1;
861
+ if (!this.screen.isAltScreen) {
862
+ for (let e = r.length - 1; e >= 0; e--) {
863
+ let t = r[e];
864
+ if (t.ch !== " " || t.fg !== null || t.bg !== null || t.flags !== 0 || i[e] !== null) {
865
+ a = e;
866
+ break;
327
867
  }
328
- }, e.prototype.transform_to_html = function(e) {
329
- var t = e.text;
330
- if (t.length === 0 || (t = this.escape_txt_for_html(t), !e.bold && !e.underline && !e.italic && e.fg === null && e.bg === null)) return t;
331
- var n = [], r = [], i = e.fg, a = e.bg;
332
- e.bold && n.push("font-weight:bold"), e.underline && n.push("text-decoration:underline"), e.italic && n.push("font-style:italic"), this._use_classes ? (i && (i.class_name === "truecolor" ? n.push("color:rgb(" + i.rgb.join(",") + ")") : r.push(i.class_name + "-fg")), a && (a.class_name === "truecolor" ? n.push("background-color:rgb(" + a.rgb.join(",") + ")") : r.push(a.class_name + "-bg"))) : (i && n.push("color:rgb(" + i.rgb.join(",") + ")"), a && n.push("background-color:rgb(" + a.rgb + ")"));
333
- var o = "", s = "";
334
- return r.length && (o = " class=\"" + r.join(" ") + "\""), n.length && (s = " style=\"" + n.join(";") + "\""), "<span" + s + o + ">" + t + "</span>";
335
- }, e.prototype.process_hyperlink = function(e) {
336
- var t = e.url.split(":");
337
- return t.length < 1 || !this._url_whitelist[t[0]] ? "" : "<a href=\"" + this.escape_txt_for_html(e.url) + "\">" + this.sub_string_to_html(e.text) + "</a>";
338
- }, e.prototype.sub_string_to_html = function(e) {
339
- var t = { _buffer: e + "" };
340
- return this.loop_over_packets(t);
341
- }, e;
342
- }();
343
- function i(e) {
344
- var t = e.raw[0].replace(/^\s+|\s+\n|\s*#[\s\S]*?\n|\n/gm, "");
345
- return new RegExp(t);
346
- }
347
- function a(e) {
348
- var t = e.raw[0].replace(/^\s+|\s+\n|\s*#[\s\S]*?\n|\n/gm, "");
349
- return new RegExp(t, "g");
350
- }
351
- Object.defineProperty(e, "__esModule", { value: !0 }), e.default = r;
352
- });
353
- })))()), u = e?.default ?? e, d = l.default?.default ?? l.default, f = "/.brsh_history", p = class {
354
- constructor(e) {
355
- let { el: t, font: n = "Roboto Mono", cursor: r = "none", outputAnimation: i = "none", animateSpeed: a = 1, onExit: o = null, mobileInput: s = "click", lineWrap: c = !1, command: l = null } = e;
868
+ }
869
+ t > a && (a = t);
870
+ }
871
+ if (a < 0) {
872
+ n.innerHTML = "";
873
+ return;
874
+ }
875
+ let o = "", s = "", c = null, l = null, u = null, d = !1, f = () => {
876
+ s &&= (o += this._formatRun(s, l, u, d), "");
877
+ };
878
+ for (let e = 0; e <= a; e++) {
879
+ let n = r[e], a = i[e], o = e === t, p = `${n.fg ?? ""}|${n.bg ?? ""}|${n.flags}|${a ?? ""}|${+!!o}`;
880
+ p !== c && (f(), c = p, l = n, u = a, d = o), s += n.ch || " ";
881
+ }
882
+ f(), n.innerHTML = o;
883
+ }
884
+ _formatRun(e, t, n, r) {
885
+ let i = [], a = [], o = t.flags || 0, s = (o & 32) != 0, c = s ? t.bg : t.fg, l = s ? t.fg : t.bg;
886
+ if (s && t.fg === null && t.bg === null && i.push("terminal__inverse"), typeof c == "number" && c >= 0 && c < 16) i.push(`${h[c]}-fg`);
887
+ else if (c !== null) {
888
+ let e = _(c);
889
+ e && a.push(`color:${e}`);
890
+ }
891
+ if (typeof l == "number" && l >= 0 && l < 16) i.push(`${h[l]}-bg`);
892
+ else if (l !== null) {
893
+ let e = _(l);
894
+ e && a.push(`background-color:${e}`);
895
+ }
896
+ o & 1 && a.push("font-weight:bold"), o & 2 && a.push("opacity:0.65"), o & 4 && a.push("font-style:italic"), o & 8 && o & 128 ? a.push("text-decoration:underline line-through") : o & 8 ? a.push("text-decoration:underline") : o & 128 && a.push("text-decoration:line-through"), o & 16 && i.push("terminal__blink"), o & 64 && a.push("visibility:hidden"), r && (i.push("terminal__cursor"), i.push(this.cursorStyle));
897
+ let u = v(e), d = u;
898
+ return (i.length > 0 || a.length > 0) && (d = `<span${i.length > 0 ? ` class="${i.join(" ")}"` : ""}${a.length > 0 ? ` style="${a.join(";")}"` : ""}>${u}</span>`), n ? `<a href="${v(n)}" target="_blank" rel="noopener noreferrer" class="terminal__link">${d}</a>` : d;
899
+ }
900
+ }, C = "\nimport os, sys, pty, select, fcntl, termios, struct, signal, json\n\ncmd = sys.argv[1]\nargs = json.loads(sys.argv[2])\ncols = int(sys.argv[3])\nrows = int(sys.argv[4])\ncwd = sys.argv[5]\n\npid, fd = pty.fork()\nif pid == 0:\n if cwd:\n try: os.chdir(cwd)\n except Exception: pass\n os.execvp(cmd, [cmd] + args)\n\ndef set_winsize(c, r):\n try:\n fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack(\"HHHH\", r, c, 0, 0))\n os.kill(pid, signal.SIGWINCH)\n except Exception:\n pass\n\nset_winsize(cols, rows)\nstdin_fd = sys.stdin.fileno()\nstdout_fd = sys.stdout.fileno()\n\ntry:\n while True:\n rlist, _, _ = select.select([fd, stdin_fd], [], [])\n if fd in rlist:\n try:\n data = os.read(fd, 8192)\n if not data:\n break\n os.write(stdout_fd, data)\n except OSError:\n break\n if stdin_fd in rlist:\n try:\n chunk = os.read(stdin_fd, 8192)\n if not chunk:\n break\n # Check for inline resize control packet: \\x1b]9999;cols;rows\\x07\n while b\"\\x1b]9999;\" in chunk:\n start = chunk.find(b\"\\x1b]9999;\")\n end = chunk.find(b\"\\x07\", start)\n if end == -1:\n break\n packet = chunk[start+7:end].decode(\"ascii\", \"ignore\")\n parts = packet.split(\";\")\n if len(parts) == 2:\n set_winsize(int(parts[0]), int(parts[1]))\n chunk = chunk[:start] + chunk[end+1:]\n if chunk:\n os.write(fd, chunk)\n except OSError:\n break\nfinally:\n try:\n os.close(fd)\n except Exception:\n pass\n try:\n _, status = os.waitpid(pid, 0)\n sys.exit(os.waitstatus_to_exitcode(status) if hasattr(os, \"waitstatus_to_exitcode\") else 0)\n except Exception:\n sys.exit(0)\n";
901
+ function w(e) {
902
+ if (typeof process < "u" && process.env && process.env.SHELL) return process.env.SHELL;
903
+ try {
904
+ let t = e ? e("fs") : null;
905
+ if (t) {
906
+ for (let e of [
907
+ "/bin/zsh",
908
+ "/usr/bin/zsh",
909
+ "/bin/bash",
910
+ "/usr/bin/bash",
911
+ "/bin/sh"
912
+ ]) if (t.existsSync(e)) return e;
913
+ }
914
+ } catch {}
915
+ return "/bin/sh";
916
+ }
917
+ function T(e, n = [], r = {}) {
918
+ let i = typeof t == "function" ? t : typeof globalThis < "u" && globalThis.process && globalThis.process.getBuiltinModule ? (e) => globalThis.process.getBuiltinModule(e) : null;
919
+ if (!i) throw Error("spawnPty is only available in a Node.js environment");
920
+ let a = e || w(i), { spawn: o } = i("child_process"), s = r.cols || 80, c = r.rows || 24, l = r.cwd || (typeof process < "u" ? process.cwd() : ""), u = Object.assign({}, typeof process < "u" ? process.env : {}, {
921
+ TERM: "xterm-256color",
922
+ COLORTERM: "truecolor",
923
+ COLUMNS: String(s),
924
+ LINES: String(c)
925
+ }, r.env || {}), d = o("python3", [
926
+ "-u",
927
+ "-c",
928
+ C,
929
+ a,
930
+ JSON.stringify(n),
931
+ String(s),
932
+ String(c),
933
+ l
934
+ ], {
935
+ env: u,
936
+ stdio: [
937
+ "pipe",
938
+ "pipe",
939
+ "pipe"
940
+ ]
941
+ }), f = /* @__PURE__ */ new Map(), p = (e, ...t) => {
942
+ let n = f.get(e);
943
+ if (n) for (let e of [...n]) e(...t);
944
+ };
945
+ return d.stdout.on("data", (e) => p("data", e)), d.on("exit", (e, t) => p("exit", e, t)), d.on("close", (e) => p("close", e)), d.on("error", (e) => p("error", e)), {
946
+ pid: d.pid,
947
+ process: d,
948
+ write(e) {
949
+ d.stdin && !d.stdin.destroyed && d.stdin.write(e);
950
+ },
951
+ resize(e, t) {
952
+ d.stdin && !d.stdin.destroyed && d.stdin.write(`\x1b]9999;${Math.max(1, e)};${Math.max(1, t)}\x07`);
953
+ },
954
+ on(e, t) {
955
+ return f.has(e) || f.set(e, []), f.get(e).push(t), this;
956
+ },
957
+ removeListener(e, t) {
958
+ let n = f.get(e);
959
+ if (!n) return this;
960
+ let r = n.indexOf(t);
961
+ return r !== -1 && n.splice(r, 1), this;
962
+ },
963
+ kill(e = "SIGTERM") {
964
+ try {
965
+ d.kill(e);
966
+ } catch {}
967
+ }
968
+ };
969
+ }
970
+ //#endregion
971
+ //#region lib/Terminal.js
972
+ var E = e?.default ?? e, D = class {
973
+ constructor() {
974
+ this._listeners = /* @__PURE__ */ new Map(), this.readable = !0, this.writable = !0, this.isTTY = !0;
975
+ }
976
+ on(e, t) {
977
+ return this._listeners.has(e) || this._listeners.set(e, []), this._listeners.get(e).push(t), this;
978
+ }
979
+ addListener(e, t) {
980
+ return this.on(e, t);
981
+ }
982
+ once(e, t) {
983
+ let n = (...r) => {
984
+ this.removeListener(e, n), t(...r);
985
+ };
986
+ return this.on(e, n);
987
+ }
988
+ removeListener(e, t) {
989
+ let n = this._listeners.get(e);
990
+ if (!n) return this;
991
+ let r = n.indexOf(t);
992
+ return r !== -1 && n.splice(r, 1), this;
993
+ }
994
+ off(e, t) {
995
+ return this.removeListener(e, t);
996
+ }
997
+ removeAllListeners(e) {
998
+ return e ? this._listeners.delete(e) : this._listeners.clear(), this;
999
+ }
1000
+ listeners(e) {
1001
+ return [...this._listeners.get(e) || []];
1002
+ }
1003
+ emit(e, ...t) {
1004
+ let n = this._listeners.get(e);
1005
+ if (!n || n.length === 0) return !1;
1006
+ for (let e of [...n]) e(...t);
1007
+ return !0;
1008
+ }
1009
+ setRawMode(e) {
1010
+ return this.isRaw = !!e, this;
1011
+ }
1012
+ resume() {
1013
+ return this;
1014
+ }
1015
+ pause() {
1016
+ return this;
1017
+ }
1018
+ setEncoding(e) {
1019
+ return this._encoding = e, this;
1020
+ }
1021
+ pipe(e) {
1022
+ return this.on("data", (t) => e.write(t)), e;
1023
+ }
1024
+ unpipe() {
1025
+ return this;
1026
+ }
1027
+ destroy() {
1028
+ this.removeAllListeners();
1029
+ }
1030
+ }, O = class {
1031
+ constructor(e = {}) {
1032
+ let { el: t, font: n = "Roboto Mono", cursor: r = "none", outputAnimation: a = "none", animateSpeed: o = 1, onExit: s = null, mobileInput: c = "click", lineWrap: l = !1, command: u = null, cols: d = 80, rows: p = 24, maxScrollback: h = 1e3, stream: g = null } = e;
356
1033
  if (!t) throw Error("you must set `el` option");
357
1034
  if (![
358
1035
  "none",
359
1036
  "block",
360
1037
  "blink"
361
1038
  ].includes(r)) throw Error("cursor value must be one of: 'none', 'block', 'blink'");
362
- if (!["none", "type"].includes(i)) throw Error("outputAnimation value must be one of: 'none', 'type'");
363
- if (!Number.isInteger(a) || a < 1 || a > 20) throw Error("animateSpeed value must be between 1 and 20");
364
- this._exitFn = o, this._cursor = r, this._outputAnimation = i, this._animateSpeed = a, this._lineWrap = c, this._working = !0, this._pendingCommands = [];
365
- let f = null;
366
- if (e.Shell !== !1 && e.Shell !== null && (f = e.Shell || typeof globalThis < "u" && (globalThis.Shell || globalThis.shell) || (typeof u == "function" ? u : null)), this.cli = e.cli || e.shell, !this.cli) {
367
- if (!f) throw Error("Shell is required by Terminal. Please import Shell from \"brsh\" or include shell.min.js before terminal.min.js.");
368
- this.cli = new f(Object.assign({}, e, { command: void 0 }));
369
- }
370
- this._buffer = [], this._cursorPos = 0, this._history = [], this._historyPos = null, this._savedInput = null, this._historyLoaded = !1, this._el = t, this._ansi = new d(), this._ansi.use_classes = !0, this._ansi.url_whitelist = {
371
- http: !0,
372
- https: !0,
373
- tel: !0,
374
- mailto: !0
375
- }, this._setupDom(this._el, n), this._attachShellListeners(), l && this.onCommand(l), s === "click" && this._mobileInput();
1039
+ if (!["none", "type"].includes(a)) throw Error("outputAnimation value must be one of: 'none', 'type'");
1040
+ if (!Number.isInteger(o) || o < 1 || o > 20) throw Error("animateSpeed value must be between 1 and 20");
1041
+ if (this._exitFn = s, this._cursor = r, this._outputAnimation = a, this._animateSpeed = o, this._lineWrap = l, this._el = t, this._dataListeners = [], this._resizeListeners = [], this._titleListeners = [], this._mouseButtonDown = null, this._typeQueue = "", this._typeTimer = null, this._isHandlingInput = !1, this._suppressAnimationUntil = 0, this.screen = new i(d, p, { maxScrollback: h }), this.parser = new m(this.screen, {
1042
+ urlWhitelist: f,
1043
+ onResponse: (e) => this._emitInput(e),
1044
+ onTitle: (e) => {
1045
+ this.title = e;
1046
+ for (let t of this._titleListeners) t(e);
1047
+ }
1048
+ }), this.cli = e.cli || e.shell || null, !this.cli && !g && e.Shell !== !1) {
1049
+ let t = e.Shell === null ? null : e.Shell || typeof globalThis < "u" && (globalThis.Shell || globalThis.shell) || (typeof E == "function" ? E : null);
1050
+ if (!t) throw Error("Shell is required by Terminal. Please import Shell from \"brsh\" or include shell.min.js before terminal.min.js.");
1051
+ this.cli = new t(Object.assign({}, e, { command: void 0 }));
1052
+ }
1053
+ this._setupDom(this._el, n), this.renderer = new S(this._cmdList, this.screen, {
1054
+ cursor: this._cursor,
1055
+ urlWhitelist: f
1056
+ }), this._listenForEvents(), this.cli && (this.attach(this.cli), typeof this.cli.on == "function" && this.cli.on("exit", (e) => {
1057
+ delete this.cli, typeof this._exitFn == "function" && this._exitFn(e);
1058
+ }), u && this.onCommand(u)), g && (this.attach(g), u && this.onCommand(u)), c === "click" && this._mobileInput(), this.renderer.flushRender();
376
1059
  }
377
- sendKey(e, t = {}) {
378
- let { alt: n = !1, ctrl: r = !1 } = t, i = this._setCurrentLine.bind(this);
379
- [
380
- "ArrowUp",
381
- "ArrowDown",
382
- "ArrowLeft",
383
- "ArrowRight",
384
- "Home",
385
- "End",
386
- "Delete"
387
- ].includes(e) || r && [
388
- "a",
389
- "e",
390
- "u",
391
- "k"
392
- ].includes(e.toLowerCase()) ? this._navKeyEvent(i, {
393
- key: e,
394
- altKey: n,
395
- ctrlKey: r
396
- }) : this._keyEvent(i, e);
1060
+ get cols() {
1061
+ return this.screen.cols;
397
1062
  }
398
- onCommand(e) {
399
- if (!this._currentLine) {
400
- this.cli && this.cli.suppressMotd(), this._pendingCommands.push(e);
1063
+ get rows() {
1064
+ return this.screen.rows;
1065
+ }
1066
+ write(e) {
1067
+ if (e == null || e === "") return;
1068
+ let t = typeof e == "string" ? e : e instanceof Uint8Array || typeof Buffer < "u" && Buffer.isBuffer(e) ? new TextDecoder().decode(e) : String(e);
1069
+ if (this._outputAnimation === "type") {
1070
+ if (this._isUserInputActive() || this._isFullScreenWrite(t)) {
1071
+ this._flushTypeQueue(), this.parser.write(t), this.renderer.flushRender(), this._autoScroll();
1072
+ return;
1073
+ }
1074
+ if ((this._isLineRewrite(t) || !/[^\r\n]/.test(t)) && (!this._typeQueue || !this._typeQueue.includes("\n"))) {
1075
+ this._flushTypeQueue(), this.parser.write(t), this.renderer.flushRender(), this._autoScroll();
1076
+ return;
1077
+ }
1078
+ this._enqueueTypeAnimation(t);
401
1079
  return;
402
1080
  }
403
- let t = this._setCurrentLine.bind(this);
404
- this._cursorPos = e.length, t(Array.from(e)), this.cli.onCommand(e);
1081
+ this.parser.write(t), this.renderer.flushRender(), this._autoScroll();
405
1082
  }
406
- _mobileInput() {
407
- let e = document.createElement("input");
408
- e.type = "text", e.style.position = "absolute", e.style.left = "-9999999px", this._el.appendChild(e), this._mobileInputBuffer = "", this._el.addEventListener("click", () => {
409
- e.focus();
410
- }), e.addEventListener("keypress", (t) => {
411
- t.key === "Enter" && (e.value = "", this._mobileInputBuffer = e.value);
412
- });
413
- let t = this._setCurrentLine.bind(this);
414
- e.addEventListener("keyup", (n) => {
415
- if (n.key === "Unidentified") {
416
- if (e.value.length < this._mobileInputBuffer.length) this._keyEvent(t, "Backspace");
417
- else if (e.value.length > this._mobileInputBuffer.length) {
418
- let n = e.value[e.value.length - 1];
419
- this._keyEvent(t, n.toLowerCase());
420
- }
421
- this._mobileInputBuffer = e.value;
422
- }
423
- });
1083
+ writeln(e = "") {
1084
+ this.write(String(e) + "\r\n");
424
1085
  }
425
- _setupDom(e, t = "Roboto Mono") {
426
- let n = document.createElement("link");
427
- n.href = `https://fonts.googleapis.com/css?family=${encodeURIComponent(t)}:ital,wght@0,400;0,700;1,400;1,700&display=swap`, n.rel = "stylesheet", document.head.appendChild(n), this._cmdList = document.createElement("ul"), this._cmdList.style.fontFamily = `'${t}', monospace`, this._cmdList.style.listStyle = "none", this._cmdList.style.padding = "0", this._lineWrap && this._cmdList.classList.add("terminal--wrap"), e.appendChild(this._cmdList), this._screen = document.createElement("pre"), this._screen.className = "terminal__screen", this._screen.style.fontFamily = `'${t}', monospace`, e.appendChild(this._screen), this._charMeasure = document.createElement("span"), this._charMeasure.className = "terminal__char-measure", this._charMeasure.style.fontFamily = `'${t}', monospace`, this._charMeasure.textContent = "X", this._charMeasure.setAttribute("aria-hidden", "true"), e.appendChild(this._charMeasure), setTimeout(() => this._measureAndSetSize(), 0), document.fonts && document.fonts.ready.then(() => this._measureAndSetSize()), typeof ResizeObserver < "u" && new ResizeObserver(() => this._measureAndSetSize()).observe(e);
1086
+ clear() {
1087
+ this.write("\x1B[2J\x1B[3J\x1B[H");
428
1088
  }
429
- _newPrompt(e) {
430
- typeof document > "u" || !this._cmdList || (this._progressLine = null, this._clearCursor(), this._currentLine = document.createElement("li"), this._cmdList.appendChild(this._currentLine), e && e(), this._autoScroll());
1089
+ reset() {
1090
+ this._flushTypeQueue(), this.parser.reset(), this.renderer.flushRender();
431
1091
  }
432
- _print(e, t = "out") {
433
- if (typeof e == "string" && e.startsWith("\r")) {
434
- let n = e.slice(1);
435
- if (this._progressLine && this._progressLine.parentNode === this._cmdList) {
436
- this._progressLine.innerHTML = this._ansi.ansi_to_html(n), this._autoScroll();
437
- return;
438
- }
439
- let r = document.createElement("li");
440
- r.classList.add("terminal__line"), r.classList.add(t), r.innerHTML = this._ansi.ansi_to_html(n), this._cmdList.appendChild(r), this._progressLine = r, this._autoScroll();
441
- return;
1092
+ onData(e) {
1093
+ return typeof e == "function" ? (this._dataListeners.push(e), () => {
1094
+ let t = this._dataListeners.indexOf(e);
1095
+ t !== -1 && this._dataListeners.splice(t, 1);
1096
+ }) : () => {};
1097
+ }
1098
+ onResize(e) {
1099
+ return typeof e == "function" ? (this._resizeListeners.push(e), () => {
1100
+ let t = this._resizeListeners.indexOf(e);
1101
+ t !== -1 && this._resizeListeners.splice(t, 1);
1102
+ }) : () => {};
1103
+ }
1104
+ onTitleChange(e) {
1105
+ return typeof e == "function" ? (this._titleListeners.push(e), () => {
1106
+ let t = this._titleListeners.indexOf(e);
1107
+ t !== -1 && this._titleListeners.splice(t, 1);
1108
+ }) : () => {};
1109
+ }
1110
+ resize(e, t) {
1111
+ let n = Math.max(1, Math.floor(e)), r = Math.max(1, Math.floor(t));
1112
+ if (!(n === this.screen.cols && r === this.screen.rows && this._lastCols === n && this._lastRows === r)) {
1113
+ this._lastCols = n, this._lastRows = r, this.screen.resize(n, r), this.cli && typeof this.cli.setTerminalSize == "function" && this.cli.setTerminalSize(n, r);
1114
+ for (let e of this._resizeListeners) e({
1115
+ cols: n,
1116
+ rows: r
1117
+ });
1118
+ this.renderer.flushRender();
442
1119
  }
443
- if (this._progressLine = null, this._outputAnimation === "none") {
444
- let n = document.createElement("li");
445
- n.classList.add("terminal__line"), n.classList.add(t), n.innerHTML = this._ansi.ansi_to_html(e), this._cmdList.appendChild(n), this._autoScroll();
446
- } else this._animateType(e, t);
447
1120
  }
448
- _autoScroll() {
449
- this._el.scrollTop + this._el.offsetHeight >= this._el.scrollHeight - 50 && this._el.scrollTo(0, this._el.scrollHeight);
1121
+ attach(e) {
1122
+ if (!e) return () => {};
1123
+ this._attachedStream = e;
1124
+ let t = (e) => {
1125
+ this.write(e);
1126
+ };
1127
+ typeof e.on == "function" ? e.on("data", t) : typeof e.addEventListener == "function" && e.addEventListener("message", (e) => this.write(e.data ?? e));
1128
+ let n = this.onData((t) => {
1129
+ typeof e.write == "function" ? e.write(t) : typeof e.send == "function" && e.send(t);
1130
+ }), r = this.onResize(({ cols: t, rows: n }) => {
1131
+ typeof e.resize == "function" && e.resize(t, n);
1132
+ });
1133
+ return typeof e.resize == "function" && e.resize(this.cols, this.rows), () => {
1134
+ n(), r(), typeof e.removeListener == "function" && e.removeListener("data", t), this._attachedStream === e && (this._attachedStream = null);
1135
+ };
450
1136
  }
451
- _animateType(e, t) {
452
- if (this._animating) return requestAnimationFrame(this._animateType.bind(this, e, t));
453
- let n = document.createElement("li");
454
- n.classList.add("terminal__line"), n.classList.add(t), this._cmdList.appendChild(n);
455
- let r;
456
- try {
457
- r = this._ansi.ansi_to_html(e);
458
- } catch {
459
- r = e;
460
- }
461
- this._animating = n;
462
- let i = 0, a = setInterval(() => {
463
- r.charAt(i) === "<" && (i = r.indexOf(">", i)), n.innerHTML = r.substr(0, i);
464
- let e = 0;
465
- for (; e < this._animateSpeed;) i++, r.charAt(i) === "<" && (i = r.indexOf(">", i)), e++;
466
- i >= r.length - 1 && (n.innerHTML = r, clearInterval(a), delete this._animating), this._autoScroll();
467
- }, 10);
468
- }
469
- _clearOutput() {
470
- this._cmdList.querySelectorAll("li").forEach((e) => this._cmdList.removeChild(e));
471
- }
472
- _clearLine(e) {
473
- this._cmdList.querySelectorAll(`li.terminal__line.${e}`).forEach((e) => this._cmdList.removeChild(e));
474
- }
475
- _setCurrentLine(e) {
476
- let t = Math.min(this._cursorPos, e.length);
477
- for (; this._currentLine.firstChild;) this._currentLine.removeChild(this._currentLine.firstChild);
478
- let n = `${this.cli.getPrompt()} `;
479
- if (this._cursor !== "none") {
480
- let r = e.slice(0, t).join(""), i = t < e.length ? e[t] : "\xA0", a = e.slice(t + 1).join("");
481
- this._currentLine.appendChild(document.createTextNode(n + r));
482
- let o = document.createElement("span");
483
- o.textContent = i, o.classList.add("terminal__cursor"), o.classList.add(this._cursor), this._currentLine.appendChild(o), a && this._currentLine.appendChild(document.createTextNode(a));
484
- } else this._currentLine.innerText = `${n}${e.join("")}`;
485
- }
486
- _clearCursor() {
487
- if (this._cursor !== "none") {
488
- let e = document.querySelectorAll(".terminal__cursor");
489
- e.length && e.forEach((e) => e.parentElement.removeChild(e));
490
- }
491
- }
492
- _statusCheck(e) {
493
- if (e === (this.cli?.constructor?.STATUS_READY || u?.STATUS_READY || globalThis.Shell?.STATUS_READY || globalThis.shell?.STATUS_READY || "READY")) {
494
- if (this._interactive && this._exitInteractiveMode(), this._working = !1, this._historyLoaded ||= (this._loadHistory(), !0), this._animating) return requestAnimationFrame(this._statusCheck.bind(this, e));
495
- if (this._newPrompt(this._setCurrentLine.bind(this, this._buffer)), this._pendingCommands && this._pendingCommands.length > 0) {
496
- let e = this._pendingCommands.shift();
497
- if (this._pendingCommands.length === 0 && this.cli) {
498
- let e = this.cli.context.getVar("-");
499
- e.includes("c") && this.cli.context.setVar("-", e.replace(/c/g, ""));
500
- }
501
- this.onCommand(e);
1137
+ spawn(e, t = [], n = {}) {
1138
+ let r = T(e, t, Object.assign({
1139
+ cols: this.cols,
1140
+ rows: this.rows
1141
+ }, n)), i = this.attach(r);
1142
+ return r.on("exit", (e) => {
1143
+ typeof this._exitFn == "function" && this._exitFn(e);
1144
+ }), {
1145
+ pty: r,
1146
+ detach: i
1147
+ };
1148
+ }
1149
+ createTty(e = {}) {
1150
+ let t = new D(), n = new D();
1151
+ e.interceptShell, n.columns = this.cols, n.rows = this.rows, n.getWindowSize = () => [n.columns, n.rows], n.write = (e, t, n) => (this.write(e), typeof t == "function" ? t() : typeof n == "function" && n(), !0);
1152
+ let r = this.onData((e) => {
1153
+ let n = t._encoding ? e : typeof Buffer < "u" ? Buffer.from(e, "utf8") : e;
1154
+ t.emit("data", n);
1155
+ }), i = this.onResize(({ cols: e, rows: t }) => {
1156
+ n.columns = e, n.rows = t, n.emit("resize");
1157
+ });
1158
+ return {
1159
+ stdin: t,
1160
+ stdout: n,
1161
+ dispose: () => {
1162
+ r(), i(), t.destroy(), n.destroy();
502
1163
  }
503
- } else this._clearCursor(), this._working = !0;
1164
+ };
504
1165
  }
505
- _loadHistory() {
506
- try {
507
- let e = this.cli.context.fs._getFileRaw(f);
508
- e && typeof e == "string" && e.length > 0 && (this._history = e.split("\n").filter((e) => e.length > 0));
509
- } catch {}
1166
+ sendKey(e, t = {}) {
1167
+ let n = this._encodeKey(e, t);
1168
+ n && this._emitInput(n);
510
1169
  }
511
- _saveHistory() {
1170
+ sendMouse(e, { button: t = 0, col: n = 1, row: r = 1, shift: i = !1, alt: a = !1, ctrl: o = !1, deltaY: s = 0 } = {}) {
1171
+ let c = this.parser.mouseTrackingMode, l = Math.max(1, Math.min(this.cols, n)), u = Math.max(1, Math.min(this.rows, r));
1172
+ if (c === 0 || i) {
1173
+ if (e === "wheel") if (this.screen.isAltScreen && this.parser.alternateScrollMode) {
1174
+ let e = s < 0 ? "ArrowUp" : "ArrowDown";
1175
+ this.sendKey(e);
1176
+ } else this.screen.isAltScreen || (this.screen.scrollViewport(s < 0 ? 3 : -3), this.renderer.flushRender());
1177
+ return !1;
1178
+ }
1179
+ let d = t;
1180
+ if (e === "wheel") d = s < 0 ? 64 : 65;
1181
+ else if (e === "mousemove") {
1182
+ if (c === 1e3 || c === 1002 && this._mouseButtonDown === null) return !1;
1183
+ d = (this._mouseButtonDown ?? 0) + 32;
1184
+ } else e === "mousedown" ? this._mouseButtonDown = t : e === "mouseup" && (this._mouseButtonDown = null, this.parser.mouseProtocol !== "sgr" && (d = 3));
1185
+ if (i && (d += 4), a && (d += 8), o && (d += 16), this.parser.mouseProtocol === "sgr") {
1186
+ let t = e === "mouseup" ? "m" : "M";
1187
+ return this._emitInput(`\x1b[<${d};${l};${u}${t}`), !0;
1188
+ }
1189
+ let f = this.parser.mouseProtocol === "utf8" ? 2015 : 223, p = Math.min(f, l), m = Math.min(f, u);
1190
+ return this._emitInput("\x1B[M" + String.fromCharCode(32 + d) + String.fromCharCode(32 + p) + String.fromCharCode(32 + m)), !0;
1191
+ }
1192
+ onCommand(e) {
1193
+ this.cli && typeof this.cli.sendCommand == "function" ? this.cli.sendCommand(e) : this._emitInput(String(e) + "\r");
1194
+ }
1195
+ getScreenText(e) {
1196
+ return this.renderer.flushRender(), this.screen.getScreenText(e);
1197
+ }
1198
+ getLineText(e, t = !0) {
1199
+ return this.renderer.flushRender(), this.screen.getLineText(e, t);
1200
+ }
1201
+ _emitInput(e) {
1202
+ if (!e) return;
1203
+ this.screen.viewportOffset > 0 && (this.screen.viewportOffset = 0, this.screen._markAllDirty(), this.renderer.flushRender()), (this._typeQueue || this._typeTimer !== null) && (this._flushTypeQueue(), this.renderer.flushRender(), this._autoScroll());
1204
+ let t = e === "\r" || e === "\n" || e === "\r\n";
1205
+ this._suppressAnimationUntil = t ? 0 : Date.now() + 80, this._isHandlingInput = !t;
512
1206
  try {
513
- this.cli.context.fs._setFileRaw(f, this._history.join("\n"), !0);
514
- } catch {}
515
- }
516
- _navigateHistory(e, t) {
517
- if (this._history.length !== 0) {
518
- if (e === -1) {
519
- if (this._historyPos === null) this._savedInput = [...this._buffer], this._historyPos = this._history.length - 1;
520
- else if (this._historyPos > 0) this._historyPos--;
521
- else return;
522
- this._buffer = Array.from(this._history[this._historyPos]);
523
- } else {
524
- if (this._historyPos === null) return;
525
- this._historyPos < this._history.length - 1 ? (this._historyPos++, this._buffer = Array.from(this._history[this._historyPos])) : (this._historyPos = null, this._buffer = this._savedInput ? [...this._savedInput] : [], this._savedInput = null);
526
- }
527
- this._cursorPos = this._buffer.length, t(this._buffer);
528
- }
529
- }
530
- _attachShellListeners() {
531
- this.cli.on("status", this._statusCheck.bind(this)), this.cli.on("stdOut", (e) => {
532
- this._interactive ? this._renderInteractiveScreen(e) : this._print(e, "out");
533
- }), this.cli.on("stdErr", (e) => {
534
- this._interactive || this._print(e, "err");
535
- }), this.cli.on("clear", () => {
536
- this._clearOutput();
537
- }), this.cli.on("interactive", () => {
538
- this._enterInteractiveMode();
539
- }), this.cli.on("exit", (e) => {
540
- delete this.cli, typeof this._exitFn == "function" && this._exitFn(e);
541
- }), this._listenForKeyEvents(this._setCurrentLine.bind(this), window);
542
- }
543
- _listenForKeyEvents(e, t) {
544
- window.addEventListener("keydown", (t) => {
545
- if (this._interactive) {
546
- t.preventDefault();
547
- let e = this._buildInteractiveKey(t);
548
- e && this.cli.onInput(e);
549
- return;
550
- }
551
- if (t.keyCode === 9 && t.preventDefault(), [
552
- "ArrowUp",
553
- "ArrowDown",
554
- "ArrowLeft",
555
- "ArrowRight",
556
- "Home",
557
- "End",
558
- "Delete"
559
- ].includes(t.key)) {
560
- t.preventDefault(), this._navKeyEvent(e, t);
561
- return;
562
- }
563
- t.ctrlKey && [
564
- "a",
565
- "e",
566
- "u",
567
- "k"
568
- ].includes(t.key.toLowerCase()) && (t.preventDefault(), this._navKeyEvent(e, t));
569
- }), t.addEventListener("keyup", (t) => {
570
- this._interactive || this._keyEvent(e, t.key);
571
- }), window.addEventListener("paste", (t) => {
572
- if (t.preventDefault(), this._interactive) return;
573
- let n = (t.clipboardData || window.clipboardData).getData("text");
574
- if (!n) return;
575
- let r = n.split(/\r?\n/);
576
- r.forEach((t, n) => {
577
- let i = t.split("");
578
- this._buffer.splice(this._cursorPos, 0, ...i), this._cursorPos += i.length, n < r.length - 1 && this._keyEvent(e, "Enter");
579
- }), e(this._buffer);
580
- });
1207
+ for (let t of this._dataListeners) t(e);
1208
+ } finally {
1209
+ this._isHandlingInput = !1;
1210
+ }
581
1211
  }
582
- _buildInteractiveKey(e) {
583
- let t = e.key;
584
- return [
1212
+ _flushTypeQueue() {
1213
+ if (this._typeTimer !== null && (clearInterval(this._typeTimer), this._typeTimer = null), this._typeQueue) {
1214
+ let e = this._typeQueue;
1215
+ this._typeQueue = "", this.parser.write(e);
1216
+ }
1217
+ }
1218
+ _isUserInputActive() {
1219
+ return !!(this._isHandlingInput || this._suppressAnimationUntil && Date.now() < this._suppressAnimationUntil);
1220
+ }
1221
+ _isFullScreenWrite(e) {
1222
+ if (this.screen.isAltScreen) return !0;
1223
+ if (this.cli && this.cli.runningCommand) {
1224
+ let e = this.cli.runningCommand;
1225
+ if (e.interactive || e._inAltScreen || e.stdinStream && e.stdinStream.isRaw) return !0;
1226
+ }
1227
+ return !!(this._attachedStream && this._attachedStream.isRaw || /\x1b\[\?(?:1049|1047|47)[hl]/.test(e) || /\x1b\[(?:\d*J|(?:\d+;\d+)?[Hf]|\d*[ABLM]|\d*;\d*r)/.test(e));
1228
+ }
1229
+ _isLineRewrite(e) {
1230
+ return !!(/\r(?!\n)/.test(e) || /\x1b\[\d*[KCDG@P]/.test(e) || /[\x08\x7f]/.test(e));
1231
+ }
1232
+ _encodeKey(e, t = {}) {
1233
+ let { alt: n = !1, ctrl: r = !1, shift: i = !1, meta: a = !1 } = t;
1234
+ if (!e || [
585
1235
  "Control",
586
1236
  "Alt",
587
1237
  "Shift",
588
- "Meta"
589
- ].includes(t) ? null : e.ctrlKey && t.length === 1 ? "^" + t.toUpperCase() : t;
590
- }
591
- _wordLeft() {
592
- let e = this._cursorPos;
593
- for (; e > 0 && this._buffer[e - 1] === " ";) e--;
594
- for (; e > 0 && this._buffer[e - 1] !== " ";) e--;
595
- return e;
596
- }
597
- _wordRight() {
598
- let e = this._cursorPos, t = this._buffer.length;
599
- for (; e < t && this._buffer[e] === " ";) e++;
600
- for (; e < t && this._buffer[e] !== " ";) e++;
601
- return e;
602
- }
603
- _navKeyEvent(e, t) {
604
- if (this._working || this._animating) return;
605
- let n = t.key;
606
- n === "ArrowLeft" ? (t.altKey ? this._cursorPos = this._wordLeft() : this._cursorPos > 0 && this._cursorPos--, e(this._buffer)) : n === "ArrowRight" ? (t.altKey ? this._cursorPos = this._wordRight() : this._cursorPos < this._buffer.length && this._cursorPos++, e(this._buffer)) : n === "Home" || t.ctrlKey && n.toLowerCase() === "a" ? (this._cursorPos = 0, e(this._buffer)) : n === "End" || t.ctrlKey && n.toLowerCase() === "e" ? (this._cursorPos = this._buffer.length, e(this._buffer)) : n === "Delete" ? this._cursorPos < this._buffer.length && (this._buffer.splice(this._cursorPos, 1), e(this._buffer)) : t.ctrlKey && n.toLowerCase() === "u" ? (this._buffer.splice(0, this._cursorPos), this._cursorPos = 0, e(this._buffer)) : t.ctrlKey && n.toLowerCase() === "k" ? (this._buffer.splice(this._cursorPos), e(this._buffer)) : n === "ArrowUp" ? this._navigateHistory(-1, e) : n === "ArrowDown" && this._navigateHistory(1, e);
607
- }
608
- _keyEvent(e, t) {
609
- if (this._clearLine("autoComplete"), !this.cli.onInput(t)) {
610
- if (t.length === 1) this._buffer.splice(this._cursorPos, 0, t), this._cursorPos++, !this._working && !this._animating && e(this._buffer);
611
- else if (t === "Enter" && !this._working) {
612
- let e = this._buffer.join("");
613
- this._buffer = [], this._cursorPos = 0, this._historyPos = null, this._savedInput = null, e.trim() && (this._history[this._history.length - 1] !== e && this._history.push(e), this._saveHistory()), this.cli.onCommand(e);
614
- } else if (t === "Backspace") this._cursorPos > 0 && (this._buffer.splice(this._cursorPos - 1, 1), this._cursorPos--, !this._working && !this._animating && e(this._buffer));
615
- else if (t === "Tab" && !this._working && !this._animating) {
616
- let t = this._buffer.join("").split(" "), n = t.pop(), r = this.cli.tabCompletion(n);
617
- if (r) {
618
- let { options: n, path: i } = r;
619
- n.length === 1 ? (t.push(`${i == null ? "" : i.concat("/")}${n[0]}`), this._buffer = t.join(" ").split("")) : this._print(n.join(" "), "autoComplete"), this._cursorPos = this._buffer.length, e(this._buffer);
620
- }
1238
+ "Meta",
1239
+ "CapsLock"
1240
+ ].includes(e) || a) return null;
1241
+ let o = this.parser.applicationCursorKeys;
1242
+ if (e === "ArrowUp") return n ? "\x1B[1;3A" : r ? "\x1B[1;5A" : o ? "\x1BOA" : "\x1B[A";
1243
+ if (e === "ArrowDown") return n ? "\x1B[1;3B" : r ? "\x1B[1;5B" : o ? "\x1BOB" : "\x1B[B";
1244
+ if (e === "ArrowRight") return n ? "\x1B[1;3C" : r ? "\x1B[1;5C" : o ? "\x1BOC" : "\x1B[C";
1245
+ if (e === "ArrowLeft") return n ? "\x1B[1;3D" : r ? "\x1B[1;5D" : o ? "\x1BOD" : "\x1B[D";
1246
+ if (e === "Home") return o ? "\x1BOH" : "\x1B[H";
1247
+ if (e === "End") return o ? "\x1BOF" : "\x1B[F";
1248
+ if (e === "Delete") return "\x1B[3~";
1249
+ if (e === "Insert") return "\x1B[2~";
1250
+ if (e === "PageUp") return "\x1B[5~";
1251
+ if (e === "PageDown") return "\x1B[6~";
1252
+ if (e === "Enter") return "\r";
1253
+ if (e === "Backspace") return n ? "\x1B" : "";
1254
+ if (e === "Tab") return i ? "\x1B[Z" : " ";
1255
+ if (e === "Escape") return "\x1B";
1256
+ let s = e.match(/^F(\d{1,2})$/);
1257
+ if (s) return {
1258
+ 1: "\x1BOP",
1259
+ 2: "\x1BOQ",
1260
+ 3: "\x1BOR",
1261
+ 4: "\x1BOS",
1262
+ 5: "\x1B[15~",
1263
+ 6: "\x1B[17~",
1264
+ 7: "\x1B[18~",
1265
+ 8: "\x1B[19~",
1266
+ 9: "\x1B[20~",
1267
+ 10: "\x1B[21~",
1268
+ 11: "\x1B[23~",
1269
+ 12: "\x1B[24~"
1270
+ }[Number(s[1])] || null;
1271
+ if (r && e.length === 1) {
1272
+ let t = e.toUpperCase().charCodeAt(0);
1273
+ if (t >= 64 && t <= 95) {
1274
+ let e = String.fromCharCode(t - 64);
1275
+ return n ? "\x1B" + e : e;
621
1276
  }
622
1277
  }
1278
+ return e.length === 1 ? n ? "\x1B" + e : e : null;
623
1279
  }
624
- _enterInteractiveMode() {
625
- this._interactive = !0, this._cmdList.style.display = "none", this._screen.style.display = "block", this._clearCursor();
1280
+ _enqueueTypeAnimation(e) {
1281
+ this._typeQueue += e, this._typeTimer === null && (this._typeTimer = setInterval(() => {
1282
+ if (!this._typeQueue) {
1283
+ clearInterval(this._typeTimer), this._typeTimer = null;
1284
+ return;
1285
+ }
1286
+ if (this._isFullScreenWrite(this._typeQueue)) {
1287
+ this._flushTypeQueue(), this.renderer.flushRender(), this._autoScroll();
1288
+ return;
1289
+ }
1290
+ let e = 0, t = 0, n = this._typeQueue.indexOf("\n"), r = n === -1 ? this._typeQueue.length : n + 1, i = this._typeQueue.slice(0, r);
1291
+ if (this._isLineRewrite(i)) t = r;
1292
+ else for (; t < this._typeQueue.length && e < this._animateSpeed;) if (this._typeQueue.charCodeAt(t) === 27) if (t++, this._typeQueue[t] === "[") {
1293
+ for (t++; t < this._typeQueue.length && this._typeQueue.charCodeAt(t) < 64;) t++;
1294
+ t < this._typeQueue.length && t++;
1295
+ } else if (this._typeQueue[t] === "]") {
1296
+ for (t++; t < this._typeQueue.length && this._typeQueue.charCodeAt(t) !== 7;) {
1297
+ if (this._typeQueue.charCodeAt(t) === 27 && this._typeQueue[t + 1] === "\\") {
1298
+ t += 2;
1299
+ break;
1300
+ }
1301
+ t++;
1302
+ }
1303
+ t < this._typeQueue.length && this._typeQueue.charCodeAt(t) === 7 && t++;
1304
+ } else t < this._typeQueue.length && t++;
1305
+ else t++, e++;
1306
+ let a = this._typeQueue.slice(t);
1307
+ a && !a.includes("\n") && this._isLineRewrite(a) && (t = this._typeQueue.length);
1308
+ let o = this._typeQueue.slice(0, t);
1309
+ this._typeQueue = this._typeQueue.slice(t), this.parser.write(o), this.renderer.flushRender(), this._autoScroll();
1310
+ }, 10));
626
1311
  }
627
- _exitInteractiveMode() {
628
- this._interactive = !1, this._screen.style.display = "none", this._screen.innerHTML = "", this._cmdList.style.display = "";
1312
+ _setupDom(e, t = "Roboto Mono") {
1313
+ if (typeof document > "u") return;
1314
+ let n = document.createElement("link");
1315
+ n.href = `https://fonts.googleapis.com/css?family=${encodeURIComponent(t)}:ital,wght@0,400;0,700;1,400;1,700&display=swap`, n.rel = "stylesheet", document.head && document.head.appendChild && document.head.appendChild(n), this._cmdList = document.createElement("ul"), this._cmdList.style.fontFamily = `'${t}', monospace`, this._cmdList.style.listStyle = "none", this._cmdList.style.padding = "0", this._cmdList.style.margin = "0", this._lineWrap && this._cmdList.classList.add("terminal--wrap"), e.appendChild(this._cmdList), this._charMeasure = document.createElement("span"), this._charMeasure.className = "terminal__char-measure", this._charMeasure.style.fontFamily = `'${t}', monospace`, this._charMeasure.textContent = "X", this._charMeasure.setAttribute("aria-hidden", "true"), e.appendChild(this._charMeasure), setTimeout(() => this._measureAndSetSize(), 0), document.fonts && document.fonts.ready && document.fonts.ready.then(() => this._measureAndSetSize()), typeof ResizeObserver < "u" && new ResizeObserver(() => this._measureAndSetSize()).observe(e);
629
1316
  }
630
- _renderInteractiveScreen(e) {
631
- let t = e.replace(/\x1b\[\d*J/g, "").replace(/\x1b\[\d+;\d+H/g, "").replace(/\x1b\[H/g, "");
632
- try {
633
- this._screen.innerHTML = this._ansi.ansi_to_html(t);
634
- } catch {
635
- this._screen.textContent = t;
1317
+ _listenForEvents() {
1318
+ if (!(typeof window > "u") && (this._onKeyDown = (e) => {
1319
+ if (e.metaKey) return;
1320
+ let t = this._encodeKey(e.key, {
1321
+ alt: !!e.altKey,
1322
+ ctrl: !!e.ctrlKey,
1323
+ shift: !!e.shiftKey,
1324
+ meta: !!e.metaKey
1325
+ });
1326
+ t && (e.preventDefault && e.preventDefault(), this._emitInput(t));
1327
+ }, this._onPaste = (e) => {
1328
+ let t = e.clipboardData || typeof window < "u" && window.clipboardData;
1329
+ if (!t || typeof t.getData != "function") return;
1330
+ let n = t.getData("text");
1331
+ if (!n) return;
1332
+ e.preventDefault && e.preventDefault();
1333
+ let r = n.replace(/\r?\n/g, "\r");
1334
+ this.parser.bracketedPasteMode ? this._emitInput(`\x1b[200~${r}\x1b[201~`) : this._emitInput(r);
1335
+ }, window.addEventListener("keydown", this._onKeyDown), window.addEventListener("paste", this._onPaste), this._el && typeof this._el.addEventListener == "function")) {
1336
+ let e = (e) => {
1337
+ if (!this._charMeasure || !this._el.getBoundingClientRect) return {
1338
+ col: 1,
1339
+ row: 1
1340
+ };
1341
+ let t = this._charMeasure.getBoundingClientRect(), n = this._el.getBoundingClientRect(), r = t.width || 8, i = t.height || 16;
1342
+ return {
1343
+ col: Math.max(1, Math.min(this.cols, Math.floor(((e.clientX || 0) - n.left) / r) + 1)),
1344
+ row: Math.max(1, Math.min(this.rows, Math.floor(((e.clientY || 0) - n.top) / i) + 1))
1345
+ };
1346
+ };
1347
+ this._el.addEventListener("mousedown", (t) => {
1348
+ let n = t.target;
1349
+ if (n && n.tagName === "A" && (this.parser.mouseTrackingMode === 0 || t.metaKey || t.ctrlKey)) return;
1350
+ let { col: r, row: i } = e(t);
1351
+ this.sendMouse("mousedown", {
1352
+ button: t.button || 0,
1353
+ col: r,
1354
+ row: i,
1355
+ shift: !!t.shiftKey,
1356
+ alt: !!t.altKey,
1357
+ ctrl: !!t.ctrlKey
1358
+ }) && t.preventDefault && t.preventDefault();
1359
+ }), this._el.addEventListener("mouseup", (t) => {
1360
+ let { col: n, row: r } = e(t);
1361
+ this.sendMouse("mouseup", {
1362
+ button: t.button || 0,
1363
+ col: n,
1364
+ row: r,
1365
+ shift: !!t.shiftKey,
1366
+ alt: !!t.altKey,
1367
+ ctrl: !!t.ctrlKey
1368
+ }) && t.preventDefault && t.preventDefault();
1369
+ }), this._el.addEventListener("mousemove", (t) => {
1370
+ if (this.parser.mouseTrackingMode === 0) return;
1371
+ let { col: n, row: r } = e(t);
1372
+ this.sendMouse("mousemove", {
1373
+ button: t.button || 0,
1374
+ col: n,
1375
+ row: r,
1376
+ shift: !!t.shiftKey,
1377
+ alt: !!t.altKey,
1378
+ ctrl: !!t.ctrlKey
1379
+ });
1380
+ }), this._el.addEventListener("wheel", (t) => {
1381
+ let { col: n, row: r } = e(t);
1382
+ this.sendMouse("wheel", {
1383
+ col: n,
1384
+ row: r,
1385
+ deltaY: t.deltaY || 0,
1386
+ shift: !!t.shiftKey,
1387
+ alt: !!t.altKey,
1388
+ ctrl: !!t.ctrlKey
1389
+ }) && t.preventDefault && t.preventDefault();
1390
+ });
636
1391
  }
637
1392
  }
1393
+ _mobileInput() {
1394
+ if (typeof document > "u" || !this._el) return;
1395
+ let e = document.createElement("input");
1396
+ e.type = "text", e.style.position = "absolute", e.style.left = "-9999999px", this._el.appendChild(e), this._mobileInputBuffer = "", this._el.addEventListener("click", () => {
1397
+ typeof e.focus == "function" && e.focus();
1398
+ }), e.addEventListener("keypress", (t) => {
1399
+ t.key === "Enter" && (e.value = "", this._mobileInputBuffer = e.value);
1400
+ }), e.addEventListener("keyup", (t) => {
1401
+ if (t.key === "Unidentified") {
1402
+ if (e.value.length < this._mobileInputBuffer.length) this.sendKey("Backspace");
1403
+ else if (e.value.length > this._mobileInputBuffer.length) {
1404
+ let t = e.value[e.value.length - 1];
1405
+ this.sendKey(t);
1406
+ }
1407
+ this._mobileInputBuffer = e.value;
1408
+ }
1409
+ });
1410
+ }
1411
+ _autoScroll() {
1412
+ !this._el || typeof this._el.scrollTo != "function" || this._el.scrollTop + this._el.offsetHeight >= this._el.scrollHeight - 50 && this._el.scrollTo(0, this._el.scrollHeight);
1413
+ }
638
1414
  _measureAndSetSize() {
639
- if (!this._charMeasure || !this.cli) return;
1415
+ if (!this._charMeasure || !this._el || typeof this._el.getBoundingClientRect != "function") return;
640
1416
  let e = this._charMeasure.getBoundingClientRect(), t = this._el.getBoundingClientRect();
641
- if (!e.width || !e.height || !t.width) return;
642
- let n = Math.max(1, Math.floor(t.width / e.width)), r = Math.max(1, Math.floor(t.height / e.height));
643
- (n !== this._lastCols || r !== this._lastRows) && (this._lastCols = n, this._lastRows = r, this.cli.setTerminalSize(n, r));
1417
+ if (!e || !t || !e.width || !e.height || !t.width || e.width >= t.width && e.height >= t.height) return;
1418
+ let n = Math.max(1, Math.floor(t.width / e.width)), r = Math.max(1, Math.floor((t.height || e.height * 24) / e.height));
1419
+ (n !== this._lastCols || r !== this._lastRows) && this.resize(n, r);
1420
+ }
1421
+ destroy() {
1422
+ this._typeTimer !== null && (clearInterval(this._typeTimer), this._typeTimer = null), typeof window < "u" && window.removeEventListener && (this._onKeyDown && window.removeEventListener("keydown", this._onKeyDown), this._onPaste && window.removeEventListener("paste", this._onPaste)), this._dataListeners = [], this._resizeListeners = [], this._titleListeners = [];
644
1423
  }
645
1424
  };
646
- p.Terminal = p, typeof globalThis < "u" && (globalThis.Terminal = p);
1425
+ O.Terminal = O, O.spawnPty = T, typeof globalThis < "u" && (globalThis.Terminal = O);
647
1426
  //#endregion
648
- export { p as default };
1427
+ export { O as default };
649
1428
 
650
1429
  //# sourceMappingURL=terminal.es.js.map