@xterm/xterm 5.4.0-beta.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.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +235 -0
  3. package/css/xterm.css +209 -0
  4. package/lib/xterm.js +2 -0
  5. package/lib/xterm.js.map +1 -0
  6. package/package.json +101 -0
  7. package/src/browser/AccessibilityManager.ts +278 -0
  8. package/src/browser/Clipboard.ts +93 -0
  9. package/src/browser/ColorContrastCache.ts +34 -0
  10. package/src/browser/Lifecycle.ts +33 -0
  11. package/src/browser/Linkifier2.ts +416 -0
  12. package/src/browser/LocalizableStrings.ts +12 -0
  13. package/src/browser/OscLinkProvider.ts +128 -0
  14. package/src/browser/RenderDebouncer.ts +83 -0
  15. package/src/browser/Terminal.ts +1317 -0
  16. package/src/browser/TimeBasedDebouncer.ts +86 -0
  17. package/src/browser/Types.d.ts +181 -0
  18. package/src/browser/Viewport.ts +401 -0
  19. package/src/browser/decorations/BufferDecorationRenderer.ts +134 -0
  20. package/src/browser/decorations/ColorZoneStore.ts +117 -0
  21. package/src/browser/decorations/OverviewRulerRenderer.ts +218 -0
  22. package/src/browser/input/CompositionHelper.ts +246 -0
  23. package/src/browser/input/Mouse.ts +54 -0
  24. package/src/browser/input/MoveToCell.ts +249 -0
  25. package/src/browser/public/Terminal.ts +260 -0
  26. package/src/browser/renderer/dom/DomRenderer.ts +509 -0
  27. package/src/browser/renderer/dom/DomRendererRowFactory.ts +526 -0
  28. package/src/browser/renderer/dom/WidthCache.ts +160 -0
  29. package/src/browser/renderer/shared/CellColorResolver.ts +137 -0
  30. package/src/browser/renderer/shared/CharAtlasCache.ts +96 -0
  31. package/src/browser/renderer/shared/CharAtlasUtils.ts +75 -0
  32. package/src/browser/renderer/shared/Constants.ts +14 -0
  33. package/src/browser/renderer/shared/CursorBlinkStateManager.ts +146 -0
  34. package/src/browser/renderer/shared/CustomGlyphs.ts +687 -0
  35. package/src/browser/renderer/shared/DevicePixelObserver.ts +41 -0
  36. package/src/browser/renderer/shared/README.md +1 -0
  37. package/src/browser/renderer/shared/RendererUtils.ts +58 -0
  38. package/src/browser/renderer/shared/SelectionRenderModel.ts +91 -0
  39. package/src/browser/renderer/shared/TextureAtlas.ts +1082 -0
  40. package/src/browser/renderer/shared/Types.d.ts +173 -0
  41. package/src/browser/selection/SelectionModel.ts +144 -0
  42. package/src/browser/selection/Types.d.ts +15 -0
  43. package/src/browser/services/CharSizeService.ts +102 -0
  44. package/src/browser/services/CharacterJoinerService.ts +339 -0
  45. package/src/browser/services/CoreBrowserService.ts +137 -0
  46. package/src/browser/services/MouseService.ts +46 -0
  47. package/src/browser/services/RenderService.ts +279 -0
  48. package/src/browser/services/SelectionService.ts +1031 -0
  49. package/src/browser/services/Services.ts +147 -0
  50. package/src/browser/services/ThemeService.ts +237 -0
  51. package/src/common/CircularList.ts +241 -0
  52. package/src/common/Clone.ts +23 -0
  53. package/src/common/Color.ts +357 -0
  54. package/src/common/CoreTerminal.ts +284 -0
  55. package/src/common/EventEmitter.ts +78 -0
  56. package/src/common/InputHandler.ts +3461 -0
  57. package/src/common/Lifecycle.ts +108 -0
  58. package/src/common/MultiKeyMap.ts +42 -0
  59. package/src/common/Platform.ts +44 -0
  60. package/src/common/SortedList.ts +118 -0
  61. package/src/common/TaskQueue.ts +166 -0
  62. package/src/common/TypedArrayUtils.ts +17 -0
  63. package/src/common/Types.d.ts +553 -0
  64. package/src/common/WindowsMode.ts +27 -0
  65. package/src/common/buffer/AttributeData.ts +196 -0
  66. package/src/common/buffer/Buffer.ts +654 -0
  67. package/src/common/buffer/BufferLine.ts +524 -0
  68. package/src/common/buffer/BufferRange.ts +13 -0
  69. package/src/common/buffer/BufferReflow.ts +223 -0
  70. package/src/common/buffer/BufferSet.ts +134 -0
  71. package/src/common/buffer/CellData.ts +94 -0
  72. package/src/common/buffer/Constants.ts +149 -0
  73. package/src/common/buffer/Marker.ts +43 -0
  74. package/src/common/buffer/Types.d.ts +52 -0
  75. package/src/common/data/Charsets.ts +256 -0
  76. package/src/common/data/EscapeSequences.ts +153 -0
  77. package/src/common/input/Keyboard.ts +398 -0
  78. package/src/common/input/TextDecoder.ts +346 -0
  79. package/src/common/input/UnicodeV6.ts +145 -0
  80. package/src/common/input/WriteBuffer.ts +246 -0
  81. package/src/common/input/XParseColor.ts +80 -0
  82. package/src/common/parser/Constants.ts +58 -0
  83. package/src/common/parser/DcsParser.ts +192 -0
  84. package/src/common/parser/EscapeSequenceParser.ts +792 -0
  85. package/src/common/parser/OscParser.ts +238 -0
  86. package/src/common/parser/Params.ts +229 -0
  87. package/src/common/parser/Types.d.ts +275 -0
  88. package/src/common/public/AddonManager.ts +53 -0
  89. package/src/common/public/BufferApiView.ts +35 -0
  90. package/src/common/public/BufferLineApiView.ts +29 -0
  91. package/src/common/public/BufferNamespaceApi.ts +36 -0
  92. package/src/common/public/ParserApi.ts +37 -0
  93. package/src/common/public/UnicodeApi.ts +27 -0
  94. package/src/common/services/BufferService.ts +151 -0
  95. package/src/common/services/CharsetService.ts +34 -0
  96. package/src/common/services/CoreMouseService.ts +318 -0
  97. package/src/common/services/CoreService.ts +87 -0
  98. package/src/common/services/DecorationService.ts +140 -0
  99. package/src/common/services/InstantiationService.ts +85 -0
  100. package/src/common/services/LogService.ts +124 -0
  101. package/src/common/services/OptionsService.ts +202 -0
  102. package/src/common/services/OscLinkService.ts +115 -0
  103. package/src/common/services/ServiceRegistry.ts +49 -0
  104. package/src/common/services/Services.ts +373 -0
  105. package/src/common/services/UnicodeService.ts +111 -0
  106. package/src/headless/Terminal.ts +136 -0
  107. package/src/headless/public/Terminal.ts +195 -0
  108. package/typings/xterm.d.ts +1857 -0
