Repository navigation
Expand file tree
/
Copy pathline_buffer.js
More file actions
375 lines (332 loc) · 13.7 KB
/
Copy pathline_buffer.js
File metadata and controls
375 lines (332 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
'use strict';
const { renderStringLength, renderSplitPos } = require('./string_util');
//
// LineBuffer — low-level line storage with per-character Uint32 attribute
// words. No view dependencies; fully unit-testable in isolation.
//
// Attribute word layout (32-bit unsigned):
//
// [31:24] fg color (0-255 ANSI palette index, or truecolor key)
// [23:16] bg color (0-255 ANSI palette index, or truecolor key)
// [15] bold
// [14] blink
// [13] underline
// [12] italic
// [11] strikethrough
// [10:8] colorSource (see ColorSource enum)
// [7] TC_FG — fg uses truecolor map entry
// [6] TC_BG — bg uses truecolor map entry
// [5:0] reserved
//
// Lines use soft (eol=false) or hard (eol=true) line-end markers.
// Hard breaks originate from Enter key or explicit \n in source text.
// Soft breaks are word-wrap artifacts and rejoin transparently.
//
// A "paragraph" is a contiguous sequence of lines whose last member has
// eol=true. All other members have eol=false.
//
const ColorSource = Object.freeze({
DEFAULT: 0,
PIPE: 1,
ANSI: 2,
IMPORTED: 3,
TRUECOLOR: 7,
});
// ─── Attribute helpers ────────────────────────────────────────────────────────
// makeAttr({ fg, bg, bold, blink, underline, italic, strikethrough,
// colorSrc, tcFg, tcBg }) → Uint32
function makeAttr({
fg = 7,
bg = 0,
bold = false,
blink = false,
underline = false,
italic = false,
strikethrough = false,
colorSrc = ColorSource.DEFAULT,
tcFg = false,
tcBg = false,
} = {}) {
// Use multiplication for fg shift to avoid signed-integer issues with << 24
return (
(((fg & 0xff) * 0x1000000) | // bits 31:24
((bg & 0xff) << 16) | // bits 23:16
(bold ? 1 << 15 : 0) |
(blink ? 1 << 14 : 0) |
(underline ? 1 << 13 : 0) |
(italic ? 1 << 12 : 0) |
(strikethrough ? 1 << 11 : 0) |
((colorSrc & 0x7) << 8) | // bits 10:8
(tcFg ? 1 << 7 : 0) |
(tcBg ? 1 << 6 : 0)) >>>
0
); // coerce to unsigned 32-bit
}
// parseAttr(attr) → plain object (all fields)
function parseAttr(attr) {
return {
fg: (attr >>> 24) & 0xff,
bg: (attr >>> 16) & 0xff,
bold: !!(attr & (1 << 15)),
blink: !!(attr & (1 << 14)),
underline: !!(attr & (1 << 13)),
italic: !!(attr & (1 << 12)),
strikethrough: !!(attr & (1 << 11)),
colorSrc: (attr >>> 8) & 0x7,
tcFg: !!(attr & (1 << 7)),
tcBg: !!(attr & (1 << 6)),
};
}
function getFg(attr) {
return (attr >>> 24) & 0xff;
}
function getBg(attr) {
return (attr >>> 16) & 0xff;
}
function getColorSrc(attr) {
return (attr >>> 8) & 0x7;
}
// ─── Uint32Array helpers ──────────────────────────────────────────────────────
function u32Insert(arr, index, value) {
const out = new Uint32Array(arr.length + 1);
out.set(arr.subarray(0, index), 0);
out[index] = value;
out.set(arr.subarray(index), index + 1);
return out;
}
function u32Delete(arr, index) {
// Empty array or out-of-range index is a no-op — mirrors String.slice's
// forgiving semantics and prevents `new Uint32Array(-1)` RangeError when
// upstream cursor state drifts past the line content.
if (arr.length === 0 || index < 0 || index >= arr.length) {
return arr;
}
const out = new Uint32Array(arr.length - 1);
out.set(arr.subarray(0, index), 0);
out.set(arr.subarray(index + 1), index);
return out;
}
function u32Concat(a, b) {
const out = new Uint32Array(a.length + b.length);
out.set(a, 0);
out.set(b, a.length);
return out;
}
// ─── Internal: word-wrap a flat string+attrs into line objects ────────────────
// _wrapText(text, attrs, width, hardEol) → Line[]
//
// Splits text at word boundaries (last space before width visible chars).
// If no space is found in the window, breaks at the width boundary (hard
// character wrap). The space at the break point is consumed (not stored in
// either line). Non-final lines get eol=false; the final line gets
// eol=hardEol.
//
// Width is measured in visible (rendered) characters — ANSI escape sequences
// and |XX pipe color codes are skipped and do not contribute to line length.
function _wrapText(text, attrs, width, hardEol) {
if (text.length === 0) {
return [{ chars: '', attrs: new Uint32Array(0), eol: hardEol, initialAttr: 0 }];
}
const lines = [];
let pos = 0;
while (pos < text.length) {
const remaining = text.slice(pos);
const remAttrs = attrs.slice(pos);
// Replace \t with ' ' for measurement — each tab renders as one space column.
// Indices stay 1:1 since both are single BMP chars, so splitAt is reusable.
const remainingVis = remaining.replace(/\t/g, ' ');
if (renderStringLength(remainingVis) <= width) {
lines.push({
chars: remaining,
attrs: remAttrs,
eol: hardEol,
initialAttr: remAttrs.length > 0 ? remAttrs[0] : 0,
});
break;
}
// Find the last space within the first `width` visible chars
const splitAt = renderSplitPos(remainingVis, width);
const window = remaining.slice(0, splitAt);
const lastSpace = window.lastIndexOf(' ');
const wrapAt = lastSpace > 0 ? lastSpace : splitAt;
const spaceConsumed = remaining[wrapAt] === ' ';
lines.push({
chars: remaining.slice(0, wrapAt),
attrs: remAttrs.slice(0, wrapAt),
eol: false,
eolSep: spaceConsumed ? ' ' : '',
initialAttr: remAttrs.length > 0 ? remAttrs[0] : 0,
});
// Consume the space at the break point (if that was the wrap reason)
pos += wrapAt + (spaceConsumed ? 1 : 0);
}
return lines;
}
// ─── LineBuffer ───────────────────────────────────────────────────────────────
class LineBuffer {
constructor({ width = 79 } = {}) {
this.width = width;
this.lines = [
{ chars: '', attrs: new Uint32Array(0), eol: true, initialAttr: 0 },
];
}
// ── Single-character mutations (no auto-wrap) ────────────────────────────
// insertChar(lineIndex, col, char, attr)
// Inserts one character at col within the given line. Does NOT reflow.
insertChar(lineIndex, col, char, attr = 0) {
const line = this.lines[lineIndex];
line.chars = line.chars.slice(0, col) + char + line.chars.slice(col);
line.attrs = u32Insert(line.attrs, col, attr);
}
// deleteChar(lineIndex, col)
// Removes the character at col from the given line. Does NOT reflow.
// Out-of-range lineIndex or col is a no-op, keeping chars/attrs in sync.
deleteChar(lineIndex, col) {
const line = this.lines[lineIndex];
if (!line || col < 0 || col >= line.chars.length) {
return;
}
line.chars = line.chars.slice(0, col) + line.chars.slice(col + 1);
line.attrs = u32Delete(line.attrs, col);
}
// ── Line structure mutations ─────────────────────────────────────────────
// splitLine(lineIndex, col)
// Splits the line at col, creating a hard break (Enter key semantics).
// The left portion keeps the current line; a new line is inserted after it
// with the right portion. The left line's eol becomes true.
splitLine(lineIndex, col) {
const line = this.lines[lineIndex];
const right = {
chars: line.chars.slice(col),
attrs: line.attrs.slice(col),
eol: line.eol, // right half inherits the original break type
initialAttr: col < line.attrs.length ? line.attrs[col] : line.initialAttr,
};
line.chars = line.chars.slice(0, col);
line.attrs = line.attrs.slice(0, col);
line.eol = true; // this is now a hard break
this.lines.splice(lineIndex + 1, 0, right);
}
// joinLines(lineIndex)
// Joins line at lineIndex with the line immediately after it (Backspace
// at column 0, or Delete at end of line). The combined line inherits
// the eol of the next line. No-op if lineIndex is the last line.
joinLines(lineIndex) {
if (lineIndex >= this.lines.length - 1) {
return;
}
const line = this.lines[lineIndex];
const next = this.lines[lineIndex + 1];
line.chars = line.chars + next.chars;
line.attrs = u32Concat(line.attrs, next.attrs);
line.eol = next.eol;
this.lines.splice(lineIndex + 1, 1);
}
// ── Paragraph operations ────────────────────────────────────────────────
// _paragraphRange(lineIndex) → { start, end }
// Returns the index range of the paragraph that contains lineIndex.
// A paragraph is bounded by hard breaks (eol=true).
_paragraphRange(lineIndex) {
let start = lineIndex;
// Walk backward while the previous line is a soft wrap (eol=false)
while (start > 0 && !this.lines[start - 1].eol) {
start--;
}
let end = lineIndex;
// Walk forward while this line is a soft wrap (eol=false)
while (end < this.lines.length - 1 && !this.lines[end].eol) {
end++;
}
return { start, end };
}
// rewrapParagraph(lineIndex) → { start, end }
// Re-wraps the paragraph containing lineIndex to fit within this.width.
// Soft-wrapped lines are rejoined (with a space) then re-split.
// Hard breaks are preserved. Returns the new index range.
rewrapParagraph(lineIndex) {
const { start, end } = this._paragraphRange(lineIndex);
const wasEol = this.lines[end].eol;
// Rejoin the paragraph: soft-wrapped lines are separated by a space
// (the space that was consumed at the wrap point).
let allChars = '';
let allAttrs = new Uint32Array(0);
for (let i = start; i <= end; i++) {
if (i > start) {
// Reinsert the stripped space; use attr of the preceding char
const prevAttrs = this.lines[i - 1].attrs;
const spaceAttr =
prevAttrs.length > 0 ? prevAttrs[prevAttrs.length - 1] : 0;
allChars += ' ';
allAttrs = u32Concat(allAttrs, new Uint32Array([spaceAttr]));
}
allChars += this.lines[i].chars;
allAttrs = u32Concat(allAttrs, this.lines[i].attrs);
}
const newLines = _wrapText(allChars, allAttrs, this.width, wasEol);
this.lines.splice(start, end - start + 1, ...newLines);
return { start, end: start + newLines.length - 1 };
}
// ── Bulk text operations ────────────────────────────────────────────────
// setText(text)
// Replaces buffer contents with plain text. \n is a hard break; long
// lines are wrapped at word boundaries to fit within this.width.
// Attributes default to 0 for all characters.
setText(text) {
this.lines = [];
const hardLines = text.split('\n');
for (const hardLine of hardLines) {
const attrs = new Uint32Array(hardLine.length);
const wrapped = _wrapText(hardLine, attrs, this.width, true /* hardEol */);
this.lines.push(...wrapped);
}
if (this.lines.length === 0) {
this.lines.push({
chars: '',
attrs: new Uint32Array(0),
eol: true,
initialAttr: 0,
});
}
}
// getText() → string
// Extracts plain text from the buffer. Hard breaks become \n; the space
// consumed at soft-wrap points is reinserted. No trailing \n is added
// for the very last line.
getText() {
const parts = [];
for (let i = 0; i < this.lines.length; i++) {
parts.push(this.lines[i].chars);
if (i < this.lines.length - 1) {
if (this.lines[i].eol) {
parts.push('\n');
} else {
// eolSep records whether a space was consumed at this soft-wrap
// boundary. Undefined means "legacy line" — default to space.
const sep = this.lines[i].eolSep;
parts.push(sep !== undefined ? sep : ' ');
}
}
}
return parts.join('');
}
// setWidth(width)
// Updates the wrap width and re-wraps every paragraph in the buffer.
setWidth(width) {
this.width = width;
let i = 0;
while (i < this.lines.length) {
const range = this.rewrapParagraph(i);
i = range.end + 1;
}
}
}
exports.LineBuffer = LineBuffer;
exports.ColorSource = ColorSource;
exports.makeAttr = makeAttr;
exports.parseAttr = parseAttr;
exports.getFg = getFg;
exports.getBg = getBg;
exports.getColorSrc = getColorSrc;
exports.u32Insert = u32Insert;
exports.u32Delete = u32Delete;
exports.u32Concat = u32Concat;