brsh 2.8.1 → 3.0.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/.github/workflows/release.yml +3 -1
- package/CHANGELOG.md +25 -0
- package/bin/brsh-shell.js +16 -131
- package/bin/exportFileSystem.js +4 -0
- package/bin/zsh-server.js +261 -0
- package/dist/brsh.css +1 -1
- package/dist/shell.es.js +783 -418
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +30 -15
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.cjs.js +77 -100
- package/dist/terminal.cjs.js.map +1 -1
- package/dist/terminal.es.js +1382 -603
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +77 -100
- package/dist/terminal.min.js.map +1 -1
- package/dist/wbt.es.js +266 -116
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +7 -5
- package/dist/wbt.min.js.map +1 -1
- package/index.js +472 -0
- package/lib/Command.js +30 -0
- package/lib/InteractiveCommand.js +28 -0
- package/lib/ReplCommand.js +15 -0
- package/lib/Terminal.js +769 -510
- package/lib/TtyStream.js +181 -0
- package/lib/builtins/cat.js +2 -2
- package/lib/builtins/grep.js +2 -2
- package/lib/local/clear.js +3 -0
- package/lib/terminal/DomRenderer.js +325 -0
- package/lib/terminal/PtyBridge.js +169 -0
- package/lib/terminal/ScreenBuffer.js +594 -0
- package/lib/terminal/VtParser.js +728 -0
- package/package.json +4 -2
- package/styles/terminal.css +33 -4
- package/test/terminal.test.js +762 -40
|
@@ -0,0 +1,594 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const ATTR_BOLD = 1;
|
|
4
|
+
const ATTR_DIM = 2;
|
|
5
|
+
const ATTR_ITALIC = 4;
|
|
6
|
+
const ATTR_UNDERLINE = 8;
|
|
7
|
+
const ATTR_BLINK = 16;
|
|
8
|
+
const ATTR_INVERSE = 32;
|
|
9
|
+
const ATTR_HIDDEN = 64;
|
|
10
|
+
const ATTR_STRIKE = 128;
|
|
11
|
+
|
|
12
|
+
function createCell(ch = ' ', fg = null, bg = null, flags = 0, link = null) {
|
|
13
|
+
return { ch, fg, bg, flags, link };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function cloneCell(cell) {
|
|
17
|
+
return {
|
|
18
|
+
ch: cell.ch,
|
|
19
|
+
fg: cell.fg,
|
|
20
|
+
bg: cell.bg,
|
|
21
|
+
flags: cell.flags,
|
|
22
|
+
link: cell.link
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class ScreenBuffer {
|
|
27
|
+
constructor(cols = 80, rows = 24, options = {}) {
|
|
28
|
+
this.cols = Math.max(1, cols);
|
|
29
|
+
this.rows = Math.max(1, rows);
|
|
30
|
+
this.maxScrollback = options.maxScrollback ?? 1000;
|
|
31
|
+
this.convertEol = options.convertEol !== false;
|
|
32
|
+
|
|
33
|
+
this.scrollback = [];
|
|
34
|
+
this.viewportOffset = 0; // 0 = at bottom; >0 = lines scrolled up into scrollback
|
|
35
|
+
|
|
36
|
+
this.primaryGrid = this._createGrid(this.cols, this.rows);
|
|
37
|
+
this.altGrid = this._createGrid(this.cols, this.rows);
|
|
38
|
+
this.isAltScreen = false;
|
|
39
|
+
|
|
40
|
+
this.cursorRow = 0;
|
|
41
|
+
this.cursorCol = 0;
|
|
42
|
+
this.pendingWrap = false;
|
|
43
|
+
this.cursorVisible = true;
|
|
44
|
+
this.autoWrap = true;
|
|
45
|
+
this.originMode = false;
|
|
46
|
+
|
|
47
|
+
this.scrollTop = 0;
|
|
48
|
+
this.scrollBottom = this.rows - 1;
|
|
49
|
+
|
|
50
|
+
// Current graphic attributes applied to newly written characters
|
|
51
|
+
this.curFg = null;
|
|
52
|
+
this.curBg = null;
|
|
53
|
+
this.curFlags = 0;
|
|
54
|
+
this.curLink = null;
|
|
55
|
+
|
|
56
|
+
this._savedPrimary = null;
|
|
57
|
+
this._savedCursorPrimary = null;
|
|
58
|
+
this._savedCursorAlt = null;
|
|
59
|
+
|
|
60
|
+
this.dirtyRows = new Set();
|
|
61
|
+
this.fullRedrawNeeded = true;
|
|
62
|
+
this._markAllDirty();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get grid() {
|
|
66
|
+
return this.isAltScreen ? this.altGrid : this.primaryGrid;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
_createRow(cols = this.cols, fillAttr = false) {
|
|
70
|
+
const fg = fillAttr ? this.curFg : null;
|
|
71
|
+
const bg = fillAttr ? this.curBg : null;
|
|
72
|
+
const flags = fillAttr ? this.curFlags : 0;
|
|
73
|
+
const row = new Array(cols);
|
|
74
|
+
for (let c = 0; c < cols; c++) {
|
|
75
|
+
row[c] = createCell(' ', fg, bg, flags, null);
|
|
76
|
+
}
|
|
77
|
+
return row;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
_createGrid(cols, rows) {
|
|
81
|
+
const grid = new Array(rows);
|
|
82
|
+
for (let r = 0; r < rows; r++) {
|
|
83
|
+
grid[r] = this._createRow(cols, false);
|
|
84
|
+
}
|
|
85
|
+
return grid;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
_markRowDirty(r) {
|
|
89
|
+
if (r >= 0 && r < this.rows) {
|
|
90
|
+
this.dirtyRows.add(r);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
_markRangeDirty(startRow, endRow) {
|
|
95
|
+
const lo = Math.max(0, startRow);
|
|
96
|
+
const hi = Math.min(this.rows - 1, endRow);
|
|
97
|
+
for (let r = lo; r <= hi; r++) {
|
|
98
|
+
this.dirtyRows.add(r);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
_markAllDirty() {
|
|
103
|
+
this.fullRedrawNeeded = true;
|
|
104
|
+
for (let r = 0; r < this.rows; r++) {
|
|
105
|
+
this.dirtyRows.add(r);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
reset() {
|
|
110
|
+
this.scrollback = [];
|
|
111
|
+
this.viewportOffset = 0;
|
|
112
|
+
this.primaryGrid = this._createGrid(this.cols, this.rows);
|
|
113
|
+
this.altGrid = this._createGrid(this.cols, this.rows);
|
|
114
|
+
this.isAltScreen = false;
|
|
115
|
+
this.cursorRow = 0;
|
|
116
|
+
this.cursorCol = 0;
|
|
117
|
+
this.pendingWrap = false;
|
|
118
|
+
this.cursorVisible = true;
|
|
119
|
+
this.autoWrap = true;
|
|
120
|
+
this.originMode = false;
|
|
121
|
+
this.scrollTop = 0;
|
|
122
|
+
this.scrollBottom = this.rows - 1;
|
|
123
|
+
this.curFg = null;
|
|
124
|
+
this.curBg = null;
|
|
125
|
+
this.curFlags = 0;
|
|
126
|
+
this.curLink = null;
|
|
127
|
+
this._savedPrimary = null;
|
|
128
|
+
this._savedCursorPrimary = null;
|
|
129
|
+
this._savedCursorAlt = null;
|
|
130
|
+
this._markAllDirty();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
resetAttributes() {
|
|
134
|
+
this.curFg = null;
|
|
135
|
+
this.curBg = null;
|
|
136
|
+
this.curFlags = 0;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
saveCursor() {
|
|
140
|
+
const state = {
|
|
141
|
+
row: this.cursorRow,
|
|
142
|
+
col: this.cursorCol,
|
|
143
|
+
pendingWrap: this.pendingWrap,
|
|
144
|
+
fg: this.curFg,
|
|
145
|
+
bg: this.curBg,
|
|
146
|
+
flags: this.curFlags,
|
|
147
|
+
originMode: this.originMode,
|
|
148
|
+
autoWrap: this.autoWrap
|
|
149
|
+
};
|
|
150
|
+
if (this.isAltScreen) {
|
|
151
|
+
this._savedCursorAlt = state;
|
|
152
|
+
} else {
|
|
153
|
+
this._savedCursorPrimary = state;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
restoreCursor() {
|
|
158
|
+
const state = this.isAltScreen ? this._savedCursorAlt : this._savedCursorPrimary;
|
|
159
|
+
if (!state) return;
|
|
160
|
+
this._markRowDirty(this.cursorRow);
|
|
161
|
+
this.cursorRow = Math.min(this.rows - 1, Math.max(0, state.row));
|
|
162
|
+
this.cursorCol = Math.min(this.cols - 1, Math.max(0, state.col));
|
|
163
|
+
this.pendingWrap = Boolean(state.pendingWrap);
|
|
164
|
+
this.curFg = state.fg;
|
|
165
|
+
this.curBg = state.bg;
|
|
166
|
+
this.curFlags = state.flags;
|
|
167
|
+
this.originMode = state.originMode;
|
|
168
|
+
this.autoWrap = state.autoWrap;
|
|
169
|
+
this._markRowDirty(this.cursorRow);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
enterAltScreen(clear = true) {
|
|
173
|
+
if (!this.isAltScreen) {
|
|
174
|
+
this._savedPrimary = {
|
|
175
|
+
cursorRow: this.cursorRow,
|
|
176
|
+
cursorCol: this.cursorCol,
|
|
177
|
+
pendingWrap: this.pendingWrap,
|
|
178
|
+
fg: this.curFg,
|
|
179
|
+
bg: this.curBg,
|
|
180
|
+
flags: this.curFlags,
|
|
181
|
+
scrollTop: this.scrollTop,
|
|
182
|
+
scrollBottom: this.scrollBottom,
|
|
183
|
+
originMode: this.originMode,
|
|
184
|
+
autoWrap: this.autoWrap
|
|
185
|
+
};
|
|
186
|
+
this.isAltScreen = true;
|
|
187
|
+
this.viewportOffset = 0;
|
|
188
|
+
this.scrollTop = 0;
|
|
189
|
+
this.scrollBottom = this.rows - 1;
|
|
190
|
+
}
|
|
191
|
+
if (clear) {
|
|
192
|
+
this.altGrid = this._createGrid(this.cols, this.rows);
|
|
193
|
+
this.cursorRow = 0;
|
|
194
|
+
this.cursorCol = 0;
|
|
195
|
+
this.pendingWrap = false;
|
|
196
|
+
}
|
|
197
|
+
this._markAllDirty();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
exitAltScreen() {
|
|
201
|
+
if (!this.isAltScreen) return;
|
|
202
|
+
this.isAltScreen = false;
|
|
203
|
+
if (this._savedPrimary) {
|
|
204
|
+
this.cursorRow = Math.min(this.rows - 1, Math.max(0, this._savedPrimary.cursorRow));
|
|
205
|
+
this.cursorCol = Math.min(this.cols - 1, Math.max(0, this._savedPrimary.cursorCol));
|
|
206
|
+
this.pendingWrap = this._savedPrimary.pendingWrap;
|
|
207
|
+
this.curFg = this._savedPrimary.fg;
|
|
208
|
+
this.curBg = this._savedPrimary.bg;
|
|
209
|
+
this.curFlags = this._savedPrimary.flags;
|
|
210
|
+
this.scrollTop = Math.min(this.rows - 1, this._savedPrimary.scrollTop);
|
|
211
|
+
this.scrollBottom = Math.min(this.rows - 1, Math.max(this.scrollTop, this._savedPrimary.scrollBottom));
|
|
212
|
+
this.originMode = this._savedPrimary.originMode;
|
|
213
|
+
this.autoWrap = this._savedPrimary.autoWrap;
|
|
214
|
+
this._savedPrimary = null;
|
|
215
|
+
} else {
|
|
216
|
+
this.scrollTop = 0;
|
|
217
|
+
this.scrollBottom = this.rows - 1;
|
|
218
|
+
}
|
|
219
|
+
this._markAllDirty();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
setScrollRegion(top, bottom) {
|
|
223
|
+
const t = Math.max(1, top || 1) - 1;
|
|
224
|
+
const b = bottom ? Math.min(this.rows, bottom) - 1 : this.rows - 1;
|
|
225
|
+
if (t < b && t >= 0 && b < this.rows) {
|
|
226
|
+
this.scrollTop = t;
|
|
227
|
+
this.scrollBottom = b;
|
|
228
|
+
} else {
|
|
229
|
+
this.scrollTop = 0;
|
|
230
|
+
this.scrollBottom = this.rows - 1;
|
|
231
|
+
}
|
|
232
|
+
this.setCursor(0, 0);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
setCursor(row, col) {
|
|
236
|
+
this._markRowDirty(this.cursorRow);
|
|
237
|
+
const minRow = this.originMode ? this.scrollTop : 0;
|
|
238
|
+
const maxRow = this.originMode ? this.scrollBottom : this.rows - 1;
|
|
239
|
+
const targetRow = (this.originMode ? this.scrollTop : 0) + row;
|
|
240
|
+
this.cursorRow = Math.max(minRow, Math.min(maxRow, targetRow));
|
|
241
|
+
this.cursorCol = Math.max(0, Math.min(this.cols - 1, col));
|
|
242
|
+
this.pendingWrap = false;
|
|
243
|
+
this._markRowDirty(this.cursorRow);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
moveCursorRelative(dRow, dCol) {
|
|
247
|
+
this._markRowDirty(this.cursorRow);
|
|
248
|
+
if (dRow !== 0) {
|
|
249
|
+
const minRow = this.cursorRow >= this.scrollTop && this.cursorRow <= this.scrollBottom
|
|
250
|
+
? (dRow < 0 ? this.scrollTop : 0)
|
|
251
|
+
: 0;
|
|
252
|
+
const maxRow = this.cursorRow >= this.scrollTop && this.cursorRow <= this.scrollBottom
|
|
253
|
+
? (dRow > 0 ? this.scrollBottom : this.rows - 1)
|
|
254
|
+
: this.rows - 1;
|
|
255
|
+
this.cursorRow = Math.max(minRow, Math.min(maxRow, this.cursorRow + dRow));
|
|
256
|
+
}
|
|
257
|
+
if (dCol !== 0) {
|
|
258
|
+
this.cursorCol = Math.max(0, Math.min(this.cols - 1, this.cursorCol + dCol));
|
|
259
|
+
}
|
|
260
|
+
this.pendingWrap = false;
|
|
261
|
+
this._markRowDirty(this.cursorRow);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
putChar(ch) {
|
|
265
|
+
if (this.pendingWrap) {
|
|
266
|
+
if (this.autoWrap) {
|
|
267
|
+
this.cursorCol = 0;
|
|
268
|
+
this.pendingWrap = false;
|
|
269
|
+
this._indexDown();
|
|
270
|
+
} else {
|
|
271
|
+
this.pendingWrap = false;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
const row = this.grid[this.cursorRow];
|
|
276
|
+
if (row && this.cursorCol >= 0 && this.cursorCol < this.cols) {
|
|
277
|
+
const cell = row[this.cursorCol];
|
|
278
|
+
cell.ch = ch;
|
|
279
|
+
cell.fg = this.curFg;
|
|
280
|
+
cell.bg = this.curBg;
|
|
281
|
+
cell.flags = this.curFlags;
|
|
282
|
+
cell.link = this.curLink;
|
|
283
|
+
this._markRowDirty(this.cursorRow);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (this.cursorCol < this.cols - 1) {
|
|
287
|
+
this.cursorCol++;
|
|
288
|
+
} else {
|
|
289
|
+
this.pendingWrap = true;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
carriageReturn() {
|
|
294
|
+
this._markRowDirty(this.cursorRow);
|
|
295
|
+
this.cursorCol = 0;
|
|
296
|
+
this.pendingWrap = false;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
lineFeed() {
|
|
300
|
+
this._markRowDirty(this.cursorRow);
|
|
301
|
+
if (this.convertEol) {
|
|
302
|
+
this.cursorCol = 0;
|
|
303
|
+
}
|
|
304
|
+
this.pendingWrap = false;
|
|
305
|
+
this._indexDown();
|
|
306
|
+
this._markRowDirty(this.cursorRow);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
index() {
|
|
310
|
+
this._markRowDirty(this.cursorRow);
|
|
311
|
+
this.pendingWrap = false;
|
|
312
|
+
this._indexDown();
|
|
313
|
+
this._markRowDirty(this.cursorRow);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
nextLine() {
|
|
317
|
+
this._markRowDirty(this.cursorRow);
|
|
318
|
+
this.cursorCol = 0;
|
|
319
|
+
this.pendingWrap = false;
|
|
320
|
+
this._indexDown();
|
|
321
|
+
this._markRowDirty(this.cursorRow);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
reverseIndex() {
|
|
325
|
+
this._markRowDirty(this.cursorRow);
|
|
326
|
+
this.pendingWrap = false;
|
|
327
|
+
if (this.cursorRow === this.scrollTop) {
|
|
328
|
+
this.scrollDown(1);
|
|
329
|
+
} else if (this.cursorRow > 0) {
|
|
330
|
+
this.cursorRow--;
|
|
331
|
+
}
|
|
332
|
+
this._markRowDirty(this.cursorRow);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
backspace() {
|
|
336
|
+
this._markRowDirty(this.cursorRow);
|
|
337
|
+
if (this.pendingWrap) {
|
|
338
|
+
this.pendingWrap = false;
|
|
339
|
+
} else if (this.cursorCol > 0) {
|
|
340
|
+
this.cursorCol--;
|
|
341
|
+
}
|
|
342
|
+
this._markRowDirty(this.cursorRow);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
tab() {
|
|
346
|
+
this._markRowDirty(this.cursorRow);
|
|
347
|
+
const nextStop = Math.min(this.cols - 1, (Math.floor(this.cursorCol / 8) + 1) * 8);
|
|
348
|
+
this.cursorCol = nextStop;
|
|
349
|
+
this.pendingWrap = false;
|
|
350
|
+
this._markRowDirty(this.cursorRow);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
_indexDown() {
|
|
354
|
+
if (this.cursorRow === this.scrollBottom) {
|
|
355
|
+
this.scrollUp(1);
|
|
356
|
+
} else if (this.cursorRow < this.rows - 1) {
|
|
357
|
+
this.cursorRow++;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
scrollUp(count = 1, top = this.scrollTop, bottom = this.scrollBottom) {
|
|
362
|
+
const g = this.grid;
|
|
363
|
+
const n = Math.min(count, bottom - top + 1);
|
|
364
|
+
for (let i = 0; i < n; i++) {
|
|
365
|
+
const removed = g[top];
|
|
366
|
+
if (!this.isAltScreen && top === 0 && this.maxScrollback > 0) {
|
|
367
|
+
this.scrollback.push(removed.map(cloneCell));
|
|
368
|
+
if (this.scrollback.length > this.maxScrollback) {
|
|
369
|
+
this.scrollback.shift();
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
for (let r = top; r < bottom; r++) {
|
|
373
|
+
g[r] = g[r + 1];
|
|
374
|
+
}
|
|
375
|
+
g[bottom] = this._createRow(this.cols, true);
|
|
376
|
+
}
|
|
377
|
+
this._markRangeDirty(top, bottom);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
scrollDown(count = 1, top = this.scrollTop, bottom = this.scrollBottom) {
|
|
381
|
+
const g = this.grid;
|
|
382
|
+
const n = Math.min(count, bottom - top + 1);
|
|
383
|
+
for (let i = 0; i < n; i++) {
|
|
384
|
+
for (let r = bottom; r > top; r--) {
|
|
385
|
+
g[r] = g[r - 1];
|
|
386
|
+
}
|
|
387
|
+
g[top] = this._createRow(this.cols, true);
|
|
388
|
+
}
|
|
389
|
+
this._markRangeDirty(top, bottom);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
eraseInLine(mode = 0) {
|
|
393
|
+
const row = this.grid[this.cursorRow];
|
|
394
|
+
if (!row) return;
|
|
395
|
+
let start = 0;
|
|
396
|
+
let end = this.cols - 1;
|
|
397
|
+
if (mode === 0) {
|
|
398
|
+
start = this.cursorCol;
|
|
399
|
+
} else if (mode === 1) {
|
|
400
|
+
end = this.cursorCol;
|
|
401
|
+
} else if (mode !== 2) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
for (let c = start; c <= end; c++) {
|
|
405
|
+
row[c] = createCell(' ', this.curFg, this.curBg, this.curFlags, null);
|
|
406
|
+
}
|
|
407
|
+
this.pendingWrap = false;
|
|
408
|
+
this._markRowDirty(this.cursorRow);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
eraseInDisplay(mode = 0) {
|
|
412
|
+
const g = this.grid;
|
|
413
|
+
if (mode === 0) {
|
|
414
|
+
this.eraseInLine(0);
|
|
415
|
+
for (let r = this.cursorRow + 1; r < this.rows; r++) {
|
|
416
|
+
g[r] = this._createRow(this.cols, true);
|
|
417
|
+
}
|
|
418
|
+
this._markRangeDirty(this.cursorRow, this.rows - 1);
|
|
419
|
+
} else if (mode === 1) {
|
|
420
|
+
for (let r = 0; r < this.cursorRow; r++) {
|
|
421
|
+
g[r] = this._createRow(this.cols, true);
|
|
422
|
+
}
|
|
423
|
+
this.eraseInLine(1);
|
|
424
|
+
this._markRangeDirty(0, this.cursorRow);
|
|
425
|
+
} else if (mode === 2 || mode === 3) {
|
|
426
|
+
for (let r = 0; r < this.rows; r++) {
|
|
427
|
+
g[r] = this._createRow(this.cols, true);
|
|
428
|
+
}
|
|
429
|
+
if (mode === 3 && !this.isAltScreen) {
|
|
430
|
+
this.scrollback = [];
|
|
431
|
+
this.viewportOffset = 0;
|
|
432
|
+
}
|
|
433
|
+
this.pendingWrap = false;
|
|
434
|
+
this._markAllDirty();
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
eraseChars(count = 1) {
|
|
439
|
+
const row = this.grid[this.cursorRow];
|
|
440
|
+
if (!row) return;
|
|
441
|
+
const end = Math.min(this.cols, this.cursorCol + Math.max(1, count));
|
|
442
|
+
for (let c = this.cursorCol; c < end; c++) {
|
|
443
|
+
row[c] = createCell(' ', this.curFg, this.curBg, this.curFlags, null);
|
|
444
|
+
}
|
|
445
|
+
this.pendingWrap = false;
|
|
446
|
+
this._markRowDirty(this.cursorRow);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
insertChars(count = 1) {
|
|
450
|
+
const row = this.grid[this.cursorRow];
|
|
451
|
+
if (!row) return;
|
|
452
|
+
const n = Math.min(this.cols - this.cursorCol, Math.max(1, count));
|
|
453
|
+
for (let c = this.cols - 1; c >= this.cursorCol + n; c--) {
|
|
454
|
+
row[c] = row[c - n];
|
|
455
|
+
}
|
|
456
|
+
for (let c = this.cursorCol; c < this.cursorCol + n; c++) {
|
|
457
|
+
row[c] = createCell(' ', this.curFg, this.curBg, this.curFlags, null);
|
|
458
|
+
}
|
|
459
|
+
this.pendingWrap = false;
|
|
460
|
+
this._markRowDirty(this.cursorRow);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
deleteChars(count = 1) {
|
|
464
|
+
const row = this.grid[this.cursorRow];
|
|
465
|
+
if (!row) return;
|
|
466
|
+
const n = Math.min(this.cols - this.cursorCol, Math.max(1, count));
|
|
467
|
+
for (let c = this.cursorCol; c < this.cols - n; c++) {
|
|
468
|
+
row[c] = row[c + n];
|
|
469
|
+
}
|
|
470
|
+
for (let c = this.cols - n; c < this.cols; c++) {
|
|
471
|
+
row[c] = createCell(' ', this.curFg, this.curBg, this.curFlags, null);
|
|
472
|
+
}
|
|
473
|
+
this.pendingWrap = false;
|
|
474
|
+
this._markRowDirty(this.cursorRow);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
insertLines(count = 1) {
|
|
478
|
+
if (this.cursorRow < this.scrollTop || this.cursorRow > this.scrollBottom) return;
|
|
479
|
+
this.scrollDown(Math.max(1, count), this.cursorRow, this.scrollBottom);
|
|
480
|
+
this.cursorCol = 0;
|
|
481
|
+
this.pendingWrap = false;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
deleteLines(count = 1) {
|
|
485
|
+
if (this.cursorRow < this.scrollTop || this.cursorRow > this.scrollBottom) return;
|
|
486
|
+
this.scrollUp(Math.max(1, count), this.cursorRow, this.scrollBottom);
|
|
487
|
+
this.cursorCol = 0;
|
|
488
|
+
this.pendingWrap = false;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
resize(newCols, newRows) {
|
|
492
|
+
const cols = Math.max(1, newCols);
|
|
493
|
+
const rows = Math.max(1, newRows);
|
|
494
|
+
if (cols === this.cols && rows === this.rows) return;
|
|
495
|
+
|
|
496
|
+
const resizeGrid = (oldGrid) => {
|
|
497
|
+
const nextGrid = new Array(rows);
|
|
498
|
+
for (let r = 0; r < rows; r++) {
|
|
499
|
+
const newRow = new Array(cols);
|
|
500
|
+
const oldRow = r < oldGrid.length ? oldGrid[r] : null;
|
|
501
|
+
for (let c = 0; c < cols; c++) {
|
|
502
|
+
newRow[c] = oldRow && c < oldRow.length
|
|
503
|
+
? oldRow[c]
|
|
504
|
+
: createCell(' ', null, null, 0, null);
|
|
505
|
+
}
|
|
506
|
+
nextGrid[r] = newRow;
|
|
507
|
+
}
|
|
508
|
+
return nextGrid;
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
this.primaryGrid = resizeGrid(this.primaryGrid);
|
|
512
|
+
this.altGrid = resizeGrid(this.altGrid);
|
|
513
|
+
this.cols = cols;
|
|
514
|
+
this.rows = rows;
|
|
515
|
+
this.scrollTop = 0;
|
|
516
|
+
this.scrollBottom = rows - 1;
|
|
517
|
+
this.cursorRow = Math.min(rows - 1, this.cursorRow);
|
|
518
|
+
this.cursorCol = Math.min(cols - 1, this.cursorCol);
|
|
519
|
+
this.pendingWrap = false;
|
|
520
|
+
this._markAllDirty();
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
scrollViewport(deltaLines) {
|
|
524
|
+
if (this.isAltScreen) return;
|
|
525
|
+
const maxOffset = this.scrollback.length;
|
|
526
|
+
const next = Math.max(0, Math.min(maxOffset, this.viewportOffset + deltaLines));
|
|
527
|
+
if (next !== this.viewportOffset) {
|
|
528
|
+
this.viewportOffset = next;
|
|
529
|
+
this._markAllDirty();
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
getVisibleRow(viewRow) {
|
|
534
|
+
if (this.isAltScreen || this.viewportOffset === 0) {
|
|
535
|
+
return this.grid[viewRow];
|
|
536
|
+
}
|
|
537
|
+
const totalScrollback = this.scrollback.length;
|
|
538
|
+
const startIdx = totalScrollback - this.viewportOffset;
|
|
539
|
+
const idx = startIdx + viewRow;
|
|
540
|
+
if (idx < totalScrollback) {
|
|
541
|
+
return this.scrollback[idx];
|
|
542
|
+
}
|
|
543
|
+
return this.grid[idx - totalScrollback];
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
getLineText(row, trimRight = true) {
|
|
547
|
+
const r = this.grid[row];
|
|
548
|
+
if (!r) return '';
|
|
549
|
+
let str = '';
|
|
550
|
+
for (let c = 0; c < r.length; c++) {
|
|
551
|
+
str += r[c].ch;
|
|
552
|
+
}
|
|
553
|
+
return trimRight ? str.replace(/\s+$/, '') : str;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
getScreenText(options = {}) {
|
|
557
|
+
const trimRight = options.trimRight !== false;
|
|
558
|
+
const lines = [];
|
|
559
|
+
for (let r = 0; r < this.rows; r++) {
|
|
560
|
+
lines.push(this.getLineText(r, trimRight));
|
|
561
|
+
}
|
|
562
|
+
return lines.join('\n');
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
getScrollbackAndScreenText(options = {}) {
|
|
566
|
+
const trimRight = options.trimRight !== false;
|
|
567
|
+
const lines = [];
|
|
568
|
+
if (!this.isAltScreen) {
|
|
569
|
+
for (const sbRow of this.scrollback) {
|
|
570
|
+
let s = '';
|
|
571
|
+
for (let c = 0; c < sbRow.length; c++) s += sbRow[c].ch;
|
|
572
|
+
lines.push(trimRight ? s.replace(/\s+$/, '') : s);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
for (let r = 0; r < this.rows; r++) {
|
|
576
|
+
lines.push(this.getLineText(r, trimRight));
|
|
577
|
+
}
|
|
578
|
+
return lines.join('\n');
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
export {
|
|
583
|
+
ScreenBuffer,
|
|
584
|
+
ATTR_BOLD,
|
|
585
|
+
ATTR_DIM,
|
|
586
|
+
ATTR_ITALIC,
|
|
587
|
+
ATTR_UNDERLINE,
|
|
588
|
+
ATTR_BLINK,
|
|
589
|
+
ATTR_INVERSE,
|
|
590
|
+
ATTR_HIDDEN,
|
|
591
|
+
ATTR_STRIKE,
|
|
592
|
+
createCell
|
|
593
|
+
};
|
|
594
|
+
export default ScreenBuffer;
|