diff --git a/src/browser/OscLinkProvider.test.ts b/src/browser/OscLinkProvider.test.ts index 0356bcfdc0..4e827b5361 100644 --- a/src/browser/OscLinkProvider.test.ts +++ b/src/browser/OscLinkProvider.test.ts @@ -62,7 +62,7 @@ describe('OscLinkProvider', () => { setUrl(line1, 2, 'bbb', 1); setUrl(line2, 0, 'cccc', 1); setText(line2, 4, 'x'); - line2!.isWrapped = true; + bufferService.buffer.setWrapped(1, true); const links = await getLinks(provider, 2); assert.lengthOf(links, 1); @@ -78,7 +78,7 @@ describe('OscLinkProvider', () => { setUrl(line1, 0, 'aaaaa', 1); setUrl(line2, 0, 'bb', 1); setText(line2, 2, 'ccc'); - line2!.isWrapped = true; + bufferService.buffer.setWrapped(1, true); const links = await getLinks(provider, 1); assert.lengthOf(links, 1); @@ -94,7 +94,7 @@ describe('OscLinkProvider', () => { setUrl(line1, 0, 'aaaaa', 1); setUrl(line2, 0, 'bbb', 2); setText(line2, 3, 'cc'); - line2!.isWrapped = true; + bufferService.buffer.setWrapped(1, true); const links = await getLinks(provider, 1); assert.lengthOf(links, 1); diff --git a/src/browser/TestUtils.test.ts b/src/browser/TestUtils.test.ts index 7b34645952..27a13d4da0 100644 --- a/src/browser/TestUtils.test.ts +++ b/src/browser/TestUtils.test.ts @@ -271,6 +271,9 @@ export class MockBuffer implements IBuffer { public clearAllMarkers(): void { throw new Error('Method not implemented.'); } + public setWrapped(row: number, value: boolean): void { + throw new Error('Method not implemented.'); + } } export class MockRenderer implements IRenderer { diff --git a/src/browser/services/SelectionService.test.ts b/src/browser/services/SelectionService.test.ts index 1f65e909ed..58025c326a 100644 --- a/src/browser/services/SelectionService.test.ts +++ b/src/browser/services/SelectionService.test.ts @@ -198,7 +198,7 @@ describe('SelectionService', () => { it('should expand upwards or downards for wrapped lines', () => { buffer.lines.set(0, stringToRow(' foo')); buffer.lines.set(1, stringToRow('bar ')); - buffer.lines.get(1)!.isWrapped = true; + buffer.setWrapped(1, true); selectionService.selectWordAt([1, 1]); assert.equal(selectionService.selectionText, 'foobar'); selectionService.model.clearSelection(); @@ -212,10 +212,10 @@ describe('SelectionService', () => { buffer.lines.set(2, stringToRow('bbbbbbbbbbbbbbbbbbbb')); buffer.lines.set(3, stringToRow('cccccccccccccccccccc')); buffer.lines.set(4, stringToRow('bar ')); - buffer.lines.get(1)!.isWrapped = true; - buffer.lines.get(2)!.isWrapped = true; - buffer.lines.get(3)!.isWrapped = true; - buffer.lines.get(4)!.isWrapped = true; + buffer.setWrapped(1, true); + buffer.setWrapped(2, true); + buffer.setWrapped(3, true); + buffer.setWrapped(4, true); selectionService.selectWordAt([18, 0]); assert.equal(selectionService.selectionText, expectedText); selectionService.model.clearSelection(); @@ -354,8 +354,8 @@ describe('SelectionService', () => { it('should select the entire wrapped line', () => { buffer.lines.set(0, stringToRow('foo')); const line2 = stringToRow('bar'); - line2.isWrapped = true; buffer.lines.set(1, line2); + buffer.setWrapped(1, true); selectionService.selectLineAt(0); assert.equal(selectionService.selectionText, 'foobar', 'The selected text is correct'); assert.deepEqual(selectionService.model.selectionStart, [0, 0]); diff --git a/src/common/CircularList.ts b/src/common/CircularList.ts index b773391f96..d47bf2678b 100644 --- a/src/common/CircularList.ts +++ b/src/common/CircularList.ts @@ -31,7 +31,6 @@ export interface ICircularList { get(index: number): T | undefined; set(index: number, value: T): void; push(value: T): void; - recycle(): T; pop(): T | undefined; splice(start: number, deleteCount: number, ...items: T[]): void; trimStart(count: number): void; @@ -136,20 +135,6 @@ export class CircularList extends Disposable implements ICircularList { } } - /** - * Advance ringbuffer index and return current element for recycling. - * Note: The buffer must be full for this method to work. - * @throws When the buffer is not full. - */ - public recycle(): T { - if (this._length !== this._maxLength) { - throw new Error('Can only recycle when the buffer is full'); - } - this._startIndex = ++this._startIndex % this._maxLength; - this.onTrimEmitter.fire(1); - return this._array[this._getCyclicIndex(this._length - 1)]!; - } - /** * Ringbuffer is at max length. */ diff --git a/src/common/InputHandler.test.ts b/src/common/InputHandler.test.ts index 9eab8cfa23..be7ed0e487 100644 --- a/src/common/InputHandler.test.ts +++ b/src/common/InputHandler.test.ts @@ -461,8 +461,10 @@ describe('InputHandler', () => { await resetToBaseState(); bufferService.buffer.y = 2; bufferService.buffer.x = 40; - inputHandler.eraseInLine(Params.fromArray([0])); assert.equal(bufferService.buffer.lines.get(2)!.isWrapped, true); + assert.equal(bufferService.buffer.lines.get(3)!.isWrapped, true); + inputHandler.eraseInLine(Params.fromArray([0])); + assert.equal(bufferService.buffer.lines.get(2)!.isWrapped, true);assert.equal(bufferService.buffer.lines.get(3)!.isWrapped, false); bufferService.buffer.y = 2; bufferService.buffer.x = 0; inputHandler.eraseInLine(Params.fromArray([0])); @@ -473,14 +475,15 @@ describe('InputHandler', () => { bufferService.buffer.y = 2; bufferService.buffer.x = 40; inputHandler.eraseInLine(Params.fromArray([1])); - assert.equal(bufferService.buffer.lines.get(2)!.isWrapped, true); + assert.equal(bufferService.buffer.lines.get(2)!.isWrapped, false); + assert.equal(bufferService.buffer.lines.get(3)!.isWrapped, true); // params[2] - erase complete line await resetToBaseState(); bufferService.buffer.y = 2; bufferService.buffer.x = 40; inputHandler.eraseInLine(Params.fromArray([2])); - assert.equal(bufferService.buffer.lines.get(2)!.isWrapped, false); + assert.equal(bufferService.buffer.lines.get(2)!.isWrapped, false);assert.equal(bufferService.buffer.lines.get(3)!.isWrapped, false); }); it('ED2 with scrollOnEraseInDisplay turned on', async () => { const inputHandler = new TestInputHandler( diff --git a/src/common/InputHandler.ts b/src/common/InputHandler.ts index 0b12dd8e1d..facfdaa98e 100644 --- a/src/common/InputHandler.ts +++ b/src/common/InputHandler.ts @@ -579,8 +579,8 @@ export class InputHandler extends Disposable implements IInputHandler { // autowrap - DECAWM // automatically wraps to the beginning of the next line if (wraparoundMode) { - const oldRow = bufferRow; - let oldCol = this._activeBuffer.x - oldWidth; + const oldRow = bufferRow as BufferLine; + const oldCol = this._activeBuffer.x - oldWidth; this._activeBuffer.x = oldWidth; this._activeBuffer.y++; if (this._activeBuffer.y === this._activeBuffer.scrollBottom + 1) { @@ -592,7 +592,7 @@ export class InputHandler extends Disposable implements IInputHandler { } // The line already exists (eg. the initial viewport), mark it as a // wrapped line - this._activeBuffer.lines.get(this._activeBuffer.ybase + this._activeBuffer.y)!.isWrapped = true; + this._activeBuffer.setWrapped(this._activeBuffer.ybase + this._activeBuffer.y, true); } // row changed, get it again bufferRow = this._activeBuffer.lines.get(this._activeBuffer.ybase + this._activeBuffer.y); @@ -606,9 +606,7 @@ export class InputHandler extends Disposable implements IInputHandler { oldCol, 0, oldWidth, false); } // clear left over cells to the right - while (oldCol < cols) { - oldRow.setCellFromCodepoint(oldCol++, 0, 1, curAttr); - } + oldRow.eraseRight(oldCol); } else { this._activeBuffer.x = cols - 1; if (chWidth === 2) { @@ -756,7 +754,7 @@ export class InputHandler extends Disposable implements IInputHandler { // reprint is common, especially on resize. Note that the windowsMode wrapped line heuristics // can mess with this so windowsMode should be disabled, which is recommended on Windows build // 21376 and above. - this._activeBuffer.lines.get(this._activeBuffer.ybase + this._activeBuffer.y)!.isWrapped = false; + this._activeBuffer.setWrapped(this._activeBuffer.ybase + this._activeBuffer.y, false); } // If the end of the line is hit, prevent this action from wrapping around to the next line. if (this._activeBuffer.x >= this._bufferService.cols) { @@ -820,7 +818,7 @@ export class InputHandler extends Disposable implements IInputHandler { && this._activeBuffer.y > this._activeBuffer.scrollTop && this._activeBuffer.y <= this._activeBuffer.scrollBottom && this._activeBuffer.lines.get(this._activeBuffer.ybase + this._activeBuffer.y)?.isWrapped) { - this._activeBuffer.lines.get(this._activeBuffer.ybase + this._activeBuffer.y)!.isWrapped = false; + this._activeBuffer.setWrapped(this._activeBuffer.ybase + this._activeBuffer.y, false); this._activeBuffer.y--; this._activeBuffer.x = this._bufferService.cols - 1; // find last taken cell - last cell can have 3 different states: @@ -1166,25 +1164,34 @@ export class InputHandler extends Disposable implements IInputHandler { /** * Helper method to erase cells in a terminal row. * The cell gets replaced with the eraseChar of the terminal. + * Clear isWrapped if start===0; + * clear isWrapped of next line if end >= cols. * @param y The row index relative to the viewport. * @param start The start x index of the range to be erased. * @param end The end x index of the range to be erased (exclusive). - * @param clearWrap clear the isWrapped flag * @param respectProtect Whether to respect the protection attribute (DECSCA). */ - private _eraseInBufferLine(y: number, start: number, end: number, clearWrap: boolean = false, respectProtect: boolean = false): void { - const line = this._activeBuffer.lines.get(this._activeBuffer.ybase + y); - if (!line) { + private _eraseInBufferLine(y: number, start: number, end: number, respectProtect: boolean = false): void { + const yAbs = y + this._activeBuffer.ybase; + const line = this._activeBuffer.lines.get(yAbs); + if (!(line instanceof BufferLine)) { return; } - line.replaceCells( - start, - end, - this._activeBuffer.getNullCell(this._eraseAttrData()), - respectProtect - ); - if (clearWrap) { - line.isWrapped = false; + if (!respectProtect && end >= this._bufferService.cols) { + const next = line.nextBufferLine; + if (next) next.asUnwrapped(line); + line.eraseRight(start); + line.logical().backgroundColor = this._curAttrData.bg & ~0xFC000000; + } else { + line.replaceCells( + start, + end, + this._activeBuffer.getNullCell(this._eraseAttrData()), + respectProtect + ); + } + if (start === 0) { + this._activeBuffer.setWrapped(this._activeBuffer.ybase + y, false); } } @@ -1194,12 +1201,8 @@ export class InputHandler extends Disposable implements IInputHandler { * @param y row index */ private _resetBufferLine(y: number, respectProtect: boolean = false): void { - const line = this._activeBuffer.lines.get(this._activeBuffer.ybase + y); - if (line) { - line.fill(this._activeBuffer.getNullCell(this._eraseAttrData()), respectProtect); - this._bufferService.buffer.clearMarkers(this._activeBuffer.ybase + y); - line.isWrapped = false; - } + this._eraseInBufferLine(y, 0, this._bufferService.cols, respectProtect); + this._bufferService.buffer.clearMarkers(this._activeBuffer.ybase + y); } /** @@ -1233,7 +1236,7 @@ export class InputHandler extends Disposable implements IInputHandler { case 0: j = this._activeBuffer.y; this._dirtyRowTracker.markDirty(j); - this._eraseInBufferLine(j++, this._activeBuffer.x, this._bufferService.cols, this._activeBuffer.x === 0, respectProtect); + this._eraseInBufferLine(j++, this._activeBuffer.x, this._bufferService.cols, respectProtect); for (; j < this._bufferService.rows; j++) { this._resetBufferLine(j, respectProtect); } @@ -1243,14 +1246,7 @@ export class InputHandler extends Disposable implements IInputHandler { j = this._activeBuffer.y; this._dirtyRowTracker.markDirty(j); // Deleted front part of line and everything before. This line will no longer be wrapped. - this._eraseInBufferLine(j, 0, this._activeBuffer.x + 1, true, respectProtect); - if (this._activeBuffer.x + 1 >= this._bufferService.cols) { - // Deleted entire previous line. This next line can no longer be wrapped. - const nextLine = this._activeBuffer.lines.get(j + 1); - if (nextLine) { - nextLine.isWrapped = false; - } - } + this._eraseInBufferLine(j, 0, this._activeBuffer.x + 1, respectProtect); while (j--) { this._resetBufferLine(j, respectProtect); } @@ -1320,13 +1316,13 @@ export class InputHandler extends Disposable implements IInputHandler { this._restrictCursor(this._bufferService.cols); switch (params.params[0]) { case 0: - this._eraseInBufferLine(this._activeBuffer.y, this._activeBuffer.x, this._bufferService.cols, this._activeBuffer.x === 0, respectProtect); + this._eraseInBufferLine(this._activeBuffer.y, this._activeBuffer.x, this._bufferService.cols, respectProtect); break; case 1: - this._eraseInBufferLine(this._activeBuffer.y, 0, this._activeBuffer.x + 1, false, respectProtect); + this._eraseInBufferLine(this._activeBuffer.y, 0, this._activeBuffer.x + 1, respectProtect); break; case 2: - this._eraseInBufferLine(this._activeBuffer.y, 0, this._bufferService.cols, true, respectProtect); + this._eraseInBufferLine(this._activeBuffer.y, 0, this._bufferService.cols, respectProtect); break; } this._dirtyRowTracker.markDirty(this._activeBuffer.y); @@ -1511,9 +1507,10 @@ export class InputHandler extends Disposable implements IInputHandler { } const param = params.params[0] || 1; for (let y = this._activeBuffer.scrollTop; y <= this._activeBuffer.scrollBottom; ++y) { - const line = this._activeBuffer.lines.get(this._activeBuffer.ybase + y)!; + const row = this._activeBuffer.ybase + y; + const line = this._activeBuffer.lines.get(row)!; line.deleteCells(0, param, this._activeBuffer.getNullCell(this._eraseAttrData())); - line.isWrapped = false; + this._activeBuffer.setWrapped(row, false); } this._dirtyRowTracker.markRangeDirty(this._activeBuffer.scrollTop, this._activeBuffer.scrollBottom); return true; @@ -1544,9 +1541,10 @@ export class InputHandler extends Disposable implements IInputHandler { } const param = params.params[0] || 1; for (let y = this._activeBuffer.scrollTop; y <= this._activeBuffer.scrollBottom; ++y) { - const line = this._activeBuffer.lines.get(this._activeBuffer.ybase + y)!; + const row = this._activeBuffer.ybase + y; + const line = this._activeBuffer.lines.get(row)!; + this._activeBuffer.setWrapped(row, false); line.insertCells(0, param, this._activeBuffer.getNullCell(this._eraseAttrData())); - line.isWrapped = false; } this._dirtyRowTracker.markRangeDirty(this._activeBuffer.scrollTop, this._activeBuffer.scrollBottom); return true; @@ -1567,9 +1565,10 @@ export class InputHandler extends Disposable implements IInputHandler { } const param = params.params[0] || 1; for (let y = this._activeBuffer.scrollTop; y <= this._activeBuffer.scrollBottom; ++y) { - const line = this._activeBuffer.lines.get(this._activeBuffer.ybase + y)!; + const row = this._activeBuffer.ybase + y; + this._activeBuffer.setWrapped(row, false); + const line = this._activeBuffer.lines.get(row)!; line.insertCells(this._activeBuffer.x, param, this._activeBuffer.getNullCell(this._eraseAttrData())); - line.isWrapped = false; } this._dirtyRowTracker.markRangeDirty(this._activeBuffer.scrollTop, this._activeBuffer.scrollBottom); return true; @@ -1590,9 +1589,10 @@ export class InputHandler extends Disposable implements IInputHandler { } const param = params.params[0] || 1; for (let y = this._activeBuffer.scrollTop; y <= this._activeBuffer.scrollBottom; ++y) { - const line = this._activeBuffer.lines.get(this._activeBuffer.ybase + y)!; + const row = this._activeBuffer.ybase + y; + const line = this._activeBuffer.lines.get(row)!; + this._activeBuffer.setWrapped(row, false); line.deleteCells(this._activeBuffer.x, param, this._activeBuffer.getNullCell(this._eraseAttrData())); - line.isWrapped = false; } this._dirtyRowTracker.markRangeDirty(this._activeBuffer.scrollTop, this._activeBuffer.scrollBottom); return true; @@ -3473,11 +3473,8 @@ export class InputHandler extends Disposable implements IInputHandler { this._setCursor(0, 0); for (let yOffset = 0; yOffset < this._bufferService.rows; ++yOffset) { const row = this._activeBuffer.ybase + this._activeBuffer.y + yOffset; - const line = this._activeBuffer.lines.get(row); - if (line) { - line.fill(cell); - line.isWrapped = false; - } + this._activeBuffer.setWrapped(row, false); + this._activeBuffer.lines.get(row)?.fill(cell); } this._dirtyRowTracker.markAllDirty(); this._setCursor(0, 0); diff --git a/src/common/WindowsMode.ts b/src/common/WindowsMode.ts index d4862e00e7..da93e6e681 100644 --- a/src/common/WindowsMode.ts +++ b/src/common/WindowsMode.ts @@ -19,9 +19,7 @@ export function updateWindowsModeWrappedState(bufferService: IBufferService): vo // wrapped. const line = bufferService.buffer.lines.get(bufferService.buffer.ybase + bufferService.buffer.y - 1); const lastChar = line?.get(bufferService.cols - 1); - - const nextLine = bufferService.buffer.lines.get(bufferService.buffer.ybase + bufferService.buffer.y); - if (nextLine && lastChar) { - nextLine.isWrapped = (lastChar[CHAR_DATA_CODE_INDEX] !== NULL_CELL_CODE && lastChar[CHAR_DATA_CODE_INDEX] !== WHITESPACE_CELL_CODE); + if (lastChar) { + bufferService.buffer.setWrapped(bufferService.buffer.ybase + bufferService.buffer.y, lastChar[CHAR_DATA_CODE_INDEX] !== NULL_CELL_CODE && lastChar[CHAR_DATA_CODE_INDEX] !== WHITESPACE_CELL_CODE); } } diff --git a/src/common/buffer/Buffer.test.ts b/src/common/buffer/Buffer.test.ts index cbb8cf9ed4..2fc5664355 100644 --- a/src/common/buffer/Buffer.test.ts +++ b/src/common/buffer/Buffer.test.ts @@ -80,40 +80,40 @@ describe('Buffer', () => { describe('wrapped', () => { it('should return a range for the first row', () => { buffer.fillViewportRows(); - buffer.lines.get(1)!.isWrapped = true; + buffer.setWrapped(1, true); assert.deepEqual(buffer.getWrappedRangeForLine(0), { first: 0, last: 1 }); }); it('should return a range for a middle row wrapping upwards', () => { buffer.fillViewportRows(); - buffer.lines.get(12)!.isWrapped = true; + buffer.setWrapped(12, true); assert.deepEqual(buffer.getWrappedRangeForLine(12), { first: 11, last: 12 }); }); it('should return a range for a middle row wrapping downwards', () => { buffer.fillViewportRows(); - buffer.lines.get(13)!.isWrapped = true; + buffer.setWrapped(13, true); assert.deepEqual(buffer.getWrappedRangeForLine(12), { first: 12, last: 13 }); }); it('should return a range for a middle row wrapping both ways', () => { buffer.fillViewportRows(); - buffer.lines.get(11)!.isWrapped = true; - buffer.lines.get(12)!.isWrapped = true; - buffer.lines.get(13)!.isWrapped = true; - buffer.lines.get(14)!.isWrapped = true; + buffer.setWrapped(11, true); + buffer.setWrapped(12, true); + buffer.setWrapped(13, true); + buffer.setWrapped(14, true); assert.deepEqual(buffer.getWrappedRangeForLine(12), { first: 10, last: 14 }); }); it('should return a range for the last row', () => { buffer.fillViewportRows(); - buffer.lines.get(23)!.isWrapped = true; + buffer.setWrapped(23, true); assert.deepEqual(buffer.getWrappedRangeForLine(buffer.lines.length - 1), { first: 22, last: 23 }); }); it('should return a range for a row that wraps upward to first row', () => { buffer.fillViewportRows(); - buffer.lines.get(1)!.isWrapped = true; + buffer.setWrapped(1, true); assert.deepEqual(buffer.getWrappedRangeForLine(1), { first: 0, last: 1 }); }); it('should return a range for a row that wraps downward to last row', () => { buffer.fillViewportRows(); - buffer.lines.get(buffer.lines.length - 1)!.isWrapped = true; + buffer.setWrapped(buffer.lines.length - 1, true); assert.deepEqual(buffer.getWrappedRangeForLine(buffer.lines.length - 2), { first: 22, last: 23 }); }); }); @@ -624,7 +624,7 @@ describe('Buffer', () => { buffer.lines.get(0)!.set(1, [0, 'b', 1, 'b'.charCodeAt(0)]); buffer.lines.get(1)!.set(0, [0, 'c', 1, 'c'.charCodeAt(0)]); buffer.lines.get(1)!.set(1, [0, 'd', 1, 'd'.charCodeAt(0)]); - buffer.lines.get(1)!.isWrapped = true; + buffer.setWrapped(1, true); // Buffer: // "ab " (wrapped) // "cd" @@ -655,7 +655,7 @@ describe('Buffer', () => { buffer.lines.get(0)!.set(i, [0, '', 0, 0]); buffer.lines.get(1)!.set(i, [0, '', 0, 0]); } - buffer.lines.get(1)!.isWrapped = true; + buffer.setWrapped(1, true); // Buffer: // 汉语汉语汉语 (wrapped) // 汉语汉语汉语 @@ -682,7 +682,7 @@ describe('Buffer', () => { buffer.lines.get(0)!.set(1, [0, 'b', 1, 'b'.charCodeAt(0)]); buffer.lines.get(1)!.set(0, [0, 'c', 1, 'c'.charCodeAt(0)]); buffer.lines.get(1)!.set(1, [0, 'd', 1, 'd'.charCodeAt(0)]); - buffer.lines.get(1)!.isWrapped = true; + buffer.setWrapped(1, true); // Buffer: // "ab " (wrapped) // "cd" @@ -716,7 +716,7 @@ describe('Buffer', () => { buffer.lines.get(0)!.set(i, [0, '', 0, 0]); buffer.lines.get(1)!.set(i, [0, '', 0, 0]); } - buffer.lines.get(1)!.isWrapped = true; + buffer.setWrapped(1, true); // Buffer: // 汉语汉语汉语 (wrapped) // 汉语汉语汉语 @@ -771,17 +771,17 @@ describe('Buffer', () => { buffer.lines.get(0)!.set(1, [0, 'b', 1, 'b'.charCodeAt(0)]); buffer.lines.get(1)!.set(0, [0, 'c', 1, 'c'.charCodeAt(0)]); buffer.lines.get(1)!.set(1, [0, 'd', 1, 'd'.charCodeAt(0)]); - buffer.lines.get(1)!.isWrapped = true; + buffer.setWrapped(1, true); buffer.lines.get(2)!.set(0, [0, 'e', 1, 'e'.charCodeAt(0)]); buffer.lines.get(2)!.set(1, [0, 'f', 1, 'f'.charCodeAt(0)]); buffer.lines.get(3)!.set(0, [0, 'g', 1, 'g'.charCodeAt(0)]); buffer.lines.get(3)!.set(1, [0, 'h', 1, 'h'.charCodeAt(0)]); - buffer.lines.get(3)!.isWrapped = true; + buffer.setWrapped(3, true); buffer.lines.get(4)!.set(0, [0, 'i', 1, 'i'.charCodeAt(0)]); buffer.lines.get(4)!.set(1, [0, 'j', 1, 'j'.charCodeAt(0)]); buffer.lines.get(5)!.set(0, [0, 'k', 1, 'k'.charCodeAt(0)]); buffer.lines.get(5)!.set(1, [0, 'l', 1, 'l'.charCodeAt(0)]); - buffer.lines.get(5)!.isWrapped = true; + buffer.setWrapped(5, true); }); describe('viewport not yet filled', () => { it('should move the cursor up and add empty lines', () => { @@ -1276,7 +1276,6 @@ describe('Buffer', () => { assert.equal(str3, '😁a'); }); }); - describe('line string cache cleanup', () => { it('should clear shared cache entries with a single timer', () => { const originalSetTimeout = globalThis.setTimeout; @@ -1370,32 +1369,4 @@ describe('Buffer', () => { }); }); - describe('memory cleanup after shrinking', () => { - it('should realign memory from idle task execution', async () => { - buffer.fillViewportRows(); - - // shrink more than 2 times to trigger lazy memory cleanup - buffer.resize(INIT_COLS / 2 - 1, INIT_ROWS); - - // sync - for (let i = 0; i < INIT_ROWS; i++) { - const line = buffer.lines.get(i)!; - // line memory is still at old size from initialization - assert.equal((line as any)._data.buffer.byteLength, INIT_COLS * 3 * 4); - // array.length and .length get immediately adjusted - assert.equal((line as any)._data.length, (INIT_COLS / 2 - 1) * 3); - assert.equal(line.length, INIT_COLS / 2 - 1); - } - - // wait for a bit to give IdleTaskQueue a chance to kick in - // and finish memory cleaning - await new Promise(r => setTimeout(r, 30)); - - // cleanup should have realigned memory with exact bytelength - for (let i = 0; i < INIT_ROWS; i++) { - const line = buffer.lines.get(i)!; - assert.equal((line as any)._data.buffer.byteLength, (INIT_COLS / 2 - 1) * 3 * 4); - } - }); - }); }); diff --git a/src/common/buffer/Buffer.ts b/src/common/buffer/Buffer.ts index c7d094a43c..e6f8ae1750 100644 --- a/src/common/buffer/Buffer.ts +++ b/src/common/buffer/Buffer.ts @@ -4,14 +4,13 @@ */ import { CircularList, IInsertEvent } from '../CircularList'; -import { Disposable, toDisposable } from '../Lifecycle'; -import { IdleTaskQueue } from '../TaskQueue'; -import { ICharset } from '../Types'; +import { Disposable } from '../Lifecycle'; import { IAttributeData, IBuffer, IBufferLine, ICellData } from './Types'; +import { ICharset } from '../Types'; import { ExtendedAttrs } from './AttributeData'; -import { BufferLine, DEFAULT_ATTR_DATA } from './BufferLine'; +import { BufferLine, LogicalLine, DEFAULT_ATTR_DATA } from './BufferLine'; import { BufferLineStringCache } from './BufferLineStringCache'; -import { getWrappedLineTrimmedLength, reflowLargerApplyNewLayout, reflowLargerCreateNewLayout, reflowLargerGetLinesToRemove, reflowSmallerGetNewLineLengths } from './BufferReflow'; +import { reflowLine, reflowLargerApplyNewLayout, reflowLargerCreateNewLayout, reflowLargerGetLinesToRemove } from './BufferReflow'; import { CellData } from './CellData'; import { NULL_CELL_CHAR, NULL_CELL_CODE, NULL_CELL_WIDTH, WHITESPACE_CELL_CHAR, WHITESPACE_CELL_CODE, WHITESPACE_CELL_WIDTH } from './Constants'; import { Marker } from './Marker'; @@ -31,6 +30,7 @@ export class Buffer extends Disposable implements IBuffer { public lines: CircularList; public ydisp: number = 0; public ybase: number = 0; + /** Row number, relative to ybase. */ public y: number = 0; public x: number = 0; public scrollBottom: number; @@ -50,8 +50,6 @@ export class Buffer extends Disposable implements IBuffer { private _cols: number; private _rows: number; private _isClearing: boolean = false; - private _memoryCleanupQueue: InstanceType; - private _memoryCleanupPosition = 0; private readonly _stringCache: BufferLineStringCache; constructor( @@ -67,9 +65,13 @@ export class Buffer extends Disposable implements IBuffer { this.scrollTop = 0; this.scrollBottom = this._rows - 1; this.setupTabStops(); - this._memoryCleanupQueue = new IdleTaskQueue(this._logService); - this._register(toDisposable(() => this._memoryCleanupQueue.clear())); - this._register(toDisposable(() => this.clearAllMarkers())); + + this.lines.onTrim(amount => { + const first = this.lines.length && this.lines.get(0); + if (first instanceof BufferLine && first.isWrapped) { + const prev = first.getPreviousLine(); + prev && first.asUnwrapped(prev); + }}); this._stringCache = this._register(new BufferLineStringCache()); } @@ -99,8 +101,16 @@ export class Buffer extends Disposable implements IBuffer { return this._whitespaceCell; } - public getBlankLine(attr: IAttributeData, isWrapped?: boolean): IBufferLine { - return new BufferLine(this._stringCache, this._bufferService.cols, this.getNullCell(attr), isWrapped); + /** + * Get an empty unwrapped line. + * @param attr Only used for the background color. + */ + public getBlankLine( + attr: IAttributeData, + logicalLine: LogicalLine = new LogicalLine() + ): IBufferLine { + logicalLine.backgroundColor = attr.bg & ~0xFC000000; + return new BufferLine(this._stringCache, this._cols, logicalLine); } public get hasScrollback(): boolean { @@ -128,6 +138,18 @@ export class Buffer extends Disposable implements IBuffer { return correctBufferLength > MAX_BUFFER_SIZE ? MAX_BUFFER_SIZE : correctBufferLength; } + public setWrapped(absrow: number, value: boolean): void { + const line = this.lines.get(absrow); + if (!line || line.isWrapped === value) + {return;} + const prevRow = this.lines.get(absrow - 1) as BufferLine; + if (value) { + (line as BufferLine).setWrapped(prevRow); + } else { + (line as BufferLine).asUnwrapped(prevRow); + } + } + /** * Fills the buffer's viewport with blank lines. */ @@ -162,13 +184,8 @@ export class Buffer extends Disposable implements IBuffer { * @param newRows The new number of rows. */ public resize(newCols: number, newRows: number): void { - // store reference to null cell with default attrs - const nullCell = this.getNullCell(DEFAULT_ATTR_DATA); this._stringCache.clear(); - // count bufferlines with overly big memory to be cleaned afterwards - let dirtyMemoryLines = 0; - // Increase max length if needed before adjustments to allow space to fill // as required. const newMaxLength = this._getCorrectBufferLength(newRows); @@ -186,8 +203,7 @@ export class Buffer extends Disposable implements IBuffer { // Deal with columns increasing (reducing needs to happen after reflow) if (this._cols < newCols) { for (let i = 0; i < this.lines.length; i++) { - // +boolean for fast 0 or 1 conversion - dirtyMemoryLines += +this.lines.get(i)!.resize(newCols, nullCell); + this.lines.get(i)!.length = newCols; } } @@ -199,7 +215,7 @@ export class Buffer extends Disposable implements IBuffer { if (this._optionsService.rawOptions.windowsPty.backend !== undefined || this._optionsService.rawOptions.windowsPty.buildNumber !== undefined) { // Just add the new missing rows on Windows as conpty reprints the screen with its // view of the world. Once a line enters scrollback for conpty it remains there - this.lines.push(new BufferLine(this._stringCache, newCols, nullCell, false)); + this.lines.push(new BufferLine(this._stringCache, newCols)); } else { if (this.ybase > 0 && this.lines.length <= this.ybase + this.y + addToY + 1) { // There is room above the buffer and there are no empty elements below the line, @@ -213,7 +229,7 @@ export class Buffer extends Disposable implements IBuffer { } else { // Add a blank line if there is no buffer left at the top to scroll to, or if there // are blank lines after the cursor - this.lines.push(new BufferLine(this._stringCache, newCols, nullCell, false)); + this.lines.push(new BufferLine(this._stringCache, newCols)); } } } @@ -266,8 +282,7 @@ export class Buffer extends Disposable implements IBuffer { // Trim the end of the line off if cols shrunk if (this._cols > newCols) { for (let i = 0; i < this.lines.length; i++) { - // +boolean for fast 0 or 1 conversion - dirtyMemoryLines += +this.lines.get(i)!.resize(newCols, nullCell); + this.lines.get(i)!.length = newCols; } } } @@ -281,35 +296,6 @@ export class Buffer extends Disposable implements IBuffer { const maxY = Math.max(0, this.lines.length - this.ybase - 1); this.y = Math.min(this.y, maxY); } - - this._memoryCleanupQueue.clear(); - // schedule memory cleanup only, if more than 10% of the lines are affected - if (dirtyMemoryLines > 0.1 * this.lines.length) { - this._memoryCleanupPosition = 0; - this._memoryCleanupQueue.enqueue(() => this._batchedMemoryCleanup()); - } - } - - private _batchedMemoryCleanup(): boolean { - let normalRun = true; - if (this._memoryCleanupPosition >= this.lines.length) { - // cleanup made it once through all lines, thus rescan in loop below to also catch shifted - // lines, which should finish rather quick if there are no more cleanups pending - this._memoryCleanupPosition = 0; - normalRun = false; - } - let counted = 0; - while (this._memoryCleanupPosition < this.lines.length) { - counted += this.lines.get(this._memoryCleanupPosition++)!.cleanupMemory(); - // cleanup max 100 lines per batch - if (counted > 100) { - return true; - } - } - // normal runs always need another rescan afterwards - // if we made it here with normalRun=false, we are in a final run - // and can end the cleanup task for sure - return normalRun; } private get _isReflowEnabled(): boolean { @@ -344,7 +330,6 @@ export class Buffer extends Disposable implements IBuffer { } private _reflowLargerAdjustViewport(newCols: number, newRows: number, countRemoved: number): void { - const nullCell = this.getNullCell(DEFAULT_ATTR_DATA); // Adjust viewport based on number of items removed let viewportAdjustments = countRemoved; while (viewportAdjustments-- > 0) { @@ -354,7 +339,7 @@ export class Buffer extends Disposable implements IBuffer { } if (this.lines.length < newRows) { // Add an extra row at the bottom of the viewport - this.lines.push(new BufferLine(this._stringCache, newCols, nullCell, false)); + this.lines.push(new BufferLine(this._stringCache, newCols)); } } else { if (this.ydisp === this.ybase) { @@ -368,7 +353,6 @@ export class Buffer extends Disposable implements IBuffer { private _reflowSmaller(newCols: number, newRows: number): void { const reflowCursorLine = this._optionsService.rawOptions.reflowCursorLine; - const nullCell = this.getNullCell(DEFAULT_ATTR_DATA); // Gather all BufferLines that need to be inserted into the Buffer here so that they can be // batched up and only committed once const toInsert = []; @@ -393,13 +377,17 @@ export class Buffer extends Disposable implements IBuffer { // wrapped lines with the cursor const absoluteY = this.ybase + this.y; if (absoluteY >= y && absoluteY < y + wrappedLines.length) { + for (let i = wrappedLines.length; --i > 0; ) { + wrappedLines[i].asUnwrapped(wrappedLines[i-1]); + } + for (let i = wrappedLines.length; --i >= 0; ) { + wrappedLines[i].eraseRight(newCols); + } continue; } } - - const lastLineLength = wrappedLines[wrappedLines.length - 1].getTrimmedLength(); - const destLineLengths = reflowSmallerGetNewLineLengths(wrappedLines, this._cols, newCols); - const linesToAdd = destLineLengths.length - wrappedLines.length; + const newLines = reflowLine(wrappedLines, newCols); + const linesToAdd = newLines.length; let trimmedLines: number; if (this.ybase === 0 && this.y !== this.lines.length - 1) { // If the top section of the buffer is not yet filled @@ -408,12 +396,6 @@ export class Buffer extends Disposable implements IBuffer { trimmedLines = Math.max(0, this.lines.length - this.lines.maxLength + linesToAdd); } - // Add the new lines - const newLines: BufferLine[] = []; - for (let i = 0; i < linesToAdd; i++) { - const newLine = this.getBlankLine(DEFAULT_ATTR_DATA, true) as BufferLine; - newLines.push(newLine); - } if (newLines.length > 0) { toInsert.push({ // countToInsert here gets the actual index, taking into account other inserted items. @@ -422,46 +404,8 @@ export class Buffer extends Disposable implements IBuffer { newLines }); countToInsert += newLines.length; + wrappedLines.push(...newLines); } - wrappedLines.push(...newLines); - - // Copy buffer data to new locations, this needs to happen backwards to do in-place - let destLineIndex = destLineLengths.length - 1; // Math.floor(cellsNeeded / newCols); - let destCol = destLineLengths[destLineIndex]; // cellsNeeded % newCols; - if (destCol === 0) { - destLineIndex--; - destCol = destLineLengths[destLineIndex]; - } - let srcLineIndex = wrappedLines.length - linesToAdd - 1; - let srcCol = lastLineLength; - while (srcLineIndex >= 0) { - const cellsToCopy = Math.min(srcCol, destCol); - if (wrappedLines[destLineIndex] === undefined) { - // Sanity check that the line exists, this has been known to fail for an unknown reason - // which would stop the reflow from happening if an exception would throw. - break; - } - wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol - cellsToCopy, destCol - cellsToCopy, cellsToCopy, true); - destCol -= cellsToCopy; - if (destCol === 0) { - destLineIndex--; - destCol = destLineLengths[destLineIndex]; - } - srcCol -= cellsToCopy; - if (srcCol === 0) { - srcLineIndex--; - const wrappedLinesIndex = Math.max(srcLineIndex, 0); - srcCol = getWrappedLineTrimmedLength(wrappedLines, wrappedLinesIndex, this._cols); - } - } - - // Null out the end of the line ends if a wide character wrapped to the following line - for (let i = 0; i < wrappedLines.length; i++) { - if (destLineLengths[i] < newCols) { - wrappedLines[i].setCell(destLineLengths[i], nullCell); - } - } - // Adjust viewport as needed let viewportAdjustments = linesToAdd - trimmedLines; while (viewportAdjustments-- > 0) { diff --git a/src/common/buffer/BufferLine.test.ts b/src/common/buffer/BufferLine.test.ts index 7fc12ad76a..8ea8fa7c96 100644 --- a/src/common/buffer/BufferLine.test.ts +++ b/src/common/buffer/BufferLine.test.ts @@ -3,7 +3,7 @@ * @license MIT */ import { NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE, DEFAULT_ATTR, Content, UnderlineStyle, BgFlags, Attributes, FgFlags } from './Constants'; -import { BufferLine } from './BufferLine'; +import { BufferLine, LogicalLine } from './BufferLine'; import { BufferLineStringCache } from './BufferLineStringCache'; import { CellData } from './CellData'; import { CharData, IBufferLine, ICellData } from './Types'; @@ -15,12 +15,21 @@ const TEST_STRING_CACHE = new BufferLineStringCache(); class TestBufferLine extends BufferLine { - constructor(cols: number, fillCellData?: ICellData, isWrapped: boolean = false) { - super(TEST_STRING_CACHE, cols, fillCellData, isWrapped); - } - - public get combined(): {[index: number]: string} { - return this._combined; + constructor(cols: number, fillCellData?: CellData, isWrapped: boolean = false) { + const lline = new LogicalLine(); + super(TEST_STRING_CACHE, cols, lline); + if (isWrapped) { + const prevLine = new BufferLine(TEST_STRING_CACHE, cols, lline); + lline.firstBufferLine = prevLine; + prevLine.nextBufferLine = this; + this.startColumn = cols; + fillCellData && prevLine.fill(fillCellData); + } else { + lline.firstBufferLine = this; + } + if (fillCellData) { + this.fill(fillCellData); + } } public get cachedString(): string | undefined { @@ -262,18 +271,6 @@ describe('BufferLine', function(): void { [123, 'z', 1, 'z'.charCodeAt(0)] ]); }); - it('clone', function(): void { - const line = new TestBufferLine(5, undefined, true); - line.setCell(0, createCellData(1, 'a', 1)); - line.setCell(1, createCellData(2, 'b', 1)); - line.setCell(2, createCellData(3, 'c', 1)); - line.setCell(3, createCellData(4, 'd', 1)); - line.setCell(4, createCellData(5, 'e', 1)); - const line2 = line.clone(); - assert.deepEqual(TestBufferLine.prototype.toArray.apply(line2), line.toArray()); - assert.equal(line2.length, line.length); - assert.equal(line2.isWrapped, line.isWrapped); - }); it('copyFrom', function(): void { const line = new TestBufferLine(5); line.setCell(0, createCellData(1, 'a', 1)); @@ -281,11 +278,10 @@ describe('BufferLine', function(): void { line.setCell(2, createCellData(3, 'c', 1)); line.setCell(3, createCellData(4, 'd', 1)); line.setCell(4, createCellData(5, 'e', 1)); - const line2 = new TestBufferLine(5, createCellData(1, 'a', 1), true); + const line2 = new TestBufferLine(5, createCellData(1, 'a', 1)); line2.copyFrom(line); assert.deepEqual(line2.toArray(), line.toArray()); assert.equal(line2.length, line.length); - assert.equal(line2.isWrapped, line.isWrapped); }); it('should support combining chars', function(): void { // CHAR_DATA_CODE_INDEX resembles current behavior in InputHandler.print @@ -296,8 +292,6 @@ describe('BufferLine', function(): void { const line2 = new TestBufferLine(5, createCellData(1, 'a', 1), true); line2.copyFrom(line); assert.deepEqual(line2.toArray(), line.toArray()); - const line3 = line.clone(); - assert.deepEqual(TestBufferLine.prototype.toArray.apply(line3), line.toArray()); }); describe('resize', function(): void { it('enlarge(false)', function(): void { @@ -322,15 +316,13 @@ describe('BufferLine', function(): void { }); it('should remove combining data on replaced cells after shrinking then enlarging', () => { const line = new TestBufferLine(10, createCellData(1, 'a', 1), false); - line.set(2, [ 0, '😁', 1, '😁'.charCodeAt(0) ]); - line.set(9, [ 0, '😁', 1, '😁'.charCodeAt(0) ]); + line.setCell(2, createCellData(0, '😁', 1)); + line.setCell(9, createCellData(0, '😁', 1)); assert.equal(line.translateToString(), 'aa😁aaaaaa😁'); - assert.equal(Object.keys(line.combined).length, 2); line.resize(5, createCellData(1, 'a', 1)); assert.equal(line.translateToString(), 'aa😁aa'); line.resize(10, createCellData(1, 'a', 1)); assert.equal(line.translateToString(), 'aa😁aaaaaaa'); - assert.equal(Object.keys(line.combined).length, 1); }); }); describe('getTrimLength', function(): void { @@ -789,13 +781,6 @@ describe('BufferLine', function(): void { // no eAttrs again cell.bg &= ~BgFlags.HAS_EXTENDED; line.setCell(4, cell); - - const nLine = line.clone(); - assert.equal(extendedAttributes(nLine, 0), extendedAttributes(line, 0)); - assert.equal(extendedAttributes(nLine, 1), extendedAttributes(line, 1)); - assert.equal(extendedAttributes(nLine, 2), extendedAttributes(line, 2)); - assert.equal(extendedAttributes(nLine, 3), extendedAttributes(line, 3)); - assert.equal(extendedAttributes(nLine, 4), extendedAttributes(line, 4)); }); it('copyFrom', () => { const initial = new TestBufferLine(5); diff --git a/src/common/buffer/BufferLine.ts b/src/common/buffer/BufferLine.ts index 30b4ed3bb6..71d98492b9 100644 --- a/src/common/buffer/BufferLine.ts +++ b/src/common/buffer/BufferLine.ts @@ -3,10 +3,10 @@ * @license MIT */ -import { CharData, IAttributeData, IBufferLine, ICellData, IExtendedAttrs } from './Types'; +import { CharData, IAttributeData, IBufferLine, ILogicalLine, ICellData, IExtendedAttrs } from './Types'; import { AttributeData } from './AttributeData'; import { CellData } from './CellData'; -import { Attributes, BgFlags, CHAR_DATA_ATTR_INDEX, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, Content, NULL_CELL_CHAR, NULL_CELL_CODE, NULL_CELL_WIDTH, WHITESPACE_CELL_CHAR } from './Constants'; +import { Attributes, BgFlags, Content, NULL_CELL_CHAR, NULL_CELL_CODE, NULL_CELL_WIDTH, WHITESPACE_CELL_CHAR } from './Constants'; import { stringFromCodePoint } from '../input/TextDecoder'; import { StringBuilder } from '../StringBuilder'; @@ -23,6 +23,18 @@ const enum Constants { CLEANUP_THRESHOLD = 2 } +/** Column count within current visible BufferLine(row). + * The left-most column is column 0. + */ +export type BufferColumn = number; + +/** Column count within current LogicalLine. + * If the display is 80 columns wide, then LineColumn of the left-most + * character of the first wrapped line would normally be 80. + * (It might be 79 if the character at column 79 is double-width.) + */ +export type LogicalColumn = number; + /** * Cell member indices. * @@ -40,7 +52,6 @@ const enum Cell { export const DEFAULT_ATTR_DATA = Object.freeze(new AttributeData()); // Work variables to avoid garbage collection -let $startIndex = 0; const $workCell = new CellData(); const $translateToStringBuilder = new StringBuilder(); @@ -56,6 +67,263 @@ export interface IBufferLineStringCache { touch?(): void; } +/* + * The data "model" of a line ignoring line wrapping. + */ +export class LogicalLine implements ILogicalLine { + /** + * @internal + */ + public _data: Uint32Array; + /** Sparse cache; only rea`d when `IS_COMBINED_MASK` is set in `_data`. */ + public _combined: {[index: LogicalColumn]: string} = {}; + /** + * @internal + */ + public _extendedAttrs: {[index: LogicalColumn]: IExtendedAttrs | undefined} = {}; + + public reflowNeeded: boolean = false; + public firstBufferLine: BufferLine | undefined; + public backgroundColor: number = 0; + /** + * Logical "trimmed" length of line. + * Must be no more than this._data.length / 3. + */ + public length: number = 0; + + constructor(cols: number = 0, data = new Uint32Array(cols * Constants.CELL_INDICIES)) { + this._data = data; + } + + /** + * @internal + */ + public resizeData(cols: number): void { + const uint32Cells = cols * Constants.CELL_INDICIES; + const oldByteLength = this._data.buffer.byteLength; + const neededByteLength = uint32Cells * 4; + if (oldByteLength >= neededByteLength) { + // optimization: avoid alloc and data copy if buffer has enough room + this._data = new Uint32Array(this._data.buffer, 0, uint32Cells); + } else { + // slow path: new alloc and full data copy + const buffer = new ArrayBuffer(Math.max(12 + neededByteLength, (3 * oldByteLength) >> 1)); + const data = new Uint32Array(buffer, 0, uint32Cells); + data.set(this._data); + this._data = data; + } + } + + public getWidth(index: LogicalColumn): number { + return index >= this.length ? NULL_CELL_WIDTH + : this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] >> Content.WIDTH_SHIFT; + } + + /** usually same as argument, but adjust if wide or at end. + * @internal + */ + public charStart(column: LogicalColumn): number { + return column > this.length ? this.length + : column > 0 && this.getWidth(column - 1) > 1 ? column - 1 + : column; + } + + /** + * Load data at `index` into `cell`. + */ + public loadCell(index: LogicalColumn, cell: ICellData): ICellData { + if (index >= this.length) { + cell.content = NULL_CELL_WIDTH << Content.WIDTH_SHIFT; + cell.fg = 0; + cell.bg = this.backgroundColor; + return cell; + } + const startIndex = index * Constants.CELL_INDICIES; + cell.content = this._data[startIndex + Cell.CONTENT]; + cell.fg = this._data[startIndex + Cell.FG]; + cell.bg = this._data[startIndex + Cell.BG]; + if (cell.content & Content.IS_COMBINED_MASK) { + cell.combinedData = this._combined[index]; + } else { + cell.combinedData = ''; + } + if (cell.bg & BgFlags.HAS_EXTENDED) { + cell.extended = this._extendedAttrs[index]!; + } else { + // Do not mutate cell.extended in place: it may still reference this line's map entry from a + // prior loadCell into a reused CellData (e.g. $workCell during insert/delete). + cell.extended = DEFAULT_ATTR_DATA.extended.clone(); + } + return cell; + } + + /** Returns the string content of the cell. */ + public getString(index: number): string { + const content = this._data[index * Constants.CELL_INDICIES + Cell.CONTENT]; + if (content & Content.IS_COMBINED_MASK) { + return this._combined[index]; + } + if (content & Content.CODEPOINT_MASK) { + return stringFromCodePoint(content & Content.CODEPOINT_MASK); + } + // return empty string for empty cells + return ''; + } + + /** Get state of protected flag. */ + public isProtected(index: number): number { + return this._data[index * Constants.CELL_INDICIES + Cell.BG] & BgFlags.PROTECTED; + } + + /** + * Set cell data from input handler. + * Since the input handler see the incoming chars as UTF32 codepoints, + * it gets an optimized access method. + * Warning - does not invalidatw the string cache - callers should do so. + * @internal + */ + public setCellFromCodepoint(index: LogicalColumn, codePoint: number, width: number, attrs: IAttributeData): void { + if (codePoint === 0 && width === 1 && index >= this.length - 1 && attrs.fg === 0 && attrs.bg === this.backgroundColor) { + if (index === this.length - 1) { + // FIXME should also truncate extendedAttrs and composedData + this.length = index; // this.length - 1; + this.trimLength(); + } + return; + } + if (index >= this.length) { + this.resizeData(index + 1); + for (let i = this.length; i < index; i++) { + this._data[i * Constants.CELL_INDICIES + Cell.CONTENT] = NULL_CELL_WIDTH << Content.WIDTH_SHIFT; + this._data[i * Constants.CELL_INDICIES + Cell.FG] = 0; + this._data[i * Constants.CELL_INDICIES + Cell.BG] = this.backgroundColor; + } + this.length = index + 1; + } + if (attrs.bg & BgFlags.HAS_EXTENDED) { + this._extendedAttrs[index] = attrs.extended; + } + this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] = codePoint | (width << Content.WIDTH_SHIFT); + this._data[index * Constants.CELL_INDICIES + Cell.FG] = attrs.fg; + this._data[index * Constants.CELL_INDICIES + Cell.BG] = attrs.bg; + } + + /** + * Cleanup underlying array buffer. + * A cleanup will be triggered if the array buffer exceeds the actual used + * memory by a factor of Constants.CLEANUP_THRESHOLD. + * Returns 0 or 1 indicating whether a cleanup happened. + */ + public cleanupMemory(threshold: number = 1.3): number { + const cols = this.length; + if (cols * Constants.CELL_INDICIES * 4 * threshold < this._data.buffer.byteLength) { + const data = new Uint32Array(Constants.CELL_INDICIES * cols); + data.set(this._data); + this._data = data; + // Remove any cut off combined data + const keys = Object.keys(this._combined); + for (let i = 0; i < keys.length; i++) { + const key = parseInt(keys[i], 10); + if (key >= cols) { + delete this._combined[key]; + } + } + // remove any cut off extended attributes + const extKeys = Object.keys(this._extendedAttrs); + for (let i = 0; i < extKeys.length; i++) { + const key = parseInt(extKeys[i], 10); + if (key >= cols) { + delete this._extendedAttrs[key]; + } + } + return 1; + } + return 0; + } + + /** + * @internal + */ + public trimLength(): void { + let index = this.length; + while (index > 0) { + index--; + const content = this._data[index * Constants.CELL_INDICIES + Cell.CONTENT]; + if (content & Content.HAS_CONTENT_MASK) { + index++; + break; + } + } + if (index < this.length) { + this.length = index; + for (let line = this.firstBufferLine; line; line = line.nextBufferLine) { + if (line.startColumn > index) { + line.startColumn = index; + } + } + // FIXME - possible optimization - trim _data _combinedData _extendedAttrs + } + } + + /** + * Warning - does not invalidate string cache. + */ + public copyCellsFrom(src: LogicalLine, srcCol: number, dstCol: number, length: number, applyInReverse: boolean): void { + let cell = applyInReverse ? length - 1 : 0; + const cellIncrement = applyInReverse ? -1 : 1; + for (let todo = length; --todo >= 0; cell += cellIncrement) { + src.loadCell(srcCol + cell, $workCell); + const dstIndex = dstCol + cell; + const content = $workCell.content & (Content.CODEPOINT_MASK|Content.IS_COMBINED_MASK); + this.setCellFromCodepoint(dstIndex, content, $workCell.getWidth(), $workCell); + if (content & Content.IS_COMBINED_MASK) { + this._combined[dstIndex] = $workCell.combinedData; + } + } + } + + /** + * Translates the buffer line to a string. + * + * @param startCol The column to start the string (0-based inclusive). + * @param endCol The column to end the string (0-based exclusive). + * @param dataLength ignore _data after dataLength + * @param outColumns if specified, this array will be filled with column numbers such that + * `returnedString[i]` is displayed at `outColumns[i]` column. `outColumns[returnedString.length]` + * is where the character following `returnedString` will be displayed. + * + * When a single cell is translated to multiple UTF-16 code units (e.g. surrogate pair) in the + * returned string, the corresponding entries in `outColumns` will have the same column number. + */ + public translateToString(startCol?: number, endCol?: number, dataLength: number = this.length, outColumns?: number[]): string { + startCol = startCol ?? 0; + endCol = endCol ?? this.length; + if (outColumns) { + outColumns.length = 0; + } + $translateToStringBuilder.reset(); + while (startCol < endCol) { + const content = startCol >= dataLength ? 0 + : this._data[startCol * Constants.CELL_INDICIES + Cell.CONTENT]; + const cp = content & Content.CODEPOINT_MASK; + const chars = (content & Content.IS_COMBINED_MASK) ? this._combined[startCol] : (cp) ? stringFromCodePoint(cp) : WHITESPACE_CELL_CHAR; + $translateToStringBuilder.append(chars); + if (outColumns) { + for (let i = 0; i < chars.length; ++i) { + outColumns.push(startCol); + } + } + startCol += (content >> Content.WIDTH_SHIFT) || 1; // always advance by at least 1 + } + if (outColumns) { + outColumns.push(startCol); + } + const result = $translateToStringBuilder.toString(); + $translateToStringBuilder.reset(); + return result; + } +} + /** * Typed array based bufferline implementation. * @@ -65,50 +333,73 @@ export interface IBufferLineStringCache { * Used during normal input in `InputHandler` for faster buffer access. * - `setCell` * This method takes a CellData object and stores the data in the buffer. - * Use `CellData.fromCharData` to create the CellData object (e.g. from JS string). + * Use `CellData.fromCharData` to create the CellData object (e.g.0 f from JS string). * * To retrieve data from the buffer use either one of the primitive methods * (if only one particular value is needed) or `loadCell`. For `loadCell` in a loop * memory allocs / GC pressure can be greatly reduced by reusing the CellData object. */ export class BufferLine implements IBufferLine { - protected _data: Uint32Array; - /** Sparse cache; only read when `IS_COMBINED_MASK` is set in `_data`. */ - protected _combined: {[index: number]: string} = {}; - /** Sparse cache; only read when `HAS_EXTENDED` is set in `_data`. */ - protected _extendedAttrs: {[index: number]: IExtendedAttrs | undefined} = {}; + private _logicalLine: LogicalLine; + public logical(): LogicalLine { return this._logicalLine; } + public nextBufferLine: BufferLine | undefined; protected _stringCacheEntryRef: WeakRef | undefined; + + /** + * Number of logical columns in previous rows. + * Also: logical column number (column number assuming infinitely-wide + * terminal) corresponding to the start of this row. + * If R is the row number (0 for the first BufferLine for a LogicalLine), + * If R is 0 for the previous LogicalBufferLine, R is 1 for first + * then startColumn will *usually* be N*W (where W is the width of + * the terminal in columns) but may be slightly + * different when a wide character at column W-1 must wrap "early". + */ + public startColumn: number = 0; + public length: number; - constructor( - protected readonly _stringCache: IBufferLineStringCache, + /** + * Last LogicalColumn of this BufferLine. + * @internal + */ + public get validEnd(): LogicalColumn { + return this.nextBufferLine ? this.nextBufferLine.startColumn : this._logicalLine.length; + } + + constructor(public readonly _stringCache: IBufferLineStringCache, cols: number, - fillCellData?: ICellData, - public isWrapped: boolean = false + logicalLine = new LogicalLine(cols) ) { - this._data = new Uint32Array(cols * Constants.CELL_INDICIES); - const cell = fillCellData ?? CellData.fromCharData([0, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]); - for (let i = 0; i < cols; ++i) { - this.setCell(i, cell); - } + this._logicalLine = logicalLine; this.length = cols; + logicalLine.firstBufferLine ??= this; + } + + public get isWrapped(): boolean { + return this._logicalLine.firstBufferLine !== this; } /** * Get cell data CharData. * @deprecated */ - public get(index: number): CharData { - const content = this._data[index * Constants.CELL_INDICIES + Cell.CONTENT]; + public get(index: BufferColumn): CharData { + const lline = this._logicalLine; + const lindex: LogicalColumn = index + this.startColumn; + if (lindex >= this.validEnd) { + return [0, '', NULL_CELL_WIDTH, 0]; + } + const content = lline._data[index * Constants.CELL_INDICIES + Cell.CONTENT]; const cp = content & Content.CODEPOINT_MASK; return [ - this._data[index * Constants.CELL_INDICIES + Cell.FG], + lline._data[lindex * Constants.CELL_INDICIES + Cell.FG], (content & Content.IS_COMBINED_MASK) - ? this._combined[index] + ? lline._combined[lindex] : (cp) ? stringFromCodePoint(cp) : '', content >> Content.WIDTH_SHIFT, (content & Content.IS_COMBINED_MASK) - ? this._combined[index].charCodeAt(this._combined[index].length - 1) + ? lline._combined[lindex].charCodeAt(lline._combined[lindex].length - 1) : cp ]; } @@ -118,14 +409,7 @@ export class BufferLine implements IBufferLine { * @deprecated */ public set(index: number, value: CharData): void { - this._invalidateStringCache(); - this._data[index * Constants.CELL_INDICIES + Cell.FG] = value[CHAR_DATA_ATTR_INDEX]; - if (value[CHAR_DATA_CHAR_INDEX].length > 1) { - this._combined[index] = value[1]; - this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] = index | Content.IS_COMBINED_MASK | (value[CHAR_DATA_WIDTH_INDEX] << Content.WIDTH_SHIFT); - } else { - this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] = value[CHAR_DATA_CHAR_INDEX].charCodeAt(0) | (value[CHAR_DATA_WIDTH_INDEX] << Content.WIDTH_SHIFT); - } + this.setCell(index, CellData.fromCharData(value)); } /** @@ -133,22 +417,29 @@ export class BufferLine implements IBufferLine { * use these when only one value is needed, otherwise use `loadCell` */ public getWidth(index: number): number { - return this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] >> Content.WIDTH_SHIFT; + const lindex: LogicalColumn = index + this.startColumn; + return lindex >= this.validEnd ? NULL_CELL_WIDTH + : this._logicalLine.getWidth(lindex); } /** Test whether content has width. */ public hasWidth(index: number): number { - return this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] & Content.WIDTH_MASK; + return this.getWidth(index); } /** Get FG cell component. */ public getFg(index: number): number { - return this._data[index * Constants.CELL_INDICIES + Cell.FG]; + const lline = this._logicalLine; + const lcolumn = index + this.startColumn; + return lcolumn >= this.validEnd ? 0 : lline._data[lcolumn * Constants.CELL_INDICIES + Cell.FG]; } /** Get BG cell component. */ public getBg(index: number): number { - return this._data[index * Constants.CELL_INDICIES + Cell.BG]; + index += this.startColumn; + const lline = this._logicalLine; + return index > lline.length ? lline.backgroundColor + : lline._data[index * Constants.CELL_INDICIES + Cell.BG]; } /** @@ -157,7 +448,12 @@ export class BufferLine implements IBufferLine { * from real empty cells. */ public hasContent(index: number): number { - return this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] & Content.HAS_CONTENT_MASK; + index += this.startColumn; + if (index >= this.validEnd) { + return 0; + } + const lline = this._logicalLine; + return lline._data[index * Constants.CELL_INDICIES + Cell.CONTENT] & Content.HAS_CONTENT_MASK; } /** @@ -165,24 +461,40 @@ export class BufferLine implements IBufferLine { * To be in line with `code` in CharData this either returns * a single UTF32 codepoint or the last codepoint of a combined string. */ - public getCodePoint(index: number): number { - const content = this._data[index * Constants.CELL_INDICIES + Cell.CONTENT]; + public getCodePoint(index: BufferColumn): number { + const lline = this._logicalLine; + const lcolumn: LogicalColumn = index + this.startColumn; + if (lcolumn >= this.validEnd) { + return 0; + } + const content = lline._data[lcolumn * Constants.CELL_INDICIES + Cell.CONTENT]; if (content & Content.IS_COMBINED_MASK) { - return this._combined[index].charCodeAt(this._combined[index].length - 1); + const combined = lline._combined[lcolumn]; + return combined.charCodeAt(combined.length - 1); } return content & Content.CODEPOINT_MASK; } /** Test whether the cell contains a combined string. */ public isCombined(index: number): number { - return this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] & Content.IS_COMBINED_MASK; + const lline = this._logicalLine; + const lcolumn: LogicalColumn = index + this.startColumn; + if (lcolumn >= this.validEnd) { + return 0; + } + return lline._data[lcolumn * Constants.CELL_INDICIES + Cell.CONTENT] & Content.IS_COMBINED_MASK; } /** Returns the string content of the cell. */ public getString(index: number): string { - const content = this._data[index * Constants.CELL_INDICIES + Cell.CONTENT]; + const lline = this._logicalLine; + const lcolumn: LogicalColumn = index + this.startColumn; + if (lcolumn >= this.validEnd) { + return ''; + } + const content = lline._data[lcolumn * Constants.CELL_INDICIES + Cell.CONTENT]; if (content & Content.IS_COMBINED_MASK) { - return this._combined[index]; + return lline._combined[lcolumn]; } if (content & Content.CODEPOINT_MASK) { return stringFromCodePoint(content & Content.CODEPOINT_MASK); @@ -193,7 +505,10 @@ export class BufferLine implements IBufferLine { /** Get state of protected flag. */ public isProtected(index: number): number { - return this._data[index * Constants.CELL_INDICIES + Cell.BG] & BgFlags.PROTECTED; + const lline = this._logicalLine; + const lcolumn = index + this.startColumn; + return index >= this.length || lcolumn >= lline.length ? 0 + : lline._data[lcolumn * Constants.CELL_INDICIES + Cell.BG] & BgFlags.PROTECTED; } /** @@ -201,23 +516,19 @@ export class BufferLine implements IBufferLine { * to GC as it significantly reduced the amount of new objects/references needed. */ public loadCell(index: number, cell: ICellData): ICellData { - $startIndex = index * Constants.CELL_INDICIES; - cell.content = this._data[$startIndex + Cell.CONTENT]; - cell.fg = this._data[$startIndex + Cell.FG]; - cell.bg = this._data[$startIndex + Cell.BG]; - if (cell.content & Content.IS_COMBINED_MASK) { - cell.combinedData = this._combined[index]; - } else { - cell.combinedData = ''; - } - if (cell.bg & BgFlags.HAS_EXTENDED) { - cell.extended = this._extendedAttrs[index]!; - } else { - // Do not mutate cell.extended in place: it may still reference this line's map entry from a - // prior loadCell into a reused CellData (e.g. $workCell during insert/delete). - cell.extended = DEFAULT_ATTR_DATA.extended.clone(); - } - return cell; + const lline = this._logicalLine; + const lcolumn = index + this.startColumn; + const lend = this.validEnd; + if (lcolumn >= lend) { + cell.content = NULL_CELL_CODE | (NULL_CELL_WIDTH << Content.WIDTH_SHIFT); + cell.fg = 0; + if (this.nextBufferLine) { + cell.bg = 0; // FIXME + } else { + cell.bg = lline.backgroundColor; + } + return cell; + } return lline.loadCell(lcolumn, cell); } /** @@ -225,15 +536,12 @@ export class BufferLine implements IBufferLine { */ public setCell(index: number, cell: ICellData): void { this._invalidateStringCache(); + // this.logicalLine.setCell(index + this.startColumn, cell); + const content = cell.content & (Content.CODEPOINT_MASK|Content.IS_COMBINED_MASK); + this.setCellFromCodepoint(index, content, cell.getWidth(), cell); if (cell.content & Content.IS_COMBINED_MASK) { - this._combined[index] = cell.combinedData; + this._logicalLine._combined[index + this.startColumn] = cell.combinedData; } - if (cell.bg & BgFlags.HAS_EXTENDED) { - this._extendedAttrs[index] = cell.extended; - } - this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] = cell.content; - this._data[index * Constants.CELL_INDICIES + Cell.FG] = cell.fg; - this._data[index * Constants.CELL_INDICIES + Cell.BG] = cell.bg; } /** @@ -243,12 +551,8 @@ export class BufferLine implements IBufferLine { */ public setCellFromCodepoint(index: number, codePoint: number, width: number, attrs: IAttributeData): void { this._invalidateStringCache(); - if (attrs.bg & BgFlags.HAS_EXTENDED) { - this._extendedAttrs[index] = attrs.extended; - } - this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] = codePoint | (width << Content.WIDTH_SHIFT); - this._data[index * Constants.CELL_INDICIES + Cell.FG] = attrs.fg; - this._data[index * Constants.CELL_INDICIES + Cell.BG] = attrs.bg; + this._logicalLine.setCellFromCodepoint(index + this.startColumn, + codePoint, width, attrs); } /** @@ -259,16 +563,24 @@ export class BufferLine implements IBufferLine { */ public addCodepointToCell(index: number, codePoint: number, width: number): void { this._invalidateStringCache(); - let content = this._data[index * Constants.CELL_INDICIES + Cell.CONTENT]; + const lline = this._logicalLine; + const lcolumn = index + this.startColumn; + if (lcolumn >= this.validEnd) { + // should not happen - we actually have no data in the cell yet + // simply set the data in the cell buffer with a width of 1 + this.setCellFromCodepoint(index, codePoint, 1, CellData.fromCharData([0, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE])); + return; + } + let content = lline._data[lcolumn * Constants.CELL_INDICIES + Cell.CONTENT]; if (content & Content.IS_COMBINED_MASK) { // we already have a combined string, simply add - this._combined[index] += stringFromCodePoint(codePoint); + lline._combined[lcolumn] += stringFromCodePoint(codePoint); } else { if (content & Content.CODEPOINT_MASK) { // normal case for combining chars: // - move current leading char + new one into combined string // - set combined flag - this._combined[index] = stringFromCodePoint(content & Content.CODEPOINT_MASK) + stringFromCodePoint(codePoint); + lline._combined[lcolumn] = stringFromCodePoint(content & Content.CODEPOINT_MASK) + stringFromCodePoint(codePoint); content &= ~Content.CODEPOINT_MASK; // set codepoint in buffer to 0 content |= Content.IS_COMBINED_MASK; } else { @@ -281,7 +593,7 @@ export class BufferLine implements IBufferLine { content &= ~Content.WIDTH_MASK; content |= width << Content.WIDTH_SHIFT; } - this._data[index * Constants.CELL_INDICIES + Cell.CONTENT] = content; + lline._data[lcolumn * Constants.CELL_INDICIES + Cell.CONTENT] = content; } public insertCells(pos: number, n: number, fillCellData: ICellData): void { @@ -373,69 +685,61 @@ export class BufferLine implements IBufferLine { } /** - * Resize BufferLine to `cols` filling excess cells with `fillCellData`. + * Resize to `cols` filling excess cells with `fillCellData`. * The underlying array buffer will not change if there is still enough space * to hold the new buffer line data. * Returns a boolean indicating, whether a `cleanupMemory` call would free - * excess memory (true after shrinking > Constants.CLEANUP_THRESHOLD). + * excess memory (true after shrinking > Constants.Constants.CLEANUP_THRESHOLD). + * Assumes single unwrapped line. + * @deprecated only used in tests */ public resize(cols: number, fillCellData: ICellData): boolean { this._invalidateStringCache(); + const logical = this._logicalLine; + if (logical.firstBufferLine !== this || this.nextBufferLine) { + throw new Error('invalid call to resize'); + } if (cols === this.length) { - return this._data.length * 4 * Constants.CLEANUP_THRESHOLD < this._data.buffer.byteLength; + return logical._data.length * 4 * Constants.CLEANUP_THRESHOLD < logical._data.buffer.byteLength; } const uint32Cells = cols * Constants.CELL_INDICIES; if (cols > this.length) { - if (this._data.buffer.byteLength >= uint32Cells * 4) { - // optimization: avoid alloc and data copy if buffer has enough room - this._data = new Uint32Array(this._data.buffer, 0, uint32Cells); - } else { - // slow path: new alloc and full data copy - const data = new Uint32Array(uint32Cells); - data.set(this._data); - this._data = data; - } + logical.resizeData(cols); for (let i = this.length; i < cols; ++i) { this.setCell(i, fillCellData); } } else { // optimization: just shrink the view on existing buffer - this._data = this._data.subarray(0, uint32Cells); + logical._data = logical._data.subarray(0, cols * Constants.CELL_INDICIES); // Remove any cut off combined data - const keys = Object.keys(this._combined); + const keys = Object.keys(logical._combined); for (let i = 0; i < keys.length; i++) { const key = parseInt(keys[i], 10); if (key >= cols) { - delete this._combined[key]; + delete logical._combined[key]; } } // remove any cut off extended attributes - const extKeys = Object.keys(this._extendedAttrs); + const extKeys = Object.keys(logical._extendedAttrs); for (let i = 0; i < extKeys.length; i++) { const key = parseInt(extKeys[i], 10); if (key >= cols) { - delete this._extendedAttrs[key]; + delete logical._extendedAttrs[key]; } } } this.length = cols; - return uint32Cells * 4 * Constants.CLEANUP_THRESHOLD < this._data.buffer.byteLength; + return uint32Cells * 4 * Constants.CLEANUP_THRESHOLD < logical._data.buffer.byteLength; } /** * Cleanup underlying array buffer. * A cleanup will be triggered if the array buffer exceeds the actual used - * memory by a factor of Constants.CLEANUP_THRESHOLD. + * memory by a factor of Constants.Constants.CLEANUP_THRESHOLD. * Returns 0 or 1 indicating whether a cleanup happened. */ public cleanupMemory(): number { - if (this._data.length * 4 * Constants.CLEANUP_THRESHOLD < this._data.buffer.byteLength) { - const data = new Uint32Array(this._data.length); - data.set(this._data); - this._data = data; - return 1; - } - return 0; + return this._logicalLine.cleanupMemory(Constants.CLEANUP_THRESHOLD); } /** fill a line with fillCharData */ @@ -450,75 +754,151 @@ export class BufferLine implements IBufferLine { } return; } - this._combined = {}; - this._extendedAttrs = {}; + const lline = this._logicalLine; + if (lline.firstBufferLine === this && !this.nextBufferLine) { + lline._combined = {}; + lline._extendedAttrs = {}; + } for (let i = 0; i < this.length; ++i) { this.setCell(i, fillCellData); } } - /** alter to a full copy of line */ + /** alter to a full copy of line + * @deprecated only used in a few tests + */ public copyFrom(line: BufferLine): void { - this._invalidateStringCache(); - if (this.length !== line.length) { - this._data = new Uint32Array(line._data); - } else { - // use high speed copy if lengths are equal - this._data.set(line._data); - } + this.copyCellsFrom(line, 0, 0, this.length, false); this.length = line.length; - this._copySparseMapsFrom(line); - this.isWrapped = line.isWrapped; } - /** create a new clone */ - public clone(): IBufferLine { - const newLine = new BufferLine(this._stringCache, 0, undefined, false); - newLine._data = new Uint32Array(this._data); - newLine.length = this.length; - newLine._copySparseMapsFrom(this); - newLine.isWrapped = this.isWrapped; - return newLine; - } - - public getTrimmedLength(): number { - for (let i = this.length - 1; i >= 0; --i) { - if ((this._data[i * Constants.CELL_INDICIES + Cell.CONTENT] & Content.HAS_CONTENT_MASK)) { - return i + (this._data[i * Constants.CELL_INDICIES + Cell.CONTENT] >> Content.WIDTH_SHIFT); + public getTrimmedLength(noBg: boolean = false): number { + const logicalLine = this._logicalLine; + const startColumn = this.startColumn; + const data = logicalLine._data; + for (let i = this.validEnd; --i >= startColumn; ) { + if ((data[i * Constants.CELL_INDICIES + Cell.CONTENT] & Content.HAS_CONTENT_MASK) + || (noBg && (data[i * Constants.CELL_INDICIES + Cell.BG] & Attributes.CM_MASK))) { + i += data[i * Constants.CELL_INDICIES + Cell.CONTENT] >> Content.WIDTH_SHIFT; + return i - startColumn; } } - return 0; + return startColumn; } public getNoBgTrimmedLength(): number { - for (let i = this.length - 1; i >= 0; --i) { - if ((this._data[i * Constants.CELL_INDICIES + Cell.CONTENT] & Content.HAS_CONTENT_MASK) || (this._data[i * Constants.CELL_INDICIES + Cell.BG] & Attributes.CM_MASK)) { - return i + (this._data[i * Constants.CELL_INDICIES + Cell.CONTENT] >> Content.WIDTH_SHIFT); - } + if (this._logicalLine.backgroundColor) { + return this.length; } - return 0; + return this.getTrimmedLength(true); } public copyCellsFrom(src: BufferLine, srcCol: number, destCol: number, length: number, applyInReverse: boolean): void { this._invalidateStringCache(); - const srcData = src._data; - if (applyInReverse) { - for (let cell = length - 1; cell >= 0; cell--) { - for (let i = 0; i < Constants.CELL_INDICIES; i++) { - this._data[(destCol + cell) * Constants.CELL_INDICIES + i] = srcData[(srcCol + cell) * Constants.CELL_INDICIES + i]; + this._logicalLine.copyCellsFrom(src._logicalLine, srcCol + src.startColumn, + destCol + this.startColumn, length, applyInReverse); + } + + public getPreviousLine(): BufferLine | undefined { + for (let row = this._logicalLine.firstBufferLine; ;) { + if (!row) { + return undefined; + } + const next = row.nextBufferLine; + if (next === this) { + return row; + } + row = next; + } + } + + public eraseRight(index: BufferColumn): void { + this._invalidateStringCache(); + const lineStart = this.startColumn; + const lineEnd = lineStart + index; + const lline = this._logicalLine; + if (this.nextBufferLine) { + const oldEnd = this.nextBufferLine.startColumn; + const count = oldEnd - lineEnd; + if (count > 0) { + let next: BufferLine | undefined = this; + for (;;) { + next = next.nextBufferLine; + if (!next) break; + next.startColumn -= count; } - this._copyCellMapsFrom(src, srcCol + cell, destCol + cell); + lline.copyCellsFrom(lline, oldEnd, lineEnd, lline.length - oldEnd, false); + lline.length -= count; } } else { - for (let cell = 0; cell < length; cell++) { - for (let i = 0; i < Constants.CELL_INDICIES; i++) { - this._data[(destCol + cell) * Constants.CELL_INDICIES + i] = srcData[(srcCol + cell) * Constants.CELL_INDICIES + i]; - } - this._copyCellMapsFrom(src, srcCol + cell, destCol + cell); + if (lineEnd < lline.length) { + lline.length = lineEnd; } } } + public setWrapped(previousLine: BufferLine): BufferLine { + const column = previousLine.startColumn + previousLine.length; + const logicalLine = previousLine._logicalLine; + const oldLogical = this._logicalLine; + logicalLine.resizeData(column + oldLogical.length); + const newData = logicalLine._data; + for (let i = logicalLine.length; i < column + oldLogical.length; i++) { + newData[i * Constants.CELL_INDICIES + Cell.CONTENT] = 0; + newData[i * Constants.CELL_INDICIES + Cell.FG] = 0; + newData[i * Constants.CELL_INDICIES + Cell.BG] = logicalLine.backgroundColor; + } + logicalLine.copyCellsFrom(oldLogical, 0, column, oldLogical.length, false); + /* + const oldData = oldLogical._data; + for (let i = 0; i < oldLogical.length; i++) { + const oldIndex = i * Constants.CELL_INDICIES; + const newIndex = (column + i) * Constants.CELL_INDICIES + const content = oldData[oldIndex + Cell.CONTENT]; + const fg = oldData[oldIndex + Cell.FG]; + const bg = oldData[oldIndex + Cell.BG]; + newData[newIndex + Cell.CONTENT] = content; + newData[newIndex + Cell.FG] = fg; + newData[newIndex + Cell.BG] = bg; + if (content & Content.IS_COMBINED_MASK) { + lprevious._combined[column + i] = oldLogical._combined[i]; + } + if (bg & BgFlags.HAS_EXTENDED) { + lprevious._extendedAttrs[column + i] = oldLogical._extendedAttrs[i]; + } + } + */ + logicalLine.length = column + oldLogical.length; + previousLine.nextBufferLine = this; + for (let line: BufferLine | undefined = this; line; line = line.nextBufferLine) { + line.startColumn += column; + line._logicalLine = logicalLine; + } + return this; + + } + + public asUnwrapped(prevRow: BufferLine): LogicalLine { + const oldStartColumn = this.startColumn; + prevRow.nextBufferLine = undefined; + const oldLine = prevRow._logicalLine; + const cell = new CellData(); + this.loadCell(oldStartColumn, cell); + const newLength = oldLine.length - oldStartColumn; + const newLogical = new LogicalLine(newLength); + newLogical.copyCellsFrom(oldLine, oldStartColumn, 0, newLength, false); + newLogical.firstBufferLine = this; + for (let nextRow: BufferLine | undefined = this; nextRow; nextRow = nextRow.nextBufferLine) { + nextRow.startColumn -= oldStartColumn; + nextRow._logicalLine = newLogical; + } + oldLine.length = oldStartColumn; + oldLine.trimLength(); + // FIXME truncate/resize + newLogical.backgroundColor = oldLine.backgroundColor; + return newLogical; + } + /** * Translates the buffer line to a string. Caching only applies to canonical full-line translation * requests (regardless of `trimRight` value). @@ -552,27 +932,19 @@ export class BufferLine implements IBufferLine { if (trimRight) { endCol = Math.min(endCol, this.getTrimmedLength()); } - if (outColumns) { - outColumns.length = 0; - } - $translateToStringBuilder.reset(); - while (startCol < endCol) { - const content = this._data[startCol * Constants.CELL_INDICIES + Cell.CONTENT]; - const cp = content & Content.CODEPOINT_MASK; - const chars = (content & Content.IS_COMBINED_MASK) ? this._combined[startCol] : (cp) ? stringFromCodePoint(cp) : WHITESPACE_CELL_CHAR; - $translateToStringBuilder.append(chars); - if (outColumns) { - for (let i = 0; i < chars.length; ++i) { - outColumns.push(startCol); - } + const lline = this._logicalLine; + const lineStart = this.startColumn; + const validEnd = this.validEnd; + startCol += lineStart; + endCol += lineStart; + const paddingNeeded = trimRight || endCol <= validEnd ? 0 + : endCol - validEnd; + const result = lline.translateToString(startCol, endCol, endCol - paddingNeeded, outColumns); + if (outColumns && lineStart) { + for (let i = outColumns.length; --i >= 0; ) { + outColumns[i] -= lineStart; } - startCol += (content >> Content.WIDTH_SHIFT) || 1; // always advance by at least 1 } - if (outColumns) { - outColumns.push(startCol); - } - const result = $translateToStringBuilder.toString(); - $translateToStringBuilder.reset(); if (isCanonicalRequest) { const cacheEntry = this._getStringCacheEntry(true)!; cacheEntry.value = result; @@ -603,24 +975,4 @@ export class BufferLine implements IBufferLine { cacheEntry.isTrimmed = false; } } - - /** Copy sparse map entries for a single cell when `_data` flags require them. */ - private _copyCellMapsFrom(src: BufferLine, srcCol: number, destCol: number): void { - const srcStart = srcCol * Constants.CELL_INDICIES; - if (src._data[srcStart + Cell.CONTENT] & Content.IS_COMBINED_MASK) { - this._combined[destCol] = src._combined[srcCol]; - } - if (src._data[srcStart + Cell.BG] & BgFlags.HAS_EXTENDED) { - this._extendedAttrs[destCol] = src._extendedAttrs[srcCol]; - } - } - - /** Rebuild sparse maps from another line, keyed only by `_data` flags. */ - private _copySparseMapsFrom(line: BufferLine): void { - this._combined = {}; - this._extendedAttrs = {}; - for (let i = 0; i < line.length; i++) { - this._copyCellMapsFrom(line, i, i); - } - } } diff --git a/src/common/buffer/BufferReflow.test.ts b/src/common/buffer/BufferReflow.test.ts index 8bcf5bf9c1..49366d0518 100644 --- a/src/common/buffer/BufferReflow.test.ts +++ b/src/common/buffer/BufferReflow.test.ts @@ -8,9 +8,74 @@ import { BufferLine } from './BufferLine'; import { BufferLineStringCache } from './BufferLineStringCache'; import { CellData } from './CellData'; import { NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE } from './Constants'; -import { reflowLargerGetLinesToRemove, reflowSmallerGetNewLineLengths } from './BufferReflow'; +import { reflowLargerGetLinesToRemove } from './BufferReflow'; import { IBufferLine } from './Types'; +/** + * Gets the new line lengths for a given wrapped line. The purpose of this function it to pre- + * compute the wrapping points since wide characters may need to be wrapped onto the following line. + * This function will return an array of numbers of where each line wraps to, the resulting array + * will only contain the values `newCols` (when the line does not end with a wide character) and + * `newCols - 1` (when the line does end with a wide character), except for the last value which + * will contain the remaining items to fill the line. + * + * Calling this with a `newCols` value of `1` will lock up. + * + * This function is now only used for testing. + * + * @param wrappedLines The wrapped lines to evaluate. + * @param oldCols The columns before resize. + * @param newCols The columns after resize. + */ +function reflowSmallerGetNewLineLengths(wrappedLines: BufferLine[], oldCols: number, newCols: number): number[] { + const newLineLengths: number[] = []; + const cellsNeeded = wrappedLines.map((l, i) => getWrappedLineTrimmedLength(wrappedLines, i, oldCols)).reduce((p, c) => p + c); + + // Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and + // linesNeeded + let srcCol = 0; + let srcLine = 0; + let cellsAvailable = 0; + while (cellsAvailable < cellsNeeded) { + if (cellsNeeded - cellsAvailable < newCols) { + // Add the final line and exit the loop + newLineLengths.push(cellsNeeded - cellsAvailable); + break; + } + srcCol += newCols; + const oldTrimmedLength = getWrappedLineTrimmedLength(wrappedLines, srcLine, oldCols); + if (srcCol > oldTrimmedLength) { + srcCol -= oldTrimmedLength; + srcLine++; + } + const endsWithWide = wrappedLines[srcLine].getWidth(srcCol - 1) === 2; + if (endsWithWide) { + srcCol--; + } + const lineLength = endsWithWide ? newCols - 1 : newCols; + newLineLengths.push(lineLength); + cellsAvailable += lineLength; + } + + return newLineLengths; +} + +function getWrappedLineTrimmedLength(lines: BufferLine[], i: number, cols: number): number { + // If this is the last row in the wrapped line, get the actual trimmed length + if (i === lines.length - 1) { + return lines[i].getTrimmedLength(); + } + // Detect whether the following line starts with a wide character and the end of the current line + // is null, if so then we can be pretty sure the null character should be excluded from the line + // length] + const endsInNull = !(lines[i].hasContent(cols - 1)) && lines[i].getWidth(cols - 1) === 1; + const followingLineStartsWithWide = lines[i + 1].getWidth(0) === 2; + if (endsInNull && followingLineStartsWithWide) { + return cols - 1; + } + return cols; +} + const TEST_STRING_CACHE = new BufferLineStringCache(); describe('BufferReflow', () => { @@ -69,7 +134,7 @@ describe('BufferReflow', () => { line1.set(3, [0, '语', 2, '语'.charCodeAt(0)]); line1.set(4, [0, '', 0, 0]); line1.set(5, [0, 'b', 1, 'b'.charCodeAt(0)]); - const line2 = new BufferLine(TEST_STRING_CACHE, 6, undefined, true); + const line2 = new BufferLine(TEST_STRING_CACHE, 6, undefined); line2.set(0, [0, 'a', 1, 'a'.charCodeAt(0)]); line2.set(1, [0, '汉', 2, '汉'.charCodeAt(0)]); line2.set(2, [0, '', 0, 0]); @@ -96,15 +161,18 @@ describe('BufferReflow', () => { assert.deepEqual(reflowSmallerGetNewLineLengths([line], 4, 2), [2, 2], 'line: 汉, 语'); }); }); + describe('reflowLargerGetLinesToRemove', () => { const nullCell = CellData.fromCharData([0, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]); function createWrappedLines(chars: string): CircularList { const lines = new CircularList(chars.length); + let previous = undefined; for (let i = 0; i < chars.length; i++) { const line = new BufferLine(TEST_STRING_CACHE, 1); line.set(0, [0, chars[i], 1, chars.charCodeAt(i)]); - line.isWrapped = i > 0; + if (previous) { line.setWrapped(previous); } + previous = line; lines.push(line); } return lines; diff --git a/src/common/buffer/BufferReflow.ts b/src/common/buffer/BufferReflow.ts index 2c0fb74ba1..765b625ea5 100644 --- a/src/common/buffer/BufferReflow.ts +++ b/src/common/buffer/BufferReflow.ts @@ -12,6 +12,39 @@ export interface INewLayoutResult { countRemoved: number; } +export function reflowLine(wrappedLines: BufferLine[], newCols: number): BufferLine[] { + const newLines: BufferLine[] = []; + let startCol = 0; + let curRow = 1; + let curLine = wrappedLines[0]; + const logical = curLine.logical(); + for (;;) { + const endCol = logical.charStart(startCol + newCols); + if (endCol >= logical.length) { + curLine.nextBufferLine = undefined; + curLine.startColumn = startCol; + break; + } + let newLine; + if (curRow < wrappedLines.length) { + newLine = wrappedLines[curRow]; + newLine.length = newCols; + } else { + newLine = new BufferLine(curLine._stringCache, newCols, logical); + newLines.push(newLine); + } + curRow++; + newLine.startColumn = endCol; + startCol = endCol; + curLine.nextBufferLine = newLine; + curLine = newLine; + } + if (curRow < wrappedLines.length) { + wrappedLines.length = curRow; + } + return newLines; +} + /** * Evaluates and returns indexes to be removed after a reflow larger occurs. Lines will be removed * when a wrapped line unwraps. @@ -50,60 +83,17 @@ export function reflowLargerGetLinesToRemove(lines: CircularList, o continue; } } - - // Copy buffer data to new locations - let destLineIndex = 0; - let destCol = getWrappedLineTrimmedLength(wrappedLines, destLineIndex, oldCols); - let srcLineIndex = 1; - let srcCol = 0; - while (srcLineIndex < wrappedLines.length) { - const srcTrimmedTineLength = getWrappedLineTrimmedLength(wrappedLines, srcLineIndex, oldCols); - const srcRemainingCells = srcTrimmedTineLength - srcCol; - const destRemainingCells = newCols - destCol; - const cellsToCopy = Math.min(srcRemainingCells, destRemainingCells); - - wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[srcLineIndex], srcCol, destCol, cellsToCopy, false); - - destCol += cellsToCopy; - if (destCol === newCols) { - destLineIndex++; - destCol = 0; - } - srcCol += cellsToCopy; - if (srcCol === srcTrimmedTineLength) { - srcLineIndex++; - srcCol = 0; - } - - // Make sure the last cell isn't wide, if it is copy it to the current dest - if (destCol === 0 && destLineIndex !== 0) { - if (wrappedLines[destLineIndex - 1].getWidth(newCols - 1) === 2) { - wrappedLines[destLineIndex].copyCellsFrom(wrappedLines[destLineIndex - 1], newCols - 1, destCol++, 1, false); - // Null out the end of the last row - wrappedLines[destLineIndex - 1].setCell(newCols - 1, nullCell); - } - } - } - - // Clear out remaining cells or fragments could remain; - wrappedLines[destLineIndex].replaceCells(destCol, newCols, nullCell); + const oldWrapped = wrappedLines.length; + reflowLine(wrappedLines, newCols); // Work backwards and remove any rows at the end that only contain null cells - let countToRemove = 0; - for (let i = wrappedLines.length - 1; i > 0; i--) { - if (i > destLineIndex || wrappedLines[i].getTrimmedLength() === 0) { - countToRemove++; - } else { - break; - } - } - + const countToRemove = oldWrapped - wrappedLines.length; if (countToRemove > 0) { - toRemove.push(y + wrappedLines.length - countToRemove); // index + toRemove.push(y + oldWrapped - countToRemove); // index toRemove.push(countToRemove); } - y += wrappedLines.length - 1; + y += oldWrapped - 1; } return toRemove; } @@ -162,56 +152,6 @@ export function reflowLargerApplyNewLayout(lines: CircularList, new lines.length = newLayout.length; } -/** - * Gets the new line lengths for a given wrapped line. The purpose of this function it to pre- - * compute the wrapping points since wide characters may need to be wrapped onto the following line. - * This function will return an array of numbers of where each line wraps to, the resulting array - * will only contain the values `newCols` (when the line does not end with a wide character) and - * `newCols - 1` (when the line does end with a wide character), except for the last value which - * will contain the remaining items to fill the line. - * - * Calling this with a `newCols` value of `1` will lock up. - * - * @param wrappedLines The wrapped lines to evaluate. - * @param oldCols The columns before resize. - * @param newCols The columns after resize. - */ -export function reflowSmallerGetNewLineLengths(wrappedLines: BufferLine[], oldCols: number, newCols: number): number[] { - const newLineLengths: number[] = []; - let cellsNeeded = 0; - for (let i = 0; i < wrappedLines.length; i++) { - cellsNeeded += getWrappedLineTrimmedLength(wrappedLines, i, oldCols); - } - - // Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and - // linesNeeded - let srcCol = 0; - let srcLine = 0; - let cellsAvailable = 0; - while (cellsAvailable < cellsNeeded) { - if (cellsNeeded - cellsAvailable < newCols) { - // Add the final line and exit the loop - newLineLengths.push(cellsNeeded - cellsAvailable); - break; - } - srcCol += newCols; - const oldTrimmedLength = getWrappedLineTrimmedLength(wrappedLines, srcLine, oldCols); - if (srcCol > oldTrimmedLength) { - srcCol -= oldTrimmedLength; - srcLine++; - } - const endsWithWide = wrappedLines[srcLine].getWidth(srcCol - 1) === 2; - if (endsWithWide) { - srcCol--; - } - const lineLength = endsWithWide ? newCols - 1 : newCols; - newLineLengths.push(lineLength); - cellsAvailable += lineLength; - } - - return newLineLengths; -} - export function getWrappedLineTrimmedLength(lines: BufferLine[], i: number, cols: number): number { // If this is the last row in the wrapped line, get the actual trimmed length if (i === lines.length - 1) { diff --git a/src/common/buffer/CellData.ts b/src/common/buffer/CellData.ts index 3c738f77ff..ab52ebcc13 100644 --- a/src/common/buffer/CellData.ts +++ b/src/common/buffer/CellData.ts @@ -88,7 +88,9 @@ export class CellData extends AttributeData implements ICellData { this.content = Content.IS_COMBINED_MASK | (value[CHAR_DATA_WIDTH_INDEX] << Content.WIDTH_SHIFT); } } - /** Get data as CharData. */ + /** Get data as CharData. + * @deprecated + */ public getAsCharData(): CharData { return [this.fg, this.getChars(), this.getWidth(), this.getCode()]; } diff --git a/src/common/buffer/Constants.ts b/src/common/buffer/Constants.ts index c02dba50d9..b5a6a4d4fa 100644 --- a/src/common/buffer/Constants.ts +++ b/src/common/buffer/Constants.ts @@ -4,6 +4,7 @@ */ export const DEFAULT_COLOR = 0; +// Only used for testing - move to TestUtils? export const DEFAULT_ATTR = (0 << 18) | (DEFAULT_COLOR << 9) | (256 << 0); export const DEFAULT_EXT = 0; diff --git a/src/common/buffer/Types.ts b/src/common/buffer/Types.ts index 994ca0f436..8e8c794033 100644 --- a/src/common/buffer/Types.ts +++ b/src/common/buffer/Types.ts @@ -113,12 +113,15 @@ export interface ICellData extends IAttributeData { getAsCharData(): CharData; } +export interface ILogicalLine { +} + /** * Interface for a line in the terminal buffer. */ export interface IBufferLine { length: number; - isWrapped: boolean; + get isWrapped(): boolean; get(index: number): CharData; set(index: number, value: CharData): void; loadCell(index: number, cell: ICellData): ICellData; @@ -132,7 +135,6 @@ export interface IBufferLine { cleanupMemory(): number; fill(fillCellData: ICellData, respectProtect?: boolean): void; copyFrom(line: IBufferLine): void; - clone(): IBufferLine; getTrimmedLength(): number; getNoBgTrimmedLength(): number; translateToString(trimRight?: boolean, startCol?: number, endCol?: number, outColumns?: number[]): string; @@ -157,10 +159,30 @@ export interface IMarker extends IDisposable { export interface IBuffer { readonly lines: ICircularList; + /** Number of rows above top visible row. + * Similar to scrollTop (i.e. affected by scrollbar), but in rows. + */ ydisp: number; + /** Number of rows in the scrollback buffer, above the home row. */ ybase: number; + + /** Row number relative to the "home" row, zero-origin. + * This is the row number changed/reported by cursor escape sequences, + * except that y is 0-origin: y=0 when we're at the home row. + * Currently assumed to be >= 0, but future may allow negative - i.e. + * in scroll-back area, as long as ybase+y >= 0. + */ y: number; + + /** Column number, zero-origin. + * Valid range is 0 through C (inclusive), if C is terminal width in columns. + * The first (left-most) column is 0. + * The right-most column is either C-1 (before the right-most column, and + * ready to write in it), or C (after the right-most column, having written + * to it, and ready to wrap). DSR 6 returns C (1-origin) in either case, + */ x: number; + tabs: any; scrollBottom: number; scrollTop: number; @@ -179,12 +201,13 @@ export interface IBuffer { getWrappedRangeForLine(y: number): { first: number, last: number }; nextStop(x?: number): number; prevStop(x?: number): number; - getBlankLine(attr: IAttributeData, isWrapped?: boolean): IBufferLine; + getBlankLine(attr: IAttributeData, logicalLine?: ILogicalLine): IBufferLine; getNullCell(attr?: IAttributeData): ICellData; getWhitespaceCell(attr?: IAttributeData): ICellData; addMarker(y: number): IMarker; clearMarkers(y: number): void; clearAllMarkers(): void; + setWrapped(row: number, value: boolean): void; } export interface IBufferSet extends IDisposable { diff --git a/src/common/services/BufferService.ts b/src/common/services/BufferService.ts index 21b0a163a4..c2d176e103 100644 --- a/src/common/services/BufferService.ts +++ b/src/common/services/BufferService.ts @@ -8,6 +8,7 @@ import { IAttributeData, IBuffer, IBufferLine, IBufferSet } from '../buffer/Type import { BufferSet } from '../buffer/BufferSet'; import { IBufferService, ILogService, IOptionsService, type IBufferResizeEvent } from './Services'; import { Emitter } from '../Event'; +import { BufferLine, LogicalLine } from '../buffer/BufferLine'; export const enum BufferServiceConstants { MINIMUM_COLS = 2, // Less than 2 can mess with wide chars @@ -67,17 +68,21 @@ export class BufferService extends Disposable implements IBufferService { */ public scroll(eraseAttr: IAttributeData, isWrapped: boolean = false): void { const buffer = this.buffer; - - let newLine: IBufferLine | undefined; - newLine = this._cachedBlankLine; - if (!newLine || newLine.length !== this.cols || newLine.getFg(0) !== eraseAttr.fg || newLine.getBg(0) !== eraseAttr.bg) { - newLine = buffer.getBlankLine(eraseAttr, isWrapped); - this._cachedBlankLine = newLine; - } - newLine.isWrapped = isWrapped; - const topRow = buffer.ybase + buffer.scrollTop; const bottomRow = buffer.ybase + buffer.scrollBottom; + const oldLine = buffer.lines.get(bottomRow) as BufferLine; + let lline: LogicalLine; + if (isWrapped) { + lline = oldLine.logical(); + } else { + lline = new LogicalLine(0); + } + const newLine = buffer.getBlankLine(eraseAttr, lline) as BufferLine; + if (isWrapped && oldLine) { + oldLine.nextBufferLine = newLine; + newLine.startColumn = lline.length; + } + lline.backgroundColor = eraseAttr.bg; if (buffer.scrollTop === 0) { // Determine whether the buffer is going to be trimmed after insertion. @@ -85,13 +90,9 @@ export class BufferService extends Disposable implements IBufferService { // Insert the line using the fastest method if (bottomRow === buffer.lines.length - 1) { - if (willBufferBeTrimmed) { - buffer.lines.recycle().copyFrom(newLine); - } else { - buffer.lines.push(newLine.clone()); - } + buffer.lines.push(newLine); } else { - buffer.lines.splice(bottomRow + 1, 0, newLine.clone()); + buffer.lines.splice(bottomRow + 1, 0, newLine); } // Only adjust ybase and ydisp when the buffer is not trimmed @@ -113,7 +114,7 @@ export class BufferService extends Disposable implements IBufferService { // scrollback, instead we can just shift them in-place. const scrollRegionHeight = bottomRow - topRow + 1 /* as it's zero-based */; buffer.lines.shiftElements(topRow + 1, scrollRegionHeight - 1, -1); - buffer.lines.set(bottomRow, newLine.clone()); + buffer.lines.set(bottomRow, newLine); } // Move the viewport to the bottom of the buffer unless the user is