@dkkoval/tui-preview 0.1.0 → 0.2.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.
package/dist/index.js CHANGED
@@ -1,178 +1,484 @@
1
- var te = Object.defineProperty;
2
- var ne = (t, e, n) => e in t ? te(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
3
- var O = (t, e, n) => ne(t, typeof e != "symbol" ? e + "" : e, n);
4
- import { jsxs as D, jsx as p } from "react/jsx-runtime";
5
- import { useMemo as re, useRef as P, useState as W, useEffect as C } from "react";
6
- const I = [
7
- "#000000",
8
- "#cd0000",
9
- "#00cd00",
10
- "#cdcd00",
11
- "#0000ee",
12
- "#cd00cd",
13
- "#00cdcd",
14
- "#e5e5e5",
15
- "#7f7f7f",
16
- "#ff0000",
17
- "#00ff00",
18
- "#ffff00",
19
- "#5c5cff",
20
- "#ff00ff",
21
- "#00ffff",
22
- "#ffffff"
23
- ];
24
- function V(t) {
25
- if (t < 16) return I[t];
26
- if (t < 232) {
27
- const n = t - 16, s = Math.floor(n / 36), a = Math.floor(n % 36 / 6), r = n % 6, i = (c) => c === 0 ? 0 : c * 40 + 55;
28
- return `rgb(${i(s)},${i(a)},${i(r)})`;
1
+ var it = Object.defineProperty;
2
+ var ot = (e, t, n) => t in e ? it(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var F = (e, t, n) => ot(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import { jsxs as O, jsx as H } from "react/jsx-runtime";
5
+ import { useMemo as at, useRef as P, useState as $, useEffect as N } from "react";
6
+ const S = 16, T = 80, tt = "/ghostty-vt.wasm", ct = 1, lt = 2, ht = 4, W = 8, K = 16, ut = 32, R = 64, Y = 128, ft = [
7
+ "black",
8
+ "red",
9
+ "green",
10
+ "yellow",
11
+ "blue",
12
+ "magenta",
13
+ "cyan",
14
+ "white",
15
+ "brightBlack",
16
+ "brightRed",
17
+ "brightGreen",
18
+ "brightYellow",
19
+ "brightBlue",
20
+ "brightMagenta",
21
+ "brightCyan",
22
+ "brightWhite"
23
+ ], wt = {
24
+ background: "#1a1b26",
25
+ foreground: "#a9b1d6",
26
+ cursor: "#c0caf5",
27
+ selectionBackground: "#33467c",
28
+ selectionForeground: "#c0caf5",
29
+ black: "#15161e",
30
+ red: "#f7768e",
31
+ green: "#9ece6a",
32
+ yellow: "#e0af68",
33
+ blue: "#7aa2f7",
34
+ magenta: "#bb9af7",
35
+ cyan: "#7dcfff",
36
+ white: "#a9b1d6",
37
+ brightBlack: "#414868",
38
+ brightRed: "#f7768e",
39
+ brightGreen: "#9ece6a",
40
+ brightYellow: "#e0af68",
41
+ brightBlue: "#7aa2f7",
42
+ brightMagenta: "#bb9af7",
43
+ brightCyan: "#7dcfff",
44
+ brightWhite: "#c0caf5"
45
+ };
46
+ class mt {
47
+ constructor(t, n) {
48
+ this.wasm = t, this.abi = n;
49
+ }
50
+ createTerminal(t, n, r) {
51
+ const o = this.wasm.ghostty_wasm_alloc_u8_array(this.abi.terminalConfigSize);
52
+ if (!o)
53
+ throw new Error("Failed to allocate terminal config.");
54
+ try {
55
+ const i = new DataView(this.wasm.memory.buffer);
56
+ let s = o;
57
+ i.setUint32(s, 1e4, !0), s += 4, i.setUint32(s, z(r.foreground), !0), s += 4, i.setUint32(s, z(r.background), !0), s += 4, i.setUint32(s, z(r.cursor), !0), s += 4;
58
+ for (const c of ft)
59
+ i.setUint32(s, z(r[c]), !0), s += 4;
60
+ const a = this.wasm.ghostty_terminal_new_with_config(t, n, o);
61
+ if (!a)
62
+ throw new Error("Failed to create libghostty terminal.");
63
+ return new dt(this.wasm, a, t, n, this.abi.cellSize);
64
+ } finally {
65
+ this.wasm.ghostty_wasm_free_u8_array(o, this.abi.terminalConfigSize);
66
+ }
29
67
  }
30
- const e = (t - 232) * 10 + 8;
31
- return `rgb(${e},${e},${e})`;
32
- }
33
- function q(t) {
34
- return t.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
35
68
  }
36
- function ie(t) {
37
- let e = null, n = null, s = !1, a = !1, r = !1;
38
- const i = [];
39
- let c = "", u = "";
40
- function h() {
41
- const o = [];
42
- return e && o.push(`color:${e}`), n && o.push(`background-color:${n}`), s && o.push("font-weight:bold"), a && o.push("font-style:italic"), r && o.push("text-decoration:underline"), o.join(";");
69
+ class dt {
70
+ constructor(t, n, r, o, i) {
71
+ F(this, "viewportPtr", 0);
72
+ F(this, "viewportLen", 0);
73
+ this.wasm = t, this.handle = n, this.cols = r, this.rows = o, this.cellSize = i;
74
+ }
75
+ write(t) {
76
+ const n = typeof t == "string" ? new TextEncoder().encode(t) : t;
77
+ if (n.length === 0) return;
78
+ const r = this.wasm.ghostty_wasm_alloc_u8_array(n.length);
79
+ if (!r)
80
+ throw new Error("Failed to allocate libghostty write buffer.");
81
+ try {
82
+ new Uint8Array(this.wasm.memory.buffer).set(n, r), this.wasm.ghostty_terminal_write(this.handle, r, n.length);
83
+ } finally {
84
+ this.wasm.ghostty_wasm_free_u8_array(r, n.length);
85
+ }
43
86
  }
44
- function m() {
45
- c && (i.push(u ? `<span style="${u}">${q(c)}</span>` : q(c)), c = "");
87
+ resize(t, n) {
88
+ t === this.cols && n === this.rows || (this.cols = t, this.rows = n, this.wasm.ghostty_terminal_resize(this.handle, t, n), this.releaseViewport());
46
89
  }
47
- function v(o) {
48
- m();
49
- let f = 0;
50
- for (; f < o.length; ) {
51
- const l = o[f];
52
- l === 0 ? (e = null, n = null, s = !1, a = !1, r = !1) : l === 1 ? s = !0 : l === 22 ? s = !1 : l === 3 ? a = !0 : l === 23 ? a = !1 : l === 4 ? r = !0 : l === 24 ? r = !1 : l >= 30 && l <= 37 ? e = I[l - 30] : l === 38 ? o[f + 1] === 2 && f + 4 < o.length ? (e = `rgb(${o[f + 2]},${o[f + 3]},${o[f + 4]})`, f += 4) : o[f + 1] === 5 && f + 2 < o.length && (e = V(o[f + 2]), f += 2) : l === 39 ? e = null : l >= 40 && l <= 47 ? n = I[l - 40] : l === 48 ? o[f + 1] === 2 && f + 4 < o.length ? (n = `rgb(${o[f + 2]},${o[f + 3]},${o[f + 4]})`, f += 4) : o[f + 1] === 5 && f + 2 < o.length && (n = V(o[f + 2]), f += 2) : l === 49 ? n = null : l >= 90 && l <= 97 ? e = I[l - 82] : l >= 100 && l <= 107 && (n = I[l - 92]), f++;
90
+ hasResponse() {
91
+ return this.wasm.ghostty_terminal_has_response(this.handle);
92
+ }
93
+ isDirty() {
94
+ return this.wasm.ghostty_render_state_update(this.handle) !== 0;
95
+ }
96
+ readResponse(t = 4096) {
97
+ const n = this.wasm.ghostty_wasm_alloc_u8_array(t);
98
+ if (!n)
99
+ throw new Error("Failed to allocate libghostty response buffer.");
100
+ try {
101
+ const r = this.wasm.ghostty_terminal_read_response(this.handle, n, t);
102
+ if (r <= 0) return null;
103
+ const o = new Uint8Array(this.wasm.memory.buffer, n, r);
104
+ return new TextDecoder().decode(o);
105
+ } finally {
106
+ this.wasm.ghostty_wasm_free_u8_array(n, t);
53
107
  }
54
- u = h();
55
108
  }
56
- let d = 0;
57
- for (; d < t.length; ) {
58
- const o = t[d];
59
- if (o === "\x1B" && d + 1 < t.length && t[d + 1] === "[") {
60
- const f = d + 2;
61
- let l = f;
62
- for (; l < t.length && (t.charCodeAt(l) < 64 || t.charCodeAt(l) > 126); )
63
- l++;
64
- if (l < t.length) {
65
- if (t[l] === "m") {
66
- const $ = t.slice(f, l), L = $ ? $.split(";").map((y) => parseInt(y, 10) || 0) : [0];
67
- v(L);
68
- }
69
- d = l + 1;
70
- } else
71
- d++;
72
- } else if (o === "\r")
73
- d++;
74
- else {
75
- const f = h();
76
- f !== u && (m(), u = f), c += o, d++;
109
+ getViewportData() {
110
+ const t = this.wasm.ghostty_render_state_get_cols(this.handle), n = this.wasm.ghostty_render_state_get_rows(this.handle);
111
+ this.cols = t, this.rows = n;
112
+ const r = Math.max(1, t * n * this.cellSize);
113
+ if ((r > this.viewportLen || this.viewportPtr === 0) && (this.releaseViewport(), this.viewportPtr = this.wasm.ghostty_wasm_alloc_u8_array(r), this.viewportLen = r, !this.viewportPtr))
114
+ throw new Error("Failed to allocate libghostty viewport buffer.");
115
+ const o = this.wasm.ghostty_render_state_get_viewport(this.handle, this.viewportPtr, this.viewportLen), i = new Uint8Array(o);
116
+ if (o > 0) {
117
+ const s = new Uint8Array(this.wasm.memory.buffer, this.viewportPtr, o);
118
+ i.set(s);
77
119
  }
120
+ return this.wasm.ghostty_render_state_mark_clean(this.handle), {
121
+ cols: t,
122
+ rows: n,
123
+ buffer: i
124
+ };
125
+ }
126
+ dispose() {
127
+ this.releaseViewport(), this.wasm.ghostty_terminal_free(this.handle);
128
+ }
129
+ releaseViewport() {
130
+ this.viewportPtr !== 0 && (this.wasm.ghostty_wasm_free_u8_array(this.viewportPtr, this.viewportLen), this.viewportPtr = 0, this.viewportLen = 0);
78
131
  }
79
- return m(), i.join("");
80
132
  }
81
- let j = null;
82
- function oe() {
83
- return j || (j = import("./ghostty-web-BfBVpf8G.js").then(async (t) => (await t.init(), t))), j;
133
+ class gt {
134
+ constructor(t, n, r, o, i, s) {
135
+ F(this, "ctx");
136
+ F(this, "dpr");
137
+ F(this, "metrics");
138
+ this.canvas = t, this.cols = n, this.rows = r, this.fontSize = o, this.fontFamily = i, this.theme = s;
139
+ const a = t.getContext("2d");
140
+ if (!a)
141
+ throw new Error("Failed to create 2D canvas context.");
142
+ this.ctx = a, this.dpr = window.devicePixelRatio || 1, this.metrics = this.measureFont(), this.resizeCanvas(n, r);
143
+ }
144
+ get cellSize() {
145
+ return { w: this.metrics.width, h: this.metrics.height };
146
+ }
147
+ render(t) {
148
+ (t.cols !== this.cols || t.rows !== this.rows) && (this.cols = t.cols, this.rows = t.rows, this.resizeCanvas(t.cols, t.rows));
149
+ const { cols: n, rows: r, buffer: o } = t, i = new DataView(o.buffer, o.byteOffset, o.byteLength), s = this.ctx, a = this.metrics.width, c = this.metrics.height;
150
+ s.fillStyle = this.theme.background, s.fillRect(0, 0, n * a, r * c);
151
+ for (let l = 0; l < r; l++)
152
+ for (let u = 0; u < n; u++) {
153
+ const h = (l * n + u) * S, f = i.getUint8(h + 11);
154
+ if (f === 0) continue;
155
+ const w = i.getUint8(h + 10), b = L(i, h + 4), y = L(i, h + 7), _ = x(w, K), g = x(w, W) ? D(b) : this.theme.foreground, d = x(w, R) ? D(y) : this.theme.background;
156
+ (_ || x(w, R)) && (s.fillStyle = _ ? g : d, s.fillRect(u * a, l * c, f * a, c));
157
+ }
158
+ for (let l = 0; l < r; l++)
159
+ for (let u = 0; u < n; u++) {
160
+ const h = (l * n + u) * S, f = i.getUint8(h + 11);
161
+ if (f === 0) continue;
162
+ const w = i.getUint8(h + 10);
163
+ if (x(w, ut)) continue;
164
+ const b = i.getUint32(h, !0);
165
+ if (b === 0) continue;
166
+ const y = x(w, K), _ = L(i, h + 4), g = L(i, h + 7), d = x(w, W) ? D(_) : this.theme.foreground, m = x(w, R) ? D(g) : this.theme.background;
167
+ s.fillStyle = y ? m : d;
168
+ let p = "";
169
+ x(w, lt) && (p += "italic "), x(w, ct) && (p += "bold "), s.font = `${p}${this.fontSize}px ${this.fontFamily}`, x(w, Y) && (s.globalAlpha = 0.5);
170
+ const E = Et(b);
171
+ if (s.fillText(E, u * a, l * c + this.metrics.baseline), x(w, Y) && (s.globalAlpha = 1), x(w, ht)) {
172
+ const v = l * c + this.metrics.baseline + 2;
173
+ s.strokeStyle = s.fillStyle, s.lineWidth = 1, s.beginPath(), s.moveTo(u * a, v), s.lineTo(u * a + f * a, v), s.stroke();
174
+ }
175
+ }
176
+ }
177
+ dispose() {
178
+ this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
179
+ }
180
+ measureFont() {
181
+ this.ctx.font = `${this.fontSize}px ${this.fontFamily}`;
182
+ const t = this.ctx.measureText("M"), n = Math.ceil(t.width), r = t.actualBoundingBoxAscent || this.fontSize * 0.8, o = t.actualBoundingBoxDescent || this.fontSize * 0.2;
183
+ return {
184
+ width: n,
185
+ height: Math.ceil(r + o) + 2,
186
+ baseline: Math.ceil(r) + 1
187
+ };
188
+ }
189
+ resizeCanvas(t, n) {
190
+ const r = t * this.metrics.width, o = n * this.metrics.height;
191
+ this.canvas.width = Math.max(1, Math.floor(r * this.dpr)), this.canvas.height = Math.max(1, Math.floor(o * this.dpr)), this.canvas.style.width = `${r}px`, this.canvas.style.height = `${o}px`, this.ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0), this.ctx.textBaseline = "alphabetic", this.ctx.textAlign = "left";
192
+ }
84
193
  }
85
- const T = { cols: 80, rows: 24 }, G = {}, se = [];
86
- function ce(t) {
87
- return "wasm" in t;
194
+ function et(e, t) {
195
+ const r = document.createElement("canvas").getContext("2d");
196
+ if (!r)
197
+ return {
198
+ w: Math.ceil(e * 0.6),
199
+ h: Math.ceil(e * 1.2)
200
+ };
201
+ r.font = `${e}px ${t}`;
202
+ const o = r.measureText("M"), i = Math.ceil(o.width), s = o.actualBoundingBoxAscent || e * 0.8, a = o.actualBoundingBoxDescent || e * 0.2;
203
+ return { w: i, h: Math.ceil(s + a) + 2 };
88
204
  }
89
- function Y(t) {
90
- const e = t ?? se;
91
- return typeof e == "function" ? e : () => e;
205
+ const Q = /* @__PURE__ */ new Map();
206
+ function yt(e = tt) {
207
+ const t = e.toString(), n = Q.get(t);
208
+ if (n)
209
+ return n;
210
+ const r = (async () => {
211
+ const o = await fetch(e);
212
+ if (!o.ok)
213
+ throw new Error(`Failed to load libghostty wasm: ${o.status} ${o.statusText}`);
214
+ const i = await o.arrayBuffer();
215
+ if (i.byteLength === 0)
216
+ throw new Error("libghostty wasm is empty.");
217
+ const s = await WebAssembly.compile(i), c = (await WebAssembly.instantiate(s, {
218
+ env: {
219
+ log: () => {
220
+ }
221
+ }
222
+ })).exports;
223
+ if (!c.memory || !c.ghostty_terminal_new || !c.ghostty_render_state_get_viewport)
224
+ throw new Error("Invalid libghostty wasm exports.");
225
+ return bt(c), new mt(c, {
226
+ cellSize: S,
227
+ terminalConfigSize: T
228
+ });
229
+ })();
230
+ return Q.set(t, r), r;
92
231
  }
93
- function le(t) {
94
- return t.cols !== void 0 || t.rows !== void 0 ? {
95
- fit: "none",
96
- size: {
97
- cols: Math.max(1, t.cols ?? T.cols),
98
- rows: Math.max(1, t.rows ?? T.rows)
232
+ async function _t(e) {
233
+ var _, g;
234
+ const t = await yt(e.wasmUrl ?? tt), n = { ...wt, ...e.theme };
235
+ let r, o;
236
+ const i = et(e.fontSize, e.fontFamily);
237
+ e.widthPx != null && e.heightPx != null ? (r = Math.max(1, Math.floor(e.widthPx / i.w)), o = Math.max(1, Math.floor(e.heightPx / i.h))) : (r = e.cols ?? 80, o = e.rows ?? 24), e.container.innerHTML = "";
238
+ const s = document.createElement("canvas");
239
+ s.style.display = "block", s.style.outline = "none", s.tabIndex = e.interactive ? 0 : -1, e.container.appendChild(s);
240
+ const a = t.createTerminal(r, o, n), c = new gt(
241
+ s,
242
+ r,
243
+ o,
244
+ e.fontSize,
245
+ e.fontFamily,
246
+ n
247
+ );
248
+ e.showCursor || a.write("\x1B[?25l"), c.render(a.getViewportData());
249
+ const l = ((_ = window.requestAnimationFrame) == null ? void 0 : _.bind(window)) ?? ((d) => window.setTimeout(d, 16)), u = ((g = window.cancelAnimationFrame) == null ? void 0 : g.bind(window)) ?? window.clearTimeout.bind(window);
250
+ let h = null, f = !1;
251
+ const w = () => {
252
+ h = null, !(f || !a.isDirty()) && c.render(a.getViewportData());
253
+ }, b = () => {
254
+ h !== null || f || (h = l(w));
255
+ }, y = e.interactive ? vt(s, (d) => {
256
+ var m;
257
+ return (m = e.onInput) == null ? void 0 : m.call(e, d);
258
+ }) : () => {
259
+ };
260
+ return {
261
+ cols: r,
262
+ rows: o,
263
+ cellSize: c.cellSize,
264
+ write(d) {
265
+ if (f) return;
266
+ const m = e.convertEol ? pt(d) : d;
267
+ a.write(m), b();
268
+ },
269
+ drainResponses() {
270
+ if (f) return [];
271
+ const d = [];
272
+ for (; a.hasResponse(); ) {
273
+ const m = a.readResponse();
274
+ if (!m) break;
275
+ d.push(m);
276
+ }
277
+ return d;
278
+ },
279
+ dispose() {
280
+ f = !0, h !== null && (u(h), h = null), y(), c.dispose(), a.dispose(), s.parentElement === e.container && e.container.removeChild(s);
99
281
  }
100
- } : { fit: "container", size: T };
282
+ };
101
283
  }
102
- let Z = !1;
103
- function ae(t) {
104
- !t || Z || (Z = !0, console.warn(
105
- "[tui-preview] Legacy props (`app`, `args`, `cols`, `rows`, `fontSize`, `fontFamily`, `theme`) are deprecated. Use `wasm`, `argv`, `fit`, `size`, and `terminal`."
106
- ));
284
+ function bt(e) {
285
+ const t = e.ghostty_wasm_alloc_u8_array(T);
286
+ if (!t)
287
+ throw new Error("Failed to allocate ABI probe config buffer.");
288
+ let n = 0, r = 0, o = 0, i = 0;
289
+ const s = 1122867, a = 4478310, [c, l, u] = X(s), [h, f, w] = X(a);
290
+ try {
291
+ new Uint8Array(e.memory.buffer, t, T).fill(0);
292
+ const b = new DataView(e.memory.buffer, t, T);
293
+ if (b.setUint32(0, 16, !0), b.setUint32(4, s, !0), b.setUint32(8, a, !0), b.setUint32(12, 7833753, !0), n = e.ghostty_terminal_new_with_config(2, 1, t), !n)
294
+ throw new Error("Failed to create ABI probe terminal.");
295
+ const y = new TextEncoder().encode("\x1B[7mX");
296
+ if (o = y.length, r = e.ghostty_wasm_alloc_u8_array(o), !r)
297
+ throw new Error("Failed to allocate ABI probe write buffer.");
298
+ if (new Uint8Array(e.memory.buffer, r, o).set(y), e.ghostty_terminal_write(n, r, o), i = e.ghostty_wasm_alloc_u8_array(S), !i)
299
+ throw new Error("Failed to allocate ABI probe viewport buffer.");
300
+ const _ = e.ghostty_render_state_get_viewport(n, i, S);
301
+ if (_ !== S)
302
+ throw new Error(
303
+ `Incompatible libghostty ABI: expected cell size ${S}, got ${_}.`
304
+ );
305
+ const g = new DataView(e.memory.buffer, i, S), d = g.getUint8(10), m = x(d, W), p = x(d, R), E = g.getUint8(4) === c && g.getUint8(5) === l && g.getUint8(6) === u, v = g.getUint8(7) === h && g.getUint8(8) === f && g.getUint8(9) === w;
306
+ if (!m || !p || !E || !v)
307
+ throw new Error("Incompatible libghostty ABI: terminal config layout mismatch.");
308
+ } finally {
309
+ i && e.ghostty_wasm_free_u8_array(i, S), r && o > 0 && e.ghostty_wasm_free_u8_array(r, o), n && e.ghostty_terminal_free(n), e.ghostty_wasm_free_u8_array(t, T);
310
+ }
107
311
  }
108
- function ue(t) {
109
- var s, a, r, i, c, u, h, m, v;
110
- if (ce(t)) {
111
- const d = t.fit ?? (t.size ? "none" : "container"), o = d === "none" ? {
112
- cols: Math.max(1, ((s = t.size) == null ? void 0 : s.cols) ?? T.cols),
113
- rows: Math.max(1, ((a = t.size) == null ? void 0 : a.rows) ?? T.rows)
114
- } : {
115
- cols: Math.max(1, ((r = t.size) == null ? void 0 : r.cols) ?? T.cols),
116
- rows: Math.max(1, ((i = t.size) == null ? void 0 : i.rows) ?? T.rows)
117
- };
118
- return {
119
- wasm: t.wasm,
120
- env: t.env ?? G,
121
- interactive: t.interactive ?? !0,
122
- mode: t.mode ?? "terminal",
123
- fit: d,
124
- size: o,
125
- terminal: {
126
- fontSize: ((c = t.terminal) == null ? void 0 : c.fontSize) ?? 14,
127
- fontFamily: ((u = t.terminal) == null ? void 0 : u.fontFamily) ?? "monospace",
128
- cursorBlink: ((h = t.terminal) == null ? void 0 : h.cursorBlink) ?? !0,
129
- convertEol: ((m = t.terminal) == null ? void 0 : m.convertEol) ?? !0,
130
- theme: (v = t.terminal) == null ? void 0 : v.theme
131
- },
132
- resolveArgv: Y(t.argv),
133
- onExit: t.onExit,
134
- onError: t.onError,
135
- onStatusChange: t.onStatusChange,
136
- usedLegacyProps: !1
137
- };
312
+ function pt(e) {
313
+ return e.replace(/\r?\n/g, `\r
314
+ `);
315
+ }
316
+ function z(e) {
317
+ if (e.startsWith("#")) {
318
+ let i = e.slice(1);
319
+ i.length === 3 && (i = `${i[0]}${i[0]}${i[1]}${i[1]}${i[2]}${i[2]}`);
320
+ const s = Number.parseInt(i, 16);
321
+ return Number.isNaN(s) ? 0 : s;
138
322
  }
139
- const { fit: e, size: n } = le(t);
323
+ const t = e.match(/rgb\((\d+),\s*(\d+),\s*(\d+)\)/);
324
+ if (!t) return 0;
325
+ const n = Number.parseInt(t[1], 10), r = Number.parseInt(t[2], 10), o = Number.parseInt(t[3], 10);
326
+ return n << 16 | r << 8 | o;
327
+ }
328
+ function X(e) {
329
+ return [e >> 16 & 255, e >> 8 & 255, e & 255];
330
+ }
331
+ function L(e, t) {
140
332
  return {
141
- wasm: t.app,
142
- env: t.env ?? G,
143
- interactive: t.interactive ?? !0,
144
- mode: "terminal",
145
- fit: e,
333
+ r: e.getUint8(t),
334
+ g: e.getUint8(t + 1),
335
+ b: e.getUint8(t + 2)
336
+ };
337
+ }
338
+ function x(e, t) {
339
+ return (e & t) !== 0;
340
+ }
341
+ function D(e) {
342
+ return `rgb(${e.r}, ${e.g}, ${e.b})`;
343
+ }
344
+ function Et(e) {
345
+ try {
346
+ return String.fromCodePoint(e);
347
+ } catch {
348
+ return " ";
349
+ }
350
+ }
351
+ function vt(e, t) {
352
+ const n = () => e.focus(), r = (i) => {
353
+ var a;
354
+ const s = (a = i.clipboardData) == null ? void 0 : a.getData("text");
355
+ s && (i.preventDefault(), t(s));
356
+ }, o = (i) => {
357
+ const s = xt(i);
358
+ s && (i.preventDefault(), t(s));
359
+ };
360
+ return e.addEventListener("mousedown", n), e.addEventListener("paste", r), e.addEventListener("keydown", o), () => {
361
+ e.removeEventListener("mousedown", n), e.removeEventListener("paste", r), e.removeEventListener("keydown", o);
362
+ };
363
+ }
364
+ function xt(e) {
365
+ if (e.isComposing || e.metaKey) return null;
366
+ let t = null;
367
+ switch (e.key) {
368
+ case "Enter":
369
+ t = "\r";
370
+ break;
371
+ case "Backspace":
372
+ t = "";
373
+ break;
374
+ case "Tab":
375
+ t = e.shiftKey ? "\x1B[Z" : " ";
376
+ break;
377
+ case "Escape":
378
+ t = "\x1B";
379
+ break;
380
+ case "ArrowUp":
381
+ t = "\x1B[A";
382
+ break;
383
+ case "ArrowDown":
384
+ t = "\x1B[B";
385
+ break;
386
+ case "ArrowRight":
387
+ t = "\x1B[C";
388
+ break;
389
+ case "ArrowLeft":
390
+ t = "\x1B[D";
391
+ break;
392
+ case "Home":
393
+ t = "\x1B[H";
394
+ break;
395
+ case "End":
396
+ t = "\x1B[F";
397
+ break;
398
+ case "Delete":
399
+ t = "\x1B[3~";
400
+ break;
401
+ case "PageUp":
402
+ t = "\x1B[5~";
403
+ break;
404
+ case "PageDown":
405
+ t = "\x1B[6~";
406
+ break;
407
+ }
408
+ return !t && e.ctrlKey && (t = At(e.key)), !t && e.key.length === 1 && !e.ctrlKey && (t = e.key), t ? e.altKey && !t.startsWith("\x1B") ? `\x1B${t}` : t : null;
409
+ }
410
+ function At(e) {
411
+ if (e.length !== 1) return null;
412
+ const t = e.toUpperCase();
413
+ if (t >= "A" && t <= "Z")
414
+ return String.fromCharCode(t.charCodeAt(0) - 64);
415
+ switch (e) {
416
+ case "@":
417
+ case " ":
418
+ return "\0";
419
+ case "[":
420
+ return "\x1B";
421
+ case "\\":
422
+ return "";
423
+ case "]":
424
+ return "";
425
+ case "^":
426
+ return "";
427
+ case "_":
428
+ return "";
429
+ default:
430
+ return null;
431
+ }
432
+ }
433
+ const Z = { cols: 80, rows: 24 }, Ut = {}, St = [];
434
+ function Ft(e) {
435
+ return e === "static" ? "static" : "interactive";
436
+ }
437
+ function Bt(e) {
438
+ const t = e ?? St;
439
+ return typeof t == "function" ? t : () => t;
440
+ }
441
+ function Ct(e) {
442
+ var o, i, s, a, c, l, u;
443
+ const t = e.fit ?? (e.size ? "none" : "container"), n = {
444
+ cols: Math.max(1, ((o = e.size) == null ? void 0 : o.cols) ?? Z.cols),
445
+ rows: Math.max(1, ((i = e.size) == null ? void 0 : i.rows) ?? Z.rows)
446
+ }, r = Ft(e.mode);
447
+ return {
448
+ wasm: e.wasm,
449
+ env: e.env ?? Ut,
450
+ interactive: r === "static" ? !1 : e.interactive ?? !0,
451
+ mode: r,
452
+ fit: t,
146
453
  size: n,
147
454
  terminal: {
148
- fontSize: t.fontSize ?? 14,
149
- fontFamily: t.fontFamily ?? "monospace",
150
- cursorBlink: !0,
151
- convertEol: !0,
152
- theme: t.theme
455
+ fontSize: ((s = e.terminal) == null ? void 0 : s.fontSize) ?? 14,
456
+ fontFamily: ((a = e.terminal) == null ? void 0 : a.fontFamily) ?? "monospace",
457
+ wasmUrl: (c = e.terminal) == null ? void 0 : c.wasmUrl,
458
+ convertEol: ((l = e.terminal) == null ? void 0 : l.convertEol) ?? !0,
459
+ theme: (u = e.terminal) == null ? void 0 : u.theme
153
460
  },
154
- resolveArgv: Y(t.args),
155
- onExit: t.onExit,
156
- onError: t.onError,
157
- onStatusChange: t.onStatusChange,
158
- usedLegacyProps: !0
461
+ resolveArgv: Bt(e.argv),
462
+ onExit: e.onExit,
463
+ onError: e.onError,
464
+ onStatusChange: e.onStatusChange
159
465
  };
160
466
  }
161
- const z = 0, fe = 6, B = 8, he = 0, J = 1, de = 2;
162
- class K {
163
- constructor(e) {
164
- O(this, "inputQueue", []);
165
- O(this, "memory");
166
- this.opts = e;
467
+ const A = 0, Tt = 6, C = 8, Mt = 1, kt = 0, j = 1, It = 2;
468
+ class zt {
469
+ constructor(t) {
470
+ F(this, "inputQueue", []);
471
+ F(this, "memory");
472
+ this.opts = t;
167
473
  }
168
474
  /** Push keyboard data from the terminal into the app's stdin */
169
- pushInput(e) {
170
- const n = typeof e == "string" ? new TextEncoder().encode(e) : e;
475
+ pushInput(t) {
476
+ const n = typeof t == "string" ? new TextEncoder().encode(t) : t;
171
477
  this.inputQueue.push(n);
172
478
  }
173
479
  /** Attach the WASM instance's memory after instantiation */
174
- attachMemory(e) {
175
- this.memory = e;
480
+ attachMemory(t) {
481
+ this.memory = t;
176
482
  }
177
483
  view() {
178
484
  return new DataView(this.memory.buffer);
@@ -183,333 +489,261 @@ class K {
183
489
  // ── WASI imports object ────────────────────────────────────────────────
184
490
  get imports() {
185
491
  return {
186
- args_sizes_get: (e, n) => {
187
- const { args: s } = this.opts, a = new TextEncoder(), r = s.reduce((i, c) => i + a.encode(c).length + 1, 0);
188
- return this.view().setUint32(e, s.length, !0), this.view().setUint32(n, r, !0), z;
492
+ args_sizes_get: (t, n) => {
493
+ const { args: r } = this.opts, o = new TextEncoder(), i = r.reduce((s, a) => s + o.encode(a).length + 1, 0);
494
+ return this.view().setUint32(t, r.length, !0), this.view().setUint32(n, i, !0), A;
189
495
  },
190
- args_get: (e, n) => {
191
- const s = new TextEncoder(), a = this.u8(), r = this.view();
192
- let i = n;
193
- return this.opts.args.forEach((c, u) => {
194
- const h = s.encode(c);
195
- a.set(h, i), a[i + h.length] = 0, r.setUint32(e + u * 4, i, !0), i += h.length + 1;
196
- }), z;
496
+ args_get: (t, n) => {
497
+ const r = new TextEncoder(), o = this.u8(), i = this.view();
498
+ let s = n;
499
+ return this.opts.args.forEach((a, c) => {
500
+ const l = r.encode(a);
501
+ o.set(l, s), o[s + l.length] = 0, i.setUint32(t + c * 4, s, !0), s += l.length + 1;
502
+ }), A;
197
503
  },
198
- environ_sizes_get: (e, n) => {
199
- const s = this.envEntries(), a = new TextEncoder(), r = s.reduce((i, c) => i + a.encode(c).length + 1, 0);
200
- return this.view().setUint32(e, s.length, !0), this.view().setUint32(n, r, !0), z;
504
+ environ_sizes_get: (t, n) => {
505
+ const r = this.envEntries(), o = new TextEncoder(), i = r.reduce((s, a) => s + o.encode(a).length + 1, 0);
506
+ return this.view().setUint32(t, r.length, !0), this.view().setUint32(n, i, !0), A;
201
507
  },
202
- environ_get: (e, n) => {
203
- const s = new TextEncoder(), a = this.u8(), r = this.view();
204
- let i = n;
205
- return this.envEntries().forEach((c, u) => {
206
- const h = s.encode(c);
207
- a.set(h, i), a[i + h.length] = 0, r.setUint32(e + u * 4, i, !0), i += h.length + 1;
208
- }), z;
508
+ environ_get: (t, n) => {
509
+ const r = new TextEncoder(), o = this.u8(), i = this.view();
510
+ let s = n;
511
+ return this.envEntries().forEach((a, c) => {
512
+ const l = r.encode(a);
513
+ o.set(l, s), o[s + l.length] = 0, i.setUint32(t + c * 4, s, !0), s += l.length + 1;
514
+ }), A;
209
515
  },
210
- fd_write: (e, n, s, a) => {
211
- if (e !== J && e !== de) return B;
212
- const r = this.view(), i = this.u8();
213
- let c = 0;
214
- const u = [];
215
- for (let v = 0; v < s; v++) {
216
- const d = r.getUint32(n + v * 8, !0), o = r.getUint32(n + v * 8 + 4, !0);
217
- u.push(i.slice(d, d + o)), c += o;
516
+ fd_write: (t, n, r, o) => {
517
+ if (t !== j && t !== It) return C;
518
+ const i = this.view(), s = this.u8();
519
+ let a = 0;
520
+ const c = [];
521
+ for (let h = 0; h < r; h++) {
522
+ const f = i.getUint32(n + h * 8, !0), w = i.getUint32(n + h * 8 + 4, !0);
523
+ c.push(s.slice(f, f + w)), a += w;
218
524
  }
219
- const h = new Uint8Array(c);
220
- let m = 0;
221
- for (const v of u)
222
- h.set(v, m), m += v.length;
223
- return e === J ? this.opts.stdout(h) : this.opts.stderr(h), r.setUint32(a, c, !0), z;
224
- },
225
- fd_read: (e, n, s, a) => {
226
- if (e !== he) return B;
227
- const r = this.inputQueue.shift();
228
- if (!r) return fe;
229
- const i = this.view(), c = this.u8();
525
+ const l = new Uint8Array(a);
230
526
  let u = 0;
231
- for (let h = 0; h < s && u < r.length; h++) {
232
- const m = i.getUint32(n + h * 8, !0), v = i.getUint32(n + h * 8 + 4, !0), d = Math.min(v, r.length - u);
233
- c.set(r.subarray(u, u + d), m), u += d;
527
+ for (const h of c)
528
+ l.set(h, u), u += h.length;
529
+ return t === j ? this.opts.stdout(l) : this.opts.stderr(l), i.setUint32(o, a, !0), A;
530
+ },
531
+ fd_read: (t, n, r, o) => {
532
+ if (t !== kt) return C;
533
+ const i = this.inputQueue[0];
534
+ if (!i) return Tt;
535
+ const s = this.view(), a = this.u8();
536
+ let c = 0;
537
+ for (let l = 0; l < r && c < i.length; l++) {
538
+ const u = s.getUint32(n + l * 8, !0), h = s.getUint32(n + l * 8 + 4, !0), f = Math.min(h, i.length - c);
539
+ a.set(i.subarray(c, c + f), u), c += f;
234
540
  }
235
- return i.setUint32(a, u, !0), z;
541
+ return c >= i.length ? this.inputQueue.shift() : c > 0 && (this.inputQueue[0] = i.subarray(c)), s.setUint32(o, c, !0), A;
236
542
  },
237
- poll_oneoff: (e, n, s, a) => {
238
- const r = this.view();
239
- let i = 0;
240
- for (let c = 0; c < s; c++) {
241
- const u = e + c * 48, h = r.getUint8(u + 8);
242
- if (h === 0 && this.inputQueue.length > 0) {
243
- const m = n + i * 32;
244
- r.setBigUint64(m, r.getBigUint64(u, !0), !0), r.setUint16(m + 8, 0, !0), r.setUint8(m + 10, h), i++;
543
+ poll_oneoff: (t, n, r, o) => {
544
+ const i = this.view();
545
+ let s = 0;
546
+ for (let a = 0; a < r; a++) {
547
+ const c = t + a * 48, l = i.getUint8(c + 8);
548
+ if (l === Mt && this.inputQueue.length > 0) {
549
+ const u = n + s * 32;
550
+ i.setBigUint64(u, i.getBigUint64(c, !0), !0), i.setUint16(u + 8, 0, !0), i.setUint8(u + 10, l), s++;
245
551
  }
246
552
  }
247
- return r.setUint32(a, i, !0), z;
553
+ return i.setUint32(o, s, !0), A;
248
554
  },
249
- proc_exit: (e) => {
250
- throw this.opts.onExit(e), new ee(e);
555
+ proc_exit: (t) => {
556
+ throw this.opts.onExit(t), new nt(t);
251
557
  },
252
- random_get: (e, n) => (crypto.getRandomValues(new Uint8Array(this.memory.buffer, e, n)), z),
558
+ random_get: (t, n) => (crypto.getRandomValues(new Uint8Array(this.memory.buffer, t, n)), A),
253
559
  // Stubs for calls TUI apps may make but we don't need to implement.
254
- fd_close: () => z,
255
- fd_seek: () => z,
256
- fd_fdstat_get: (e, n) => (this.view().setUint8(n, e <= 2 ? 2 : 0), z),
257
- fd_prestat_get: () => B,
258
- fd_prestat_dir_name: () => B,
259
- path_open: () => B,
260
- sched_yield: () => z,
261
- clock_time_get: (e, n, s) => {
262
- const a = BigInt(Date.now()) * 1000000n;
263
- return this.view().setBigUint64(s, a, !0), z;
560
+ fd_close: () => A,
561
+ fd_seek: () => A,
562
+ fd_fdstat_get: (t, n) => (this.view().setUint8(n, t <= 2 ? 2 : 0), A),
563
+ fd_prestat_get: () => C,
564
+ fd_prestat_dir_name: () => C,
565
+ path_open: () => C,
566
+ sched_yield: () => A,
567
+ clock_time_get: (t, n, r) => {
568
+ const o = BigInt(Date.now()) * 1000000n;
569
+ return this.view().setBigUint64(r, o, !0), A;
264
570
  }
265
571
  };
266
572
  }
267
573
  envEntries() {
268
- const e = {
574
+ const t = {
269
575
  TERM: "xterm-256color",
270
576
  COLORTERM: "truecolor",
271
577
  ...this.opts.env
272
578
  };
273
- return Object.entries(e).map(([n, s]) => `${n}=${s}`);
579
+ return Object.entries(t).map(([n, r]) => `${n}=${r}`);
274
580
  }
275
581
  }
276
- class ee extends Error {
277
- constructor(e) {
278
- super(`WASI exit: ${e}`), this.code = e;
582
+ class nt extends Error {
583
+ constructor(t) {
584
+ super(`WASI exit: ${t}`), this.code = t;
279
585
  }
280
586
  }
281
- async function X(t, e) {
282
- const s = await (await fetch(t)).arrayBuffer(), a = await WebAssembly.compile(s), r = {
283
- wasi_snapshot_preview1: e.imports
284
- }, i = await WebAssembly.instantiate(a, r);
285
- e.attachMemory(i.exports.memory);
286
- const c = i.exports._start;
287
- if (!c) throw new Error("WASM module has no _start export");
587
+ const q = /* @__PURE__ */ new Map();
588
+ async function Lt(e, t) {
589
+ const n = e.toString();
590
+ let r = q.get(n);
591
+ if (!r) {
592
+ const a = await fetch(e);
593
+ if (!a.ok)
594
+ throw new Error(`Failed to load app wasm: ${a.status} ${a.statusText}`);
595
+ const c = await a.arrayBuffer();
596
+ if (c.byteLength === 0)
597
+ throw new Error("App wasm is empty.");
598
+ r = await WebAssembly.compile(c), q.set(n, r);
599
+ }
600
+ const o = {
601
+ wasi_snapshot_preview1: t.imports
602
+ }, i = await WebAssembly.instantiate(r, o);
603
+ t.attachMemory(i.exports.memory);
604
+ const s = i.exports._start;
605
+ if (!s) throw new Error("WASM module has no _start export");
288
606
  return {
289
607
  run: async () => {
290
608
  try {
291
- c();
292
- } catch (u) {
293
- if (!(u instanceof ee)) throw u;
609
+ s();
610
+ } catch (a) {
611
+ if (!(a instanceof nt)) throw a;
294
612
  }
295
613
  }
296
614
  };
297
615
  }
298
- function ve(t) {
299
- var $, L;
300
- const e = re(() => ue(t), [t]), n = P(null), s = P(null), a = P(null), [r, i] = W("loading"), [c, u] = W(""), h = P(null), [m, v] = W(e.size), [d, o] = W(null);
301
- C(() => {
302
- ae(e.usedLegacyProps);
303
- }, [e.usedLegacyProps]), C(() => {
304
- e.mode === "terminal" && v(e.size);
305
- }, [e.mode, e.fit, e.size.cols, e.size.rows]), C(() => {
306
- if (e.mode !== "terminal" || e.fit !== "container" || !n.current) return;
307
- const y = new ResizeObserver(([_]) => {
308
- var E, x;
309
- const { width: U, height: b } = _.contentRect;
310
- if (U > 0 && b > 0) {
311
- const w = ((E = h.current) == null ? void 0 : E.w) ?? e.terminal.fontSize * 0.6, g = ((x = h.current) == null ? void 0 : x.h) ?? e.terminal.fontSize * 1.2;
312
- v({
313
- cols: Math.max(1, Math.floor(U / w)),
314
- rows: Math.max(1, Math.floor(b / g))
315
- });
316
- }
616
+ function $t(e) {
617
+ var h;
618
+ const t = at(() => Ct(e), [e]), n = P(null), r = P(null), [o, i] = $("loading"), [s, a] = $(""), c = P(null), [l, u] = $(
619
+ t.fit === "container" ? null : t.size
620
+ );
621
+ return N(() => {
622
+ u(t.fit === "container" ? null : t.size);
623
+ }, [t.fit, t.size.cols, t.size.rows]), N(() => {
624
+ if (t.fit !== "container" || !n.current) return;
625
+ const f = n.current, w = et(t.terminal.fontSize, t.terminal.fontFamily), b = (g, d) => {
626
+ var M, k;
627
+ const m = ((M = c.current) == null ? void 0 : M.w) ?? w.w, p = ((k = c.current) == null ? void 0 : k.h) ?? w.h, E = Math.max(1, Math.floor(g / m)), v = Math.max(1, Math.floor(d / p));
628
+ u((B) => B && B.cols === E && B.rows === v ? B : { cols: E, rows: v });
629
+ }, y = f.getBoundingClientRect();
630
+ y.width > 0 && y.height > 0 && b(y.width, y.height);
631
+ const _ = new ResizeObserver(([g]) => {
632
+ const { width: d, height: m } = g.contentRect;
633
+ d > 0 && m > 0 && b(d, m);
317
634
  });
318
- return y.observe(n.current), () => y.disconnect();
319
- }, [e.mode, e.fit, e.terminal.fontSize]), C(() => {
320
- if (e.mode !== "terminal" || !m || !s.current) return;
321
- let y = !1;
322
- const _ = s.current, U = m, b = (w) => {
323
- var g;
324
- i(w), (g = e.onStatusChange) == null || g.call(e, w);
325
- }, E = (w) => {
326
- var g;
327
- b("error"), u(w instanceof Error ? w.message : String(w)), (g = e.onError) == null || g.call(e, w);
635
+ return _.observe(f), () => _.disconnect();
636
+ }, [t.fit, t.terminal.fontSize, t.terminal.fontFamily]), N(() => {
637
+ if (!l || !r.current) return;
638
+ let f = !1, w = null;
639
+ const b = r.current, y = l, _ = (m) => {
640
+ var p;
641
+ i(m), (p = t.onStatusChange) == null || p.call(t, m);
642
+ }, g = (m) => {
643
+ var p;
644
+ _("error"), a(m instanceof Error ? m.message : String(m)), (p = t.onError) == null || p.call(t, m);
328
645
  };
329
- b("loading"), u("");
330
- async function x() {
646
+ _("loading"), a("");
647
+ async function d() {
331
648
  try {
332
- const w = await oe();
333
- if (y) return;
334
- _.innerHTML = "";
335
- const g = new w.Terminal({
336
- cols: U.cols,
337
- rows: U.rows,
338
- fontSize: e.terminal.fontSize,
339
- fontFamily: e.terminal.fontFamily,
340
- theme: e.terminal.theme,
341
- disableStdin: !e.interactive,
342
- cursorBlink: e.terminal.cursorBlink,
343
- convertEol: e.terminal.convertEol
649
+ let m = y.cols, p = y.rows, E = null;
650
+ const v = await _t({
651
+ container: b,
652
+ cols: y.cols,
653
+ rows: y.rows,
654
+ fontSize: t.terminal.fontSize,
655
+ fontFamily: t.terminal.fontFamily,
656
+ theme: t.terminal.theme,
657
+ convertEol: t.terminal.convertEol,
658
+ interactive: t.mode !== "static" && t.interactive,
659
+ showCursor: t.mode !== "static",
660
+ wasmUrl: t.terminal.wasmUrl,
661
+ onInput: (U) => E == null ? void 0 : E.pushInput(U)
344
662
  });
345
- a.current = g, g.open(_);
346
- let A = g.cols, M = g.rows;
347
- if (e.fit === "container") {
348
- const S = new w.FitAddon();
349
- g.loadAddon(S), S.fit(), A = g.cols, M = g.rows, n.current && A > 0 && M > 0 && (h.current = {
350
- w: n.current.clientWidth / A,
351
- h: n.current.clientHeight / M
352
- });
663
+ if (f) {
664
+ v.dispose();
665
+ return;
353
666
  }
354
- const H = e.resolveArgv({ cols: A, rows: M }), R = new TextDecoder(), F = new K({
355
- args: [e.wasm.toString(), ...H],
356
- env: e.env,
357
- stdout: (S) => g.write(R.decode(S)),
358
- stderr: (S) => g.write(R.decode(S)),
359
- onExit: (S) => {
360
- var Q;
361
- y || (b("exited"), (Q = e.onExit) == null || Q.call(e, S));
667
+ w = () => v.dispose(), m = v.cols, p = v.rows, c.current = v.cellSize;
668
+ const M = t.resolveArgv({ cols: m, rows: p }), k = new TextDecoder(), B = new TextDecoder(), G = (U, I) => {
669
+ const V = I.decode(U, { stream: !0 });
670
+ V && v.write(V);
671
+ for (const st of v.drainResponses())
672
+ E == null || E.pushInput(st);
673
+ };
674
+ E = new zt({
675
+ args: [t.wasm.toString(), ...M],
676
+ env: t.env,
677
+ stdout: (U) => G(U, k),
678
+ stderr: (U) => G(U, B),
679
+ onExit: (U) => {
680
+ var I;
681
+ f || (_("exited"), (I = t.onExit) == null || I.call(t, U));
362
682
  }
363
683
  });
364
- e.interactive && g.onData((S) => F.pushInput(S));
365
- const k = await X(e.wasm, F);
366
- if (y) return;
367
- b("running"), queueMicrotask(() => {
368
- y || k.run().catch((S) => {
369
- y || E(S);
684
+ const rt = await Lt(t.wasm, E);
685
+ if (f) return;
686
+ _("running"), queueMicrotask(() => {
687
+ f || rt.run().catch((U) => {
688
+ f || g(U);
370
689
  });
371
690
  });
372
- } catch (w) {
373
- y || E(w);
691
+ } catch (m) {
692
+ f || g(m);
374
693
  }
375
694
  }
376
- return x(), () => {
377
- var w;
378
- y = !0, (w = a.current) == null || w.dispose(), a.current = null;
695
+ return d(), () => {
696
+ f = !0, w == null || w(), w = null;
379
697
  };
380
698
  }, [
381
- e.mode,
382
- m,
383
- e.wasm,
384
- e.resolveArgv,
385
- e.env,
386
- e.fit,
387
- e.interactive,
388
- e.onExit,
389
- e.onError,
390
- e.onStatusChange,
391
- e.terminal.fontSize,
392
- e.terminal.fontFamily,
393
- e.terminal.theme,
394
- e.terminal.cursorBlink,
395
- e.terminal.convertEol
396
- ]), C(() => {
397
- if (e.mode !== "static") return;
398
- let y = !1;
399
- const _ = (b) => {
400
- var E;
401
- i(b), (E = e.onStatusChange) == null || E.call(e, b);
402
- };
403
- _("loading"), u(""), o(null);
404
- async function U() {
405
- var b;
406
- try {
407
- const E = new TextDecoder(), x = [], w = new K({
408
- args: [e.wasm.toString(), ...e.resolveArgv(e.size)],
409
- env: e.env,
410
- stdout: (A) => x.push(new Uint8Array(A)),
411
- stderr: () => {
412
- },
413
- onExit: (A) => {
414
- var M;
415
- if (!y) {
416
- const H = x.reduce((k, S) => k + S.length, 0), R = new Uint8Array(H);
417
- let F = 0;
418
- for (const k of x)
419
- R.set(k, F), F += k.length;
420
- o(ie(E.decode(R))), _("exited"), (M = e.onExit) == null || M.call(e, A);
421
- }
422
- }
423
- }), g = await X(e.wasm, w);
424
- if (y) return;
425
- _("running"), await g.run();
426
- } catch (E) {
427
- y || (_("error"), u(E instanceof Error ? E.message : String(E)), (b = e.onError) == null || b.call(e, E));
428
- }
429
- }
430
- return U(), () => {
431
- y = !0;
432
- };
433
- }, [
434
- e.mode,
435
- e.wasm,
436
- e.resolveArgv,
437
- e.env,
438
- e.size.cols,
439
- e.size.rows,
440
- e.onExit,
441
- e.onError,
442
- e.onStatusChange
443
- ]);
444
- const f = (($ = e.terminal.theme) == null ? void 0 : $.background) ?? "#1a1b26", l = ((L = e.terminal.theme) == null ? void 0 : L.foreground) ?? "#a9b1d6";
445
- return e.mode === "static" ? /* @__PURE__ */ D(
446
- "div",
447
- {
448
- className: t.className,
449
- style: {
450
- position: "relative",
451
- background: f,
452
- borderRadius: 6,
453
- overflow: "hidden",
454
- ...t.style
455
- },
456
- children: [
457
- r === "loading" && /* @__PURE__ */ p("div", { style: N, children: "Loading…" }),
458
- r === "error" && /* @__PURE__ */ D("div", { style: { ...N, color: "#f7768e" }, children: [
459
- "Error: ",
460
- c
461
- ] }),
462
- d !== null && /* @__PURE__ */ p(
463
- "pre",
464
- {
465
- style: {
466
- margin: 0,
467
- padding: "0.5em",
468
- fontFamily: e.terminal.fontFamily,
469
- fontSize: e.terminal.fontSize,
470
- color: l,
471
- lineHeight: 1.2,
472
- background: "transparent",
473
- overflow: "auto"
474
- },
475
- dangerouslySetInnerHTML: { __html: d }
476
- }
477
- )
478
- ]
479
- }
480
- ) : /* @__PURE__ */ D(
699
+ t.mode,
700
+ l,
701
+ t.wasm,
702
+ t.resolveArgv,
703
+ t.env,
704
+ t.fit,
705
+ t.interactive,
706
+ t.onExit,
707
+ t.onError,
708
+ t.onStatusChange,
709
+ t.terminal.fontSize,
710
+ t.terminal.fontFamily,
711
+ t.terminal.theme,
712
+ t.terminal.wasmUrl,
713
+ t.terminal.convertEol
714
+ ]), /* @__PURE__ */ O(
481
715
  "div",
482
716
  {
483
717
  ref: n,
484
- className: t.className,
718
+ className: e.className,
485
719
  style: {
486
720
  position: "relative",
487
- display: "inline-block",
488
- background: f,
721
+ display: t.fit === "container" ? "block" : "inline-block",
722
+ background: ((h = t.terminal.theme) == null ? void 0 : h.background) ?? "#1a1b26",
489
723
  borderRadius: 6,
490
724
  overflow: "hidden",
491
- ...t.style
725
+ ...e.style
492
726
  },
493
727
  children: [
494
- /* @__PURE__ */ p("div", { ref: s, style: { display: r === "error" ? "none" : void 0 } }),
495
- r === "loading" && /* @__PURE__ */ p("div", { style: N, children: "Loading…" }),
496
- r === "error" && /* @__PURE__ */ D("div", { style: { ...N, color: "#f7768e" }, children: [
728
+ /* @__PURE__ */ H("div", { ref: r, style: { display: o === "error" ? "none" : void 0 } }),
729
+ o === "loading" && /* @__PURE__ */ H("div", { style: J, children: "Loading…" }),
730
+ o === "error" && /* @__PURE__ */ O("div", { style: { ...J, color: "#f7768e" }, children: [
497
731
  "Error: ",
498
- c
732
+ s
499
733
  ] })
500
734
  ]
501
735
  }
502
736
  );
503
737
  }
504
- const N = {
738
+ const J = {
505
739
  padding: "1rem",
506
740
  fontFamily: "monospace",
507
741
  fontSize: 14,
508
742
  color: "#a9b1d6"
509
743
  };
510
744
  export {
511
- ve as TuiPreview,
512
- K as WasiBridge,
513
- ee as WasiExitError,
514
- X as instantiateApp
745
+ $t as TuiPreview,
746
+ zt as WasiBridge,
747
+ nt as WasiExitError,
748
+ Lt as instantiateApp
515
749
  };