@@ -0,0 +1,654 @@
1
+ /**
2
+ * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3
+ * @license MIT
4
+ */
5
+
6
+ import { CircularList, IInsertEvent } from 'common/CircularList';
7
+ import { IdleTaskQueue } from 'common/TaskQueue';
8
+ import { IAttributeData, IBufferLine, ICellData, ICharset } from 'common/Types';
9
+ import { ExtendedAttrs } from 'common/buffer/AttributeData';
10
+ import { BufferLine, DEFAULT_ATTR_DATA } from 'common/buffer/BufferLine';
11
+ import { getWrappedLineTrimmedLength, reflowLargerApplyNewLayout, reflowLargerCreateNewLayout, reflowLargerGetLinesToRemove, reflowSmallerGetNewLineLengths } from 'common/buffer/BufferReflow';
12
+ import { CellData } from 'common/buffer/CellData';
13
+ import { NULL_CELL_CHAR, NULL_CELL_CODE, NULL_CELL_WIDTH, WHITESPACE_CELL_CHAR, WHITESPACE_CELL_CODE, WHITESPACE_CELL_WIDTH } from 'common/buffer/Constants';
14
+ import { Marker } from 'common/buffer/Marker';
15
+ import { IBuffer } from 'common/buffer/Types';
16
+ import { DEFAULT_CHARSET } from 'common/data/Charsets';
17
+ import { IBufferService, IOptionsService } from 'common/services/Services';
18
+
19
+ export const MAX_BUFFER_SIZE = 4294967295; // 2^32 - 1
20
+
21
+ /**
22
+ * This class represents a terminal buffer (an internal state of the terminal), where the
23
+ * following information is stored (in high-level):
24
+ * - text content of this particular buffer
25
+ * - cursor position
26
+ * - scroll position
27
+ */
28
+ export class Buffer implements IBuffer {
29
+ public lines: CircularList<IBufferLine>;
30
+ public ydisp: number = 0;
31
+ public ybase: number = 0;
32
+ public y: number = 0;
33
+ public x: number = 0;
34
+ public scrollBottom: number;
35
+ public scrollTop: number;
36
+ public tabs: { [column: number]: boolean | undefined } = {};
37
+ public savedY: number = 0;
38
+ public savedX: number = 0;
39
+ public savedCurAttrData = DEFAULT_ATTR_DATA.clone();
40
+ public savedCharset: ICharset | undefined = DEFAULT_CHARSET;
41
+ public markers: Marker[] = [];
42
+ private _nullCell: ICellData = CellData.fromCharData([0, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]);
43
+ private _whitespaceCell: ICellData = CellData.fromCharData([0, WHITESPACE_CELL_CHAR, WHITESPACE_CELL_WIDTH, WHITESPACE_CELL_CODE]);
44
+ private _cols: number;
45
+ private _rows: number;
46
+ private _isClearing: boolean = false;
47
+
48
+ constructor(
49
+ private _hasScrollback: boolean,
50
+ private _optionsService: IOptionsService,
51
+ private _bufferService: IBufferService
52
+ ) {
53
+ this._cols = this._bufferService.cols;
54
+ this._rows = this._bufferService.rows;
55
+ this.lines = new CircularList<IBufferLine>(this._getCorrectBufferLength(this._rows));
56
+ this.scrollTop = 0;
57
+ this.scrollBottom = this._rows - 1;
58
+ this.setupTabStops();
59
+ }
60
+
61
+ public getNullCell(attr?: IAttributeData): ICellData {
62
+ if (attr) {
63
+ this._nullCell.fg = attr.fg;
64
+ this._nullCell.bg = attr.bg;
65
+ this._nullCell.extended = attr.extended;
66
+ } else {
67
+ this._nullCell.fg = 0;
68
+ this._nullCell.bg = 0;
69
+ this._nullCell.extended = new ExtendedAttrs();
70
+ }
71
+ return this._nullCell;
72
+ }
73
+
74
+ public getWhitespaceCell(attr?: IAttributeData): ICellData {
75
+ if (attr) {
76
+ this._whitespaceCell.fg = attr.fg;
77
+ this._whitespaceCell.bg = attr.bg;
78
+ this._whitespaceCell.extended = attr.extended;
79
+ } else {
80
+ this._whitespaceCell.fg = 0;
81
+ this._whitespaceCell.bg = 0;
82
+ this._whitespaceCell.extended = new ExtendedAttrs();
83
+ }
84
+ return this._whitespaceCell;
85
+ }
86
+
87
+ public getBlankLine(attr: IAttributeData, isWrapped?: boolean): IBufferLine {
88
+ return new BufferLine(this._bufferService.cols, this.getNullCell(attr), isWrapped);
89
+ }
90
+
91
+ public get hasScrollback(): boolean {
92
+ return this._hasScrollback && this.lines.maxLength > this._rows;
93
+ }
94
+
95
+ public get isCursorInViewport(): boolean {
96
+ const absoluteY = this.ybase + this.y;
97
+ const relativeY = absoluteY - this.ydisp;
98
+ return (relativeY >= 0 && relativeY < this._rows);
99
+ }
100
+
101
+ /**
102
+ * Gets the correct buffer length based on the rows provided, the terminal's
103
+ * scrollback and whether this buffer is flagged to have scrollback or not.
104
+ * @param rows The terminal rows to use in the calculation.
105
+ */
106
+ private _getCorrectBufferLength(rows: number): number {
107
+ if (!this._hasScrollback) {
108
+ return rows;
109
+ }
110
+
111
+ const correctBufferLength = rows + this._optionsService.rawOptions.scrollback;
112
+
113
+ return correctBufferLength > MAX_BUFFER_SIZE ? MAX_BUFFER_SIZE : correctBufferLength;
114
+ }
115
+
116
+ /**
117
+ * Fills the buffer's viewport with blank lines.
118
+ */
119
+ public fillViewportRows(fillAttr?: IAttributeData): void {
120
+ if (this.lines.length === 0) {
121
+ if (fillAttr === undefined) {
122
+ fillAttr = DEFAULT_ATTR_DATA;
123
+ }
124
+ let i = this._rows;
125
+ while (i--) {
126
+ this.lines.push(this.getBlankLine(fillAttr));
127
+ }
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Clears the buffer to it's initial state, discarding all previous data.
133
+ */
134
+ public clear(): void {
135
+ this.ydisp = 0;
136
+ this.ybase = 0;
137
+ this.y = 0;
138
+ this.x = 0;
139
+ this.lines = new CircularList<IBufferLine>(this._getCorrectBufferLength(this._rows));
140
+ this.scrollTop = 0;
141
+ this.scrollBottom = this._rows - 1;
142
+ this.setupTabStops();
143
+ }
144
+
145
+ /**
146
+ * Resizes the buffer, adjusting its data accordingly.
147
+ * @param newCols The new number of columns.
148
+ * @param newRows The new number of rows.
149
+ */
150
+ public resize(newCols: number, newRows: number): void {
151
+ // store reference to null cell with default attrs
152
+ const nullCell = this.getNullCell(DEFAULT_ATTR_DATA);
153
+
154
+ // count bufferlines with overly big memory to be cleaned afterwards
155
+ let dirtyMemoryLines = 0;
156
+
157
+ // Increase max length if needed before adjustments to allow space to fill
158
+ // as required.
159
+ const newMaxLength = this._getCorrectBufferLength(newRows);
160
+ if (newMaxLength > this.lines.maxLength) {
161
+ this.lines.maxLength = newMaxLength;
162
+ }
163
+
164
+ // The following adjustments should only happen if the buffer has been
165
+ // initialized/filled.
166
+ if (this.lines.length > 0) {
167
+ // Deal with columns increasing (reducing needs to happen after reflow)
168
+ if (this._cols < newCols) {
169
+ for (let i = 0; i < this.lines.length; i++) {
170
+ // +boolean for fast 0 or 1 conversion
171
+ dirtyMemoryLines += +this.lines.get(i)!.resize(newCols, nullCell);
172
+ }
173
+ }
174
+
175
+ // Resize rows in both directions as needed
176
+ let addToY = 0;
177
+ if (this._rows < newRows) {
178
+ for (let y = this._rows; y < newRows; y++) {
179
+ if (this.lines.length < newRows + this.ybase) {
180
+ if (this._optionsService.rawOptions.windowsMode || this._optionsService.rawOptions.windowsPty.backend !== undefined || this._optionsService.rawOptions.windowsPty.buildNumber !== undefined) {
181
+ // Just add the new missing rows on Windows as conpty reprints the screen with it's
182
+ // view of the world. Once a line enters scrollback for conpty it remains there
183
+ this.lines.push(new BufferLine(newCols, nullCell));
184
+ } else {
185
+ if (this.ybase > 0 && this.lines.length <= this.ybase + this.y + addToY + 1) {
186
+ // There is room above the buffer and there are no empty elements below the line,
187
+ // scroll up
188
+ this.ybase--;
189
+ addToY++;
190
+ if (this.ydisp > 0) {
191
+ // Viewport is at the top of the buffer, must increase downwards
192
+ this.ydisp--;
193
+ }
194
+ } else {
195
+ // Add a blank line if there is no buffer left at the top to scroll to, or if there
196
+ // are blank lines after the cursor
197
+ this.lines.push(new BufferLine(newCols, nullCell));
198
+ }
199
+ }
200
+ }
201
+ }
202
+ } else { // (this._rows >= newRows)
203
+ for (let y = this._rows; y > newRows; y--) {
204
+ if (this.lines.length > newRows + this.ybase) {
205
+ if (this.lines.length > this.ybase + this.y + 1) {
206
+ // The line is a blank line below the cursor, remove it
207
+ this.lines.pop();
208
+ } else {
209
+ // The line is the cursor, scroll down
210
+ this.ybase++;
211
+ this.ydisp++;
212
+ }
213
+ }
214
+ }
215
+ }
216
+
217
+ // Reduce max length if needed after adjustments, this is done after as it
218
+ // would otherwise cut data from the bottom of the buffer.
219
+ if (newMaxLength < this.lines.maxLength) {
220
+ // Trim from the top of the buffer and adjust ybase and ydisp.
221
+ const amountToTrim = this.lines.length - newMaxLength;
222
+ if (amountToTrim > 0) {
223
+ this.lines.trimStart(amountToTrim);
224
+ this.ybase = Math.max(this.ybase - amountToTrim, 0);
225
+ this.ydisp = Math.max(this.ydisp - amountToTrim, 0);
226
+ this.savedY = Math.max(this.savedY - amountToTrim, 0);
227
+ }
228
+ this.lines.maxLength = newMaxLength;
229
+ }
230
+
231
+ // Make sure that the cursor stays on screen
232
+ this.x = Math.min(this.x, newCols - 1);
233
+ this.y = Math.min(this.y, newRows - 1);
234
+ if (addToY) {
235
+ this.y += addToY;
236
+ }
237
+ this.savedX = Math.min(this.savedX, newCols - 1);
238
+
239
+ this.scrollTop = 0;
240
+ }
241
+
242
+ this.scrollBottom = newRows - 1;
243
+
244
+ if (this._isReflowEnabled) {
245
+ this._reflow(newCols, newRows);
246
+
247
+ // Trim the end of the line off if cols shrunk
248
+ if (this._cols > newCols) {
249
+ for (let i = 0; i < this.lines.length; i++) {
250
+ // +boolean for fast 0 or 1 conversion
251
+ dirtyMemoryLines += +this.lines.get(i)!.resize(newCols, nullCell);
252
+ }
253
+ }
254
+ }
255
+
256
+ this._cols = newCols;
257
+ this._rows = newRows;
258
+
259
+ this._memoryCleanupQueue.clear();
260
+ // schedule memory cleanup only, if more than 10% of the lines are affected
261
+ if (dirtyMemoryLines > 0.1 * this.lines.length) {
262
+ this._memoryCleanupPosition = 0;
263
+ this._memoryCleanupQueue.enqueue(() => this._batchedMemoryCleanup());
264
+ }
265
+ }
266
+
267
+ private _memoryCleanupQueue = new IdleTaskQueue();
268
+ private _memoryCleanupPosition = 0;
269
+
270
+ private _batchedMemoryCleanup(): boolean {
271
+ let normalRun = true;
272
+ if (this._memoryCleanupPosition >= this.lines.length) {
273
+ // cleanup made it once through all lines, thus rescan in loop below to also catch shifted
274
+ // lines, which should finish rather quick if there are no more cleanups pending
275
+ this._memoryCleanupPosition = 0;
276
+ normalRun = false;
277
+ }
278
+ let counted = 0;
279
+ while (this._memoryCleanupPosition < this.lines.length) {
280
+ counted += this.lines.get(this._memoryCleanupPosition++)!.cleanupMemory();
281
+ // cleanup max 100 lines per batch
282
+ if (counted > 100) {
283
+ return true;
284
+ }
285
+ }
286
+ // normal runs always need another rescan afterwards
287
+ // if we made it here with normalRun=false, we are in a final run
288
+ // and can end the cleanup task for sure
289
+ return normalRun;
290
+ }
291
+
292
+ private get _isReflowEnabled(): boolean {
293
+ const windowsPty = this._optionsService.rawOptions.windowsPty;
294
+ if (windowsPty && windowsPty.buildNumber) {
295
+ return this._hasScrollback && windowsPty.backend === 'conpty' && windowsPty.buildNumber >= 21376;
296
+ }
297
+ return this._hasScrollback && !this._optionsService.rawOptions.windowsMode;
298
+ }
299
+
300
+ private _reflow(newCols: number, newRows: number): void {
301
+ if (this._cols === newCols) {
302
+ return;
303
+ }
304
+
305
+ // Iterate through rows, ignore the last one as it cannot be wrapped
306
+ if (newCols > this._cols) {
307
+ this._reflowLarger(newCols, newRows);
308
+ } else {
309
+ this._reflowSmaller(newCols, newRows);
310
+ }
311
+ }
312
+
313
+ private _reflowLarger(newCols: number, newRows: number): void {
314
+ const toRemove: number[] = reflowLargerGetLinesToRemove(this.lines, this._cols, newCols, this.ybase + this.y, this.getNullCell(DEFAULT_ATTR_DATA));
315
+ if (toRemove.length > 0) {
316
+ const newLayoutResult = reflowLargerCreateNewLayout(this.lines, toRemove);
317
+ reflowLargerApplyNewLayout(this.lines, newLayoutResult.layout);
318
+ this._reflowLargerAdjustViewport(newCols, newRows, newLayoutResult.countRemoved);
319
+ }
320
+ }
321
+
322
+ private _reflowLargerAdjustViewport(newCols: number, newRows: number, countRemoved: number): void {
323
+ const nullCell = this.getNullCell(DEFAULT_ATTR_DATA);
324
+ // Adjust viewport based on number of items removed
325
+ let viewportAdjustments = countRemoved;
326
+ while (viewportAdjustments-- > 0) {
327
+ if (this.ybase === 0) {
328
+ if (this.y > 0) {
329
+ this.y--;
330
+ }
331
+ if (this.lines.length < newRows) {
332
+ // Add an extra row at the bottom of the viewport
333
+ this.lines.push(new BufferLine(newCols, nullCell));
334
+ }
335
+ } else {
336
+ if (this.ydisp === this.ybase) {
337
+ this.ydisp--;
338
+ }
339
+ this.ybase--;
340
+ }
341
+ }
342
+ this.savedY = Math.max(this.savedY - countRemoved, 0);
343
+ }
344
+
345
+ private _reflowSmaller(newCols: number, newRows: number): void {
346
+ const nullCell = this.getNullCell(DEFAULT_ATTR_DATA);
347
+ // Gather all BufferLines that need to be inserted into the Buffer here so that they can be
348
+ // batched up and only committed once
349
+ const toInsert = [];
350
+ let countToInsert = 0;
351
+ // Go backwards as many lines may be trimmed and this will avoid considering them
352
+ for (let y = this.lines.length - 1; y >= 0; y--) {
353
+ // Check whether this line is a problem
354
+ let nextLine = this.lines.get(y) as BufferLine;
355
+ if (!nextLine || !nextLine.isWrapped && nextLine.getTrimmedLength() <= newCols) {
356
+ continue;
357
+ }
358
+
359
+ // Gather wrapped lines and adjust y to be the starting line
360
+ const wrappedLines: BufferLine[] = [nextLine];
361
+ while (nextLine.isWrapped && y > 0) {
362
+ nextLine = this.lines.get(--y) as BufferLine;
363
+ wrappedLines.unshift(nextLine);
364
+ }
365
+
366
+ // If these lines contain the cursor don't touch them, the program will handle fixing up
367
+ // wrapped lines with the cursor
368
+ const absoluteY = this.ybase + this.y;
369
+ if (absoluteY >= y && absoluteY < y + wrappedLines.length) {
370
+ continue;
371
+ }
372
+
373
+ const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength();
374
+ const destLineLengths = reflowSmallerGetNewLineLengths(wrappedLines, this._cols, newCols);
375
+ const linesToAdd = destLineLengths.length - wrappedLines.length;
376
+ let trimmedLines: number;
377
+ if (this.ybase === 0 && this.y !== this.lines.length - 1) {
378
+ // If the top section of the buffer is not yet filled
379
+ trimmedLines = Math.max(0, this.y - this.lines.maxLength + linesToAdd);
380
+ } else {
381
+ trimmedLines = Math.max(0, this.lines.length - this.lines.maxLength + linesToAdd);
382
+ }
383
+
384
+ // Add the new lines
385
+ const newLines: BufferLine[] = [];
386
+ for (let i = 0; i < linesToAdd; i++) {
387
+ const newLine = this.getBlankLine(DEFAULT_ATTR_DATA, true) as BufferLine;
388
+ newLines.push(newLine);
389
+ }
390
+ if (newLines.length > 0) {
391
+ toInsert.push({
392
+ // countToInsert here gets the actual index, taking into account other inserted items.
393
+ // using this we can iterate through the list forwards
394
+ start: y + wrappedLines.length + countToInsert,
395
+ newLines
396
+ });
397
+ countToInsert += newLines.length;
398
+ }
399
+ wrappedLines.push(...newLines);
400
+
401
+ // Copy buffer data to new locations, this needs to happen backwards to do in-place
402
+ let destLineIndex = destLineLengths.length - 1; // Math.floor(cellsNeeded / newCols);
403
+ let destCol = destLineLengths[destLineIndex]; // cellsNeeded % newCols;
404
+ if (destCol === 0) {
405
+ destLineIndex--;
406
+ destCol = destLineLengths[destLineIndex];
407
+ }
408
+ let srcLineIndex = wrappedLines.length - linesToAdd - 1;
409
+ let srcCol = lastLineLength;
410
+ while (srcLineIndex >= 0) {
411
+ const cellsToCopy = Math.min(srcCol, destCol);
412
+ if (wrappedLines[destLineIndex] === undefined) {
413
+ // Sanity check that the line exists, this has been known to fail for an unknown reason
414
+ // which would stop the reflow from happening if an exception would throw.
415
+ break;
416
+ }
417
+ wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol - cellsToCopy, destCol - cellsToCopy, cellsToCopy, true);
418
+ destCol -= cellsToCopy;
419
+ if (destCol === 0) {
420
+ destLineIndex--;
421
+ destCol = destLineLengths[destLineIndex];
422
+ }
423
+ srcCol -= cellsToCopy;
424
+ if (srcCol === 0) {
425
+ srcLineIndex--;
426
+ const wrappedLinesIndex = Math.max(srcLineIndex, 0);
427
+ srcCol = getWrappedLineTrimmedLength(wrappedLines, wrappedLinesIndex, this._cols);
428
+ }
429
+ }
430
+
431
+ // Null out the end of the line ends if a wide character wrapped to the following line
432
+ for (let i = 0; i < wrappedLines.length; i++) {
433
+ if (destLineLengths[i] < newCols) {
434
+ wrappedLines[i].setCell(destLineLengths[i], nullCell);
435
+ }
436
+ }
437
+
438
+ // Adjust viewport as needed
439
+ let viewportAdjustments = linesToAdd - trimmedLines;
440
+ while (viewportAdjustments-- > 0) {
441
+ if (this.ybase === 0) {
442
+ if (this.y < newRows - 1) {
443
+ this.y++;
444
+ this.lines.pop();
445
+ } else {
446
+ this.ybase++;
447
+ this.ydisp++;
448
+ }
449
+ } else {
450
+ // Ensure ybase does not exceed its maximum value
451
+ if (this.ybase < Math.min(this.lines.maxLength, this.lines.length + countToInsert) - newRows) {
452
+ if (this.ybase === this.ydisp) {
453
+ this.ydisp++;
454
+ }
455
+ this.ybase++;
456
+ }
457
+ }
458
+ }
459
+ this.savedY = Math.min(this.savedY + linesToAdd, this.ybase + newRows - 1);
460
+ }
461
+
462
+ // Rearrange lines in the buffer if there are any insertions, this is done at the end rather
463
+ // than earlier so that it's a single O(n) pass through the buffer, instead of O(n^2) from many
464
+ // costly calls to CircularList.splice.
465
+ if (toInsert.length > 0) {
466
+ // Record buffer insert events and then play them back backwards so that the indexes are
467
+ // correct
468
+ const insertEvents: IInsertEvent[] = [];
469
+
470
+ // Record original lines so they don't get overridden when we rearrange the list
471
+ const originalLines: BufferLine[] = [];
472
+ for (let i = 0; i < this.lines.length; i++) {
473
+ originalLines.push(this.lines.get(i) as BufferLine);
474
+ }
475
+ const originalLinesLength = this.lines.length;
476
+
477
+ let originalLineIndex = originalLinesLength - 1;
478
+ let nextToInsertIndex = 0;
479
+ let nextToInsert = toInsert[nextToInsertIndex];
480
+ this.lines.length = Math.min(this.lines.maxLength, this.lines.length + countToInsert);
481
+ let countInsertedSoFar = 0;
482
+ for (let i = Math.min(this.lines.maxLength - 1, originalLinesLength + countToInsert - 1); i >= 0; i--) {
483
+ if (nextToInsert && nextToInsert.start > originalLineIndex + countInsertedSoFar) {
484
+ // Insert extra lines here, adjusting i as needed
485
+ for (let nextI = nextToInsert.newLines.length - 1; nextI >= 0; nextI--) {
486
+ this.lines.set(i--, nextToInsert.newLines[nextI]);
487
+ }
488
+ i++;
489
+
490
+ // Create insert events for later
491
+ insertEvents.push({
492
+ index: originalLineIndex + 1,
493
+ amount: nextToInsert.newLines.length
494
+ });
495
+
496
+ countInsertedSoFar += nextToInsert.newLines.length;
497
+ nextToInsert = toInsert[++nextToInsertIndex];
498
+ } else {
499
+ this.lines.set(i, originalLines[originalLineIndex--]);
500
+ }
501
+ }
502
+
503
+ // Update markers
504
+ let insertCountEmitted = 0;
505
+ for (let i = insertEvents.length - 1; i >= 0; i--) {
506
+ insertEvents[i].index += insertCountEmitted;
507
+ this.lines.onInsertEmitter.fire(insertEvents[i]);
508
+ insertCountEmitted += insertEvents[i].amount;
509
+ }
510
+ const amountToTrim = Math.max(0, originalLinesLength + countToInsert - this.lines.maxLength);
511
+ if (amountToTrim > 0) {
512
+ this.lines.onTrimEmitter.fire(amountToTrim);
513
+ }
514
+ }
515
+ }
516
+
517
+ /**
518
+ * Translates a buffer line to a string, with optional start and end columns.
519
+ * Wide characters will count as two columns in the resulting string. This
520
+ * function is useful for getting the actual text underneath the raw selection
521
+ * position.
522
+ * @param lineIndex The absolute index of the line being translated.
523
+ * @param trimRight Whether to trim whitespace to the right.
524
+ * @param startCol The column to start at.
525
+ * @param endCol The column to end at.
526
+ */
527
+ public translateBufferLineToString(lineIndex: number, trimRight: boolean, startCol: number = 0, endCol?: number): string {
528
+ const line = this.lines.get(lineIndex);
529
+ if (!line) {
530
+ return '';
531
+ }
532
+ return line.translateToString(trimRight, startCol, endCol);
533
+ }
534
+
535
+ public getWrappedRangeForLine(y: number): { first: number, last: number } {
536
+ let first = y;
537
+ let last = y;
538
+ // Scan upwards for wrapped lines
539
+ while (first > 0 && this.lines.get(first)!.isWrapped) {
540
+ first--;
541
+ }
542
+ // Scan downwards for wrapped lines
543
+ while (last + 1 < this.lines.length && this.lines.get(last + 1)!.isWrapped) {
544
+ last++;
545
+ }
546
+ return { first, last };
547
+ }
548
+
549
+ /**
550
+ * Setup the tab stops.
551
+ * @param i The index to start setting up tab stops from.
552
+ */
553
+ public setupTabStops(i?: number): void {
554
+ if (i !== null && i !== undefined) {
555
+ if (!this.tabs[i]) {
556
+ i = this.prevStop(i);
557
+ }
558
+ } else {
559
+ this.tabs = {};
560
+ i = 0;
561
+ }
562
+
563
+ for (; i < this._cols; i += this._optionsService.rawOptions.tabStopWidth) {
564
+ this.tabs[i] = true;
565
+ }
566
+ }
567
+
568
+ /**
569
+ * Move the cursor to the previous tab stop from the given position (default is current).
570
+ * @param x The position to move the cursor to the previous tab stop.
571
+ */
572
+ public prevStop(x?: number): number {
573
+ if (x === null || x === undefined) {
574
+ x = this.x;
575
+ }
576
+ while (!this.tabs[--x] && x > 0);
577
+ return x >= this._cols ? this._cols - 1 : x < 0 ? 0 : x;
578
+ }
579
+
580
+ /**
581
+ * Move the cursor one tab stop forward from the given position (default is current).
582
+ * @param x The position to move the cursor one tab stop forward.
583
+ */
584
+ public nextStop(x?: number): number {
585
+ if (x === null || x === undefined) {
586
+ x = this.x;
587
+ }
588
+ while (!this.tabs[++x] && x < this._cols);
589
+ return x >= this._cols ? this._cols - 1 : x < 0 ? 0 : x;
590
+ }
591
+
592
+ /**
593
+ * Clears markers on single line.
594
+ * @param y The line to clear.
595
+ */
596
+ public clearMarkers(y: number): void {
597
+ this._isClearing = true;
598
+ for (let i = 0; i < this.markers.length; i++) {
599
+ if (this.markers[i].line === y) {
600
+ this.markers[i].dispose();
601
+ this.markers.splice(i--, 1);
602
+ }
603
+ }
604
+ this._isClearing = false;
605
+ }
606
+
607
+ /**
608
+ * Clears markers on all lines
609
+ */
610
+ public clearAllMarkers(): void {
611
+ this._isClearing = true;
612
+ for (let i = 0; i < this.markers.length; i++) {
613
+ this.markers[i].dispose();
614
+ this.markers.splice(i--, 1);
615
+ }
616
+ this._isClearing = false;
617
+ }
618
+
619
+ public addMarker(y: number): Marker {
620
+ const marker = new Marker(y);
621
+ this.markers.push(marker);
622
+ marker.register(this.lines.onTrim(amount => {
623
+ marker.line -= amount;
624
+ // The marker should be disposed when the line is trimmed from the buffer
625
+ if (marker.line < 0) {
626
+ marker.dispose();
627
+ }
628
+ }));
629
+ marker.register(this.lines.onInsert(event => {
630
+ if (marker.line >= event.index) {
631
+ marker.line += event.amount;
632
+ }
633
+ }));
634
+ marker.register(this.lines.onDelete(event => {
635
+ // Delete the marker if it's within the range
636
+ if (marker.line >= event.index && marker.line < event.index + event.amount) {
637
+ marker.dispose();
638
+ }
639
+
640
+ // Shift the marker if it's after the deleted range
641
+ if (marker.line > event.index) {
642
+ marker.line -= event.amount;
643
+ }
644
+ }));
645
+ marker.register(marker.onDispose(() => this._removeMarker(marker)));
646
+ return marker;
647
+ }
648
+
649
+ private _removeMarker(marker: Marker): void {
650
+ if (!this._isClearing) {
651
+ this.markers.splice(this.markers.indexOf(marker), 1);
652
+ }
653
+ }
654
+ }