afield 0.0.0-stage → 0.1.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/LICENSE +15 -0
- package/README.md +165 -2
- package/afield +214 -0
- package/package.json +42 -4
- package/pi-extensions/afield-session.ts +59 -0
- package/pi-extensions/elastic-footer.ts +167 -0
- package/server/attach.js +264 -0
- package/server/auth.js +169 -0
- package/server/client.js +64 -0
- package/server/control.js +65 -0
- package/server/identity.js +45 -0
- package/server/index.js +375 -0
- package/server/mirror.js +159 -0
- package/server/origin.js +51 -0
- package/server/picker.js +187 -0
- package/server/places.js +348 -0
- package/server/protocol.js +51 -0
- package/server/quick-tunnel.js +40 -0
- package/server/registry.js +190 -0
- package/server/serve.js +100 -0
- package/server/session-main.js +27 -0
- package/server/session.js +112 -0
- package/server/supervisor.js +432 -0
- package/server/viewer.js +65 -0
- package/src/app.js +1683 -0
- package/src/apple-touch-icon.png +0 -0
- package/src/index.html +96 -0
- package/src/keys.js +59 -0
- package/src/pi-icon.svg +13 -0
- package/src/style.css +559 -0
- package/src/transport.js +153 -0
- package/src/ttyd.js +51 -0
- package/src/viewport.js +168 -0
- package/tsconfig.json +14 -0
- package/vendor/wterm/LICENSE +191 -0
- package/vendor/wterm/README.md +7 -0
- package/vendor/wterm/packages/@wterm/dom/src/debug.ts +347 -0
- package/vendor/wterm/packages/@wterm/dom/src/hyperlink.ts +8 -0
- package/vendor/wterm/packages/@wterm/dom/src/index.ts +13 -0
- package/vendor/wterm/packages/@wterm/dom/src/input.ts +662 -0
- package/vendor/wterm/packages/@wterm/dom/src/renderer.ts +725 -0
- package/vendor/wterm/packages/@wterm/dom/src/terminal.css +195 -0
- package/vendor/wterm/packages/@wterm/dom/src/wterm.ts +609 -0
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
// Modified for afield from upstream wterm; see vendor/wterm/README.md.
|
|
2
|
+
import type { CellData, TerminalCore } from "@wterm/core";
|
|
3
|
+
|
|
4
|
+
const DEFAULT_COLOR = 256;
|
|
5
|
+
const FLAG_BOLD = 0x01;
|
|
6
|
+
const FLAG_DIM = 0x02;
|
|
7
|
+
const FLAG_ITALIC = 0x04;
|
|
8
|
+
const FLAG_UNDERLINE = 0x08;
|
|
9
|
+
const FLAG_REVERSE = 0x20;
|
|
10
|
+
const FLAG_INVISIBLE = 0x40;
|
|
11
|
+
const FLAG_STRIKETHROUGH = 0x80;
|
|
12
|
+
const DEFAULT_SCROLLBACK_OVERSCAN_ROWS = 10;
|
|
13
|
+
|
|
14
|
+
export interface RenderViewport {
|
|
15
|
+
scrollTop: number;
|
|
16
|
+
clientHeight: number;
|
|
17
|
+
rowHeight: number;
|
|
18
|
+
overscanRows?: number;
|
|
19
|
+
scrollbackDiscardedCount?: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function rgbToCSS(packed: number): string {
|
|
23
|
+
const r = (packed >> 16) & 0xff;
|
|
24
|
+
const g = (packed >> 8) & 0xff;
|
|
25
|
+
const b = packed & 0xff;
|
|
26
|
+
return `rgb(${r},${g},${b})`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function colorToCSS(index: number): string | null {
|
|
30
|
+
if (index === DEFAULT_COLOR) return null;
|
|
31
|
+
if (index < 16) return `var(--term-color-${index})`;
|
|
32
|
+
if (index < 232) {
|
|
33
|
+
const n = index - 16;
|
|
34
|
+
const r = Math.floor(n / 36) * 51;
|
|
35
|
+
const g = (Math.floor(n / 6) % 6) * 51;
|
|
36
|
+
const b = (n % 6) * 51;
|
|
37
|
+
return `rgb(${r},${g},${b})`;
|
|
38
|
+
}
|
|
39
|
+
const level = (index - 232) * 10 + 8;
|
|
40
|
+
return `rgb(${level},${level},${level})`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function cellFgCSS(fg: number, fgRgb: number | undefined): string | null {
|
|
44
|
+
if (fgRgb !== undefined) return rgbToCSS(fgRgb);
|
|
45
|
+
return colorToCSS(fg);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function cellBgCSS(bg: number, bgRgb: number | undefined): string | null {
|
|
49
|
+
if (bgRgb !== undefined) return rgbToCSS(bgRgb);
|
|
50
|
+
return colorToCSS(bg);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function buildCellStyle(
|
|
54
|
+
fg: number,
|
|
55
|
+
bg: number,
|
|
56
|
+
flags: number,
|
|
57
|
+
fgRgb?: number,
|
|
58
|
+
bgRgb?: number,
|
|
59
|
+
): string {
|
|
60
|
+
let fgIdx = fg,
|
|
61
|
+
bgIdx = bg,
|
|
62
|
+
fgR = fgRgb,
|
|
63
|
+
bgR = bgRgb;
|
|
64
|
+
|
|
65
|
+
if (flags & FLAG_REVERSE) {
|
|
66
|
+
const tmpIdx = fgIdx;
|
|
67
|
+
fgIdx = bgIdx;
|
|
68
|
+
bgIdx = tmpIdx;
|
|
69
|
+
const tmpR = fgR;
|
|
70
|
+
fgR = bgR;
|
|
71
|
+
bgR = tmpR;
|
|
72
|
+
if (fgR === undefined && fgIdx === DEFAULT_COLOR) fgIdx = 0;
|
|
73
|
+
if (bgR === undefined && bgIdx === DEFAULT_COLOR) bgIdx = 7;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const fgCSS = cellFgCSS(fgIdx, fgR);
|
|
77
|
+
const bgCSS = cellBgCSS(bgIdx, bgR);
|
|
78
|
+
|
|
79
|
+
let style = "";
|
|
80
|
+
if (fgCSS) style += `color:${fgCSS};`;
|
|
81
|
+
if (bgCSS) style += `background:${bgCSS};`;
|
|
82
|
+
if (flags & FLAG_BOLD) style += "font-weight:bold;";
|
|
83
|
+
if (flags & FLAG_DIM) style += "opacity:0.5;";
|
|
84
|
+
if (flags & FLAG_ITALIC) style += "font-style:italic;";
|
|
85
|
+
|
|
86
|
+
const decorations: string[] = [];
|
|
87
|
+
if (flags & FLAG_UNDERLINE) decorations.push("underline");
|
|
88
|
+
if (flags & FLAG_STRIKETHROUGH) decorations.push("line-through");
|
|
89
|
+
if (decorations.length) style += `text-decoration:${decorations.join(" ")};`;
|
|
90
|
+
|
|
91
|
+
if (flags & FLAG_INVISIBLE) style += "visibility:hidden;";
|
|
92
|
+
return style;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function appendRun(parent: HTMLElement, text: string, style: string): void {
|
|
96
|
+
const span = document.createElement("span");
|
|
97
|
+
if (style) span.style.cssText = style;
|
|
98
|
+
span.textContent = text;
|
|
99
|
+
parent.appendChild(span);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function escapeHTML(text: string): string {
|
|
103
|
+
return text
|
|
104
|
+
.replace(/&/g, "&")
|
|
105
|
+
.replace(/</g, "<")
|
|
106
|
+
.replace(/>/g, ">")
|
|
107
|
+
.replace(/"/g, """);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function safeLinkHref(uri: string | undefined): string | undefined {
|
|
111
|
+
if (!uri) return undefined;
|
|
112
|
+
try {
|
|
113
|
+
const url = new URL(uri);
|
|
114
|
+
return url.protocol === "http:" || url.protocol === "https:"
|
|
115
|
+
? url.href
|
|
116
|
+
: undefined;
|
|
117
|
+
} catch {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function linkIdentity(cell: CellData): string {
|
|
123
|
+
if (!cell.linkUri) return "";
|
|
124
|
+
return cell.linkKey ?? `fallback\0${cell.linkId ?? ""}\0${cell.linkUri}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function resolveColors(
|
|
128
|
+
fg: number,
|
|
129
|
+
bg: number,
|
|
130
|
+
flags: number,
|
|
131
|
+
fgRgb?: number,
|
|
132
|
+
bgRgb?: number,
|
|
133
|
+
): { fg: string; bg: string } {
|
|
134
|
+
let fgIdx = fg,
|
|
135
|
+
bgIdx = bg,
|
|
136
|
+
fgR = fgRgb,
|
|
137
|
+
bgR = bgRgb;
|
|
138
|
+
|
|
139
|
+
if (flags & FLAG_REVERSE) {
|
|
140
|
+
[fgIdx, bgIdx] = [bgIdx, fgIdx];
|
|
141
|
+
[fgR, bgR] = [bgR, fgR];
|
|
142
|
+
if (fgR === undefined && fgIdx === DEFAULT_COLOR) fgIdx = 0;
|
|
143
|
+
if (bgR === undefined && bgIdx === DEFAULT_COLOR) bgIdx = 7;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
fg: cellFgCSS(fgIdx, fgR) || "var(--term-fg)",
|
|
147
|
+
bg: cellBgCSS(bgIdx, bgR) || "var(--term-bg)",
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Pixel-snapped vertical gradient stops keyed off `--term-row-height` so that
|
|
152
|
+
// every cell paints the eighth-block boundary on the same physical pixel —
|
|
153
|
+
// using raw percentages (e.g. `12.5%`) at the canonical 17px row-height
|
|
154
|
+
// resolves to 2.125px and the browser rounds it differently across cells,
|
|
155
|
+
// producing the per-cell jog Claude Code's horizontal-rule (`▔▔▔▔▔`) makes
|
|
156
|
+
// visible against the row immediately below.
|
|
157
|
+
const SNAP_1_8 = "round(calc(var(--term-row-height) * 0.125), 1px)";
|
|
158
|
+
const SNAP_2_8 = "round(calc(var(--term-row-height) * 0.25), 1px)";
|
|
159
|
+
const SNAP_3_8 = "round(calc(var(--term-row-height) * 0.375), 1px)";
|
|
160
|
+
const SNAP_4_8 = "round(calc(var(--term-row-height) * 0.5), 1px)";
|
|
161
|
+
const SNAP_5_8 = "round(calc(var(--term-row-height) * 0.625), 1px)";
|
|
162
|
+
const SNAP_6_8 = "round(calc(var(--term-row-height) * 0.75), 1px)";
|
|
163
|
+
const SNAP_7_8 = "round(calc(var(--term-row-height) * 0.875), 1px)";
|
|
164
|
+
|
|
165
|
+
function getBlockBackground(cp: number, fg: string, bg: string): string {
|
|
166
|
+
switch (cp) {
|
|
167
|
+
case 0x2580:
|
|
168
|
+
return `linear-gradient(${fg} ${SNAP_4_8},${bg} ${SNAP_4_8})`;
|
|
169
|
+
case 0x2581:
|
|
170
|
+
return `linear-gradient(${bg} ${SNAP_7_8},${fg} ${SNAP_7_8})`;
|
|
171
|
+
case 0x2582:
|
|
172
|
+
return `linear-gradient(${bg} ${SNAP_6_8},${fg} ${SNAP_6_8})`;
|
|
173
|
+
case 0x2583:
|
|
174
|
+
return `linear-gradient(${bg} ${SNAP_5_8},${fg} ${SNAP_5_8})`;
|
|
175
|
+
case 0x2584:
|
|
176
|
+
return `linear-gradient(${bg} ${SNAP_4_8},${fg} ${SNAP_4_8})`;
|
|
177
|
+
case 0x2585:
|
|
178
|
+
return `linear-gradient(${bg} ${SNAP_3_8},${fg} ${SNAP_3_8})`;
|
|
179
|
+
case 0x2586:
|
|
180
|
+
return `linear-gradient(${bg} ${SNAP_2_8},${fg} ${SNAP_2_8})`;
|
|
181
|
+
case 0x2587:
|
|
182
|
+
return `linear-gradient(${bg} ${SNAP_1_8},${fg} ${SNAP_1_8})`;
|
|
183
|
+
case 0x2588:
|
|
184
|
+
return fg;
|
|
185
|
+
case 0x2589:
|
|
186
|
+
return `linear-gradient(to right,${fg} 87.5%,${bg} 87.5%)`;
|
|
187
|
+
case 0x258a:
|
|
188
|
+
return `linear-gradient(to right,${fg} 75%,${bg} 75%)`;
|
|
189
|
+
case 0x258b:
|
|
190
|
+
return `linear-gradient(to right,${fg} 62.5%,${bg} 62.5%)`;
|
|
191
|
+
case 0x258c:
|
|
192
|
+
return `linear-gradient(to right,${fg} 50%,${bg} 50%)`;
|
|
193
|
+
case 0x258d:
|
|
194
|
+
return `linear-gradient(to right,${fg} 37.5%,${bg} 37.5%)`;
|
|
195
|
+
case 0x258e:
|
|
196
|
+
return `linear-gradient(to right,${fg} 25%,${bg} 25%)`;
|
|
197
|
+
case 0x258f:
|
|
198
|
+
return `linear-gradient(to right,${fg} 12.5%,${bg} 12.5%)`;
|
|
199
|
+
case 0x2590:
|
|
200
|
+
return `linear-gradient(to right,${bg} 50%,${fg} 50%)`;
|
|
201
|
+
case 0x2591:
|
|
202
|
+
return `color-mix(in srgb,${fg} 25%,${bg})`;
|
|
203
|
+
case 0x2592:
|
|
204
|
+
return `color-mix(in srgb,${fg} 50%,${bg})`;
|
|
205
|
+
case 0x2593:
|
|
206
|
+
return `color-mix(in srgb,${fg} 75%,${bg})`;
|
|
207
|
+
case 0x2594:
|
|
208
|
+
return `linear-gradient(${fg} ${SNAP_1_8},${bg} ${SNAP_1_8})`;
|
|
209
|
+
case 0x2595:
|
|
210
|
+
return `linear-gradient(to right,${bg} 87.5%,${fg} 87.5%)`;
|
|
211
|
+
default: {
|
|
212
|
+
const QUADRANTS: Record<number, [boolean, boolean, boolean, boolean]> = {
|
|
213
|
+
0x2596: [false, false, true, false],
|
|
214
|
+
0x2597: [false, false, false, true],
|
|
215
|
+
0x2598: [true, false, false, false],
|
|
216
|
+
0x2599: [true, false, true, true],
|
|
217
|
+
0x259a: [true, false, false, true],
|
|
218
|
+
0x259b: [true, true, true, false],
|
|
219
|
+
0x259c: [true, true, false, true],
|
|
220
|
+
0x259d: [false, true, false, false],
|
|
221
|
+
0x259e: [false, true, true, false],
|
|
222
|
+
0x259f: [false, true, true, true],
|
|
223
|
+
};
|
|
224
|
+
const q = QUADRANTS[cp];
|
|
225
|
+
if (!q) return fg;
|
|
226
|
+
const [tl, tr, bl, br] = q;
|
|
227
|
+
if (tl && tr && bl && br) return fg;
|
|
228
|
+
const layers: string[] = [];
|
|
229
|
+
const POS = ["0 0", "100% 0", "0 100%", "100% 100%"];
|
|
230
|
+
q.forEach((filled, i) => {
|
|
231
|
+
if (filled)
|
|
232
|
+
layers.push(
|
|
233
|
+
`linear-gradient(${fg},${fg}) ${POS[i]}/50% 50% no-repeat`,
|
|
234
|
+
);
|
|
235
|
+
});
|
|
236
|
+
layers.push(bg);
|
|
237
|
+
return layers.join(",");
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export class Renderer {
|
|
243
|
+
private container: HTMLElement;
|
|
244
|
+
private rows = 0;
|
|
245
|
+
private cols = 0;
|
|
246
|
+
|
|
247
|
+
/** The painted grid rows, in order. Public by contract with scroller
|
|
248
|
+
* consumers that adjust painted geometry after the paint (see
|
|
249
|
+
* `WTermOptions.onAfterPaint`). */
|
|
250
|
+
rowEls: HTMLDivElement[] = [];
|
|
251
|
+
private prevCursorRow = -1;
|
|
252
|
+
private prevCursorCol = -1;
|
|
253
|
+
private prevContainerBg = "";
|
|
254
|
+
private prevRowBg: string[] = [];
|
|
255
|
+
|
|
256
|
+
private _scrollbackRowEls: HTMLDivElement[] = [];
|
|
257
|
+
private _scrollbackStartKey = 0;
|
|
258
|
+
private _renderedScrollbackCount = -1;
|
|
259
|
+
private _renderedDiscardedCount = -1;
|
|
260
|
+
private _scrollbackTopSpacer: HTMLDivElement | null = null;
|
|
261
|
+
private _scrollbackBottomSpacer: HTMLDivElement | null = null;
|
|
262
|
+
|
|
263
|
+
constructor(container: HTMLElement) {
|
|
264
|
+
this.container = container;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
setup(cols: number, rows: number): void {
|
|
268
|
+
this.cols = cols;
|
|
269
|
+
this.rows = rows;
|
|
270
|
+
this.container.innerHTML = "";
|
|
271
|
+
this.rowEls = [];
|
|
272
|
+
this.prevRowBg = [];
|
|
273
|
+
this._scrollbackRowEls = [];
|
|
274
|
+
this._scrollbackStartKey = 0;
|
|
275
|
+
this._renderedScrollbackCount = -1;
|
|
276
|
+
this._renderedDiscardedCount = -1;
|
|
277
|
+
|
|
278
|
+
const fragment = document.createDocumentFragment();
|
|
279
|
+
this._scrollbackTopSpacer = document.createElement("div");
|
|
280
|
+
this._scrollbackTopSpacer.className = "term-scrollback-spacer";
|
|
281
|
+
fragment.appendChild(this._scrollbackTopSpacer);
|
|
282
|
+
|
|
283
|
+
this._scrollbackBottomSpacer = document.createElement("div");
|
|
284
|
+
this._scrollbackBottomSpacer.className = "term-scrollback-spacer";
|
|
285
|
+
fragment.appendChild(this._scrollbackBottomSpacer);
|
|
286
|
+
|
|
287
|
+
for (let r = 0; r < rows; r++) {
|
|
288
|
+
const rowEl = document.createElement("div");
|
|
289
|
+
rowEl.className = "term-row";
|
|
290
|
+
fragment.appendChild(rowEl);
|
|
291
|
+
this.rowEls.push(rowEl);
|
|
292
|
+
}
|
|
293
|
+
this.container.appendChild(fragment);
|
|
294
|
+
this.prevCursorRow = -1;
|
|
295
|
+
this.prevCursorCol = -1;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private _buildRowContent(
|
|
299
|
+
rowEl: HTMLDivElement,
|
|
300
|
+
getCell: (col: number) => CellData,
|
|
301
|
+
lineLen: number,
|
|
302
|
+
cursorCol: number,
|
|
303
|
+
rowIndex: number,
|
|
304
|
+
): void {
|
|
305
|
+
let html = "";
|
|
306
|
+
let runStyle = "";
|
|
307
|
+
let runText = "";
|
|
308
|
+
let runCells: string[] = [];
|
|
309
|
+
let runStart = 0;
|
|
310
|
+
let runLinkKey = "";
|
|
311
|
+
let runLinkUri: string | undefined;
|
|
312
|
+
let outputLinkKey = "";
|
|
313
|
+
|
|
314
|
+
const appendContent = (
|
|
315
|
+
content: string,
|
|
316
|
+
linkKey: string,
|
|
317
|
+
uri: string | undefined,
|
|
318
|
+
) => {
|
|
319
|
+
const href = safeLinkHref(uri);
|
|
320
|
+
const nextLinkKey = href ? linkKey : "";
|
|
321
|
+
if (nextLinkKey !== outputLinkKey) {
|
|
322
|
+
if (outputLinkKey) html += "</a>";
|
|
323
|
+
if (nextLinkKey) {
|
|
324
|
+
html += `<a class="term-link" href="${escapeHTML(href!)}" target="_blank" rel="noopener noreferrer">`;
|
|
325
|
+
}
|
|
326
|
+
outputLinkKey = nextLinkKey;
|
|
327
|
+
}
|
|
328
|
+
html += content;
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
const flushRun = (endCol: number) => {
|
|
332
|
+
if (!runText) return;
|
|
333
|
+
const escaped = escapeHTML(runText);
|
|
334
|
+
let content = "";
|
|
335
|
+
|
|
336
|
+
if (cursorCol >= runStart && cursorCol < endCol) {
|
|
337
|
+
const offset = cursorCol - runStart;
|
|
338
|
+
const before = runCells.slice(0, offset).join("");
|
|
339
|
+
const cursorChar = runCells[offset] || " ";
|
|
340
|
+
const after = runCells.slice(offset + 1).join("");
|
|
341
|
+
|
|
342
|
+
if (before) {
|
|
343
|
+
content += runStyle
|
|
344
|
+
? `<span style="${runStyle}">${escapeHTML(before)}</span>`
|
|
345
|
+
: `<span>${escapeHTML(before)}</span>`;
|
|
346
|
+
}
|
|
347
|
+
content += runStyle
|
|
348
|
+
? `<span class="term-cursor" style="${runStyle}">${escapeHTML(cursorChar)}</span>`
|
|
349
|
+
: `<span class="term-cursor">${escapeHTML(cursorChar)}</span>`;
|
|
350
|
+
if (after) {
|
|
351
|
+
content += runStyle
|
|
352
|
+
? `<span style="${runStyle}">${escapeHTML(after)}</span>`
|
|
353
|
+
: `<span>${escapeHTML(after)}</span>`;
|
|
354
|
+
}
|
|
355
|
+
} else {
|
|
356
|
+
content += runStyle
|
|
357
|
+
? `<span style="${runStyle}">${escaped}</span>`
|
|
358
|
+
: `<span>${escaped}</span>`;
|
|
359
|
+
}
|
|
360
|
+
appendContent(content, runLinkKey, runLinkUri);
|
|
361
|
+
runText = "";
|
|
362
|
+
runCells = [];
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
const appendStyledSpan = (
|
|
366
|
+
className: string,
|
|
367
|
+
style: string,
|
|
368
|
+
text: string,
|
|
369
|
+
linkKey: string,
|
|
370
|
+
linkUri?: string,
|
|
371
|
+
) => {
|
|
372
|
+
const classAttr = className ? ` class="${className}"` : "";
|
|
373
|
+
const styleAttr = style ? ` style="${style}"` : "";
|
|
374
|
+
appendContent(
|
|
375
|
+
`<span${classAttr}${styleAttr}>${escapeHTML(text)}</span>`,
|
|
376
|
+
linkKey,
|
|
377
|
+
linkUri,
|
|
378
|
+
);
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
for (let col = 0; col < this.cols; col++) {
|
|
382
|
+
const cell = getCell(col);
|
|
383
|
+
const inBounds = col < lineLen;
|
|
384
|
+
const cp = inBounds ? cell.char : 0;
|
|
385
|
+
const width = inBounds ? (cell.width ?? 1) : 1;
|
|
386
|
+
const cellLinkKey = inBounds ? linkIdentity(cell) : "";
|
|
387
|
+
const cellLinkUri = inBounds ? cell.linkUri : undefined;
|
|
388
|
+
|
|
389
|
+
if (inBounds && width === 0) {
|
|
390
|
+
flushRun(col);
|
|
391
|
+
// Skipping is only right when this continues the wide cell to the
|
|
392
|
+
// left, which already covers both columns and its cursor. A width-0
|
|
393
|
+
// cell with no wide cell before it owns its column, so dropping it
|
|
394
|
+
// would shorten the row.
|
|
395
|
+
const continuesWide = col > 0 && (getCell(col - 1).width ?? 1) === 2;
|
|
396
|
+
if (!continuesWide) {
|
|
397
|
+
appendStyledSpan(
|
|
398
|
+
col === cursorCol ? "term-cursor" : "",
|
|
399
|
+
"",
|
|
400
|
+
" ",
|
|
401
|
+
cellLinkKey,
|
|
402
|
+
cellLinkUri,
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
runStyle = "";
|
|
406
|
+
runLinkKey = "";
|
|
407
|
+
runLinkUri = undefined;
|
|
408
|
+
runText = "";
|
|
409
|
+
runCells = [];
|
|
410
|
+
runStart = col + 1;
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (inBounds && width === 2) {
|
|
415
|
+
flushRun(col);
|
|
416
|
+
|
|
417
|
+
// A scrollback row keeps the width it was stored at, so a narrower
|
|
418
|
+
// grid can put the last rendered column on a wide lead whose
|
|
419
|
+
// continuation is outside the row. Drawing the pair here would spill
|
|
420
|
+
// a second column past the row.
|
|
421
|
+
if (col + 1 >= this.cols) {
|
|
422
|
+
appendStyledSpan(
|
|
423
|
+
col === cursorCol ? "term-cursor" : "",
|
|
424
|
+
"",
|
|
425
|
+
" ",
|
|
426
|
+
cellLinkKey,
|
|
427
|
+
cellLinkUri,
|
|
428
|
+
);
|
|
429
|
+
runStyle = "";
|
|
430
|
+
runLinkKey = "";
|
|
431
|
+
runLinkUri = undefined;
|
|
432
|
+
runText = "";
|
|
433
|
+
runCells = [];
|
|
434
|
+
runStart = col + 1;
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const ch = cell.chars ?? (cp >= 32 ? String.fromCodePoint(cp) : " ");
|
|
439
|
+
const style = buildCellStyle(
|
|
440
|
+
cell.fg,
|
|
441
|
+
cell.bg,
|
|
442
|
+
cell.flags,
|
|
443
|
+
cell.fgRgb,
|
|
444
|
+
cell.bgRgb,
|
|
445
|
+
);
|
|
446
|
+
const cls =
|
|
447
|
+
cursorCol >= col && cursorCol < col + 2
|
|
448
|
+
? "term-wide term-cursor"
|
|
449
|
+
: "term-wide";
|
|
450
|
+
appendStyledSpan(cls, style, ch, cellLinkKey, cellLinkUri);
|
|
451
|
+
|
|
452
|
+
runStyle = "";
|
|
453
|
+
runLinkKey = "";
|
|
454
|
+
runLinkUri = undefined;
|
|
455
|
+
runText = "";
|
|
456
|
+
runCells = [];
|
|
457
|
+
runStart = col + 2;
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (inBounds && cp >= 0x2580 && cp <= 0x259f) {
|
|
462
|
+
flushRun(col);
|
|
463
|
+
|
|
464
|
+
const colors = resolveColors(
|
|
465
|
+
cell.fg,
|
|
466
|
+
cell.bg,
|
|
467
|
+
cell.flags,
|
|
468
|
+
cell.fgRgb,
|
|
469
|
+
cell.bgRgb,
|
|
470
|
+
);
|
|
471
|
+
const cls = col === cursorCol ? "term-block term-cursor" : "term-block";
|
|
472
|
+
const bg = getBlockBackground(cp, colors.fg, colors.bg);
|
|
473
|
+
const dim = cell.flags & FLAG_DIM ? "opacity:0.5;" : "";
|
|
474
|
+
appendContent(
|
|
475
|
+
`<span class="${cls}" style="background:${bg};${dim}"></span>`,
|
|
476
|
+
cellLinkKey,
|
|
477
|
+
cellLinkUri,
|
|
478
|
+
);
|
|
479
|
+
|
|
480
|
+
runStyle = "";
|
|
481
|
+
runLinkKey = "";
|
|
482
|
+
runLinkUri = undefined;
|
|
483
|
+
runText = "";
|
|
484
|
+
runCells = [];
|
|
485
|
+
runStart = col + 1;
|
|
486
|
+
} else {
|
|
487
|
+
const ch =
|
|
488
|
+
cell.chars ?? (inBounds && cp >= 32 ? String.fromCodePoint(cp) : " ");
|
|
489
|
+
const style = inBounds
|
|
490
|
+
? buildCellStyle(cell.fg, cell.bg, cell.flags, cell.fgRgb, cell.bgRgb)
|
|
491
|
+
: "";
|
|
492
|
+
|
|
493
|
+
if (style !== runStyle || cellLinkKey !== runLinkKey) {
|
|
494
|
+
flushRun(col);
|
|
495
|
+
runStyle = style;
|
|
496
|
+
runLinkKey = cellLinkKey;
|
|
497
|
+
runLinkUri = cellLinkUri;
|
|
498
|
+
runText = ch;
|
|
499
|
+
runCells = [ch];
|
|
500
|
+
runStart = col;
|
|
501
|
+
} else {
|
|
502
|
+
runText += ch;
|
|
503
|
+
runCells.push(ch);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
flushRun(this.cols);
|
|
508
|
+
if (outputLinkKey) html += "</a>";
|
|
509
|
+
|
|
510
|
+
rowEl.innerHTML = html;
|
|
511
|
+
|
|
512
|
+
let bgCss = "";
|
|
513
|
+
if (lineLen >= this.cols && this.cols > 0) {
|
|
514
|
+
const lastCell = getCell(this.cols - 1);
|
|
515
|
+
let bgIdx = lastCell.bg;
|
|
516
|
+
let bgR = lastCell.bgRgb;
|
|
517
|
+
if (lastCell.flags & FLAG_REVERSE) {
|
|
518
|
+
bgIdx = lastCell.fg;
|
|
519
|
+
bgR = lastCell.fgRgb;
|
|
520
|
+
if (bgR === undefined && bgIdx === DEFAULT_COLOR) bgIdx = 7;
|
|
521
|
+
}
|
|
522
|
+
bgCss = cellBgCSS(bgIdx, bgR) || "";
|
|
523
|
+
}
|
|
524
|
+
const boxShadow = bgCss ? `0 1px 0 ${bgCss}` : "";
|
|
525
|
+
if (rowIndex >= 0) {
|
|
526
|
+
if (bgCss !== (this.prevRowBg[rowIndex] ?? "")) {
|
|
527
|
+
rowEl.style.background = bgCss;
|
|
528
|
+
rowEl.style.boxShadow = boxShadow;
|
|
529
|
+
this.prevRowBg[rowIndex] = bgCss;
|
|
530
|
+
}
|
|
531
|
+
} else {
|
|
532
|
+
rowEl.style.background = bgCss;
|
|
533
|
+
rowEl.style.boxShadow = boxShadow;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
private _buildScrollbackRowEl(
|
|
538
|
+
core: TerminalCore,
|
|
539
|
+
sbOffset: number,
|
|
540
|
+
): HTMLDivElement {
|
|
541
|
+
const rowEl = document.createElement("div");
|
|
542
|
+
rowEl.className = "term-row term-scrollback-row";
|
|
543
|
+
const lineLen = core.getScrollbackLineLen(sbOffset);
|
|
544
|
+
|
|
545
|
+
this._buildRowContent(
|
|
546
|
+
rowEl,
|
|
547
|
+
(col) => core.getScrollbackCell(sbOffset, col),
|
|
548
|
+
lineLen,
|
|
549
|
+
-1,
|
|
550
|
+
-1,
|
|
551
|
+
);
|
|
552
|
+
return rowEl;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
private syncScrollback(core: TerminalCore, viewport?: RenderViewport): void {
|
|
556
|
+
const scrollbackCount = core.getScrollbackCount();
|
|
557
|
+
const rowHeight = viewport?.rowHeight ?? 0;
|
|
558
|
+
const virtual = viewport !== undefined && rowHeight > 0;
|
|
559
|
+
const overscan = viewport?.overscanRows ?? DEFAULT_SCROLLBACK_OVERSCAN_ROWS;
|
|
560
|
+
const hasDiscardedCount = viewport?.scrollbackDiscardedCount !== undefined;
|
|
561
|
+
const discardedCount = viewport?.scrollbackDiscardedCount ?? 0;
|
|
562
|
+
const viewportHeight =
|
|
563
|
+
viewport && viewport.clientHeight > 0
|
|
564
|
+
? viewport.clientHeight
|
|
565
|
+
: this.rows * rowHeight;
|
|
566
|
+
const firstVisible = virtual
|
|
567
|
+
? Math.floor(viewport.scrollTop / rowHeight)
|
|
568
|
+
: 0;
|
|
569
|
+
const visibleRows = virtual
|
|
570
|
+
? Math.ceil(viewportHeight / rowHeight)
|
|
571
|
+
: scrollbackCount;
|
|
572
|
+
let start = virtual
|
|
573
|
+
? Math.max(0, Math.min(scrollbackCount, firstVisible - overscan))
|
|
574
|
+
: 0;
|
|
575
|
+
let end = virtual
|
|
576
|
+
? Math.max(
|
|
577
|
+
start,
|
|
578
|
+
Math.min(scrollbackCount, firstVisible + visibleRows + overscan),
|
|
579
|
+
)
|
|
580
|
+
: scrollbackCount;
|
|
581
|
+
const selection = this.container.ownerDocument.getSelection();
|
|
582
|
+
const selectionInContainer =
|
|
583
|
+
selection !== null &&
|
|
584
|
+
!selection.isCollapsed &&
|
|
585
|
+
(this.container.contains(selection.anchorNode) ||
|
|
586
|
+
this.container.contains(selection.focusNode));
|
|
587
|
+
if (selectionInContainer && this._scrollbackRowEls.length > 0) {
|
|
588
|
+
start = Math.min(
|
|
589
|
+
start,
|
|
590
|
+
Math.max(0, this._scrollbackStartKey - discardedCount),
|
|
591
|
+
);
|
|
592
|
+
end = Math.max(
|
|
593
|
+
end,
|
|
594
|
+
Math.min(
|
|
595
|
+
scrollbackCount,
|
|
596
|
+
this._scrollbackStartKey -
|
|
597
|
+
discardedCount +
|
|
598
|
+
this._scrollbackRowEls.length,
|
|
599
|
+
),
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const startKey = discardedCount + start;
|
|
604
|
+
if (
|
|
605
|
+
hasDiscardedCount &&
|
|
606
|
+
scrollbackCount === this._renderedScrollbackCount &&
|
|
607
|
+
discardedCount === this._renderedDiscardedCount &&
|
|
608
|
+
startKey === this._scrollbackStartKey &&
|
|
609
|
+
end - start === this._scrollbackRowEls.length
|
|
610
|
+
) {
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
const previous = new Map<number, HTMLDivElement>();
|
|
615
|
+
for (let i = 0; i < this._scrollbackRowEls.length; i++) {
|
|
616
|
+
previous.set(this._scrollbackStartKey + i, this._scrollbackRowEls[i]);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
const endKey = discardedCount + end;
|
|
620
|
+
for (const [key, rowEl] of previous) {
|
|
621
|
+
if (key < startKey || key >= endKey) rowEl.remove();
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
const nextRows: HTMLDivElement[] = [];
|
|
625
|
+
let nextSibling = this._scrollbackTopSpacer?.nextSibling ?? null;
|
|
626
|
+
for (let index = start; index < end; index++) {
|
|
627
|
+
const key = discardedCount + index;
|
|
628
|
+
const offset = scrollbackCount - 1 - index;
|
|
629
|
+
const candidate = this._buildScrollbackRowEl(core, offset);
|
|
630
|
+
const existing = previous.get(key);
|
|
631
|
+
let rowEl = candidate;
|
|
632
|
+
let positioned = false;
|
|
633
|
+
|
|
634
|
+
if (
|
|
635
|
+
existing &&
|
|
636
|
+
existing.innerHTML === candidate.innerHTML &&
|
|
637
|
+
existing.style.cssText === candidate.style.cssText
|
|
638
|
+
) {
|
|
639
|
+
rowEl = existing;
|
|
640
|
+
} else if (existing) {
|
|
641
|
+
existing.replaceWith(candidate);
|
|
642
|
+
positioned = true;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
if (!positioned && rowEl !== nextSibling) {
|
|
646
|
+
this.container.insertBefore(
|
|
647
|
+
rowEl,
|
|
648
|
+
nextSibling ?? this._scrollbackBottomSpacer,
|
|
649
|
+
);
|
|
650
|
+
}
|
|
651
|
+
nextSibling = rowEl.nextSibling;
|
|
652
|
+
nextRows.push(rowEl);
|
|
653
|
+
}
|
|
654
|
+
this._scrollbackRowEls = nextRows;
|
|
655
|
+
this._scrollbackStartKey = startKey;
|
|
656
|
+
this._renderedScrollbackCount = scrollbackCount;
|
|
657
|
+
this._renderedDiscardedCount = discardedCount;
|
|
658
|
+
|
|
659
|
+
if (this._scrollbackTopSpacer) {
|
|
660
|
+
this._scrollbackTopSpacer.style.height = `${start * rowHeight}px`;
|
|
661
|
+
}
|
|
662
|
+
if (this._scrollbackBottomSpacer) {
|
|
663
|
+
this._scrollbackBottomSpacer.style.height = `${(scrollbackCount - end) * rowHeight}px`;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
render(core: TerminalCore, viewport?: RenderViewport): void {
|
|
668
|
+
const rows = core.getRows();
|
|
669
|
+
const cols = core.getCols();
|
|
670
|
+
|
|
671
|
+
let resized = false;
|
|
672
|
+
if (rows !== this.rows || cols !== this.cols) {
|
|
673
|
+
this.setup(cols, rows);
|
|
674
|
+
resized = true;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
this.syncScrollback(core, viewport);
|
|
678
|
+
|
|
679
|
+
const cursor = core.getCursor();
|
|
680
|
+
const cursorVisible = cursor.visible;
|
|
681
|
+
|
|
682
|
+
const needsCursorUpdate =
|
|
683
|
+
cursor.row !== this.prevCursorRow || cursor.col !== this.prevCursorCol;
|
|
684
|
+
|
|
685
|
+
for (let r = 0; r < this.rows; r++) {
|
|
686
|
+
const isDirty = resized || core.isDirtyRow(r);
|
|
687
|
+
const hadCursor = r === this.prevCursorRow && needsCursorUpdate;
|
|
688
|
+
const hasCursor = r === cursor.row;
|
|
689
|
+
|
|
690
|
+
if (isDirty || hadCursor || (hasCursor && needsCursorUpdate)) {
|
|
691
|
+
const cCol = hasCursor && cursorVisible ? cursor.col : -1;
|
|
692
|
+
this._buildRowContent(
|
|
693
|
+
this.rowEls[r],
|
|
694
|
+
(col) => core.getCell(r, col),
|
|
695
|
+
this.cols,
|
|
696
|
+
cCol,
|
|
697
|
+
r,
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
this.prevCursorRow = cursor.row;
|
|
703
|
+
this.prevCursorCol = cursor.col;
|
|
704
|
+
|
|
705
|
+
const lastRowDirty = resized || core.isDirtyRow(this.rows - 1);
|
|
706
|
+
if (lastRowDirty) {
|
|
707
|
+
const bottomRight = core.getCell(this.rows - 1, this.cols - 1);
|
|
708
|
+
let gridBgIdx = bottomRight.bg;
|
|
709
|
+
let gridBgRgb = bottomRight.bgRgb;
|
|
710
|
+
if (bottomRight.flags & FLAG_REVERSE) {
|
|
711
|
+
gridBgIdx = bottomRight.fg;
|
|
712
|
+
gridBgRgb = bottomRight.fgRgb;
|
|
713
|
+
if (gridBgRgb === undefined && gridBgIdx === DEFAULT_COLOR)
|
|
714
|
+
gridBgIdx = 7;
|
|
715
|
+
}
|
|
716
|
+
const containerBg = cellBgCSS(gridBgIdx, gridBgRgb) || "";
|
|
717
|
+
if (containerBg !== this.prevContainerBg) {
|
|
718
|
+
this.container.style.background = containerBg;
|
|
719
|
+
this.prevContainerBg = containerBg;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
core.clearDirty();
|
|
724
|
+
}
|
|
725
|
+
}
|