termigan 0.0.0-stage → 0.0.1
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 +21 -0
- package/README.md +304 -2
- package/assets/logo.svg +97 -0
- package/dist/termigan.wasm +0 -0
- package/docs/api.md +295 -0
- package/docs/architecture.md +102 -0
- package/package.json +62 -5
- package/types/index.d.ts +123 -0
- package/web/canvas-renderer.js +427 -0
- package/web/input-controller.js +181 -0
- package/web/pty-socket.js +125 -0
- package/web/termigan.js +870 -0
- package/web/viewport-controller.js +184 -0
- package/web/wasm-bridge.js +264 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
export class ViewportController {
|
|
2
|
+
constructor({
|
|
3
|
+
remoteCols = 80,
|
|
4
|
+
remoteRows = 24,
|
|
5
|
+
baseFontSize = 13,
|
|
6
|
+
minFontSize = 6,
|
|
7
|
+
} = {}) {
|
|
8
|
+
this._sizeMode = "mirror";
|
|
9
|
+
this._expanded = false;
|
|
10
|
+
this._remoteCols = Math.max(1, remoteCols | 0);
|
|
11
|
+
this._remoteRows = Math.max(2, remoteRows | 0);
|
|
12
|
+
this._baseFontSize = baseFontSize;
|
|
13
|
+
this._minFontSize = minFontSize;
|
|
14
|
+
this._scrollbackLen = 0;
|
|
15
|
+
this._scrollOffset = 0;
|
|
16
|
+
this._lastEmittedFitCols = 0;
|
|
17
|
+
this._lastEmittedFitRows = 0;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get sizeMode() {
|
|
21
|
+
return this._sizeMode;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get expanded() {
|
|
25
|
+
return this._expanded;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get remoteCols() {
|
|
29
|
+
return this._remoteCols;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get remoteRows() {
|
|
33
|
+
return this._remoteRows;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get scrollbackLen() {
|
|
37
|
+
return this._scrollbackLen;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get scrollOffset() {
|
|
41
|
+
return this._scrollOffset;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setRemoteSize(cols, rows) {
|
|
45
|
+
const nextCols = Math.max(1, cols | 0);
|
|
46
|
+
const nextRows = Math.max(2, rows | 0);
|
|
47
|
+
const changed = nextCols !== this._remoteCols || nextRows !== this._remoteRows;
|
|
48
|
+
this._remoteCols = nextCols;
|
|
49
|
+
this._remoteRows = nextRows;
|
|
50
|
+
return changed;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
setSizeMode(mode) {
|
|
54
|
+
if (mode !== "mirror" && mode !== "fit") {
|
|
55
|
+
throw new Error(`Invalid size mode: ${mode}`);
|
|
56
|
+
}
|
|
57
|
+
const changed = this._sizeMode !== mode;
|
|
58
|
+
this._sizeMode = mode;
|
|
59
|
+
if (mode === "mirror") {
|
|
60
|
+
this._lastEmittedFitCols = 0;
|
|
61
|
+
this._lastEmittedFitRows = 0;
|
|
62
|
+
}
|
|
63
|
+
return changed;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
setExpanded(expanded) {
|
|
67
|
+
const next = Boolean(expanded);
|
|
68
|
+
const changed = this._expanded !== next;
|
|
69
|
+
this._expanded = next;
|
|
70
|
+
return changed;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
proposeFitSize(containerW, containerH, baseMetrics) {
|
|
74
|
+
const padW = baseMetrics.padX * 2 + baseMetrics.scrollbarWidth;
|
|
75
|
+
const padH = baseMetrics.padY * 2;
|
|
76
|
+
const availW = Math.max(baseMetrics.cellWidth, containerW - padW);
|
|
77
|
+
const availH = Math.max(baseMetrics.cellHeight * 2, containerH - padH);
|
|
78
|
+
const cols = Math.max(1, Math.floor(availW / baseMetrics.cellWidth));
|
|
79
|
+
const rows = Math.max(2, Math.floor(availH / baseMetrics.cellHeight));
|
|
80
|
+
return { cols, rows };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
computeLayout(containerW, containerH, baseMetrics) {
|
|
84
|
+
const safeW = Math.max(1, containerW);
|
|
85
|
+
const safeH = Math.max(1, containerH);
|
|
86
|
+
const padW = baseMetrics.padX * 2 + baseMetrics.scrollbarWidth;
|
|
87
|
+
const padH = baseMetrics.padY * 2;
|
|
88
|
+
|
|
89
|
+
if (this._sizeMode === "fit") {
|
|
90
|
+
const fit = this.proposeFitSize(safeW, safeH, baseMetrics);
|
|
91
|
+
const emitResize =
|
|
92
|
+
fit.cols !== this._lastEmittedFitCols ||
|
|
93
|
+
fit.rows !== this._lastEmittedFitRows;
|
|
94
|
+
if (emitResize) {
|
|
95
|
+
this._lastEmittedFitCols = fit.cols;
|
|
96
|
+
this._lastEmittedFitRows = fit.rows;
|
|
97
|
+
}
|
|
98
|
+
const renderedWidth = fit.cols * baseMetrics.cellWidth + padW;
|
|
99
|
+
const renderedHeight = fit.rows * baseMetrics.cellHeight + padH;
|
|
100
|
+
return {
|
|
101
|
+
cols: fit.cols,
|
|
102
|
+
rows: fit.rows,
|
|
103
|
+
fontSize: this._baseFontSize,
|
|
104
|
+
cssScale: 1,
|
|
105
|
+
renderedWidth,
|
|
106
|
+
renderedHeight,
|
|
107
|
+
emitResize,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const cols = this._remoteCols;
|
|
112
|
+
const rows = this._remoteRows;
|
|
113
|
+
const baseGridW = cols * baseMetrics.cellWidth + padW;
|
|
114
|
+
const baseGridH = rows * baseMetrics.cellHeight + padH;
|
|
115
|
+
|
|
116
|
+
const scaleW = safeW / baseGridW;
|
|
117
|
+
const scaleH = safeH / baseGridH;
|
|
118
|
+
const targetScale = Math.min(1, scaleW, scaleH);
|
|
119
|
+
|
|
120
|
+
const desiredFont = this._baseFontSize * targetScale;
|
|
121
|
+
const fontSize = Math.max(this._minFontSize, Math.min(this._baseFontSize, desiredFont));
|
|
122
|
+
const fontRatio = fontSize / this._baseFontSize;
|
|
123
|
+
const nativeW = cols * (baseMetrics.cellWidth * fontRatio) + padW;
|
|
124
|
+
const nativeH = rows * (baseMetrics.cellHeight * fontRatio) + padH;
|
|
125
|
+
const cssScale = Math.min(1, safeW / nativeW, safeH / nativeH);
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
cols,
|
|
129
|
+
rows,
|
|
130
|
+
fontSize,
|
|
131
|
+
cssScale,
|
|
132
|
+
renderedWidth: nativeW * cssScale,
|
|
133
|
+
renderedHeight: nativeH * cssScale,
|
|
134
|
+
emitResize: false,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
onScrollbackUpdated(nextLen, altScreen) {
|
|
139
|
+
const clampedLen = Math.max(0, nextLen | 0);
|
|
140
|
+
if (altScreen) {
|
|
141
|
+
this._scrollbackLen = clampedLen;
|
|
142
|
+
if (this._scrollOffset > clampedLen) {
|
|
143
|
+
this._scrollOffset = clampedLen;
|
|
144
|
+
}
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const delta = clampedLen - this._scrollbackLen;
|
|
148
|
+
this._scrollbackLen = clampedLen;
|
|
149
|
+
if (this._scrollOffset === 0) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (delta > 0) {
|
|
153
|
+
this._scrollOffset = Math.min(clampedLen, this._scrollOffset + delta);
|
|
154
|
+
} else {
|
|
155
|
+
this._scrollOffset = Math.min(clampedLen, this._scrollOffset);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
handleWheel(deltaLines, altScreen) {
|
|
160
|
+
if (deltaLines === 0) {
|
|
161
|
+
return { handledAsNormalScroll: !altScreen, changed: false, forwardWheelLines: 0 };
|
|
162
|
+
}
|
|
163
|
+
if (altScreen) {
|
|
164
|
+
return { handledAsNormalScroll: false, changed: false, forwardWheelLines: deltaLines };
|
|
165
|
+
}
|
|
166
|
+
const prev = this._scrollOffset;
|
|
167
|
+
const next = Math.max(0, Math.min(this._scrollbackLen, prev - deltaLines));
|
|
168
|
+
this._scrollOffset = next;
|
|
169
|
+
return { handledAsNormalScroll: true, changed: next !== prev, forwardWheelLines: 0 };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
setScrollOffset(offset) {
|
|
173
|
+
const next = Math.max(0, Math.min(this._scrollbackLen, offset | 0));
|
|
174
|
+
const changed = next !== this._scrollOffset;
|
|
175
|
+
this._scrollOffset = next;
|
|
176
|
+
return changed;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
scrollToBottom() {
|
|
180
|
+
const changed = this._scrollOffset !== 0;
|
|
181
|
+
this._scrollOffset = 0;
|
|
182
|
+
return changed;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
const textEncoder = new TextEncoder();
|
|
2
|
+
const textDecoder = new TextDecoder();
|
|
3
|
+
|
|
4
|
+
const moduleCache = new Map();
|
|
5
|
+
|
|
6
|
+
export async function loadTermiganModule(wasmSource) {
|
|
7
|
+
if (wasmSource instanceof WebAssembly.Module) {
|
|
8
|
+
return wasmSource;
|
|
9
|
+
}
|
|
10
|
+
if (wasmSource instanceof Uint8Array || wasmSource instanceof ArrayBuffer) {
|
|
11
|
+
return WebAssembly.compile(wasmSource);
|
|
12
|
+
}
|
|
13
|
+
const url = String(wasmSource);
|
|
14
|
+
if (!moduleCache.has(url)) {
|
|
15
|
+
const promise = (async () => {
|
|
16
|
+
const response = await fetch(url);
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
throw new Error(`Failed to fetch ${url}: ${response.status}`);
|
|
19
|
+
}
|
|
20
|
+
const bytes = await response.arrayBuffer();
|
|
21
|
+
return WebAssembly.compile(bytes);
|
|
22
|
+
})();
|
|
23
|
+
moduleCache.set(url, promise);
|
|
24
|
+
}
|
|
25
|
+
return moduleCache.get(url);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function decodeFrame(bytes) {
|
|
29
|
+
if (bytes.byteLength < 36) {
|
|
30
|
+
throw new Error(`Truncated frame header: ${bytes.byteLength}`);
|
|
31
|
+
}
|
|
32
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
33
|
+
const cols = view.getUint32(0, true);
|
|
34
|
+
const rows = view.getUint32(4, true);
|
|
35
|
+
const cursorRow = view.getUint32(8, true);
|
|
36
|
+
const cursorCol = view.getUint32(12, true);
|
|
37
|
+
const scrollbackLen = view.getUint32(16, true);
|
|
38
|
+
const scrollOffset = view.getUint32(20, true);
|
|
39
|
+
const dirtyRowCount = view.getUint32(24, true);
|
|
40
|
+
const flags = view.getUint8(28);
|
|
41
|
+
const cursorStyle = view.getUint8(29);
|
|
42
|
+
const titleSeq = view.getUint32(32, true);
|
|
43
|
+
|
|
44
|
+
const dirtyRows = new Array(dirtyRowCount);
|
|
45
|
+
let offset = 36;
|
|
46
|
+
const rowStride = 5 + cols * 16;
|
|
47
|
+
for (let i = 0; i < dirtyRowCount; i++) {
|
|
48
|
+
const rowIndex = view.getUint32(offset, true);
|
|
49
|
+
const rowFlags = view.getUint8(offset + 4);
|
|
50
|
+
const cellStart = offset + 5;
|
|
51
|
+
const cells = new Array(cols);
|
|
52
|
+
for (let c = 0; c < cols; c++) {
|
|
53
|
+
const p = cellStart + c * 16;
|
|
54
|
+
cells[c] = {
|
|
55
|
+
cp: view.getUint32(p, true),
|
|
56
|
+
fgKind: view.getUint8(p + 4),
|
|
57
|
+
fgR: view.getUint8(p + 5),
|
|
58
|
+
fgG: view.getUint8(p + 6),
|
|
59
|
+
fgB: view.getUint8(p + 7),
|
|
60
|
+
bgKind: view.getUint8(p + 8),
|
|
61
|
+
bgR: view.getUint8(p + 9),
|
|
62
|
+
bgG: view.getUint8(p + 10),
|
|
63
|
+
bgB: view.getUint8(p + 11),
|
|
64
|
+
attrs: view.getUint16(p + 12, true),
|
|
65
|
+
width: view.getUint8(p + 14),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
dirtyRows[i] = {
|
|
69
|
+
rowIndex,
|
|
70
|
+
wrapped: (rowFlags & 1) !== 0,
|
|
71
|
+
cells,
|
|
72
|
+
};
|
|
73
|
+
offset += rowStride;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
cols,
|
|
78
|
+
rows,
|
|
79
|
+
cursorRow,
|
|
80
|
+
cursorCol,
|
|
81
|
+
scrollbackLen,
|
|
82
|
+
scrollOffset,
|
|
83
|
+
cursorVisible: (flags & 1) !== 0,
|
|
84
|
+
altScreen: (flags & 2) !== 0,
|
|
85
|
+
appCursorKeys: (flags & 4) !== 0,
|
|
86
|
+
bracketedPaste: (flags & 8) !== 0,
|
|
87
|
+
mouseActive: (flags & 16) !== 0,
|
|
88
|
+
sgrMouse: (flags & 32) !== 0,
|
|
89
|
+
cursorStyle,
|
|
90
|
+
titleSeq,
|
|
91
|
+
dirtyRows,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function encodeModifiers({ shift = false, alt = false, ctrl = false, meta = false } = {}) {
|
|
96
|
+
return (shift ? 1 : 0) | (alt ? 2 : 0) | (ctrl ? 4 : 0) | (meta ? 8 : 0);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export class WasmTerminalEngine {
|
|
100
|
+
constructor(instance, cols, rows, scrollbackCap) {
|
|
101
|
+
this._instance = instance;
|
|
102
|
+
this._exports = instance.exports;
|
|
103
|
+
this._handle = this._exports.term_create(cols, rows, scrollbackCap);
|
|
104
|
+
this._disposed = false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
static async create(wasmSource, { cols = 80, rows = 24, scrollbackCap = 10000 } = {}) {
|
|
108
|
+
const mod = await loadTermiganModule(wasmSource);
|
|
109
|
+
const instance = await WebAssembly.instantiate(mod, {});
|
|
110
|
+
return new WasmTerminalEngine(instance, cols, rows, scrollbackCap);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get disposed() {
|
|
114
|
+
return this._disposed;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
_stageBytes(bytes) {
|
|
118
|
+
const ptr = this._exports.term_alloc_in(bytes.byteLength);
|
|
119
|
+
if (bytes.byteLength > 0) {
|
|
120
|
+
const mem = new Uint8Array(this._exports.memory.buffer, ptr, bytes.byteLength);
|
|
121
|
+
mem.set(bytes);
|
|
122
|
+
}
|
|
123
|
+
return bytes.byteLength;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
_readAndFreeOut(len) {
|
|
127
|
+
if (len === 0) {
|
|
128
|
+
this._exports.term_out_free();
|
|
129
|
+
return new Uint8Array(0);
|
|
130
|
+
}
|
|
131
|
+
const ptr = this._exports.term_out_ptr();
|
|
132
|
+
const copy = new Uint8Array(len);
|
|
133
|
+
copy.set(new Uint8Array(this._exports.memory.buffer, ptr, len));
|
|
134
|
+
this._exports.term_out_free();
|
|
135
|
+
return copy;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
write(data) {
|
|
139
|
+
if (this._disposed) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const bytes = typeof data === "string"
|
|
143
|
+
? textEncoder.encode(data)
|
|
144
|
+
: data instanceof Uint8Array
|
|
145
|
+
? data
|
|
146
|
+
: new Uint8Array(data);
|
|
147
|
+
const len = this._stageBytes(bytes);
|
|
148
|
+
this._exports.term_write(this._handle, len);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
reset() {
|
|
152
|
+
if (this._disposed) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
this._exports.term_reset(this._handle);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
resize(cols, rows) {
|
|
159
|
+
if (this._disposed) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
this._exports.term_resize(this._handle, Math.max(1, cols | 0), Math.max(2, rows | 0));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
render(scrollOffset = 0, forceFull = false) {
|
|
166
|
+
if (this._disposed) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
const len = this._exports.term_render(this._handle, scrollOffset >>> 0, forceFull ? 1 : 0);
|
|
170
|
+
const bytes = this._readAndFreeOut(len);
|
|
171
|
+
return decodeFrame(bytes);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
encodeKey(keySpec) {
|
|
175
|
+
if (this._disposed) {
|
|
176
|
+
return new Uint8Array(0);
|
|
177
|
+
}
|
|
178
|
+
const keyBytes = textEncoder.encode(keySpec.key || "");
|
|
179
|
+
const len = this._stageBytes(keyBytes);
|
|
180
|
+
const mods = encodeModifiers(keySpec);
|
|
181
|
+
const outLen = this._exports.term_encode_key(this._handle, len, mods);
|
|
182
|
+
return this._readAndFreeOut(outLen);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
encodePaste(textOrBytes) {
|
|
186
|
+
if (this._disposed) {
|
|
187
|
+
return new Uint8Array(0);
|
|
188
|
+
}
|
|
189
|
+
const bytes = typeof textOrBytes === "string"
|
|
190
|
+
? textEncoder.encode(textOrBytes)
|
|
191
|
+
: new Uint8Array(textOrBytes);
|
|
192
|
+
const len = this._stageBytes(bytes);
|
|
193
|
+
const outLen = this._exports.term_encode_paste(this._handle, len);
|
|
194
|
+
return this._readAndFreeOut(outLen);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
encodeWheel(delta, col, row) {
|
|
198
|
+
if (this._disposed) {
|
|
199
|
+
return new Uint8Array(0);
|
|
200
|
+
}
|
|
201
|
+
const outLen = this._exports.term_encode_wheel(this._handle, delta | 0, col >>> 0, row >>> 0);
|
|
202
|
+
return this._readAndFreeOut(outLen);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
encodeMouse(button, col, row, eventKind, mods = 0) {
|
|
206
|
+
if (this._disposed) {
|
|
207
|
+
return new Uint8Array(0);
|
|
208
|
+
}
|
|
209
|
+
const outLen = this._exports.term_encode_mouse(
|
|
210
|
+
this._handle,
|
|
211
|
+
button >>> 0,
|
|
212
|
+
col >>> 0,
|
|
213
|
+
row >>> 0,
|
|
214
|
+
eventKind >>> 0,
|
|
215
|
+
mods >>> 0,
|
|
216
|
+
);
|
|
217
|
+
return this._readAndFreeOut(outLen);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
takeReplies() {
|
|
221
|
+
if (this._disposed) {
|
|
222
|
+
return new Uint8Array(0);
|
|
223
|
+
}
|
|
224
|
+
const outLen = this._exports.term_take_replies(this._handle);
|
|
225
|
+
return this._readAndFreeOut(outLen);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
getTitle() {
|
|
229
|
+
if (this._disposed) {
|
|
230
|
+
return "";
|
|
231
|
+
}
|
|
232
|
+
const outLen = this._exports.term_get_title(this._handle);
|
|
233
|
+
const bytes = this._readAndFreeOut(outLen);
|
|
234
|
+
return textDecoder.decode(bytes);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
getText(startAbsRow, startCol, endAbsRow, endCol) {
|
|
238
|
+
if (this._disposed) {
|
|
239
|
+
return "";
|
|
240
|
+
}
|
|
241
|
+
const outLen = this._exports.term_get_text(
|
|
242
|
+
this._handle,
|
|
243
|
+
startAbsRow >>> 0,
|
|
244
|
+
startCol >>> 0,
|
|
245
|
+
endAbsRow >>> 0,
|
|
246
|
+
endCol >>> 0,
|
|
247
|
+
);
|
|
248
|
+
const bytes = this._readAndFreeOut(outLen);
|
|
249
|
+
return textDecoder.decode(bytes);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
liveBytes() {
|
|
253
|
+
return this._exports.term_live_bytes();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
dispose() {
|
|
257
|
+
if (this._disposed) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
this._disposed = true;
|
|
261
|
+
this._exports.term_destroy(this._handle);
|
|
262
|
+
this._handle = 0;
|
|
263
|
+
}
|
|
264
|
+
}
|