@effect-tui/emulator 0.1.7
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 +18 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +386 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -0
- package/src/index.ts +415 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Kit Langton
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @effect-tui/emulator
|
|
2
|
+
|
|
3
|
+
Lightweight ANSI terminal emulator for testing render output.
|
|
4
|
+
|
|
5
|
+
Features:
|
|
6
|
+
- Cursor movement (CUU/CUD/CUF/CUB, CUP, CHA)
|
|
7
|
+
- SGR styles (fg/bg + basic attributes)
|
|
8
|
+
- Screen/line erase (ED/EL)
|
|
9
|
+
- In-memory cell grid for diffing against `CellBuffer`
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
```ts
|
|
13
|
+
import { AnsiEmulator } from "@effect-tui/emulator"
|
|
14
|
+
|
|
15
|
+
const emu = new AnsiEmulator(80, 24)
|
|
16
|
+
emu.apply(ansiOutput)
|
|
17
|
+
const diff = emu.diffBuffer(nextBuffer)
|
|
18
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { CellBuffer, Palette, type Wcwidth } from "@effect-tui/core";
|
|
2
|
+
export interface EmulatorBufferSnapshot {
|
|
3
|
+
width: number;
|
|
4
|
+
height: number;
|
|
5
|
+
g: Uint32Array;
|
|
6
|
+
s: Uint32Array;
|
|
7
|
+
cw: Uint8Array;
|
|
8
|
+
}
|
|
9
|
+
export interface BufferDiff {
|
|
10
|
+
index: number;
|
|
11
|
+
x: number;
|
|
12
|
+
y: number;
|
|
13
|
+
actual: {
|
|
14
|
+
g: number;
|
|
15
|
+
s: number;
|
|
16
|
+
cw: number;
|
|
17
|
+
};
|
|
18
|
+
expected: {
|
|
19
|
+
g: number;
|
|
20
|
+
s: number;
|
|
21
|
+
cw: number;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export interface AnsiEmulatorOptions {
|
|
25
|
+
palette?: Palette;
|
|
26
|
+
wcwidth?: Wcwidth;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Lightweight ANSI terminal emulator focused on correctness for renderer tests.
|
|
30
|
+
* - Tracks cursor position
|
|
31
|
+
* - Applies SGR (fg/bg/bold/italic/underline/inverse)
|
|
32
|
+
* - Handles CUU/CUD/CUF/CUB, CUP, CHA, EL, ED
|
|
33
|
+
* - Maintains a grid of cells with glyph + style + width
|
|
34
|
+
*/
|
|
35
|
+
export declare class AnsiEmulator {
|
|
36
|
+
width: number;
|
|
37
|
+
height: number;
|
|
38
|
+
cursorX: number;
|
|
39
|
+
cursorY: number;
|
|
40
|
+
private readonly palette;
|
|
41
|
+
private readonly wcwidth;
|
|
42
|
+
private styleSpec;
|
|
43
|
+
private styleId;
|
|
44
|
+
private g;
|
|
45
|
+
private s;
|
|
46
|
+
private cw;
|
|
47
|
+
constructor(width: number, height: number, options?: AnsiEmulatorOptions);
|
|
48
|
+
reset(): void;
|
|
49
|
+
resize(width: number, height: number): void;
|
|
50
|
+
apply(input: string): void;
|
|
51
|
+
snapshot(): EmulatorBufferSnapshot;
|
|
52
|
+
toCellBuffer(): CellBuffer;
|
|
53
|
+
diffBuffer(buffer: CellBuffer): BufferDiff | null;
|
|
54
|
+
matchesBuffer(buffer: CellBuffer): boolean;
|
|
55
|
+
private findCsiEnd;
|
|
56
|
+
private findOscEnd;
|
|
57
|
+
private handleCsi;
|
|
58
|
+
private parseParams;
|
|
59
|
+
private applySgr;
|
|
60
|
+
private writeCodePoint;
|
|
61
|
+
private lineFeed;
|
|
62
|
+
private scrollUp;
|
|
63
|
+
private clearScreen;
|
|
64
|
+
private clearLine;
|
|
65
|
+
private clearRow;
|
|
66
|
+
private clampRow;
|
|
67
|
+
private clampCol;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,OAAO,EAAkC,KAAK,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAEpG,MAAM,WAAW,sBAAsB;IACtC,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;IACd,CAAC,EAAE,WAAW,CAAA;I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;IAkBjD,aAAa,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO;IAI1C,OAAO,CAAC,UAAU;IAQlB,OAAO,CAAC,UAAU;IAQlB,OAAO,CAAC,SAAS;IAuDjB,OAAO,CAAC,WAAW;IAWnB,OAAO,CAAC,QAAQ;IA0DhB,OAAO,CAAC,cAAc;IAsBtB,OAAO,CAAC,QAAQ;IAQhB,OAAO,CAAC,QAAQ;IAgBhB,OAAO,CAAC,WAAW;IAoBnB,OAAO,CAAC,SAAS;IAYjB,OAAO,CAAC,QAAQ;IAUhB,OAAO,CAAC,QAAQ;IAIhB,OAAO,CAAC,QAAQ;CAGhB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { CellBuffer, Palette, defaultWcwidth } from "@effect-tui/core";
|
|
2
|
+
/**
|
|
3
|
+
* Lightweight ANSI terminal emulator focused on correctness for renderer tests.
|
|
4
|
+
* - Tracks cursor position
|
|
5
|
+
* - Applies SGR (fg/bg/bold/italic/underline/inverse)
|
|
6
|
+
* - Handles CUU/CUD/CUF/CUB, CUP, CHA, EL, ED
|
|
7
|
+
* - Maintains a grid of cells with glyph + style + width
|
|
8
|
+
*/
|
|
9
|
+
export class AnsiEmulator {
|
|
10
|
+
width;
|
|
11
|
+
height;
|
|
12
|
+
cursorX = 0;
|
|
13
|
+
cursorY = 0;
|
|
14
|
+
palette;
|
|
15
|
+
wcwidth;
|
|
16
|
+
styleSpec;
|
|
17
|
+
styleId = 0;
|
|
18
|
+
g;
|
|
19
|
+
s;
|
|
20
|
+
cw;
|
|
21
|
+
constructor(width, height, options = {}) {
|
|
22
|
+
this.width = Math.max(1, width | 0);
|
|
23
|
+
this.height = Math.max(1, height | 0);
|
|
24
|
+
this.palette = options.palette ?? new Palette();
|
|
25
|
+
this.wcwidth = options.wcwidth ?? defaultWcwidth;
|
|
26
|
+
const size = this.width * this.height;
|
|
27
|
+
this.g = new Uint32Array(size);
|
|
28
|
+
this.s = new Uint32Array(size);
|
|
29
|
+
this.cw = new Uint8Array(size);
|
|
30
|
+
this.clearScreen(2);
|
|
31
|
+
}
|
|
32
|
+
reset() {
|
|
33
|
+
this.cursorX = 0;
|
|
34
|
+
this.cursorY = 0;
|
|
35
|
+
this.styleSpec = undefined;
|
|
36
|
+
this.styleId = 0;
|
|
37
|
+
this.clearScreen(2);
|
|
38
|
+
}
|
|
39
|
+
resize(width, height) {
|
|
40
|
+
this.width = Math.max(1, width | 0);
|
|
41
|
+
this.height = Math.max(1, height | 0);
|
|
42
|
+
const size = this.width * this.height;
|
|
43
|
+
this.g = new Uint32Array(size);
|
|
44
|
+
this.s = new Uint32Array(size);
|
|
45
|
+
this.cw = new Uint8Array(size);
|
|
46
|
+
this.cursorX = 0;
|
|
47
|
+
this.cursorY = 0;
|
|
48
|
+
this.clearScreen(2);
|
|
49
|
+
}
|
|
50
|
+
apply(input) {
|
|
51
|
+
let i = 0;
|
|
52
|
+
while (i < input.length) {
|
|
53
|
+
const ch = input[i];
|
|
54
|
+
if (ch === "\u001b") {
|
|
55
|
+
const next = input[i + 1];
|
|
56
|
+
if (next === "[") {
|
|
57
|
+
const end = this.findCsiEnd(input, i + 2);
|
|
58
|
+
if (end < 0)
|
|
59
|
+
break;
|
|
60
|
+
const paramsRaw = input.slice(i + 2, end);
|
|
61
|
+
const final = input[end];
|
|
62
|
+
this.handleCsi(paramsRaw, final);
|
|
63
|
+
i = end + 1;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (next === "]") {
|
|
67
|
+
const end = this.findOscEnd(input, i + 2);
|
|
68
|
+
i = end >= 0 ? end : input.length;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (next === "7") {
|
|
72
|
+
i += 2;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (next === "8") {
|
|
76
|
+
i += 2;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
i += 1;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const cp = input.codePointAt(i) ?? 0;
|
|
83
|
+
const size = cp > 0xffff ? 2 : 1;
|
|
84
|
+
if (cp === 10) {
|
|
85
|
+
this.lineFeed();
|
|
86
|
+
i += size;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (cp === 13) {
|
|
90
|
+
this.cursorX = 0;
|
|
91
|
+
i += size;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (cp === 8) {
|
|
95
|
+
this.cursorX = Math.max(0, this.cursorX - 1);
|
|
96
|
+
i += size;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (cp === 9) {
|
|
100
|
+
const nextTab = Math.min(this.width, ((this.cursorX >> 3) + 1) << 3);
|
|
101
|
+
this.cursorX = nextTab;
|
|
102
|
+
i += size;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
this.writeCodePoint(cp);
|
|
106
|
+
i += size;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
snapshot() {
|
|
110
|
+
return {
|
|
111
|
+
width: this.width,
|
|
112
|
+
height: this.height,
|
|
113
|
+
g: this.g,
|
|
114
|
+
s: this.s,
|
|
115
|
+
cw: this.cw,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
toCellBuffer() {
|
|
119
|
+
const buf = new CellBuffer(this.width, this.height, this.wcwidth);
|
|
120
|
+
buf.g.set(this.g);
|
|
121
|
+
buf.s.set(this.s);
|
|
122
|
+
buf.cw.set(this.cw);
|
|
123
|
+
return buf;
|
|
124
|
+
}
|
|
125
|
+
diffBuffer(buffer) {
|
|
126
|
+
const total = this.width * this.height;
|
|
127
|
+
for (let i = 0; i < total; i++) {
|
|
128
|
+
if (buffer.g[i] !== this.g[i] || buffer.s[i] !== this.s[i] || buffer.cw[i] !== this.cw[i]) {
|
|
129
|
+
const x = i % this.width;
|
|
130
|
+
const y = Math.floor(i / this.width);
|
|
131
|
+
return {
|
|
132
|
+
index: i,
|
|
133
|
+
x,
|
|
134
|
+
y,
|
|
135
|
+
actual: { g: this.g[i], s: this.s[i], cw: this.cw[i] },
|
|
136
|
+
expected: { g: buffer.g[i], s: buffer.s[i], cw: buffer.cw[i] },
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
matchesBuffer(buffer) {
|
|
143
|
+
return this.diffBuffer(buffer) === null;
|
|
144
|
+
}
|
|
145
|
+
findCsiEnd(input, start) {
|
|
146
|
+
for (let i = start; i < input.length; i++) {
|
|
147
|
+
const code = input.charCodeAt(i);
|
|
148
|
+
if (code >= 0x40 && code <= 0x7e)
|
|
149
|
+
return i;
|
|
150
|
+
}
|
|
151
|
+
return -1;
|
|
152
|
+
}
|
|
153
|
+
findOscEnd(input, start) {
|
|
154
|
+
for (let i = start; i < input.length; i++) {
|
|
155
|
+
if (input[i] === "\u0007")
|
|
156
|
+
return i + 1;
|
|
157
|
+
if (input[i] === "\u001b" && input[i + 1] === "\\")
|
|
158
|
+
return i + 2;
|
|
159
|
+
}
|
|
160
|
+
return -1;
|
|
161
|
+
}
|
|
162
|
+
handleCsi(paramsRaw, final) {
|
|
163
|
+
const params = this.parseParams(paramsRaw);
|
|
164
|
+
switch (final) {
|
|
165
|
+
case "A": {
|
|
166
|
+
const n = params[0] || 1;
|
|
167
|
+
this.cursorY = Math.max(0, this.cursorY - n);
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case "B": {
|
|
171
|
+
const n = params[0] || 1;
|
|
172
|
+
this.cursorY = Math.min(this.height - 1, this.cursorY + n);
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case "C": {
|
|
176
|
+
const n = params[0] || 1;
|
|
177
|
+
this.cursorX = Math.min(this.width - 1, this.cursorX + n);
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
case "D": {
|
|
181
|
+
const n = params[0] || 1;
|
|
182
|
+
this.cursorX = Math.max(0, this.cursorX - n);
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case "G": {
|
|
186
|
+
const col = params[0] || 1;
|
|
187
|
+
this.cursorX = this.clampCol(col - 1);
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
case "H":
|
|
191
|
+
case "f": {
|
|
192
|
+
const row = params[0] || 1;
|
|
193
|
+
const col = params[1] || 1;
|
|
194
|
+
this.cursorY = this.clampRow(row - 1);
|
|
195
|
+
this.cursorX = this.clampCol(col - 1);
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
case "J": {
|
|
199
|
+
const mode = params[0] ?? 0;
|
|
200
|
+
this.clearScreen(mode);
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
case "K": {
|
|
204
|
+
const mode = params[0] ?? 0;
|
|
205
|
+
this.clearLine(mode);
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
case "m": {
|
|
209
|
+
this.applySgr(params);
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
default:
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
parseParams(paramsRaw) {
|
|
217
|
+
if (paramsRaw.length === 0)
|
|
218
|
+
return [0];
|
|
219
|
+
const parts = paramsRaw.split(";");
|
|
220
|
+
return parts.map((part) => {
|
|
221
|
+
const cleaned = part.replace(/[^\d-]/g, "");
|
|
222
|
+
if (cleaned === "")
|
|
223
|
+
return 0;
|
|
224
|
+
const num = Number(cleaned);
|
|
225
|
+
return Number.isFinite(num) ? num : 0;
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
applySgr(params) {
|
|
229
|
+
if (params.length === 0) {
|
|
230
|
+
this.styleSpec = undefined;
|
|
231
|
+
this.styleId = 0;
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
let spec = this.styleSpec ? { ...this.styleSpec } : {};
|
|
235
|
+
for (let i = 0; i < params.length; i++) {
|
|
236
|
+
const code = params[i] ?? 0;
|
|
237
|
+
if (code === 0) {
|
|
238
|
+
spec = {};
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (code === 1)
|
|
242
|
+
spec.bold = true;
|
|
243
|
+
else if (code === 3)
|
|
244
|
+
spec.italic = true;
|
|
245
|
+
else if (code === 4)
|
|
246
|
+
spec.underline = true;
|
|
247
|
+
else if (code === 7)
|
|
248
|
+
spec.inverse = true;
|
|
249
|
+
else if (code === 22)
|
|
250
|
+
spec.bold = false;
|
|
251
|
+
else if (code === 23)
|
|
252
|
+
spec.italic = false;
|
|
253
|
+
else if (code === 24)
|
|
254
|
+
spec.underline = false;
|
|
255
|
+
else if (code === 27)
|
|
256
|
+
spec.inverse = false;
|
|
257
|
+
else if (code === 39)
|
|
258
|
+
spec.fg = undefined;
|
|
259
|
+
else if (code === 49)
|
|
260
|
+
spec.bg = undefined;
|
|
261
|
+
else if (code === 38 || code === 48) {
|
|
262
|
+
const isBg = code === 48;
|
|
263
|
+
const mode = params[i + 1];
|
|
264
|
+
if (mode === 5) {
|
|
265
|
+
const value = params[i + 2];
|
|
266
|
+
if (typeof value === "number") {
|
|
267
|
+
if (isBg)
|
|
268
|
+
spec.bg = value;
|
|
269
|
+
else
|
|
270
|
+
spec.fg = value;
|
|
271
|
+
}
|
|
272
|
+
i += 2;
|
|
273
|
+
}
|
|
274
|
+
else if (mode === 2) {
|
|
275
|
+
const r = params[i + 2];
|
|
276
|
+
const g = params[i + 3];
|
|
277
|
+
const b = params[i + 4];
|
|
278
|
+
if (r !== undefined && g !== undefined && b !== undefined) {
|
|
279
|
+
const rgb = { r, g, b };
|
|
280
|
+
if (isBg)
|
|
281
|
+
spec.bg = rgb;
|
|
282
|
+
else
|
|
283
|
+
spec.fg = rgb;
|
|
284
|
+
}
|
|
285
|
+
i += 4;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (Object.keys(spec).length === 0) {
|
|
290
|
+
this.styleSpec = undefined;
|
|
291
|
+
this.styleId = 0;
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
this.styleSpec = spec;
|
|
295
|
+
this.styleId = this.palette.id(spec);
|
|
296
|
+
}
|
|
297
|
+
writeCodePoint(cp) {
|
|
298
|
+
if (this.cursorY < 0 || this.cursorY >= this.height)
|
|
299
|
+
return;
|
|
300
|
+
if (this.cursorX < 0 || this.cursorX >= this.width)
|
|
301
|
+
return;
|
|
302
|
+
const width = this.wcwidth(cp) || 1;
|
|
303
|
+
if (this.cursorX + width > this.width)
|
|
304
|
+
return;
|
|
305
|
+
const idx = this.cursorY * this.width + this.cursorX;
|
|
306
|
+
this.g[idx] = cp;
|
|
307
|
+
this.s[idx] = this.styleId;
|
|
308
|
+
this.cw[idx] = width;
|
|
309
|
+
if (width === 2 && this.cursorX + 1 < this.width) {
|
|
310
|
+
const nextIdx = idx + 1;
|
|
311
|
+
this.g[nextIdx] = 0;
|
|
312
|
+
this.s[nextIdx] = this.styleId;
|
|
313
|
+
this.cw[nextIdx] = 0;
|
|
314
|
+
}
|
|
315
|
+
this.cursorX += width;
|
|
316
|
+
}
|
|
317
|
+
lineFeed() {
|
|
318
|
+
if (this.cursorY === this.height - 1) {
|
|
319
|
+
this.scrollUp(1);
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
this.cursorY += 1;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
scrollUp(lines) {
|
|
326
|
+
const count = Math.max(1, lines | 0);
|
|
327
|
+
const rowSize = this.width;
|
|
328
|
+
for (let y = 0; y < this.height - count; y++) {
|
|
329
|
+
const dst = y * rowSize;
|
|
330
|
+
const src = (y + count) * rowSize;
|
|
331
|
+
this.g.copyWithin(dst, src, src + rowSize);
|
|
332
|
+
this.s.copyWithin(dst, src, src + rowSize);
|
|
333
|
+
this.cw.copyWithin(dst, src, src + rowSize);
|
|
334
|
+
}
|
|
335
|
+
for (let y = this.height - count; y < this.height; y++) {
|
|
336
|
+
this.clearRow(y, 0, this.width);
|
|
337
|
+
}
|
|
338
|
+
this.cursorY = this.height - 1;
|
|
339
|
+
}
|
|
340
|
+
clearScreen(mode) {
|
|
341
|
+
if (mode === 2) {
|
|
342
|
+
for (let y = 0; y < this.height; y++) {
|
|
343
|
+
this.clearRow(y, 0, this.width);
|
|
344
|
+
}
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (mode === 1) {
|
|
348
|
+
for (let y = 0; y < this.cursorY; y++) {
|
|
349
|
+
this.clearRow(y, 0, this.width);
|
|
350
|
+
}
|
|
351
|
+
this.clearRow(this.cursorY, 0, this.cursorX + 1);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
for (let y = this.cursorY; y < this.height; y++) {
|
|
355
|
+
const start = y === this.cursorY ? this.cursorX : 0;
|
|
356
|
+
this.clearRow(y, start, this.width);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
clearLine(mode) {
|
|
360
|
+
if (mode === 2) {
|
|
361
|
+
this.clearRow(this.cursorY, 0, this.width);
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (mode === 1) {
|
|
365
|
+
this.clearRow(this.cursorY, 0, this.cursorX + 1);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
this.clearRow(this.cursorY, this.cursorX, this.width);
|
|
369
|
+
}
|
|
370
|
+
clearRow(y, startX, endX) {
|
|
371
|
+
const rowStart = y * this.width;
|
|
372
|
+
for (let x = Math.max(0, startX); x < Math.min(this.width, endX); x++) {
|
|
373
|
+
const idx = rowStart + x;
|
|
374
|
+
this.g[idx] = 32;
|
|
375
|
+
this.s[idx] = this.styleId;
|
|
376
|
+
this.cw[idx] = 1;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
clampRow(row) {
|
|
380
|
+
return Math.max(0, Math.min(this.height - 1, row));
|
|
381
|
+
}
|
|
382
|
+
clampCol(col) {
|
|
383
|
+
return Math.max(0, Math.min(this.width - 1, col));
|
|
384
|
+
}
|
|
385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CACD"}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@effect-tui/emulator",
|
|
3
|
+
"version": "0.1.7",
|
|
4
|
+
"description": "Lightweight ANSI terminal emulator for effect-tui tests",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"src",
|
|
9
|
+
"README.md",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"bun": "./src/index.ts",
|
|
15
|
+
"import": "./src/index.ts",
|
|
16
|
+
"types": "./src/index.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"types": "./src/index.ts",
|
|
20
|
+
"keywords": [
|
|
21
|
+
"terminal",
|
|
22
|
+
"tui",
|
|
23
|
+
"ansi",
|
|
24
|
+
"emulator",
|
|
25
|
+
"testing"
|
|
26
|
+
],
|
|
27
|
+
"author": "Kit Langton",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/kitlangton/effect-tui.git",
|
|
32
|
+
"directory": "packages/effect-tui-emulator"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/kitlangton/effect-tui",
|
|
35
|
+
"bugs": "https://github.com/kitlangton/effect-tui/issues",
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p .",
|
|
41
|
+
"typecheck": "tsc -p . --noEmit",
|
|
42
|
+
"test": "vitest run",
|
|
43
|
+
"test:watch": "vitest",
|
|
44
|
+
"format": "biome format --write .",
|
|
45
|
+
"format:check": "biome format .",
|
|
46
|
+
"prepublishOnly": "bun run typecheck && bun run build"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@effect-tui/core": "workspace:^"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "^25.0.3",
|
|
53
|
+
"typescript": "^5.9.3",
|
|
54
|
+
"vitest": "^4.0.16"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
import { CellBuffer, Palette, type StyleSpec, defaultWcwidth, type Wcwidth } from "@effect-tui/core"
|
|
2
|
+
|
|
3
|
+
export interface EmulatorBufferSnapshot {
|
|
4
|
+
width: number
|
|
5
|
+
height: number
|
|
6
|
+
g: Uint32Array
|
|
7
|
+
s: Uint32Array
|
|
8
|
+
cw: Uint8Array
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface BufferDiff {
|
|
12
|
+
index: number
|
|
13
|
+
x: number
|
|
14
|
+
y: number
|
|
15
|
+
actual: { g: number; s: number; cw: number }
|
|
16
|
+
expected: { g: number; s: number; cw: number }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface AnsiEmulatorOptions {
|
|
20
|
+
palette?: Palette
|
|
21
|
+
wcwidth?: Wcwidth
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Lightweight ANSI terminal emulator focused on correctness for renderer tests.
|
|
26
|
+
* - Tracks cursor position
|
|
27
|
+
* - Applies SGR (fg/bg/bold/italic/underline/inverse)
|
|
28
|
+
* - Handles CUU/CUD/CUF/CUB, CUP, CHA, EL, ED
|
|
29
|
+
* - Maintains a grid of cells with glyph + style + width
|
|
30
|
+
*/
|
|
31
|
+
export class AnsiEmulator {
|
|
32
|
+
width: number
|
|
33
|
+
height: number
|
|
34
|
+
cursorX = 0
|
|
35
|
+
cursorY = 0
|
|
36
|
+
|
|
37
|
+
private readonly palette: Palette
|
|
38
|
+
private readonly wcwidth: Wcwidth
|
|
39
|
+
private styleSpec: StyleSpec | undefined
|
|
40
|
+
private styleId = 0
|
|
41
|
+
|
|
42
|
+
private g: Uint32Array
|
|
43
|
+
private s: Uint32Array
|
|
44
|
+
private cw: Uint8Array
|
|
45
|
+
|
|
46
|
+
constructor(width: number, height: number, options: AnsiEmulatorOptions = {}) {
|
|
47
|
+
this.width = Math.max(1, width | 0)
|
|
48
|
+
this.height = Math.max(1, height | 0)
|
|
49
|
+
this.palette = options.palette ?? new Palette()
|
|
50
|
+
this.wcwidth = options.wcwidth ?? defaultWcwidth
|
|
51
|
+
const size = this.width * this.height
|
|
52
|
+
this.g = new Uint32Array(size)
|
|
53
|
+
this.s = new Uint32Array(size)
|
|
54
|
+
this.cw = new Uint8Array(size)
|
|
55
|
+
this.clearScreen(2)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
reset(): void {
|
|
59
|
+
this.cursorX = 0
|
|
60
|
+
this.cursorY = 0
|
|
61
|
+
this.styleSpec = undefined
|
|
62
|
+
this.styleId = 0
|
|
63
|
+
this.clearScreen(2)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
resize(width: number, height: number): void {
|
|
67
|
+
this.width = Math.max(1, width | 0)
|
|
68
|
+
this.height = Math.max(1, height | 0)
|
|
69
|
+
const size = this.width * this.height
|
|
70
|
+
this.g = new Uint32Array(size)
|
|
71
|
+
this.s = new Uint32Array(size)
|
|
72
|
+
this.cw = new Uint8Array(size)
|
|
73
|
+
this.cursorX = 0
|
|
74
|
+
this.cursorY = 0
|
|
75
|
+
this.clearScreen(2)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
apply(input: string): void {
|
|
79
|
+
let i = 0
|
|
80
|
+
while (i < input.length) {
|
|
81
|
+
const ch = input[i]
|
|
82
|
+
if (ch === "\u001b") {
|
|
83
|
+
const next = input[i + 1]
|
|
84
|
+
if (next === "[") {
|
|
85
|
+
const end = this.findCsiEnd(input, i + 2)
|
|
86
|
+
if (end < 0) break
|
|
87
|
+
const paramsRaw = input.slice(i + 2, end)
|
|
88
|
+
const final = input[end]
|
|
89
|
+
this.handleCsi(paramsRaw, final)
|
|
90
|
+
i = end + 1
|
|
91
|
+
continue
|
|
92
|
+
}
|
|
93
|
+
if (next === "]") {
|
|
94
|
+
const end = this.findOscEnd(input, i + 2)
|
|
95
|
+
i = end >= 0 ? end : input.length
|
|
96
|
+
continue
|
|
97
|
+
}
|
|
98
|
+
if (next === "7") {
|
|
99
|
+
i += 2
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
if (next === "8") {
|
|
103
|
+
i += 2
|
|
104
|
+
continue
|
|
105
|
+
}
|
|
106
|
+
i += 1
|
|
107
|
+
continue
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const cp = input.codePointAt(i) ?? 0
|
|
111
|
+
const size = cp > 0xffff ? 2 : 1
|
|
112
|
+
|
|
113
|
+
if (cp === 10) {
|
|
114
|
+
this.lineFeed()
|
|
115
|
+
i += size
|
|
116
|
+
continue
|
|
117
|
+
}
|
|
118
|
+
if (cp === 13) {
|
|
119
|
+
this.cursorX = 0
|
|
120
|
+
i += size
|
|
121
|
+
continue
|
|
122
|
+
}
|
|
123
|
+
if (cp === 8) {
|
|
124
|
+
this.cursorX = Math.max(0, this.cursorX - 1)
|
|
125
|
+
i += size
|
|
126
|
+
continue
|
|
127
|
+
}
|
|
128
|
+
if (cp === 9) {
|
|
129
|
+
const nextTab = Math.min(this.width, ((this.cursorX >> 3) + 1) << 3)
|
|
130
|
+
this.cursorX = nextTab
|
|
131
|
+
i += size
|
|
132
|
+
continue
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
this.writeCodePoint(cp)
|
|
136
|
+
i += size
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
snapshot(): EmulatorBufferSnapshot {
|
|
141
|
+
return {
|
|
142
|
+
width: this.width,
|
|
143
|
+
height: this.height,
|
|
144
|
+
g: this.g,
|
|
145
|
+
s: this.s,
|
|
146
|
+
cw: this.cw,
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
toCellBuffer(): CellBuffer {
|
|
151
|
+
const buf = new CellBuffer(this.width, this.height, this.wcwidth)
|
|
152
|
+
buf.g.set(this.g)
|
|
153
|
+
buf.s.set(this.s)
|
|
154
|
+
buf.cw.set(this.cw)
|
|
155
|
+
return buf
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
diffBuffer(buffer: CellBuffer): BufferDiff | null {
|
|
159
|
+
const total = this.width * this.height
|
|
160
|
+
for (let i = 0; i < total; i++) {
|
|
161
|
+
if (buffer.g[i] !== this.g[i] || buffer.s[i] !== this.s[i] || buffer.cw[i] !== this.cw[i]) {
|
|
162
|
+
const x = i % this.width
|
|
163
|
+
const y = Math.floor(i / this.width)
|
|
164
|
+
return {
|
|
165
|
+
index: i,
|
|
166
|
+
x,
|
|
167
|
+
y,
|
|
168
|
+
actual: { g: this.g[i], s: this.s[i], cw: this.cw[i] },
|
|
169
|
+
expected: { g: buffer.g[i], s: buffer.s[i], cw: buffer.cw[i] },
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return null
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
matchesBuffer(buffer: CellBuffer): boolean {
|
|
177
|
+
return this.diffBuffer(buffer) === null
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private findCsiEnd(input: string, start: number): number {
|
|
181
|
+
for (let i = start; i < input.length; i++) {
|
|
182
|
+
const code = input.charCodeAt(i)
|
|
183
|
+
if (code >= 0x40 && code <= 0x7e) return i
|
|
184
|
+
}
|
|
185
|
+
return -1
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private findOscEnd(input: string, start: number): number {
|
|
189
|
+
for (let i = start; i < input.length; i++) {
|
|
190
|
+
if (input[i] === "\u0007") return i + 1
|
|
191
|
+
if (input[i] === "\u001b" && input[i + 1] === "\\") return i + 2
|
|
192
|
+
}
|
|
193
|
+
return -1
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private handleCsi(paramsRaw: string, final: string): void {
|
|
197
|
+
const params = this.parseParams(paramsRaw)
|
|
198
|
+
switch (final) {
|
|
199
|
+
case "A": {
|
|
200
|
+
const n = params[0] || 1
|
|
201
|
+
this.cursorY = Math.max(0, this.cursorY - n)
|
|
202
|
+
break
|
|
203
|
+
}
|
|
204
|
+
case "B": {
|
|
205
|
+
const n = params[0] || 1
|
|
206
|
+
this.cursorY = Math.min(this.height - 1, this.cursorY + n)
|
|
207
|
+
break
|
|
208
|
+
}
|
|
209
|
+
case "C": {
|
|
210
|
+
const n = params[0] || 1
|
|
211
|
+
this.cursorX = Math.min(this.width - 1, this.cursorX + n)
|
|
212
|
+
break
|
|
213
|
+
}
|
|
214
|
+
case "D": {
|
|
215
|
+
const n = params[0] || 1
|
|
216
|
+
this.cursorX = Math.max(0, this.cursorX - n)
|
|
217
|
+
break
|
|
218
|
+
}
|
|
219
|
+
case "G": {
|
|
220
|
+
const col = params[0] || 1
|
|
221
|
+
this.cursorX = this.clampCol(col - 1)
|
|
222
|
+
break
|
|
223
|
+
}
|
|
224
|
+
case "H":
|
|
225
|
+
case "f": {
|
|
226
|
+
const row = params[0] || 1
|
|
227
|
+
const col = params[1] || 1
|
|
228
|
+
this.cursorY = this.clampRow(row - 1)
|
|
229
|
+
this.cursorX = this.clampCol(col - 1)
|
|
230
|
+
break
|
|
231
|
+
}
|
|
232
|
+
case "J": {
|
|
233
|
+
const mode = params[0] ?? 0
|
|
234
|
+
this.clearScreen(mode)
|
|
235
|
+
break
|
|
236
|
+
}
|
|
237
|
+
case "K": {
|
|
238
|
+
const mode = params[0] ?? 0
|
|
239
|
+
this.clearLine(mode)
|
|
240
|
+
break
|
|
241
|
+
}
|
|
242
|
+
case "m": {
|
|
243
|
+
this.applySgr(params)
|
|
244
|
+
break
|
|
245
|
+
}
|
|
246
|
+
default:
|
|
247
|
+
break
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private parseParams(paramsRaw: string): number[] {
|
|
252
|
+
if (paramsRaw.length === 0) return [0]
|
|
253
|
+
const parts = paramsRaw.split(";")
|
|
254
|
+
return parts.map((part) => {
|
|
255
|
+
const cleaned = part.replace(/[^\d-]/g, "")
|
|
256
|
+
if (cleaned === "") return 0
|
|
257
|
+
const num = Number(cleaned)
|
|
258
|
+
return Number.isFinite(num) ? num : 0
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private applySgr(params: number[]): void {
|
|
263
|
+
if (params.length === 0) {
|
|
264
|
+
this.styleSpec = undefined
|
|
265
|
+
this.styleId = 0
|
|
266
|
+
return
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let spec: StyleSpec = this.styleSpec ? { ...this.styleSpec } : {}
|
|
270
|
+
for (let i = 0; i < params.length; i++) {
|
|
271
|
+
const code = params[i] ?? 0
|
|
272
|
+
if (code === 0) {
|
|
273
|
+
spec = {}
|
|
274
|
+
continue
|
|
275
|
+
}
|
|
276
|
+
if (code === 1) spec.bold = true
|
|
277
|
+
else if (code === 3) spec.italic = true
|
|
278
|
+
else if (code === 4) spec.underline = true
|
|
279
|
+
else if (code === 7) spec.inverse = true
|
|
280
|
+
else if (code === 22) spec.bold = false
|
|
281
|
+
else if (code === 23) spec.italic = false
|
|
282
|
+
else if (code === 24) spec.underline = false
|
|
283
|
+
else if (code === 27) spec.inverse = false
|
|
284
|
+
else if (code === 39) spec.fg = undefined
|
|
285
|
+
else if (code === 49) spec.bg = undefined
|
|
286
|
+
else if (code === 38 || code === 48) {
|
|
287
|
+
const isBg = code === 48
|
|
288
|
+
const mode = params[i + 1]
|
|
289
|
+
if (mode === 5) {
|
|
290
|
+
const value = params[i + 2]
|
|
291
|
+
if (typeof value === "number") {
|
|
292
|
+
if (isBg) spec.bg = value
|
|
293
|
+
else spec.fg = value
|
|
294
|
+
}
|
|
295
|
+
i += 2
|
|
296
|
+
} else if (mode === 2) {
|
|
297
|
+
const r = params[i + 2]
|
|
298
|
+
const g = params[i + 3]
|
|
299
|
+
const b = params[i + 4]
|
|
300
|
+
if (r !== undefined && g !== undefined && b !== undefined) {
|
|
301
|
+
const rgb = { r, g, b }
|
|
302
|
+
if (isBg) spec.bg = rgb
|
|
303
|
+
else spec.fg = rgb
|
|
304
|
+
}
|
|
305
|
+
i += 4
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
if (Object.keys(spec).length === 0) {
|
|
311
|
+
this.styleSpec = undefined
|
|
312
|
+
this.styleId = 0
|
|
313
|
+
return
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
this.styleSpec = spec
|
|
317
|
+
this.styleId = this.palette.id(spec)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
private writeCodePoint(cp: number): void {
|
|
321
|
+
if (this.cursorY < 0 || this.cursorY >= this.height) return
|
|
322
|
+
if (this.cursorX < 0 || this.cursorX >= this.width) return
|
|
323
|
+
|
|
324
|
+
const width = this.wcwidth(cp) || 1
|
|
325
|
+
if (this.cursorX + width > this.width) return
|
|
326
|
+
|
|
327
|
+
const idx = this.cursorY * this.width + this.cursorX
|
|
328
|
+
this.g[idx] = cp
|
|
329
|
+
this.s[idx] = this.styleId
|
|
330
|
+
this.cw[idx] = width
|
|
331
|
+
|
|
332
|
+
if (width === 2 && this.cursorX + 1 < this.width) {
|
|
333
|
+
const nextIdx = idx + 1
|
|
334
|
+
this.g[nextIdx] = 0
|
|
335
|
+
this.s[nextIdx] = this.styleId
|
|
336
|
+
this.cw[nextIdx] = 0
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
this.cursorX += width
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
private lineFeed(): void {
|
|
343
|
+
if (this.cursorY === this.height - 1) {
|
|
344
|
+
this.scrollUp(1)
|
|
345
|
+
} else {
|
|
346
|
+
this.cursorY += 1
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private scrollUp(lines: number): void {
|
|
351
|
+
const count = Math.max(1, lines | 0)
|
|
352
|
+
const rowSize = this.width
|
|
353
|
+
for (let y = 0; y < this.height - count; y++) {
|
|
354
|
+
const dst = y * rowSize
|
|
355
|
+
const src = (y + count) * rowSize
|
|
356
|
+
this.g.copyWithin(dst, src, src + rowSize)
|
|
357
|
+
this.s.copyWithin(dst, src, src + rowSize)
|
|
358
|
+
this.cw.copyWithin(dst, src, src + rowSize)
|
|
359
|
+
}
|
|
360
|
+
for (let y = this.height - count; y < this.height; y++) {
|
|
361
|
+
this.clearRow(y, 0, this.width)
|
|
362
|
+
}
|
|
363
|
+
this.cursorY = this.height - 1
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private clearScreen(mode: number): void {
|
|
367
|
+
if (mode === 2) {
|
|
368
|
+
for (let y = 0; y < this.height; y++) {
|
|
369
|
+
this.clearRow(y, 0, this.width)
|
|
370
|
+
}
|
|
371
|
+
return
|
|
372
|
+
}
|
|
373
|
+
if (mode === 1) {
|
|
374
|
+
for (let y = 0; y < this.cursorY; y++) {
|
|
375
|
+
this.clearRow(y, 0, this.width)
|
|
376
|
+
}
|
|
377
|
+
this.clearRow(this.cursorY, 0, this.cursorX + 1)
|
|
378
|
+
return
|
|
379
|
+
}
|
|
380
|
+
for (let y = this.cursorY; y < this.height; y++) {
|
|
381
|
+
const start = y === this.cursorY ? this.cursorX : 0
|
|
382
|
+
this.clearRow(y, start, this.width)
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
private clearLine(mode: number): void {
|
|
387
|
+
if (mode === 2) {
|
|
388
|
+
this.clearRow(this.cursorY, 0, this.width)
|
|
389
|
+
return
|
|
390
|
+
}
|
|
391
|
+
if (mode === 1) {
|
|
392
|
+
this.clearRow(this.cursorY, 0, this.cursorX + 1)
|
|
393
|
+
return
|
|
394
|
+
}
|
|
395
|
+
this.clearRow(this.cursorY, this.cursorX, this.width)
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
private clearRow(y: number, startX: number, endX: number): void {
|
|
399
|
+
const rowStart = y * this.width
|
|
400
|
+
for (let x = Math.max(0, startX); x < Math.min(this.width, endX); x++) {
|
|
401
|
+
const idx = rowStart + x
|
|
402
|
+
this.g[idx] = 32
|
|
403
|
+
this.s[idx] = this.styleId
|
|
404
|
+
this.cw[idx] = 1
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private clampRow(row: number): number {
|
|
409
|
+
return Math.max(0, Math.min(this.height - 1, row))
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
private clampCol(col: number): number {
|
|
413
|
+
return Math.max(0, Math.min(this.width - 1, col))
|
|
414
|
+
}
|
|
415
|
+
}
|