From 67df9a5cdc84db7446cf73662f73f770a3ed1cd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 8 Nov 2018 04:03:48 +0100 Subject: [PATCH] cleanup --- src/Buffer.test.ts | 3 +-- src/Buffer.ts | 54 ---------------------------------------------- src/BufferLine.ts | 11 ++++++---- 3 files changed, 8 insertions(+), 60 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index bb1a08d8..c1585a7c 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -508,11 +508,10 @@ describe('Buffer', () => { // the dangling last cell is wrongly added in the string // --> fixable after resolving #1685 terminal.writeSync(input); - // TODO: reenable after fix const s = terminal.buffer.iterator(true).next().content; assert.equal(input, s); for (let i = 10; i < input.length; ++i) { - const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i); // TODO: remove +1 after fix + const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i); const j = (i - 0) << 1; assert.deepEqual([(j / terminal.cols) | 0, j % terminal.cols], bufferIndex); } diff --git a/src/Buffer.ts b/src/Buffer.ts index 39dc5d57..63b0f8d2 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -271,65 +271,11 @@ export class Buffer implements IBuffer { * @param endCol The column to end at. */ public translateBufferLineToString(lineIndex: number, trimRight: boolean, startCol: number = 0, endCol: number = null): string { - // Get full line - let lineString = ''; const line = this.lines.get(lineIndex); if (!line) { return ''; } return line.translateToString(trimRight, startCol, endCol); - - // Initialize column and index values. Column values represent the actual - // cell column, indexes represent the index in the string. Indexes are - // needed here because some chars are 0 characters long (eg. after wide - // chars) and some chars are longer than 1 characters long (eg. emojis). - let startIndex = startCol; - // Only set endCol to the line length when it is null. 0 is a valid column. - if (endCol === null) { - endCol = line.length; - } - let endIndex = endCol; - - for (let i = 0; i < line.length; i++) { - const char = line.get(i); - lineString += char[CHAR_DATA_CHAR_INDEX]; - // Adjust start and end cols for wide characters if they affect their - // column indexes - if (char[CHAR_DATA_WIDTH_INDEX] === 0) { - if (startCol >= i) { - startIndex--; - } - if (endCol > i) { - endIndex--; - } - } else { - // Adjust the columns to take glyphs that are represented by multiple - // code points into account. - if (char[CHAR_DATA_CHAR_INDEX].length > 1) { - if (startCol > i) { - startIndex += char[CHAR_DATA_CHAR_INDEX].length - 1; - } - if (endCol > i) { - endIndex += char[CHAR_DATA_CHAR_INDEX].length - 1; - } - } - } - } - - // Calculate the final end col by trimming whitespace on the right of the - // line if needed. - if (trimRight) { - const rightWhitespaceIndex = lineString.search(/\s+$/); - if (rightWhitespaceIndex !== -1) { - endIndex = Math.min(endIndex, rightWhitespaceIndex); - } - // Return the empty string if only trimmed whitespace is selected - if (endIndex <= startIndex) { - return ''; - } - } - - return lineString.substring(startIndex, endIndex); } public getWrappedRangeForLine(y: number): { first: number, last: number } { diff --git a/src/BufferLine.ts b/src/BufferLine.ts index 3bf7fec4..12a0bf5d 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -143,6 +143,9 @@ const enum Cell { WIDTH = 2 } +/** single vs. combined char distinction */ +const COMBINED = 0x80000000; + /** * Typed array based bufferline implementation. * Note: Unlike the JS variant the access to the data @@ -177,11 +180,11 @@ export class BufferLineTypedArray implements IBufferLine { const stringData = this._data[index * CELL_SIZE + Cell.STRING]; return [ this._data[index * CELL_SIZE + Cell.FLAGS], - (stringData & 0x80000000) + (stringData & COMBINED) ? this._combined[index] : (stringData) ? String.fromCharCode(stringData) : '', this._data[index * CELL_SIZE + Cell.WIDTH], - (stringData & 0x80000000) + (stringData & COMBINED) ? this._combined[index].charCodeAt(this._combined[index].length - 1) : stringData ]; @@ -191,7 +194,7 @@ export class BufferLineTypedArray implements IBufferLine { this._data[index * CELL_SIZE + Cell.FLAGS] = value[0]; if (value[1].length > 1) { this._combined[index] = value[1]; - this._data[index * CELL_SIZE + Cell.STRING] = index | 0x80000000; + this._data[index * CELL_SIZE + Cell.STRING] = index | COMBINED; } else { this._data[index * CELL_SIZE + Cell.STRING] = value[1].charCodeAt(0); } @@ -320,7 +323,7 @@ export class BufferLineTypedArray implements IBufferLine { let result = ''; while (startCol < length) { const stringData = this._data[startCol * CELL_SIZE + Cell.STRING]; - result += (stringData & 0x80000000) ? this._combined[startCol] : (stringData) ? String.fromCharCode(stringData) : ' '; + result += (stringData & COMBINED) ? this._combined[startCol] : (stringData) ? String.fromCharCode(stringData) : ' '; startCol += this._data[startCol * CELL_SIZE + Cell.WIDTH] || 1; } return result;