From ee6fa6ca002e84ee6ce6587b4a47664098e383e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 31 Oct 2018 12:42:56 +0100 Subject: [PATCH 01/42] change NULL_CHAR and trim --- src/Buffer.ts | 11 +++++++--- src/BufferLine.ts | 44 ++++++++++++++++++++++++++++++++++++- src/Terminal.integration.ts | 2 +- src/Types.ts | 2 ++ 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index e54f752b..7c5d2652 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -16,9 +16,13 @@ export const CHAR_DATA_WIDTH_INDEX = 2; export const CHAR_DATA_CODE_INDEX = 3; export const MAX_BUFFER_SIZE = 4294967295; // 2^32 - 1 -export const NULL_CELL_CHAR = ' '; -export const NULL_CELL_WIDTH = 1; -export const NULL_CELL_CODE = 32; +// export const NULL_CELL_CHAR = ' '; +// export const NULL_CELL_WIDTH = 1; +// export const NULL_CELL_CODE = 32; + +export const NULL_CELL_CHAR = ''; +export const NULL_CELL_WIDTH = 0; +export const NULL_CELL_CODE = 0; /** * This class represents a terminal buffer (an internal state of the terminal), where the @@ -273,6 +277,7 @@ export class Buffer implements IBuffer { 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 diff --git a/src/BufferLine.ts b/src/BufferLine.ts index f1ea9cf0..e1cb6a4f 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -3,7 +3,7 @@ * @license MIT */ import { CharData, IBufferLine } from './Types'; -import { NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR } from './Buffer'; +import { NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX } from './Buffer'; /** * Class representing a terminal line. @@ -108,6 +108,27 @@ export class BufferLine implements IBufferLine { newLine.copyFrom(this); return newLine; } + + public getTrimmedLength(): number { + for (let i = this.length - 1; i >= 0; --i) { + const ch = this.get(i); + if (ch[CHAR_DATA_CHAR_INDEX] !== '') { + return i + ch[CHAR_DATA_WIDTH_INDEX] - 1; + } + } + return 0; + } + + public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = null): string { + let length = endCol || this.length; + if (trimRight) + length = Math.min(length, this.getTrimmedLength()); + let result = ''; + for (let i = startCol; i < length; ++i) { + result += this.get(i)[CHAR_DATA_CHAR_INDEX] || ' '; + } + return result; + } } /** typed array slots taken by one cell */ @@ -279,4 +300,25 @@ export class BufferLineTypedArray implements IBufferLine { newLine.isWrapped = this.isWrapped; return newLine; } + + public getTrimmedLength(): number { + for (let i = this.length - 1; i >= 0; --i) { + if (this._data[i * CELL_SIZE + Cell.STRING] !== 0) { // 0 ==> ''.charCodeAt(0) ==> NaN ==> 0 + return i + 1; + } + } + return 0; + } + + public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = null): string { + let length = endCol || this.length; + if (trimRight) + length = Math.min(length, this.getTrimmedLength()); + let result = ''; + for (let i = startCol; i < length; ++i) { + const stringData = this._data[i * CELL_SIZE + Cell.STRING]; + result += (stringData & 0x80000000) ? this._combined[i] : (stringData) ? String.fromCharCode(stringData) : ' '; + } + return result; + } } diff --git a/src/Terminal.integration.ts b/src/Terminal.integration.ts index 66fd3502..21c4a2d0 100644 --- a/src/Terminal.integration.ts +++ b/src/Terminal.integration.ts @@ -67,7 +67,7 @@ function terminalToString(term: Terminal): string { for (let line = term.buffer.ybase; line < term.buffer.ybase + term.rows; line++) { lineText = ''; for (let cell = 0; cell < term.cols; ++cell) { - lineText += term.buffer.lines.get(line).get(cell)[CHAR_DATA_CHAR_INDEX]; + lineText += term.buffer.lines.get(line).get(cell)[CHAR_DATA_CHAR_INDEX] || ' '; } // rtrim empty cells as xterm does lineText = lineText.replace(/\s+$/, ''); diff --git a/src/Types.ts b/src/Types.ts index e8578426..b0ccf88d 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -524,6 +524,8 @@ export interface IBufferLine { fill(fillCharData: CharData): void; copyFrom(line: IBufferLine): void; clone(): IBufferLine; + getTrimmedLength(): number; + translateToString(trimRight?: boolean, startCol?: number, endCol?: number): string; } export interface IBufferLineConstructor { From 62f1d64af4c7a1eea239309867ca890426be3273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 31 Oct 2018 14:26:54 +0100 Subject: [PATCH 02/42] fix trimmedLength for TypedArray --- src/BufferLine.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BufferLine.ts b/src/BufferLine.ts index e1cb6a4f..54207142 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -304,7 +304,7 @@ export class BufferLineTypedArray implements IBufferLine { public getTrimmedLength(): number { for (let i = this.length - 1; i >= 0; --i) { if (this._data[i * CELL_SIZE + Cell.STRING] !== 0) { // 0 ==> ''.charCodeAt(0) ==> NaN ==> 0 - return i + 1; + return i + this._data[i * CELL_SIZE + Cell.WIDTH] - 1; } } return 0; From 1c111d3e6060ad4f28e23ab87d33166327cd6a00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 31 Oct 2018 14:53:55 +0100 Subject: [PATCH 03/42] skip empty cells after fullwidth --- src/BufferLine.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/BufferLine.ts b/src/BufferLine.ts index 54207142..c4b62588 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -124,8 +124,9 @@ export class BufferLine implements IBufferLine { if (trimRight) length = Math.min(length, this.getTrimmedLength()); let result = ''; - for (let i = startCol; i < length; ++i) { - result += this.get(i)[CHAR_DATA_CHAR_INDEX] || ' '; + while (startCol < endCol) { + result += this.get(startCol)[CHAR_DATA_CHAR_INDEX] || ' '; + startCol += this.get(startCol)[CHAR_DATA_WIDTH_INDEX] || 1; } return result; } @@ -315,9 +316,10 @@ export class BufferLineTypedArray implements IBufferLine { if (trimRight) length = Math.min(length, this.getTrimmedLength()); let result = ''; - for (let i = startCol; i < length; ++i) { - const stringData = this._data[i * CELL_SIZE + Cell.STRING]; - result += (stringData & 0x80000000) ? this._combined[i] : (stringData) ? String.fromCharCode(stringData) : ' '; + while (startCol < endCol) { + const stringData = this._data[startCol * CELL_SIZE + Cell.STRING]; + result += (stringData & 0x80000000) ? this._combined[startCol] : (stringData) ? String.fromCharCode(stringData) : ' '; + startCol += this._data[startCol * CELL_SIZE + Cell.WIDTH] || 1; } return result; } From 8ffcd65cff8529b1ad36ef5a2a0a7e8dc5d00cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 31 Oct 2018 15:11:18 +0100 Subject: [PATCH 04/42] fix buffer tests --- src/Buffer.test.ts | 6 +++--- src/Buffer.ts | 5 ++--- src/BufferLine.ts | 8 ++++---- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index db8a460d..bb1a08d8 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -509,10 +509,10 @@ describe('Buffer', () => { // --> fixable after resolving #1685 terminal.writeSync(input); // TODO: reenable after fix - // const s = terminal.buffer.contents(true).toArray()[0]; - // assert.equal(input, s); + 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 + 1); // TODO: remove +1 after fix + const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i); // TODO: remove +1 after fix 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 7c5d2652..39dc5d57 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -21,7 +21,7 @@ export const MAX_BUFFER_SIZE = 4294967295; // 2^32 - 1 // export const NULL_CELL_CODE = 32; export const NULL_CELL_CHAR = ''; -export const NULL_CELL_WIDTH = 0; +export const NULL_CELL_WIDTH = 1; export const NULL_CELL_CODE = 0; /** @@ -488,8 +488,7 @@ export class BufferStringIterator implements IBufferStringIterator { range.last = Math.min(range.last, this._buffer.lines.length); let result = ''; for (let i = range.first; i <= range.last; ++i) { - // TODO: always apply trimRight after fixing #1685 - result += this._buffer.translateBufferLineToString(i, (this._trimRight) ? i === range.last : false); + result += this._buffer.translateBufferLineToString(i, this._trimRight); } this._current = range.last + 1; return {range: range, content: result}; diff --git a/src/BufferLine.ts b/src/BufferLine.ts index c4b62588..e35b67d6 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -113,7 +113,7 @@ export class BufferLine implements IBufferLine { for (let i = this.length - 1; i >= 0; --i) { const ch = this.get(i); if (ch[CHAR_DATA_CHAR_INDEX] !== '') { - return i + ch[CHAR_DATA_WIDTH_INDEX] - 1; + return i + ch[CHAR_DATA_WIDTH_INDEX]; } } return 0; @@ -124,7 +124,7 @@ export class BufferLine implements IBufferLine { if (trimRight) length = Math.min(length, this.getTrimmedLength()); let result = ''; - while (startCol < endCol) { + while (startCol < length) { result += this.get(startCol)[CHAR_DATA_CHAR_INDEX] || ' '; startCol += this.get(startCol)[CHAR_DATA_WIDTH_INDEX] || 1; } @@ -305,7 +305,7 @@ export class BufferLineTypedArray implements IBufferLine { public getTrimmedLength(): number { for (let i = this.length - 1; i >= 0; --i) { if (this._data[i * CELL_SIZE + Cell.STRING] !== 0) { // 0 ==> ''.charCodeAt(0) ==> NaN ==> 0 - return i + this._data[i * CELL_SIZE + Cell.WIDTH] - 1; + return i + this._data[i * CELL_SIZE + Cell.WIDTH]; } } return 0; @@ -316,7 +316,7 @@ export class BufferLineTypedArray implements IBufferLine { if (trimRight) length = Math.min(length, this.getTrimmedLength()); let result = ''; - while (startCol < endCol) { + while (startCol < length) { const stringData = this._data[startCol * CELL_SIZE + Cell.STRING]; result += (stringData & 0x80000000) ? this._combined[startCol] : (stringData) ? String.fromCharCode(stringData) : ' '; startCol += this._data[startCol * CELL_SIZE + Cell.WIDTH] || 1; From 745e228e982d5092ac64202e2f4e51848ca9dbaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 31 Oct 2018 15:26:40 +0100 Subject: [PATCH 05/42] fix input handler and terminal tests --- src/InputHandler.test.ts | 10 +++++----- src/Terminal.test.ts | 42 ++++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/InputHandler.test.ts b/src/InputHandler.test.ts index aaaf57c3..46f8795a 100644 --- a/src/InputHandler.test.ts +++ b/src/InputHandler.test.ts @@ -210,7 +210,7 @@ describe('InputHandler', () => { describe('regression tests', function(): void { function lineContent(line: IBufferLine): string { let content = ''; - for (let i = 0; i < line.length; ++i) content += line.get(i)[CHAR_DATA_CHAR_INDEX]; + for (let i = 0; i < line.length; ++i) content += line.get(i)[CHAR_DATA_CHAR_INDEX] || ' '; return content; } @@ -476,12 +476,12 @@ describe('InputHandler', () => { const termNotConverting = new Terminal({cols: 15, rows: 10}); (termNotConverting as any)._inputHandler.parse('Hello\nWorld'); for (let i = 0; i < termNotConverting.cols; ++i) { - s += termNotConverting.buffer.lines.get(0).get(i)[CHAR_DATA_CHAR_INDEX]; + s += termNotConverting.buffer.lines.get(0).get(i)[CHAR_DATA_CHAR_INDEX] || ' '; } expect(s).equals('Hello '); s = ''; for (let i = 0; i < termNotConverting.cols; ++i) { - s += termNotConverting.buffer.lines.get(1).get(i)[CHAR_DATA_CHAR_INDEX]; + s += termNotConverting.buffer.lines.get(1).get(i)[CHAR_DATA_CHAR_INDEX] || ' '; } expect(s).equals(' World '); @@ -490,12 +490,12 @@ describe('InputHandler', () => { const termConverting = new Terminal({cols: 15, rows: 10, convertEol: true}); (termConverting as any)._inputHandler.parse('Hello\nWorld'); for (let i = 0; i < termConverting.cols; ++i) { - s += termConverting.buffer.lines.get(0).get(i)[CHAR_DATA_CHAR_INDEX]; + s += termConverting.buffer.lines.get(0).get(i)[CHAR_DATA_CHAR_INDEX] || ' '; } expect(s).equals('Hello '); s = ''; for (let i = 0; i < termConverting.cols; ++i) { - s += termConverting.buffer.lines.get(1).get(i)[CHAR_DATA_CHAR_INDEX]; + s += termConverting.buffer.lines.get(1).get(i)[CHAR_DATA_CHAR_INDEX] || ' '; } expect(s).equals('World '); }); diff --git a/src/Terminal.test.ts b/src/Terminal.test.ts index fd59144c..da4a39c0 100644 --- a/src/Terminal.test.ts +++ b/src/Terminal.test.ts @@ -462,7 +462,7 @@ describe('term.js addons', () => { assert.equal(term.buffer.lines.length, INIT_ROWS + 1); assert.equal(term.buffer.lines.get(0).get(0)[CHAR_DATA_CHAR_INDEX], 'a'); assert.equal(term.buffer.lines.get(INIT_ROWS - 1).get(0)[CHAR_DATA_CHAR_INDEX], 'b'); - assert.equal(term.buffer.lines.get(INIT_ROWS).get(0)[CHAR_DATA_CHAR_INDEX], ' '); + assert.equal(term.buffer.lines.get(INIT_ROWS).get(0)[CHAR_DATA_CHAR_INDEX], ''); }); it('should properly scroll inside a scroll region (scrollTop set)', () => { @@ -491,7 +491,7 @@ describe('term.js addons', () => { assert.equal(term.buffer.lines.get(1).get(0)[CHAR_DATA_CHAR_INDEX], 'b'); assert.equal(term.buffer.lines.get(2).get(0)[CHAR_DATA_CHAR_INDEX], 'c'); assert.equal(term.buffer.lines.get(3).get(0)[CHAR_DATA_CHAR_INDEX], 'd'); - assert.equal(term.buffer.lines.get(4).get(0)[CHAR_DATA_CHAR_INDEX], ' ', 'a blank line should be added at scrollBottom\'s index'); + assert.equal(term.buffer.lines.get(4).get(0)[CHAR_DATA_CHAR_INDEX], '', 'a blank line should be added at scrollBottom\'s index'); assert.equal(term.buffer.lines.get(5).get(0)[CHAR_DATA_CHAR_INDEX], 'e'); }); @@ -509,7 +509,7 @@ describe('term.js addons', () => { assert.equal(term.buffer.lines.get(0).get(0)[CHAR_DATA_CHAR_INDEX], 'a'); assert.equal(term.buffer.lines.get(1).get(0)[CHAR_DATA_CHAR_INDEX], 'c', '\'b\' should be removed from the buffer'); assert.equal(term.buffer.lines.get(2).get(0)[CHAR_DATA_CHAR_INDEX], 'd'); - assert.equal(term.buffer.lines.get(3).get(0)[CHAR_DATA_CHAR_INDEX], ' ', 'a blank line should be added at scrollBottom\'s index'); + assert.equal(term.buffer.lines.get(3).get(0)[CHAR_DATA_CHAR_INDEX], '', 'a blank line should be added at scrollBottom\'s index'); assert.equal(term.buffer.lines.get(4).get(0)[CHAR_DATA_CHAR_INDEX], 'e'); }); }); @@ -530,9 +530,9 @@ describe('term.js addons', () => { assert.equal(term.buffer.lines.length, INIT_ROWS); // 'a' gets pushed out of buffer assert.equal(term.buffer.lines.get(0).get(0)[CHAR_DATA_CHAR_INDEX], 'b'); - assert.equal(term.buffer.lines.get(1).get(0)[CHAR_DATA_CHAR_INDEX], ' '); + assert.equal(term.buffer.lines.get(1).get(0)[CHAR_DATA_CHAR_INDEX], ''); assert.equal(term.buffer.lines.get(INIT_ROWS - 2).get(0)[CHAR_DATA_CHAR_INDEX], 'c'); - assert.equal(term.buffer.lines.get(INIT_ROWS - 1).get(0)[CHAR_DATA_CHAR_INDEX], ' '); + assert.equal(term.buffer.lines.get(INIT_ROWS - 1).get(0)[CHAR_DATA_CHAR_INDEX], ''); }); it('should properly scroll inside a scroll region (scrollTop set)', () => { @@ -560,7 +560,7 @@ describe('term.js addons', () => { assert.equal(term.buffer.lines.get(0).get(0)[CHAR_DATA_CHAR_INDEX], 'b'); assert.equal(term.buffer.lines.get(1).get(0)[CHAR_DATA_CHAR_INDEX], 'c'); assert.equal(term.buffer.lines.get(2).get(0)[CHAR_DATA_CHAR_INDEX], 'd'); - assert.equal(term.buffer.lines.get(3).get(0)[CHAR_DATA_CHAR_INDEX], ' ', 'a blank line should be added at scrollBottom\'s index'); + assert.equal(term.buffer.lines.get(3).get(0)[CHAR_DATA_CHAR_INDEX], '', 'a blank line should be added at scrollBottom\'s index'); assert.equal(term.buffer.lines.get(4).get(0)[CHAR_DATA_CHAR_INDEX], 'e'); }); @@ -578,7 +578,7 @@ describe('term.js addons', () => { assert.equal(term.buffer.lines.get(0).get(0)[CHAR_DATA_CHAR_INDEX], 'a'); assert.equal(term.buffer.lines.get(1).get(0)[CHAR_DATA_CHAR_INDEX], 'c', '\'b\' should be removed from the buffer'); assert.equal(term.buffer.lines.get(2).get(0)[CHAR_DATA_CHAR_INDEX], 'd'); - assert.equal(term.buffer.lines.get(3).get(0)[CHAR_DATA_CHAR_INDEX], ' ', 'a blank line should be added at scrollBottom\'s index'); + assert.equal(term.buffer.lines.get(3).get(0)[CHAR_DATA_CHAR_INDEX], '', 'a blank line should be added at scrollBottom\'s index'); assert.equal(term.buffer.lines.get(4).get(0)[CHAR_DATA_CHAR_INDEX], 'e'); }); }); @@ -776,7 +776,7 @@ describe('term.js addons', () => { expect(tchar[CHAR_DATA_CHAR_INDEX]).eql(high + String.fromCharCode(i)); expect(tchar[CHAR_DATA_CHAR_INDEX].length).eql(2); expect(tchar[CHAR_DATA_WIDTH_INDEX]).eql(1); - expect(term.buffer.lines.get(0).get(1)[CHAR_DATA_CHAR_INDEX]).eql(' '); + expect(term.buffer.lines.get(0).get(1)[CHAR_DATA_CHAR_INDEX]).eql(''); term.reset(); } }); @@ -787,7 +787,7 @@ describe('term.js addons', () => { term.write(high + String.fromCharCode(i)); expect(term.buffer.lines.get(0).get(term.buffer.x - 1)[CHAR_DATA_CHAR_INDEX]).eql(high + String.fromCharCode(i)); expect(term.buffer.lines.get(0).get(term.buffer.x - 1)[CHAR_DATA_CHAR_INDEX].length).eql(2); - expect(term.buffer.lines.get(1).get(0)[CHAR_DATA_CHAR_INDEX]).eql(' '); + expect(term.buffer.lines.get(1).get(0)[CHAR_DATA_CHAR_INDEX]).eql(''); term.reset(); } }); @@ -800,7 +800,7 @@ describe('term.js addons', () => { expect(term.buffer.lines.get(0).get(term.cols - 1)[CHAR_DATA_CHAR_INDEX]).eql('a'); expect(term.buffer.lines.get(1).get(0)[CHAR_DATA_CHAR_INDEX]).eql(high + String.fromCharCode(i)); expect(term.buffer.lines.get(1).get(0)[CHAR_DATA_CHAR_INDEX].length).eql(2); - expect(term.buffer.lines.get(1).get(1)[CHAR_DATA_CHAR_INDEX]).eql(' '); + expect(term.buffer.lines.get(1).get(1)[CHAR_DATA_CHAR_INDEX]).eql(''); term.reset(); } }); @@ -813,7 +813,7 @@ describe('term.js addons', () => { // auto wraparound mode should cut off the rest of the line expect(term.buffer.lines.get(0).get(term.cols - 1)[CHAR_DATA_CHAR_INDEX]).eql('a'); expect(term.buffer.lines.get(0).get(term.cols - 1)[CHAR_DATA_CHAR_INDEX].length).eql(1); - expect(term.buffer.lines.get(1).get(1)[CHAR_DATA_CHAR_INDEX]).eql(' '); + expect(term.buffer.lines.get(1).get(1)[CHAR_DATA_CHAR_INDEX]).eql(''); term.reset(); } }); @@ -826,7 +826,7 @@ describe('term.js addons', () => { expect(tchar[CHAR_DATA_CHAR_INDEX]).eql(high + String.fromCharCode(i)); expect(tchar[CHAR_DATA_CHAR_INDEX].length).eql(2); expect(tchar[CHAR_DATA_WIDTH_INDEX]).eql(1); - expect(term.buffer.lines.get(0).get(1)[CHAR_DATA_CHAR_INDEX]).eql(' '); + expect(term.buffer.lines.get(0).get(1)[CHAR_DATA_CHAR_INDEX]).eql(''); term.reset(); } }); @@ -845,8 +845,8 @@ describe('term.js addons', () => { expect(term.buffer.lines.get(0).get(term.cols - 1)[CHAR_DATA_CHAR_INDEX]).eql('e\u0301'); expect(term.buffer.lines.get(0).get(term.cols - 1)[CHAR_DATA_CHAR_INDEX].length).eql(2); expect(term.buffer.lines.get(0).get(term.cols - 1)[CHAR_DATA_WIDTH_INDEX]).eql(1); - expect(term.buffer.lines.get(0).get(1)[CHAR_DATA_CHAR_INDEX]).eql(' '); - expect(term.buffer.lines.get(0).get(1)[CHAR_DATA_CHAR_INDEX].length).eql(1); + expect(term.buffer.lines.get(0).get(1)[CHAR_DATA_CHAR_INDEX]).eql(''); + expect(term.buffer.lines.get(0).get(1)[CHAR_DATA_CHAR_INDEX].length).eql(0); expect(term.buffer.lines.get(0).get(1)[CHAR_DATA_WIDTH_INDEX]).eql(1); }); it('multiple combined é', () => { @@ -928,8 +928,8 @@ describe('term.js addons', () => { } } let tchar = term.buffer.lines.get(0).get(term.cols - 1); - expect(tchar[CHAR_DATA_CHAR_INDEX]).eql(' '); - expect(tchar[CHAR_DATA_CHAR_INDEX].length).eql(1); + expect(tchar[CHAR_DATA_CHAR_INDEX]).eql(''); + expect(tchar[CHAR_DATA_CHAR_INDEX].length).eql(0); expect(tchar[CHAR_DATA_WIDTH_INDEX]).eql(1); tchar = term.buffer.lines.get(1).get(0); expect(tchar[CHAR_DATA_CHAR_INDEX]).eql('¥'); @@ -953,8 +953,8 @@ describe('term.js addons', () => { } } let tchar = term.buffer.lines.get(0).get(term.cols - 1); - expect(tchar[CHAR_DATA_CHAR_INDEX]).eql(' '); - expect(tchar[CHAR_DATA_CHAR_INDEX].length).eql(1); + expect(tchar[CHAR_DATA_CHAR_INDEX]).eql(''); + expect(tchar[CHAR_DATA_CHAR_INDEX].length).eql(0); expect(tchar[CHAR_DATA_WIDTH_INDEX]).eql(1); tchar = term.buffer.lines.get(1).get(0); expect(tchar[CHAR_DATA_CHAR_INDEX]).eql('¥\u0301'); @@ -998,8 +998,8 @@ describe('term.js addons', () => { } } let tchar = term.buffer.lines.get(0).get(term.cols - 1); - expect(tchar[CHAR_DATA_CHAR_INDEX]).eql(' '); - expect(tchar[CHAR_DATA_CHAR_INDEX].length).eql(1); + expect(tchar[CHAR_DATA_CHAR_INDEX]).eql(''); + expect(tchar[CHAR_DATA_CHAR_INDEX].length).eql(0); expect(tchar[CHAR_DATA_WIDTH_INDEX]).eql(1); tchar = term.buffer.lines.get(1).get(0); expect(tchar[CHAR_DATA_CHAR_INDEX]).eql('\ud843\ude6d\u0301'); @@ -1063,7 +1063,7 @@ describe('term.js addons', () => { expect(term.buffer.lines.get(0).length).eql(term.cols); expect(term.buffer.lines.get(0).get(10)[CHAR_DATA_CHAR_INDEX]).eql('a'); expect(term.buffer.lines.get(0).get(11)[CHAR_DATA_CHAR_INDEX]).eql('¥'); - expect(term.buffer.lines.get(0).get(79)[CHAR_DATA_CHAR_INDEX]).eql(' '); // fullwidth char got replaced + expect(term.buffer.lines.get(0).get(79)[CHAR_DATA_CHAR_INDEX]).eql(''); // fullwidth char got replaced term.write('b'); expect(term.buffer.lines.get(0).length).eql(term.cols); expect(term.buffer.lines.get(0).get(11)[CHAR_DATA_CHAR_INDEX]).eql('b'); From cabd4a721c9cb73e0b694b6b1fe27beed040f4b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 31 Oct 2018 15:30:29 +0100 Subject: [PATCH 06/42] fix dom renderer --- src/BufferLine.ts | 6 ++++-- src/renderer/dom/DomRendererRowFactory.ts | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/BufferLine.ts b/src/BufferLine.ts index e35b67d6..3bf7fec4 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -121,8 +121,9 @@ export class BufferLine implements IBufferLine { public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = null): string { let length = endCol || this.length; - if (trimRight) + if (trimRight) { length = Math.min(length, this.getTrimmedLength()); + } let result = ''; while (startCol < length) { result += this.get(startCol)[CHAR_DATA_CHAR_INDEX] || ' '; @@ -313,8 +314,9 @@ export class BufferLineTypedArray implements IBufferLine { public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = null): string { let length = endCol || this.length; - if (trimRight) + if (trimRight) { length = Math.min(length, this.getTrimmedLength()); + } let result = ''; while (startCol < length) { const stringData = this._data[startCol * CELL_SIZE + Cell.STRING]; diff --git a/src/renderer/dom/DomRendererRowFactory.ts b/src/renderer/dom/DomRendererRowFactory.ts index 4bb59902..ce94ce33 100644 --- a/src/renderer/dom/DomRendererRowFactory.ts +++ b/src/renderer/dom/DomRendererRowFactory.ts @@ -31,7 +31,7 @@ export class DomRendererRowFactory { } const charData = lineData.get(x); - const char: string = charData[CHAR_DATA_CHAR_INDEX]; + const char: string = charData[CHAR_DATA_CHAR_INDEX] || ' '; const attr: number = charData[CHAR_DATA_ATTR_INDEX]; const width: number = charData[CHAR_DATA_WIDTH_INDEX]; From 200b59b47ca3f77b3b0652d7e95e9ed2898f26dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 31 Oct 2018 15:35:30 +0100 Subject: [PATCH 07/42] fix canvas renderer --- src/renderer/TextRenderLayer.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/renderer/TextRenderLayer.ts b/src/renderer/TextRenderLayer.ts index 7f10e7c9..08850b72 100644 --- a/src/renderer/TextRenderLayer.ts +++ b/src/renderer/TextRenderLayer.ts @@ -90,6 +90,11 @@ export class TextRenderLayer extends BaseRenderLayer { continue; } + // Simply skip empty cells... + if (!chars) { + continue; + } + // Process any joined character ranges as needed. Because of how the // ranges are produced, we know that they are valid for the characters // and attributes of our input. From 5395503031708bd12b2918239074fb0ea3ce6748 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Wed, 31 Oct 2018 15:47:35 +0100 Subject: [PATCH 08/42] apply code and char replacement to renderer --- src/renderer/TextRenderLayer.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/renderer/TextRenderLayer.ts b/src/renderer/TextRenderLayer.ts index 08850b72..24c948eb 100644 --- a/src/renderer/TextRenderLayer.ts +++ b/src/renderer/TextRenderLayer.ts @@ -72,11 +72,11 @@ export class TextRenderLayer extends BaseRenderLayer { const joinedRanges = joinerRegistry ? joinerRegistry.getJoinedCharacters(row) : []; for (let x = 0; x < terminal.cols; x++) { const charData = line.get(x); - let code: number = charData[CHAR_DATA_CODE_INDEX]; + let code: number = charData[CHAR_DATA_CODE_INDEX] || 32; // Can either represent character(s) for a single cell or multiple cells // if indicated by a character joiner. - let chars: string = charData[CHAR_DATA_CHAR_INDEX]; + let chars: string = charData[CHAR_DATA_CHAR_INDEX] || ' '; const attr: number = charData[CHAR_DATA_ATTR_INDEX]; let width: number = charData[CHAR_DATA_WIDTH_INDEX]; @@ -90,11 +90,6 @@ export class TextRenderLayer extends BaseRenderLayer { continue; } - // Simply skip empty cells... - if (!chars) { - continue; - } - // Process any joined character ranges as needed. Because of how the // ranges are produced, we know that they are valid for the characters // and attributes of our input. 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 09/42] 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; From 2f9c04c3af9fe08b196856b93aee3a22a22a9bc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 8 Nov 2018 05:00:08 +0100 Subject: [PATCH 10/42] cleanup test cases --- src/InputHandler.test.ts | 419 ++++++++++++--------------------------- 1 file changed, 126 insertions(+), 293 deletions(-) diff --git a/src/InputHandler.test.ts b/src/InputHandler.test.ts index 46f8795a..eacc54db 100644 --- a/src/InputHandler.test.ts +++ b/src/InputHandler.test.ts @@ -6,130 +6,9 @@ import { assert, expect } from 'chai'; import { InputHandler } from './InputHandler'; import { MockInputHandlingTerminal } from './utils/TestUtils.test'; -import { NULL_CELL_CHAR, NULL_CELL_CODE, NULL_CELL_WIDTH, CHAR_DATA_CHAR_INDEX } from './Buffer'; import { Terminal } from './Terminal'; import { IBufferLine } from './Types'; - -// TODO: This and the sections related to this object in associated tests can be -// removed safely after InputHandler refactors are finished -class OldInputHandler extends InputHandler { - public eraseInLine(params: number[]): void { - switch (params[0]) { - case 0: - this.eraseRight(this._terminal.buffer.x, this._terminal.buffer.y); - break; - case 1: - this.eraseLeft(this._terminal.buffer.x, this._terminal.buffer.y); - break; - case 2: - this.eraseLine(this._terminal.buffer.y); - break; - } - } - - public eraseInDisplay(params: number[]): void { - let j; - switch (params[0]) { - case 0: - this.eraseRight(this._terminal.buffer.x, this._terminal.buffer.y); - j = this._terminal.buffer.y + 1; - for (; j < this._terminal.rows; j++) { - this.eraseLine(j); - } - break; - case 1: - this.eraseLeft(this._terminal.buffer.x, this._terminal.buffer.y); - j = this._terminal.buffer.y; - while (j--) { - this.eraseLine(j); - } - break; - case 2: - j = this._terminal.rows; - while (j--) this.eraseLine(j); - break; - case 3: - // Clear scrollback (everything not in viewport) - const scrollBackSize = this._terminal.buffer.lines.length - this._terminal.rows; - if (scrollBackSize > 0) { - this._terminal.buffer.lines.trimStart(scrollBackSize); - this._terminal.buffer.ybase = Math.max(this._terminal.buffer.ybase - scrollBackSize, 0); - this._terminal.buffer.ydisp = Math.max(this._terminal.buffer.ydisp - scrollBackSize, 0); - // Force a scroll event to refresh viewport - this._terminal.emit('scroll', 0); - } - break; - } - } - - /** - * Erase in the identified line everything from "x" to the end of the line (right). - * @param x The column from which to start erasing to the end of the line. - * @param y The line in which to operate. - */ - public eraseRight(x: number, y: number): void { - const line = this._terminal.buffer.lines.get(this._terminal.buffer.ybase + y); - if (!line) { - return; - } - line.replaceCells(x, this._terminal.cols, [this._terminal.eraseAttr(), NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]); - this._terminal.updateRange(y); - } - - /** - * Erase in the identified line everything from "x" to the start of the line (left). - * @param x The column from which to start erasing to the start of the line. - * @param y The line in which to operate. - */ - public eraseLeft(x: number, y: number): void { - const line = this._terminal.buffer.lines.get(this._terminal.buffer.ybase + y); - if (!line) { - return; - } - line.replaceCells(0, x + 1, [this._terminal.eraseAttr(), NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]); - this._terminal.updateRange(y); - } - - /** - * Erase all content in the given line - * @param y The line to erase all of its contents. - */ - public eraseLine(y: number): void { - this.eraseRight(0, y); - } - - public insertChars(params: number[]): void { - let param = params[0]; - if (param < 1) param = 1; - - // make buffer local for faster access - const buffer = this._terminal.buffer; - - const row = buffer.y + buffer.ybase; - let j = buffer.x; - while (param-- && j < this._terminal.cols) { - buffer.lines.get(row).insertCells(j++, 1, [this._terminal.eraseAttr(), NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]); - } - } - - public deleteChars(params: number[]): void { - let param: number = params[0]; - if (param < 1) { - param = 1; - } - - // make buffer local for faster access - const buffer = this._terminal.buffer; - - const row = buffer.y + buffer.ybase; - while (param--) { - buffer.lines.get(row).deleteCells(buffer.x, 1, [this._terminal.eraseAttr(), NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]); - } - this._terminal.updateRange(buffer.y); - } -} - describe('InputHandler', () => { describe('save and restore cursor', () => { const terminal = new MockInputHandlingTerminal(); @@ -208,296 +87,250 @@ describe('InputHandler', () => { }); }); describe('regression tests', function(): void { - function lineContent(line: IBufferLine): string { - let content = ''; - for (let i = 0; i < line.length; ++i) content += line.get(i)[CHAR_DATA_CHAR_INDEX] || ' '; - return content; - } - - function termContent(term: Terminal): string[] { + function termContent(term: Terminal, trim: boolean): string[] { const result = []; - for (let i = 0; i < term.rows; ++i) result.push(lineContent(term.buffer.lines.get(i))); + for (let i = 0; i < term.rows; ++i) result.push(term.buffer.lines.get(i).translateToString(trim)); return result; } it('insertChars', function(): void { const term = new Terminal(); const inputHandler = new InputHandler(term); - const oldInputHandler = new OldInputHandler(term); // insert some data in first and second line inputHandler.parse(Array(term.cols - 9).join('a')); inputHandler.parse('1234567890'); inputHandler.parse(Array(term.cols - 9).join('a')); inputHandler.parse('1234567890'); - const line1: IBufferLine = term.buffer.lines.get(0); // line for old variant - const line2: IBufferLine = term.buffer.lines.get(1); // line for new variant - expect(lineContent(line1)).equals(Array(term.cols - 9).join('a') + '1234567890'); - expect(lineContent(line2)).equals(Array(term.cols - 9).join('a') + '1234567890'); + const line1: IBufferLine = term.buffer.lines.get(0); + expect(line1.translateToString(false)).equals(Array(term.cols - 9).join('a') + '1234567890'); // insert one char from params = [0] term.buffer.y = 0; term.buffer.x = 70; - oldInputHandler.insertChars([0]); - expect(lineContent(line1)).equals(Array(term.cols - 9).join('a') + ' 123456789'); - term.buffer.y = 1; - term.buffer.x = 70; inputHandler.insertChars([0]); - expect(lineContent(line2)).equals(Array(term.cols - 9).join('a') + ' 123456789'); - expect(lineContent(line2)).equals(lineContent(line1)); + expect(line1.translateToString(false)).equals(Array(term.cols - 9).join('a') + ' 123456789'); // insert one char from params = [1] term.buffer.y = 0; term.buffer.x = 70; - oldInputHandler.insertChars([1]); - expect(lineContent(line1)).equals(Array(term.cols - 9).join('a') + ' 12345678'); - term.buffer.y = 1; - term.buffer.x = 70; inputHandler.insertChars([1]); - expect(lineContent(line2)).equals(Array(term.cols - 9).join('a') + ' 12345678'); - expect(lineContent(line2)).equals(lineContent(line1)); + expect(line1.translateToString(false)).equals(Array(term.cols - 9).join('a') + ' 12345678'); // insert two chars from params = [2] term.buffer.y = 0; term.buffer.x = 70; - oldInputHandler.insertChars([2]); - expect(lineContent(line1)).equals(Array(term.cols - 9).join('a') + ' 123456'); - term.buffer.y = 1; - term.buffer.x = 70; inputHandler.insertChars([2]); - expect(lineContent(line2)).equals(Array(term.cols - 9).join('a') + ' 123456'); - expect(lineContent(line2)).equals(lineContent(line1)); + expect(line1.translateToString(false)).equals(Array(term.cols - 9).join('a') + ' 123456'); // insert 10 chars from params = [10] term.buffer.y = 0; term.buffer.x = 70; - oldInputHandler.insertChars([10]); - expect(lineContent(line1)).equals(Array(term.cols - 9).join('a') + ' '); - term.buffer.y = 1; - term.buffer.x = 70; inputHandler.insertChars([10]); - expect(lineContent(line2)).equals(Array(term.cols - 9).join('a') + ' '); - expect(lineContent(line2)).equals(lineContent(line1)); + expect(line1.translateToString(false)).equals(Array(term.cols - 9).join('a') + ' '); + expect(line1.translateToString(true)).equals(Array(term.cols - 9).join('a')); }); it('deleteChars', function(): void { const term = new Terminal(); const inputHandler = new InputHandler(term); - const oldInputHandler = new OldInputHandler(term); // insert some data in first and second line inputHandler.parse(Array(term.cols - 9).join('a')); inputHandler.parse('1234567890'); inputHandler.parse(Array(term.cols - 9).join('a')); inputHandler.parse('1234567890'); - const line1: IBufferLine = term.buffer.lines.get(0); // line for old variant - const line2: IBufferLine = term.buffer.lines.get(1); // line for new variant - expect(lineContent(line1)).equals(Array(term.cols - 9).join('a') + '1234567890'); - expect(lineContent(line2)).equals(Array(term.cols - 9).join('a') + '1234567890'); + const line1: IBufferLine = term.buffer.lines.get(0); + expect(line1.translateToString(false)).equals(Array(term.cols - 9).join('a') + '1234567890'); // delete one char from params = [0] term.buffer.y = 0; term.buffer.x = 70; - oldInputHandler.deleteChars([0]); - expect(lineContent(line1)).equals(Array(term.cols - 9).join('a') + '234567890 '); - term.buffer.y = 1; - term.buffer.x = 70; inputHandler.deleteChars([0]); - expect(lineContent(line2)).equals(Array(term.cols - 9).join('a') + '234567890 '); - expect(lineContent(line2)).equals(lineContent(line1)); + expect(line1.translateToString(false)).equals(Array(term.cols - 9).join('a') + '234567890 '); + expect(line1.translateToString(true)).equals(Array(term.cols - 9).join('a') + '234567890'); // insert one char from params = [1] term.buffer.y = 0; term.buffer.x = 70; - oldInputHandler.deleteChars([1]); - expect(lineContent(line1)).equals(Array(term.cols - 9).join('a') + '34567890 '); - term.buffer.y = 1; - term.buffer.x = 70; inputHandler.deleteChars([1]); - expect(lineContent(line2)).equals(Array(term.cols - 9).join('a') + '34567890 '); - expect(lineContent(line2)).equals(lineContent(line1)); + expect(line1.translateToString(false)).equals(Array(term.cols - 9).join('a') + '34567890 '); + expect(line1.translateToString(true)).equals(Array(term.cols - 9).join('a') + '34567890'); // insert two chars from params = [2] term.buffer.y = 0; term.buffer.x = 70; - oldInputHandler.deleteChars([2]); - expect(lineContent(line1)).equals(Array(term.cols - 9).join('a') + '567890 '); - term.buffer.y = 1; - term.buffer.x = 70; inputHandler.deleteChars([2]); - expect(lineContent(line2)).equals(Array(term.cols - 9).join('a') + '567890 '); - expect(lineContent(line2)).equals(lineContent(line1)); + expect(line1.translateToString(false)).equals(Array(term.cols - 9).join('a') + '567890 '); + expect(line1.translateToString(true)).equals(Array(term.cols - 9).join('a') + '567890'); // insert 10 chars from params = [10] term.buffer.y = 0; term.buffer.x = 70; - oldInputHandler.deleteChars([10]); - expect(lineContent(line1)).equals(Array(term.cols - 9).join('a') + ' '); - term.buffer.y = 1; - term.buffer.x = 70; inputHandler.deleteChars([10]); - expect(lineContent(line2)).equals(Array(term.cols - 9).join('a') + ' '); - expect(lineContent(line2)).equals(lineContent(line1)); + expect(line1.translateToString(false)).equals(Array(term.cols - 9).join('a') + ' '); + expect(line1.translateToString(true)).equals(Array(term.cols - 9).join('a')); }); it('eraseInLine', function(): void { const term = new Terminal(); const inputHandler = new InputHandler(term); - const oldInputHandler = new OldInputHandler(term); // fill 6 lines to test 3 different states inputHandler.parse(Array(term.cols + 1).join('a')); inputHandler.parse(Array(term.cols + 1).join('a')); inputHandler.parse(Array(term.cols + 1).join('a')); - inputHandler.parse(Array(term.cols + 1).join('a')); - inputHandler.parse(Array(term.cols + 1).join('a')); - inputHandler.parse(Array(term.cols + 1).join('a')); // params[0] - right erase term.buffer.y = 0; term.buffer.x = 70; - oldInputHandler.eraseInLine([0]); - expect(lineContent(term.buffer.lines.get(0))).equals(Array(71).join('a') + ' '); + inputHandler.eraseInLine([0]); + expect(term.buffer.lines.get(0).translateToString(false)).equals(Array(71).join('a') + ' '); + + // params[1] - left erase term.buffer.y = 1; term.buffer.x = 70; - inputHandler.eraseInLine([0]); - expect(lineContent(term.buffer.lines.get(1))).equals(Array(71).join('a') + ' '); + inputHandler.eraseInLine([1]); + expect(term.buffer.lines.get(1).translateToString(false)).equals(Array(71).join(' ') + ' aaaaaaaaa'); // params[1] - left erase term.buffer.y = 2; term.buffer.x = 70; - oldInputHandler.eraseInLine([1]); - expect(lineContent(term.buffer.lines.get(2))).equals(Array(71).join(' ') + ' aaaaaaaaa'); - term.buffer.y = 3; - term.buffer.x = 70; - inputHandler.eraseInLine([1]); - expect(lineContent(term.buffer.lines.get(3))).equals(Array(71).join(' ') + ' aaaaaaaaa'); - - // params[1] - left erase - term.buffer.y = 4; - term.buffer.x = 70; - oldInputHandler.eraseInLine([2]); - expect(lineContent(term.buffer.lines.get(4))).equals(Array(term.cols + 1).join(' ')); - term.buffer.y = 5; - term.buffer.x = 70; inputHandler.eraseInLine([2]); - expect(lineContent(term.buffer.lines.get(5))).equals(Array(term.cols + 1).join(' ')); + expect(term.buffer.lines.get(2).translateToString(false)).equals(Array(term.cols + 1).join(' ')); }); it('eraseInDisplay', function(): void { - const termOld = new Terminal(); - const inputHandlerOld = new OldInputHandler(termOld); - const termNew = new Terminal(); - const inputHandlerNew = new InputHandler(termNew); + const term = new Terminal({cols: 80, rows: 7}); + const inputHandler = new InputHandler(term); // fill display with a's - for (let i = 0; i < termOld.rows; ++i) inputHandlerOld.parse(Array(termOld.cols + 1).join('a')); - for (let i = 0; i < termNew.rows; ++i) inputHandlerNew.parse(Array(termOld.cols + 1).join('a')); - const data = []; - for (let i = 0; i < termOld.rows; ++i) data.push(Array(termOld.cols + 1).join('a')); - expect(termContent(termOld)).eql(data); - expect(termContent(termOld)).eql(termContent(termNew)); + for (let i = 0; i < term.rows; ++i) inputHandler.parse(Array(term.cols + 1).join('a')); // params [0] - right and below erase - termOld.buffer.y = 5; - termOld.buffer.x = 40; - inputHandlerOld.eraseInDisplay([0]); - termNew.buffer.y = 5; - termNew.buffer.x = 40; - inputHandlerNew.eraseInDisplay([0]); - expect(termContent(termNew)).eql(termContent(termOld)); + term.buffer.y = 5; + term.buffer.x = 40; + inputHandler.eraseInDisplay([0]); + expect(termContent(term, false)).eql([ + Array(term.cols + 1).join('a'), + Array(term.cols + 1).join('a'), + Array(term.cols + 1).join('a'), + Array(term.cols + 1).join('a'), + Array(term.cols + 1).join('a'), + Array(40 + 1).join('a') + Array(term.cols - 40 + 1).join(' '), + Array(term.cols + 1).join(' ') + ]); + expect(termContent(term, true)).eql([ + Array(term.cols + 1).join('a'), + Array(term.cols + 1).join('a'), + Array(term.cols + 1).join('a'), + Array(term.cols + 1).join('a'), + Array(term.cols + 1).join('a'), + Array(40 + 1).join('a'), + '' + ]); // reset - termOld.buffer.y = 0; - termOld.buffer.x = 0; - termNew.buffer.y = 0; - termNew.buffer.x = 0; - for (let i = 0; i < termOld.rows; ++i) inputHandlerOld.parse(Array(termOld.cols + 1).join('a')); - for (let i = 0; i < termNew.rows; ++i) inputHandlerNew.parse(Array(termOld.cols + 1).join('a')); + term.buffer.y = 0; + term.buffer.x = 0; + for (let i = 0; i < term.rows; ++i) inputHandler.parse(Array(term.cols + 1).join('a')); // params [1] - left and above - termOld.buffer.y = 5; - termOld.buffer.x = 40; - inputHandlerOld.eraseInDisplay([1]); - termNew.buffer.y = 5; - termNew.buffer.x = 40; - inputHandlerNew.eraseInDisplay([1]); - expect(termContent(termNew)).eql(termContent(termOld)); + term.buffer.y = 5; + term.buffer.x = 40; + inputHandler.eraseInDisplay([1]); + expect(termContent(term, false)).eql([ + Array(term.cols + 1).join(' '), + Array(term.cols + 1).join(' '), + Array(term.cols + 1).join(' '), + Array(term.cols + 1).join(' '), + Array(term.cols + 1).join(' '), + Array(41 + 1).join(' ') + Array(term.cols - 41 + 1).join('a'), + Array(term.cols + 1).join('a') + ]); + expect(termContent(term, true)).eql([ + '', + '', + '', + '', + '', + Array(41 + 1).join(' ') + Array(term.cols - 41 + 1).join('a'), + Array(term.cols + 1).join('a') + ]); // reset - termOld.buffer.y = 0; - termOld.buffer.x = 0; - termNew.buffer.y = 0; - termNew.buffer.x = 0; - for (let i = 0; i < termOld.rows; ++i) inputHandlerOld.parse(Array(termOld.cols + 1).join('a')); - for (let i = 0; i < termNew.rows; ++i) inputHandlerNew.parse(Array(termOld.cols + 1).join('a')); + term.buffer.y = 0; + term.buffer.x = 0; + for (let i = 0; i < term.rows; ++i) inputHandler.parse(Array(term.cols + 1).join('a')); // params [2] - whole screen - termOld.buffer.y = 5; - termOld.buffer.x = 40; - inputHandlerOld.eraseInDisplay([2]); - termNew.buffer.y = 5; - termNew.buffer.x = 40; - inputHandlerNew.eraseInDisplay([2]); - expect(termContent(termNew)).eql(termContent(termOld)); + term.buffer.y = 5; + term.buffer.x = 40; + inputHandler.eraseInDisplay([2]); + expect(termContent(term, false)).eql([ + Array(term.cols + 1).join(' '), + Array(term.cols + 1).join(' '), + Array(term.cols + 1).join(' '), + Array(term.cols + 1).join(' '), + Array(term.cols + 1).join(' '), + Array(term.cols + 1).join(' '), + Array(term.cols + 1).join(' ') + ]); + expect(termContent(term, true)).eql([ + '', + '', + '', + '', + '', + '', + '' + ]); // reset and add a wrapped line - termNew.buffer.y = 0; - termNew.buffer.x = 0; - inputHandlerNew.parse(Array(termNew.cols + 1).join('a')); // line 0 - inputHandlerNew.parse(Array(termNew.cols + 10).join('a')); // line 1 and 2 - for (let i = 3; i < termOld.rows; ++i) inputHandlerNew.parse(Array(termNew.cols + 1).join('a')); + term.buffer.y = 0; + term.buffer.x = 0; + inputHandler.parse(Array(term.cols + 1).join('a')); // line 0 + inputHandler.parse(Array(term.cols + 10).join('a')); // line 1 and 2 + for (let i = 3; i < term.rows; ++i) inputHandler.parse(Array(term.cols + 1).join('a')); // params[1] left and above with wrap // confirm precondition that line 2 is wrapped - expect(termNew.buffer.lines.get(2).isWrapped).true; - termNew.buffer.y = 2; - termNew.buffer.x = 40; - inputHandlerNew.eraseInDisplay([1]); - expect(termNew.buffer.lines.get(2).isWrapped).false; + expect(term.buffer.lines.get(2).isWrapped).true; + term.buffer.y = 2; + term.buffer.x = 40; + inputHandler.eraseInDisplay([1]); + expect(term.buffer.lines.get(2).isWrapped).false; // reset and add a wrapped line - termNew.buffer.y = 0; - termNew.buffer.x = 0; - inputHandlerNew.parse(Array(termNew.cols + 1).join('a')); // line 0 - inputHandlerNew.parse(Array(termNew.cols + 10).join('a')); // line 1 and 2 - for (let i = 3; i < termOld.rows; ++i) inputHandlerNew.parse(Array(termNew.cols + 1).join('a')); + term.buffer.y = 0; + term.buffer.x = 0; + inputHandler.parse(Array(term.cols + 1).join('a')); // line 0 + inputHandler.parse(Array(term.cols + 10).join('a')); // line 1 and 2 + for (let i = 3; i < term.rows; ++i) inputHandler.parse(Array(term.cols + 1).join('a')); // params[1] left and above with wrap // confirm precondition that line 2 is wrapped - expect(termNew.buffer.lines.get(2).isWrapped).true; - termNew.buffer.y = 1; - termNew.buffer.x = 90; // Cursor is beyond last column - inputHandlerNew.eraseInDisplay([1]); - expect(termNew.buffer.lines.get(2).isWrapped).false; + expect(term.buffer.lines.get(2).isWrapped).true; + term.buffer.y = 1; + term.buffer.x = 90; // Cursor is beyond last column + inputHandler.eraseInDisplay([1]); + expect(term.buffer.lines.get(2).isWrapped).false; }); }); it('convertEol setting', function(): void { // not converting - let s = ''; const termNotConverting = new Terminal({cols: 15, rows: 10}); (termNotConverting as any)._inputHandler.parse('Hello\nWorld'); - for (let i = 0; i < termNotConverting.cols; ++i) { - s += termNotConverting.buffer.lines.get(0).get(i)[CHAR_DATA_CHAR_INDEX] || ' '; - } - expect(s).equals('Hello '); - s = ''; - for (let i = 0; i < termNotConverting.cols; ++i) { - s += termNotConverting.buffer.lines.get(1).get(i)[CHAR_DATA_CHAR_INDEX] || ' '; - } - expect(s).equals(' World '); + expect(termNotConverting.buffer.lines.get(0).translateToString(false)).equals('Hello '); + expect(termNotConverting.buffer.lines.get(1).translateToString(false)).equals(' World '); + expect(termNotConverting.buffer.lines.get(0).translateToString(true)).equals('Hello'); + expect(termNotConverting.buffer.lines.get(1).translateToString(true)).equals(' World'); // converting - s = ''; const termConverting = new Terminal({cols: 15, rows: 10, convertEol: true}); (termConverting as any)._inputHandler.parse('Hello\nWorld'); - for (let i = 0; i < termConverting.cols; ++i) { - s += termConverting.buffer.lines.get(0).get(i)[CHAR_DATA_CHAR_INDEX] || ' '; - } - expect(s).equals('Hello '); - s = ''; - for (let i = 0; i < termConverting.cols; ++i) { - s += termConverting.buffer.lines.get(1).get(i)[CHAR_DATA_CHAR_INDEX] || ' '; - } - expect(s).equals('World '); + expect(termConverting.buffer.lines.get(0).translateToString(false)).equals('Hello '); + expect(termConverting.buffer.lines.get(1).translateToString(false)).equals('World '); + expect(termConverting.buffer.lines.get(0).translateToString(true)).equals('Hello'); + expect(termConverting.buffer.lines.get(1).translateToString(true)).equals('World'); }); describe('print', () => { it('should not cause an infinite loop (regression test)', () => { From a15b866013d3f3c165258d6ca8e8e49a564d8489 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 8 Nov 2018 05:25:12 +0100 Subject: [PATCH 11/42] test cases for getTrimLength and translateToString --- src/BufferLine.test.ts | 128 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/src/BufferLine.test.ts b/src/BufferLine.test.ts index b824cc38..93b2759f 100644 --- a/src/BufferLine.test.ts +++ b/src/BufferLine.test.ts @@ -5,7 +5,7 @@ import * as chai from 'chai'; import { BufferLine } from './BufferLine'; import { CharData, IBufferLine } from './Types'; -import { NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE } from './Buffer'; +import { NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE, DEFAULT_ATTR } from './Buffer'; class TestBufferLine extends BufferLine { @@ -200,4 +200,130 @@ describe('BufferLine', function(): void { chai.expect(line.toArray()).eql(Array(7).fill([1, 'a', 0, 'a'.charCodeAt(0)])); }); }); + describe('getTrimLength', function(): void { + it('empty line', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + chai.expect(line.getTrimmedLength()).equal(0); + }); + it('ASCII', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + line.set(0, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(2, [1, 'a', 1, 'a'.charCodeAt(0)]); + chai.expect(line.getTrimmedLength()).equal(3); + }); + it('surrogate', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + line.set(0, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(2, [1, '𝄞', 1, '𝄞'.charCodeAt(0)]); + chai.expect(line.getTrimmedLength()).equal(3); + }); + it('combining', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + line.set(0, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(2, [1, 'e\u0301', 1, '\u0301'.charCodeAt(0)]); + chai.expect(line.getTrimmedLength()).equal(3); + }); + it('fullwidth', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + line.set(0, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(2, [1, '1', 2, '1'.charCodeAt(0)]); + line.set(3, [0, '', 0, undefined]); + chai.expect(line.getTrimmedLength()).equal(4); // also counts null cell after fullwidth + }); + }); + describe('translateToString with and w\'o trimming', function(): void { + it('empty line', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + chai.expect(line.translateToString(false)).equal(' '); + chai.expect(line.translateToString(true)).equal(''); + }); + it('ASCII', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + line.set(0, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(2, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(4, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(5, [1, 'a', 1, 'a'.charCodeAt(0)]); + chai.expect(line.translateToString(false)).equal('a a aa '); + chai.expect(line.translateToString(true)).equal('a a aa'); + chai.expect(line.translateToString(false, 0, 5)).equal('a a a'); + chai.expect(line.translateToString(false, 0, 4)).equal('a a '); + chai.expect(line.translateToString(false, 0, 3)).equal('a a'); + chai.expect(line.translateToString(true, 0, 5)).equal('a a a'); + chai.expect(line.translateToString(true, 0, 4)).equal('a a '); + chai.expect(line.translateToString(true, 0, 3)).equal('a a'); + + }); + it('surrogate', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + line.set(0, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(2, [1, '𝄞', 1, '𝄞'.charCodeAt(0)]); + line.set(4, [1, '𝄞', 1, '𝄞'.charCodeAt(0)]); + line.set(5, [1, '𝄞', 1, '𝄞'.charCodeAt(0)]); + chai.expect(line.translateToString(false)).equal('a 𝄞 𝄞𝄞 '); + chai.expect(line.translateToString(true)).equal('a 𝄞 𝄞𝄞'); + chai.expect(line.translateToString(false, 0, 5)).equal('a 𝄞 𝄞'); + chai.expect(line.translateToString(false, 0, 4)).equal('a 𝄞 '); + chai.expect(line.translateToString(false, 0, 3)).equal('a 𝄞'); + chai.expect(line.translateToString(true, 0, 5)).equal('a 𝄞 𝄞'); + chai.expect(line.translateToString(true, 0, 4)).equal('a 𝄞 '); + chai.expect(line.translateToString(true, 0, 3)).equal('a 𝄞'); + }); + it('combining', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + line.set(0, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(2, [1, 'e\u0301', 1, '\u0301'.charCodeAt(0)]); + line.set(4, [1, 'e\u0301', 1, '\u0301'.charCodeAt(0)]); + line.set(5, [1, 'e\u0301', 1, '\u0301'.charCodeAt(0)]); + chai.expect(line.translateToString(false)).equal('a e\u0301 e\u0301e\u0301 '); + chai.expect(line.translateToString(true)).equal('a e\u0301 e\u0301e\u0301'); + chai.expect(line.translateToString(false, 0, 5)).equal('a e\u0301 e\u0301'); + chai.expect(line.translateToString(false, 0, 4)).equal('a e\u0301 '); + chai.expect(line.translateToString(false, 0, 3)).equal('a e\u0301'); + chai.expect(line.translateToString(true, 0, 5)).equal('a e\u0301 e\u0301'); + chai.expect(line.translateToString(true, 0, 4)).equal('a e\u0301 '); + chai.expect(line.translateToString(true, 0, 3)).equal('a e\u0301'); + }); + it('fullwidth', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + line.set(0, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(2, [1, '1', 2, '1'.charCodeAt(0)]); + line.set(3, [0, '', 0, undefined]); + line.set(5, [1, '1', 2, '1'.charCodeAt(0)]); + line.set(6, [0, '', 0, undefined]); + line.set(7, [1, '1', 2, '1'.charCodeAt(0)]); + line.set(8, [0, '', 0, undefined]); + chai.expect(line.translateToString(false)).equal('a 1 11 '); + chai.expect(line.translateToString(true)).equal('a 1 11'); + chai.expect(line.translateToString(false, 0, 7)).equal('a 1 1'); + chai.expect(line.translateToString(false, 0, 6)).equal('a 1 1'); + chai.expect(line.translateToString(false, 0, 5)).equal('a 1 '); + chai.expect(line.translateToString(false, 0, 4)).equal('a 1'); + chai.expect(line.translateToString(false, 0, 3)).equal('a 1'); + chai.expect(line.translateToString(false, 0, 2)).equal('a '); + chai.expect(line.translateToString(true, 0, 7)).equal('a 1 1'); + chai.expect(line.translateToString(true, 0, 6)).equal('a 1 1'); + chai.expect(line.translateToString(true, 0, 5)).equal('a 1 '); + chai.expect(line.translateToString(true, 0, 4)).equal('a 1'); + chai.expect(line.translateToString(true, 0, 3)).equal('a 1'); + chai.expect(line.translateToString(true, 0, 2)).equal('a '); + }); + it('space at end', function(): void { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE], false); + line.set(0, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(2, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(4, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(5, [1, 'a', 1, 'a'.charCodeAt(0)]); + line.set(6, [1, ' ', 1, ' '.charCodeAt(0)]); + chai.expect(line.translateToString(false)).equal('a a aa '); + chai.expect(line.translateToString(true)).equal('a a aa '); + }); + it('should always return some sane value', function(): void { + // sanity check - broken line with invalid out of bound null width cells + // this can atm happen with deleting/inserting chars in inputhandler by "breaking" + // fullwidth pairs --> needs to be fixed after settling BufferLine impl + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, 0, NULL_CELL_CODE], false); + chai.expect(line.translateToString(false)).equal(' '); + chai.expect(line.translateToString(true)).equal(''); + }); + }); }); From 99e4a9649f4babfc9716cc4a7ddeee5c177a56b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 29 Nov 2018 21:48:48 +0100 Subject: [PATCH 12/42] fix failing test cases --- src/InputHandler.test.ts | 28 +++++++++++----------------- src/addons/search/search.test.ts | 5 +++-- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/InputHandler.test.ts b/src/InputHandler.test.ts index bc2b10c2..85efa0ee 100644 --- a/src/InputHandler.test.ts +++ b/src/InputHandler.test.ts @@ -6,7 +6,7 @@ import { assert, expect } from 'chai'; import { InputHandler } from './InputHandler'; import { MockInputHandlingTerminal } from './utils/TestUtils.test'; -import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_ATTR_INDEX, DEFAULT_ATTR } from './Buffer'; +import { CHAR_DATA_ATTR_INDEX, DEFAULT_ATTR } from './Buffer'; import { Terminal } from './Terminal'; import { IBufferLine } from './Types'; @@ -345,34 +345,28 @@ describe('InputHandler', () => { let term: Terminal; let handler: InputHandler; - function lineContent(line: IBufferLine): string { - let content = ''; - for (let i = 0; i < line.length; ++i) content += line.get(i)[CHAR_DATA_CHAR_INDEX]; - return content; - } - beforeEach(() => { term = new Terminal(); handler = new InputHandler(term); }); it('should handle DECSET/DECRST 47 (alt screen buffer)', () => { handler.parse('\x1b[?47h\r\n\x1b[31mJUNK\x1b[?47lTEST'); - expect(lineContent(term.buffer.lines.get(0))).to.equal(Array(term.cols + 1).join(' ')); - expect(lineContent(term.buffer.lines.get(1))).to.equal(' TEST' + Array(term.cols - 7).join(' ')); + expect(term.buffer.translateBufferLineToString(0, true)).to.equal(''); + expect(term.buffer.translateBufferLineToString(1, true)).to.equal(' TEST'); // Text color of 'TEST' should be red expect((term.buffer.lines.get(1).get(4)[CHAR_DATA_ATTR_INDEX] >> 9) & 0x1ff).to.equal(1); }); it('should handle DECSET/DECRST 1047 (alt screen buffer)', () => { handler.parse('\x1b[?1047h\r\n\x1b[31mJUNK\x1b[?1047lTEST'); - expect(lineContent(term.buffer.lines.get(0))).to.equal(Array(term.cols + 1).join(' ')); - expect(lineContent(term.buffer.lines.get(1))).to.equal(' TEST' + Array(term.cols - 7).join(' ')); + expect(term.buffer.translateBufferLineToString(0, true)).to.equal(''); + expect(term.buffer.translateBufferLineToString(1, true)).to.equal(' TEST'); // Text color of 'TEST' should be red expect((term.buffer.lines.get(1).get(4)[CHAR_DATA_ATTR_INDEX] >> 9) & 0x1ff).to.equal(1); }); it('should handle DECSET/DECRST 1048 (alt screen cursor)', () => { handler.parse('\x1b[?1048h\r\n\x1b[31mJUNK\x1b[?1048lTEST'); - expect(lineContent(term.buffer.lines.get(0))).to.equal('TEST' + Array(term.cols - 3).join(' ')); - expect(lineContent(term.buffer.lines.get(1))).to.equal('JUNK' + Array(term.cols - 3).join(' ')); + expect(term.buffer.translateBufferLineToString(0, true)).to.equal('TEST'); + expect(term.buffer.translateBufferLineToString(1, true)).to.equal('JUNK'); // Text color of 'TEST' should be default expect(term.buffer.lines.get(0).get(0)[CHAR_DATA_ATTR_INDEX]).to.equal(DEFAULT_ATTR); // Text color of 'JUNK' should be red @@ -380,18 +374,18 @@ describe('InputHandler', () => { }); it('should handle DECSET/DECRST 1049 (alt screen buffer+cursor)', () => { handler.parse('\x1b[?1049h\r\n\x1b[31mJUNK\x1b[?1049lTEST'); - expect(lineContent(term.buffer.lines.get(0))).to.equal('TEST' + Array(term.cols - 3).join(' ')); - expect(lineContent(term.buffer.lines.get(1))).to.equal(Array(term.cols + 1).join(' ')); + expect(term.buffer.translateBufferLineToString(0, true)).to.equal('TEST'); + expect(term.buffer.translateBufferLineToString(1, true)).to.equal(''); // Text color of 'TEST' should be default expect(term.buffer.lines.get(0).get(0)[CHAR_DATA_ATTR_INDEX]).to.equal(DEFAULT_ATTR); }); it('should handle DECSET/DECRST 1049 - maintains saved cursor for alt buffer', () => { handler.parse('\x1b[?1049h\r\n\x1b[31m\x1b[s\x1b[?1049lTEST'); - expect(lineContent(term.buffer.lines.get(0))).to.equal('TEST' + Array(term.cols - 3).join(' ')); + expect(term.buffer.translateBufferLineToString(0, true)).to.equal('TEST'); // Text color of 'TEST' should be default expect(term.buffer.lines.get(0).get(0)[CHAR_DATA_ATTR_INDEX]).to.equal(DEFAULT_ATTR); handler.parse('\x1b[?1049h\x1b[uTEST'); - expect(lineContent(term.buffer.lines.get(1))).to.equal('TEST' + Array(term.cols - 3).join(' ')); + expect(term.buffer.translateBufferLineToString(1, true)).to.equal('TEST'); // Text color of 'TEST' should be red expect((term.buffer.lines.get(1).get(0)[CHAR_DATA_ATTR_INDEX] >> 9) & 0x1ff).to.equal(1); }); diff --git a/src/addons/search/search.test.ts b/src/addons/search/search.test.ts index 3e0b8154..ab31e1f6 100644 --- a/src/addons/search/search.test.ts +++ b/src/addons/search/search.test.ts @@ -116,10 +116,11 @@ describe('search addon', () => { expect(tilda2).eql({col: 0, row: 3, term: '~'}); }); it('should not select empty lines', () => { + // with addition of a real null char printing spaces is not considered empty anymore search.apply(MockTerminal); const term = new MockTerminal({cols: 20, rows: 3}); - term.core.write(' '); - term.pushWriteData(); + // with addition of a null char printing spaces is not considered empty anymore + // therefore we write nothing here const line = term.searchHelper.findInLine('^.*$', 0, { regex: true }); expect(line).eql(undefined); }); From 9d4beb024a506552328741392fc280b4d9c1ba90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 29 Nov 2018 22:11:38 +0100 Subject: [PATCH 13/42] keep whitespace chars --- src/Buffer.ts | 8 ++++---- src/BufferLine.ts | 14 +++++++------- src/Terminal.integration.ts | 4 ++-- src/renderer/TextRenderLayer.ts | 6 +++--- src/renderer/dom/DomRendererRowFactory.ts | 4 ++-- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index f9eed90f..07a5442c 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -17,14 +17,14 @@ export const CHAR_DATA_WIDTH_INDEX = 2; export const CHAR_DATA_CODE_INDEX = 3; export const MAX_BUFFER_SIZE = 4294967295; // 2^32 - 1 -// export const NULL_CELL_CHAR = ' '; -// export const NULL_CELL_WIDTH = 1; -// export const NULL_CELL_CODE = 32; - export const NULL_CELL_CHAR = ''; export const NULL_CELL_WIDTH = 1; export const NULL_CELL_CODE = 0; +export const WHITESPACE_CELL_CHAR = ' '; +export const WHITESPACE_CELL_WIDTH = 1; +export const WHITESPACE_CELL_CODE = 32; + /** * This class represents a terminal buffer (an internal state of the terminal), where the * following information is stored (in high-level): diff --git a/src/BufferLine.ts b/src/BufferLine.ts index 53a194c5..5a462d27 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -3,7 +3,7 @@ * @license MIT */ import { CharData, IBufferLine } from './Types'; -import { NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX } from './Buffer'; +import { NULL_CELL_CODE, NULL_CELL_WIDTH, NULL_CELL_CHAR, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, WHITESPACE_CELL_CHAR } from './Buffer'; /** * Class representing a terminal line. @@ -123,7 +123,7 @@ export class BufferLine implements IBufferLine { } let result = ''; while (startCol < length) { - result += this.get(startCol)[CHAR_DATA_CHAR_INDEX] || ' '; + result += this.get(startCol)[CHAR_DATA_CHAR_INDEX] || WHITESPACE_CELL_CHAR; startCol += this.get(startCol)[CHAR_DATA_WIDTH_INDEX] || 1; } return result; @@ -141,7 +141,7 @@ const enum Cell { } /** single vs. combined char distinction */ -const COMBINED = 0x80000000; +const IS_COMBINED_BIT_MASK = 0x80000000; /** * Typed array based bufferline implementation. @@ -177,11 +177,11 @@ export class BufferLineTypedArray implements IBufferLine { const stringData = this._data[index * CELL_SIZE + Cell.STRING]; return [ this._data[index * CELL_SIZE + Cell.FLAGS], - (stringData & COMBINED) + (stringData & IS_COMBINED_BIT_MASK) ? this._combined[index] : (stringData) ? String.fromCharCode(stringData) : '', this._data[index * CELL_SIZE + Cell.WIDTH], - (stringData & COMBINED) + (stringData & IS_COMBINED_BIT_MASK) ? this._combined[index].charCodeAt(this._combined[index].length - 1) : stringData ]; @@ -191,7 +191,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 | COMBINED; + this._data[index * CELL_SIZE + Cell.STRING] = index | IS_COMBINED_BIT_MASK; } else { this._data[index * CELL_SIZE + Cell.STRING] = value[1].charCodeAt(0); } @@ -320,7 +320,7 @@ export class BufferLineTypedArray implements IBufferLine { let result = ''; while (startCol < length) { const stringData = this._data[startCol * CELL_SIZE + Cell.STRING]; - result += (stringData & COMBINED) ? this._combined[startCol] : (stringData) ? String.fromCharCode(stringData) : ' '; + result += (stringData & IS_COMBINED_BIT_MASK) ? this._combined[startCol] : (stringData) ? String.fromCharCode(stringData) : WHITESPACE_CELL_CHAR; startCol += this._data[startCol * CELL_SIZE + Cell.WIDTH] || 1; } return result; diff --git a/src/Terminal.integration.ts b/src/Terminal.integration.ts index 21c4a2d0..d2a5cd7c 100644 --- a/src/Terminal.integration.ts +++ b/src/Terminal.integration.ts @@ -13,7 +13,7 @@ import * as path from 'path'; import * as pty from 'node-pty'; import { assert } from 'chai'; import { Terminal } from './Terminal'; -import { CHAR_DATA_CHAR_INDEX } from './Buffer'; +import { CHAR_DATA_CHAR_INDEX, WHITESPACE_CELL_CHAR } from './Buffer'; import { IViewport } from './Types'; class TestTerminal extends Terminal { @@ -67,7 +67,7 @@ function terminalToString(term: Terminal): string { for (let line = term.buffer.ybase; line < term.buffer.ybase + term.rows; line++) { lineText = ''; for (let cell = 0; cell < term.cols; ++cell) { - lineText += term.buffer.lines.get(line).get(cell)[CHAR_DATA_CHAR_INDEX] || ' '; + lineText += term.buffer.lines.get(line).get(cell)[CHAR_DATA_CHAR_INDEX] || WHITESPACE_CELL_CHAR; } // rtrim empty cells as xterm does lineText = lineText.replace(/\s+$/, ''); diff --git a/src/renderer/TextRenderLayer.ts b/src/renderer/TextRenderLayer.ts index 7ab2d238..ade2dd4c 100644 --- a/src/renderer/TextRenderLayer.ts +++ b/src/renderer/TextRenderLayer.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { CHAR_DATA_ATTR_INDEX, CHAR_DATA_CODE_INDEX, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, NULL_CELL_CODE } from '../Buffer'; +import { CHAR_DATA_ATTR_INDEX, CHAR_DATA_CODE_INDEX, CHAR_DATA_CHAR_INDEX, CHAR_DATA_WIDTH_INDEX, NULL_CELL_CODE, WHITESPACE_CELL_CHAR, WHITESPACE_CELL_CODE } from '../Buffer'; import { FLAGS, IColorSet, IRenderDimensions, ICharacterJoinerRegistry } from './Types'; import { CharData, ITerminal } from '../Types'; import { INVERTED_DEFAULT_COLOR, DEFAULT_COLOR } from './atlas/Types'; @@ -73,11 +73,11 @@ export class TextRenderLayer extends BaseRenderLayer { const joinedRanges = joinerRegistry ? joinerRegistry.getJoinedCharacters(row) : []; for (let x = 0; x < terminal.cols; x++) { const charData = line.get(x); - let code: number = charData[CHAR_DATA_CODE_INDEX] || 32; + let code: number = charData[CHAR_DATA_CODE_INDEX] || WHITESPACE_CELL_CODE; // Can either represent character(s) for a single cell or multiple cells // if indicated by a character joiner. - let chars: string = charData[CHAR_DATA_CHAR_INDEX] || ' '; + let chars: string = charData[CHAR_DATA_CHAR_INDEX] || WHITESPACE_CELL_CHAR; const attr: number = charData[CHAR_DATA_ATTR_INDEX]; let width: number = charData[CHAR_DATA_WIDTH_INDEX]; diff --git a/src/renderer/dom/DomRendererRowFactory.ts b/src/renderer/dom/DomRendererRowFactory.ts index 4a3715c5..8db1dcf8 100644 --- a/src/renderer/dom/DomRendererRowFactory.ts +++ b/src/renderer/dom/DomRendererRowFactory.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_ATTR_INDEX, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CODE_INDEX, NULL_CELL_CODE } from '../../Buffer'; +import { CHAR_DATA_CHAR_INDEX, CHAR_DATA_ATTR_INDEX, CHAR_DATA_WIDTH_INDEX, CHAR_DATA_CODE_INDEX, NULL_CELL_CODE, WHITESPACE_CELL_CHAR } from '../../Buffer'; import { FLAGS } from '../Types'; import { IBufferLine } from '../../Types'; import { DEFAULT_COLOR, INVERTED_DEFAULT_COLOR } from '../atlas/Types'; @@ -41,7 +41,7 @@ export class DomRendererRowFactory { for (let x = 0; x < lineLength; x++) { const charData = lineData.get(x); - const char: string = charData[CHAR_DATA_CHAR_INDEX] || ' '; + const char: string = charData[CHAR_DATA_CHAR_INDEX] || WHITESPACE_CELL_CHAR; const attr: number = charData[CHAR_DATA_ATTR_INDEX]; const width: number = charData[CHAR_DATA_WIDTH_INDEX]; From 376c790e1d3feb5facc6817c383b296378d4f3cd Mon Sep 17 00:00:00 2001 From: Noam Date: Fri, 30 Nov 2018 20:39:05 +0200 Subject: [PATCH 14/42] implement find multiple matches in line. start search from current selection. add unit tests. --- src/addons/search/Interfaces.ts | 7 ++- src/addons/search/SearchHelper.ts | 86 +++++++++++++++++++------------ src/addons/search/search.test.ts | 70 +++++++++++++++++++++++-- 3 files changed, 126 insertions(+), 37 deletions(-) diff --git a/src/addons/search/Interfaces.ts b/src/addons/search/Interfaces.ts index af06c5d1..e03b70c4 100644 --- a/src/addons/search/Interfaces.ts +++ b/src/addons/search/Interfaces.ts @@ -25,10 +25,13 @@ export interface ISearchOptions { regex?: boolean; wholeWord?: boolean; caseSensitive?: boolean; + reverseSearch?: boolean; } -export interface ISearchResult { - term: string; +export interface ISearchIndex { col: number; row: number; } +export interface ISearchResult extends ISearchIndex { + term: string; +} diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index 7919932a..dd055991 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult } from './Interfaces'; +import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult, ISearchIndex } from './Interfaces'; const nonWordCharacters = ' ~!@#$%^&*()+`-=[]{}|\;:"\',./<>?'; /** @@ -29,27 +29,30 @@ export class SearchHelper implements ISearchHelper { } let result: ISearchResult; - let startRow = this._terminal._core.buffer.ydisp; + let startCol: number = 0; if (this._terminal._core.selectionManager.selectionEnd) { // Start from the selection end if there is a selection if (this._terminal.getSelection().length !== 0) { startRow = this._terminal._core.selectionManager.selectionEnd[1]; + startCol = this._terminal._core.selectionManager.selectionEnd[0]; } } // Search from ydisp + 1 to end - for (let y = startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { - result = this._findInLine(term, y, searchOptions); + for (let y = startRow; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { + result = this._findInLine(term, {row: y, col: startCol}, searchOptions); if (result) { break; } + startCol = 0; } // Search from the top to the current ydisp if (!result) { for (let y = 0; y < startRow; y++) { - result = this._findInLine(term, y, searchOptions); + startCol = 0; + result = this._findInLine(term, {row: y, col: startCol}, searchOptions); if (result) { break; } @@ -72,28 +75,35 @@ export class SearchHelper implements ISearchHelper { return false; } - let result: ISearchResult; + searchOptions.reverseSearch = true; + let result: ISearchResult; let startRow = this._terminal._core.buffer.ydisp; + let startCol: number = this._terminal._core.buffer.lines.get(startRow).length; + if (this._terminal._core.selectionManager.selectionStart) { // Start from the selection end if there is a selection if (this._terminal.getSelection().length !== 0) { startRow = this._terminal._core.selectionManager.selectionStart[1]; + startCol = this._terminal._core.selectionManager.selectionStart[0]; } } // Search from ydisp + 1 to end - for (let y = startRow - 1; y >= 0; y--) { - result = this._findInLine(term, y, searchOptions); + for (let y = startRow; y >= 0; y--) { + result = this._findInLine(term, {row: y, col: startCol}, searchOptions); if (result) { break; } + startCol = y > 0 ? this._terminal._core.buffer.lines.get(y - 1).length : 0; } // Search from the top to the current ydisp if (!result) { - for (let y = this._terminal._core.buffer.ybase + this._terminal.rows - 1; y > startRow; y--) { - result = this._findInLine(term, y, searchOptions); + const searchFrom = this._terminal._core.buffer.ybase + this._terminal.rows - 1; + for (let y = searchFrom; y > startRow; y--) { + startCol = this._terminal._core.buffer.lines.get(y).length; + result = this._findInLine(term, {row: y, col: startCol}, searchOptions); if (result) { break; } @@ -125,61 +135,73 @@ export class SearchHelper implements ISearchHelper { * @param searchOptions Search options. * @return The search result if it was found. */ - protected _findInLine(term: string, y: number, searchOptions: ISearchOptions = {}): ISearchResult { - if (this._terminal._core.buffer.lines.get(y).isWrapped) { + protected _findInLine(term: string, searchIndex: ISearchIndex, searchOptions: ISearchOptions = {}): ISearchResult { + if (this._terminal._core.buffer.lines.get(searchIndex.row).isWrapped) { return; } - const stringLine = this.translateBufferLineToStringWithWrap(y, true); - const searchStringLine = searchOptions.caseSensitive ? stringLine : stringLine.toLowerCase(); + const stringLine = this.translateBufferLineToStringWithWrap(searchIndex.row, true); const searchTerm = searchOptions.caseSensitive ? term : term.toLowerCase(); - let searchIndex = -1; + const searchStringLine = searchOptions.caseSensitive ? stringLine : stringLine.toLowerCase(); + let resultIndex = -1; if (searchOptions.regex) { const searchRegex = RegExp(searchTerm, 'g'); - const foundTerm = searchRegex.exec(searchStringLine); - if (foundTerm && foundTerm[0].length > 0) { - searchIndex = searchRegex.lastIndex - foundTerm[0].length; - term = foundTerm[0]; + let foundTerm: RegExpExecArray; + if (searchOptions.reverseSearch) { + while (foundTerm = searchRegex.exec(searchStringLine.slice(0, searchIndex.col))) { + resultIndex = searchRegex.lastIndex - foundTerm[0].length; + term = foundTerm[0]; + searchRegex.lastIndex -= (term.length - 1); + } + } else { + foundTerm = searchRegex.exec(searchStringLine.slice(searchIndex.col)); + if (foundTerm && foundTerm[0].length > 0) { + resultIndex = searchIndex.col + (searchRegex.lastIndex - foundTerm[0].length); + term = foundTerm[0]; + } } } else { - searchIndex = searchStringLine.indexOf(searchTerm); + if (searchOptions.reverseSearch) { + resultIndex = searchStringLine.lastIndexOf(searchTerm, searchIndex.col - searchTerm.length); + } else { + resultIndex = searchStringLine.indexOf(searchTerm, searchIndex.col); + } } - if (searchIndex >= 0) { + if (resultIndex >= 0) { // Adjust the row number and search index if needed since a "line" of text can span multiple rows - if (searchIndex >= this._terminal.cols) { - y += Math.floor(searchIndex / this._terminal.cols); - searchIndex = searchIndex % this._terminal.cols; + if (resultIndex >= this._terminal.cols) { + searchIndex.row += Math.floor(resultIndex / this._terminal.cols); + resultIndex = resultIndex % this._terminal.cols; } - if (searchOptions.wholeWord && !this._isWholeWord(searchIndex, searchStringLine, term)) { + if (searchOptions.wholeWord && !this._isWholeWord(resultIndex, searchStringLine, term)) { return; } - const line = this._terminal._core.buffer.lines.get(y); + const line = this._terminal._core.buffer.lines.get(searchIndex.row); - for (let i = 0; i < searchIndex; i++) { + for (let i = 0; i < resultIndex; i++) { const charData = line.get(i); // Adjust the searchIndex to normalize emoji into single chars const char = charData[1/*CHAR_DATA_CHAR_INDEX*/]; if (char.length > 1) { - searchIndex -= char.length - 1; + resultIndex -= char.length - 1; } // Adjust the searchIndex for empty characters following wide unicode // chars (eg. CJK) const charWidth = charData[2/*CHAR_DATA_WIDTH_INDEX*/]; if (charWidth === 0) { - searchIndex++; + resultIndex++; } } return { term, - col: searchIndex, - row: y + col: resultIndex, + row: searchIndex.row }; } } - /** * Translates a buffer line to a string, including subsequent lines if they are wraps. * Wide characters will count as two columns in the resulting string. This diff --git a/src/addons/search/search.test.ts b/src/addons/search/search.test.ts index 3e0b8154..be9ec479 100644 --- a/src/addons/search/search.test.ts +++ b/src/addons/search/search.test.ts @@ -7,7 +7,7 @@ declare var require: any; import { assert, expect } from 'chai'; import * as search from './search'; import { SearchHelper } from './SearchHelper'; -import { ISearchOptions, ISearchResult } from './Interfaces'; +import { ISearchOptions, ISearchResult, ISearchIndex } from './Interfaces'; class MockTerminalPlain {} @@ -29,8 +29,11 @@ class MockTerminal { } class TestSearchHelper extends SearchHelper { - public findInLine(term: string, y: number, searchOptions?: ISearchOptions): ISearchResult { - return this._findInLine(term, y, searchOptions); + public findInLine(term: string, rowNumber: number, searchOptions?: ISearchOptions): ISearchResult { + return this._findInLine(term, {row: rowNumber, col: 0}, searchOptions); + } + public findFromIndex(term: string, searchIndex: ISearchIndex, searchOptions?: ISearchOptions): ISearchResult { + return this._findInLine(term, searchIndex, searchOptions); } } @@ -247,5 +250,66 @@ describe('search addon', () => { expect(hello4).eql(undefined); expect(hello5).eql(undefined); }); + it('should find multiple matches in line', function(): void { + search.apply(MockTerminal); + const term = new MockTerminal({cols: 20, rows: 5}); + term.core.write('helloooo helloooo\r\naaaAAaaAAA'); + term.pushWriteData(); + const searchOptions = { + regex: false, + wholeWord: false, + caseSensitive: false + }; + const find0 = term.searchHelper.findFromIndex('hello', {row: 0, col: 0}, searchOptions); + const find1 = term.searchHelper.findFromIndex('hello', {row: 0, col: find0.col + find0.term.length}, searchOptions); + const find2 = term.searchHelper.findFromIndex('aaaa', {row: 1, col: 0}, searchOptions); + const find3 = term.searchHelper.findFromIndex('aaaa', {row: 1, col: find2.col + find2.term.length}, searchOptions); + const find4 = term.searchHelper.findFromIndex('aaaa', {row: 1, col: find3.col + find3.term.length}, searchOptions); + expect(find0).eql({col: 0, row: 0, term: 'hello'}); + expect(find1).eql({col: 9, row: 0, term: 'hello'}); + expect(find2).eql({col: 0, row: 1, term: 'aaaa'}); + expect(find3).eql({col: 4, row: 1, term: 'aaaa'}); + expect(find4).eql(undefined); + }); + it('should find multiple matches in line - reverse search', function(): void { + search.apply(MockTerminal); + const term = new MockTerminal({cols: 20, rows: 5}); + term.core.write('it is what it is'); + term.pushWriteData(); + const searchOptions = { + regex: false, + wholeWord: false, + caseSensitive: false, + reverseSearch: true + }; + const find0 = term.searchHelper.findFromIndex('is', {row: 0, col: 16}, searchOptions); + const find1 = term.searchHelper.findFromIndex('is', {row: 0, col: find0.col}, searchOptions); + const find2 = term.searchHelper.findFromIndex('it', {row: 0, col: 16}, searchOptions); + const find3 = term.searchHelper.findFromIndex('it', {row: 0, col: find2.col}, searchOptions); + expect(find0).eql({col: 14, row: 0, term: 'is'}); + expect(find1).eql({col: 3, row: 0, term: 'is'}); + expect(find2).eql({col: 11, row: 0, term: 'it'}); + expect(find3).eql({col: 0, row: 0, term: 'it'}); + }); + it('should find multiple matches in line - reverse search with regex', function(): void { + search.apply(MockTerminal); + const term = new MockTerminal({cols: 20, rows: 5}); + term.core.write('zzzABCzzzzABCABC'); + term.pushWriteData(); + const searchOptions = { + regex: true, + wholeWord: false, + caseSensitive: true, + reverseSearch: true + }; + const find0 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: 16}, searchOptions); + const find1 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find0.col}, searchOptions); + const find2 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find1.col}, searchOptions); + const find3 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find2.col}, searchOptions); + expect(find0).eql({col: 13, row: 0, term: 'ABC'}); + expect(find1).eql({col: 10, row: 0, term: 'ABC'}); + expect(find2).eql({col: 3, row: 0, term: 'ABC'}); + expect(find3).eql(undefined); + }); }); }); From 278d696709b799c20481107ebf2a6ff53ae0764b Mon Sep 17 00:00:00 2001 From: Noam Date: Fri, 7 Dec 2018 22:56:57 +0200 Subject: [PATCH 15/42] remove reverseSearch from ISearchOptions add isReverseSearch argument to findInLine modify unit tests --- src/addons/search/Interfaces.ts | 2 +- src/addons/search/SearchHelper.ts | 15 ++++++--------- src/addons/search/search.test.ts | 28 ++++++++++++++-------------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/addons/search/Interfaces.ts b/src/addons/search/Interfaces.ts index e03b70c4..e15224d1 100644 --- a/src/addons/search/Interfaces.ts +++ b/src/addons/search/Interfaces.ts @@ -25,13 +25,13 @@ export interface ISearchOptions { regex?: boolean; wholeWord?: boolean; caseSensitive?: boolean; - reverseSearch?: boolean; } export interface ISearchIndex { col: number; row: number; } + export interface ISearchResult extends ISearchIndex { term: string; } diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index dd055991..5052a07d 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -5,7 +5,6 @@ import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult, ISearchIndex } from './Interfaces'; const nonWordCharacters = ' ~!@#$%^&*()+`-=[]{}|\;:"\',./<>?'; - /** * A class that knows how to search the terminal and how to display the results. */ @@ -74,9 +73,7 @@ export class SearchHelper implements ISearchHelper { if (!term || term.length === 0) { return false; } - - searchOptions.reverseSearch = true; - + const isReverseSearch = true; let result: ISearchResult; let startRow = this._terminal._core.buffer.ydisp; let startCol: number = this._terminal._core.buffer.lines.get(startRow).length; @@ -91,7 +88,7 @@ export class SearchHelper implements ISearchHelper { // Search from ydisp + 1 to end for (let y = startRow; y >= 0; y--) { - result = this._findInLine(term, {row: y, col: startCol}, searchOptions); + result = this._findInLine(term, {row: y, col: startCol}, searchOptions, isReverseSearch); if (result) { break; } @@ -103,7 +100,7 @@ export class SearchHelper implements ISearchHelper { const searchFrom = this._terminal._core.buffer.ybase + this._terminal.rows - 1; for (let y = searchFrom; y > startRow; y--) { startCol = this._terminal._core.buffer.lines.get(y).length; - result = this._findInLine(term, {row: y, col: startCol}, searchOptions); + result = this._findInLine(term, {row: y, col: startCol}, searchOptions, isReverseSearch); if (result) { break; } @@ -135,7 +132,7 @@ export class SearchHelper implements ISearchHelper { * @param searchOptions Search options. * @return The search result if it was found. */ - protected _findInLine(term: string, searchIndex: ISearchIndex, searchOptions: ISearchOptions = {}): ISearchResult { + protected _findInLine(term: string, searchIndex: ISearchIndex, searchOptions: ISearchOptions = {}, isReverseSearch: boolean = false): ISearchResult { if (this._terminal._core.buffer.lines.get(searchIndex.row).isWrapped) { return; } @@ -148,7 +145,7 @@ export class SearchHelper implements ISearchHelper { if (searchOptions.regex) { const searchRegex = RegExp(searchTerm, 'g'); let foundTerm: RegExpExecArray; - if (searchOptions.reverseSearch) { + if (isReverseSearch) { while (foundTerm = searchRegex.exec(searchStringLine.slice(0, searchIndex.col))) { resultIndex = searchRegex.lastIndex - foundTerm[0].length; term = foundTerm[0]; @@ -162,7 +159,7 @@ export class SearchHelper implements ISearchHelper { } } } else { - if (searchOptions.reverseSearch) { + if (isReverseSearch) { resultIndex = searchStringLine.lastIndexOf(searchTerm, searchIndex.col - searchTerm.length); } else { resultIndex = searchStringLine.indexOf(searchTerm, searchIndex.col); diff --git a/src/addons/search/search.test.ts b/src/addons/search/search.test.ts index be9ec479..0a38f755 100644 --- a/src/addons/search/search.test.ts +++ b/src/addons/search/search.test.ts @@ -32,8 +32,8 @@ class TestSearchHelper extends SearchHelper { public findInLine(term: string, rowNumber: number, searchOptions?: ISearchOptions): ISearchResult { return this._findInLine(term, {row: rowNumber, col: 0}, searchOptions); } - public findFromIndex(term: string, searchIndex: ISearchIndex, searchOptions?: ISearchOptions): ISearchResult { - return this._findInLine(term, searchIndex, searchOptions); + public findFromIndex(term: string, searchIndex: ISearchIndex, searchOptions?: ISearchOptions, isReverseSearch?: boolean): ISearchResult { + return this._findInLine(term, searchIndex, searchOptions, isReverseSearch); } } @@ -279,13 +279,13 @@ describe('search addon', () => { const searchOptions = { regex: false, wholeWord: false, - caseSensitive: false, - reverseSearch: true + caseSensitive: false }; - const find0 = term.searchHelper.findFromIndex('is', {row: 0, col: 16}, searchOptions); - const find1 = term.searchHelper.findFromIndex('is', {row: 0, col: find0.col}, searchOptions); - const find2 = term.searchHelper.findFromIndex('it', {row: 0, col: 16}, searchOptions); - const find3 = term.searchHelper.findFromIndex('it', {row: 0, col: find2.col}, searchOptions); + const isReverseSearch = true; + const find0 = term.searchHelper.findFromIndex('is', {row: 0, col: 16}, searchOptions, isReverseSearch); + const find1 = term.searchHelper.findFromIndex('is', {row: 0, col: find0.col}, searchOptions, isReverseSearch); + const find2 = term.searchHelper.findFromIndex('it', {row: 0, col: 16}, searchOptions, isReverseSearch); + const find3 = term.searchHelper.findFromIndex('it', {row: 0, col: find2.col}, searchOptions, isReverseSearch); expect(find0).eql({col: 14, row: 0, term: 'is'}); expect(find1).eql({col: 3, row: 0, term: 'is'}); expect(find2).eql({col: 11, row: 0, term: 'it'}); @@ -299,13 +299,13 @@ describe('search addon', () => { const searchOptions = { regex: true, wholeWord: false, - caseSensitive: true, - reverseSearch: true + caseSensitive: true }; - const find0 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: 16}, searchOptions); - const find1 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find0.col}, searchOptions); - const find2 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find1.col}, searchOptions); - const find3 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find2.col}, searchOptions); + const isReverseSearch = true; + const find0 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: 16}, searchOptions, isReverseSearch); + const find1 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find0.col}, searchOptions, isReverseSearch); + const find2 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find1.col}, searchOptions, isReverseSearch); + const find3 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find2.col}, searchOptions, isReverseSearch); expect(find0).eql({col: 13, row: 0, term: 'ABC'}); expect(find1).eql({col: 10, row: 0, term: 'ABC'}); expect(find2).eql({col: 3, row: 0, term: 'ABC'}); From 91ef7f24a60d6ac59c7a425fe7e72a5dc5624ea6 Mon Sep 17 00:00:00 2001 From: Noj Vek Date: Mon, 10 Dec 2018 15:54:35 -0800 Subject: [PATCH 16/42] Fixes #1660: Search as you type --- demo/client.ts | 38 ++++++++++++++---------------- src/addons/search/Interfaces.ts | 2 ++ src/addons/search/SearchHelper.ts | 39 +++++++++++++++++++------------ 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/demo/client.ts b/demo/client.ts index d5196d37..7fd2a894 100644 --- a/demo/client.ts +++ b/demo/client.ts @@ -14,6 +14,7 @@ import * as fullscreen from '../lib/addons/fullscreen/fullscreen'; import * as search from '../lib/addons/search/search'; import * as webLinks from '../lib/addons/webLinks/webLinks'; import * as winptyCompat from '../lib/addons/winptyCompat/winptyCompat'; +import { ISearchOptions } from '../lib/addons/search/Interfaces'; // Pulling in the module's types relies on the above, it's looks a // little weird here as we're importing "this" module @@ -50,6 +51,14 @@ function setPadding(): void { term.fit(); } +function getSearchOptions(): ISearchOptions { + return { + regex: (document.getElementById('regex') as HTMLInputElement).checked, + wholeWord: (document.getElementById('whole-word') as HTMLInputElement).checked, + caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked, + }; +} + createTerminal(); const disposeRecreateButtonHandler = () => { @@ -97,27 +106,16 @@ function createTerminal(): void { addDomListener(paddingElement, 'change', setPadding); - addDomListener(actionElements.findNext, 'keypress', (e) => { - if (e.key === 'Enter') { - e.preventDefault(); - const searchOptions = { - regex: (document.getElementById('regex') as HTMLInputElement).checked, - wholeWord: (document.getElementById('whole-word') as HTMLInputElement).checked, - caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked - }; - term.findNext(actionElements.findNext.value, searchOptions); - } + addDomListener(actionElements.findNext, 'keyup', (e) => { + const searchOptions = getSearchOptions(); + searchOptions.incremental = e.key !== `Enter`; + term.findNext(actionElements.findNext.value, searchOptions); }); - addDomListener(actionElements.findPrevious, 'keypress', (e) => { - if (e.key === 'Enter') { - e.preventDefault(); - const searchOptions = { - regex: (document.getElementById('regex') as HTMLInputElement).checked, - wholeWord: (document.getElementById('whole-word') as HTMLInputElement).checked, - caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked - }; - term.findPrevious(actionElements.findPrevious.value, searchOptions); - } + + addDomListener(actionElements.findPrevious, 'keyup', (e) => { + const searchOptions = getSearchOptions(); + searchOptions.incremental = e.key !== `Enter`; + term.findPrevious(actionElements.findPrevious.value, searchOptions); }); // fit is called within a setTimeout, cols and rows need this. diff --git a/src/addons/search/Interfaces.ts b/src/addons/search/Interfaces.ts index af06c5d1..96788120 100644 --- a/src/addons/search/Interfaces.ts +++ b/src/addons/search/Interfaces.ts @@ -25,6 +25,8 @@ export interface ISearchOptions { regex?: boolean; wholeWord?: boolean; caseSensitive?: boolean; + /** Assume caller implements 'search as you type' where findNext gets called when search input changes */ + incremental?: boolean; } export interface ISearchResult { diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index 7919932a..d5ec0b48 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -24,29 +24,34 @@ export class SearchHelper implements ISearchHelper { * @return Whether a result was found. */ public findNext(term: string, searchOptions?: ISearchOptions): boolean { + const selectionManager = this._terminal._core.selectionManager; + const {incremental} = searchOptions; + let result: ISearchResult; + if (!term || term.length === 0) { + selectionManager.clearSelection(); return false; } - let result: ISearchResult; - let startRow = this._terminal._core.buffer.ydisp; - if (this._terminal._core.selectionManager.selectionEnd) { + + if (selectionManager.selectionEnd) { // Start from the selection end if there is a selection + // For incremental search, use existing row if (this._terminal.getSelection().length !== 0) { - startRow = this._terminal._core.selectionManager.selectionEnd[1]; + startRow = incremental ? selectionManager.selectionStart[1] : selectionManager.selectionEnd[1]; } } - // Search from ydisp + 1 to end - for (let y = startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { + // Search from startRow to end + for (let y = incremental ? startRow: startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { result = this._findInLine(term, y, searchOptions); if (result) { break; } } - // Search from the top to the current ydisp + // Search from the top to the startRow if (!result) { for (let y = 0; y < startRow; y++) { result = this._findInLine(term, y, searchOptions); @@ -68,29 +73,33 @@ export class SearchHelper implements ISearchHelper { * @return Whether a result was found. */ public findPrevious(term: string, searchOptions?: ISearchOptions): boolean { + const selectionManager = this._terminal._core.selectionManager; + const {incremental} = searchOptions; + let result: ISearchResult; + if (!term || term.length === 0) { + selectionManager.clearSelection(); return false; } - let result: ISearchResult; - let startRow = this._terminal._core.buffer.ydisp; - if (this._terminal._core.selectionManager.selectionStart) { - // Start from the selection end if there is a selection + + if (selectionManager.selectionStart) { + // Start from the selection start if there is a selection if (this._terminal.getSelection().length !== 0) { - startRow = this._terminal._core.selectionManager.selectionStart[1]; + startRow = selectionManager.selectionStart[1]; } } - // Search from ydisp + 1 to end - for (let y = startRow - 1; y >= 0; y--) { + // Search from startRow to top + for (let y = incremental ? startRow : startRow - 1; y >= 0; y--) { result = this._findInLine(term, y, searchOptions); if (result) { break; } } - // Search from the top to the current ydisp + // Search from the bottom to startRow if (!result) { for (let y = this._terminal._core.buffer.ybase + this._terminal.rows - 1; y > startRow; y--) { result = this._findInLine(term, y, searchOptions); From 9005de1c42cc4cc3684aa87e7222efee4209b5d5 Mon Sep 17 00:00:00 2001 From: Noj Vek Date: Mon, 10 Dec 2018 17:13:28 -0800 Subject: [PATCH 17/42] adding a linesCache ttl and cursor move invalidation --- demo/client.ts | 2 +- src/addons/search/SearchHelper.ts | 55 ++++++++++++++++++++++++++----- src/addons/search/tsconfig.json | 1 + 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/demo/client.ts b/demo/client.ts index 700fa38d..d99ff66d 100644 --- a/demo/client.ts +++ b/demo/client.ts @@ -55,7 +55,7 @@ function getSearchOptions(): ISearchOptions { return { regex: (document.getElementById('regex') as HTMLInputElement).checked, wholeWord: (document.getElementById('whole-word') as HTMLInputElement).checked, - caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked, + caseSensitive: (document.getElementById('case-sensitive') as HTMLInputElement).checked }; } diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index d5ec0b48..cfb29c8b 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -4,16 +4,24 @@ */ import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult } from './Interfaces'; -const nonWordCharacters = ' ~!@#$%^&*()+`-=[]{}|\;:"\',./<>?'; + +const NON_WORD_CHARACTERS = ' ~!@#$%^&*()+`-=[]{}|\;:"\',./<>?'; +const LINES_CACHE_TIME_TO_LIVE = 15 * 1000; // 15 secs /** * A class that knows how to search the terminal and how to display the results. */ export class SearchHelper implements ISearchHelper { + /** + * translateBufferLineToStringWithWrap is a fairly expensive call. + * We memoize the calls into an array that has a time based ttl. + * _linesCache is also invalidated when the terminal cursor moves. + */ + private _linesCache: string[] = null; + private _linesCacheTimeoutId = 0; + constructor(private _terminal: ISearchAddonTerminal) { - // TODO: Search for multiple instances on 1 line - // TODO: Don't use the actual selection, instead use a "find selection" so multiple instances can be highlighted - // TODO: Highlight other instances in the viewport + this._destroyLinesCache = this._destroyLinesCache.bind(this); } /** @@ -43,8 +51,10 @@ export class SearchHelper implements ISearchHelper { } } + this._initLinesCache(); + // Search from startRow to end - for (let y = incremental ? startRow: startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { + for (let y = incremental ? startRow : startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { result = this._findInLine(term, y, searchOptions); if (result) { break; @@ -91,6 +101,8 @@ export class SearchHelper implements ISearchHelper { } } + this._initLinesCache(); + // Search from startRow to top for (let y = incremental ? startRow : startRow - 1; y >= 0; y--) { result = this._findInLine(term, y, searchOptions); @@ -113,6 +125,28 @@ export class SearchHelper implements ISearchHelper { return this._selectResult(result); } + /** + * Sets up a line cache with a ttl + */ + private _initLinesCache(): void { + if (!this._linesCache) { + this._linesCache = new Array(this._terminal._core.buffer.length); + this._terminal.on('cursormove', this._destroyLinesCache); + } + + window.clearTimeout(this._linesCacheTimeoutId); + this._linesCacheTimeoutId = window.setTimeout(() => this._destroyLinesCache(), LINES_CACHE_TIME_TO_LIVE); + } + + private _destroyLinesCache(): void { + this._linesCache = null; + this._terminal.off('cursormove', this._destroyLinesCache); + if (this._linesCacheTimeoutId) { + window.clearTimeout(this._linesCacheTimeoutId); + this._linesCacheTimeoutId = 0; + } + } + /** * A found substring is a whole word if it doesn't have an alphanumeric character directly adjacent to it. * @param searchIndex starting indext of the potential whole word substring @@ -120,8 +154,8 @@ export class SearchHelper implements ISearchHelper { * @param term the substring that starts at searchIndex */ private _isWholeWord(searchIndex: number, line: string, term: string): boolean { - return (((searchIndex === 0) || (nonWordCharacters.indexOf(line[searchIndex - 1]) !== -1)) && - (((searchIndex + term.length) === line.length) || (nonWordCharacters.indexOf(line[searchIndex + term.length]) !== -1))); + return (((searchIndex === 0) || (NON_WORD_CHARACTERS.indexOf(line[searchIndex - 1]) !== -1)) && + (((searchIndex + term.length) === line.length) || (NON_WORD_CHARACTERS.indexOf(line[searchIndex + term.length]) !== -1))); } /** @@ -139,7 +173,12 @@ export class SearchHelper implements ISearchHelper { return; } - const stringLine = this.translateBufferLineToStringWithWrap(y, true); + let stringLine = this._linesCache[y]; + if (stringLine === void 0) { + stringLine = this.translateBufferLineToStringWithWrap(y, true); + this._linesCache[y] = stringLine; + } + const searchStringLine = searchOptions.caseSensitive ? stringLine : stringLine.toLowerCase(); const searchTerm = searchOptions.caseSensitive ? term : term.toLowerCase(); let searchIndex = -1; diff --git a/src/addons/search/tsconfig.json b/src/addons/search/tsconfig.json index c34a0bc5..e7a1ff3d 100644 --- a/src/addons/search/tsconfig.json +++ b/src/addons/search/tsconfig.json @@ -3,6 +3,7 @@ "module": "commonjs", "target": "es5", "lib": [ + "dom", "es5" ], "rootDir": ".", From a0d583b075e454ef9f2f2fe3d4c08ed4c47f664c Mon Sep 17 00:00:00 2001 From: Noj Vek Date: Mon, 10 Dec 2018 17:23:28 -0800 Subject: [PATCH 18/42] handle non-cached scenario in tests land --- src/addons/search/SearchHelper.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index cfb29c8b..8e6e4e12 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -173,10 +173,12 @@ export class SearchHelper implements ISearchHelper { return; } - let stringLine = this._linesCache[y]; + let stringLine = this._linesCache ? this._linesCache[y] : void 0; if (stringLine === void 0) { stringLine = this.translateBufferLineToStringWithWrap(y, true); - this._linesCache[y] = stringLine; + if (this._linesCache) { + this._linesCache[y] = stringLine; + } } const searchStringLine = searchOptions.caseSensitive ? stringLine : stringLine.toLowerCase(); From 62be3047d8430fa424ed877fde370fa067ff124e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Sat, 15 Dec 2018 00:01:18 +0100 Subject: [PATCH 19/42] infer types fix --- src/renderer/dom/DomRendererRowFactory.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/renderer/dom/DomRendererRowFactory.ts b/src/renderer/dom/DomRendererRowFactory.ts index 8db1dcf8..8bcde39a 100644 --- a/src/renderer/dom/DomRendererRowFactory.ts +++ b/src/renderer/dom/DomRendererRowFactory.ts @@ -41,9 +41,9 @@ export class DomRendererRowFactory { for (let x = 0; x < lineLength; x++) { const charData = lineData.get(x); - const char: string = charData[CHAR_DATA_CHAR_INDEX] || WHITESPACE_CELL_CHAR; - const attr: number = charData[CHAR_DATA_ATTR_INDEX]; - const width: number = charData[CHAR_DATA_WIDTH_INDEX]; + const char = charData[CHAR_DATA_CHAR_INDEX] || WHITESPACE_CELL_CHAR; + const attr = charData[CHAR_DATA_ATTR_INDEX]; + const width = charData[CHAR_DATA_WIDTH_INDEX]; // The character to the left is a wide character, drawing is owned by the char at x-1 if (width === 0) { From 3927364f0ec9565e624c4aa6c902397c6eaccf38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Sat, 15 Dec 2018 00:03:54 +0100 Subject: [PATCH 20/42] remove nonsense comments --- src/addons/search/search.test.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/addons/search/search.test.ts b/src/addons/search/search.test.ts index ab31e1f6..a70fd115 100644 --- a/src/addons/search/search.test.ts +++ b/src/addons/search/search.test.ts @@ -116,11 +116,8 @@ describe('search addon', () => { expect(tilda2).eql({col: 0, row: 3, term: '~'}); }); it('should not select empty lines', () => { - // with addition of a real null char printing spaces is not considered empty anymore search.apply(MockTerminal); const term = new MockTerminal({cols: 20, rows: 3}); - // with addition of a null char printing spaces is not considered empty anymore - // therefore we write nothing here const line = term.searchHelper.findInLine('^.*$', 0, { regex: true }); expect(line).eql(undefined); }); From 5460068d52154726fda3179fbbe53fa615662ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Mon, 17 Dec 2018 23:59:48 +0100 Subject: [PATCH 21/42] fix invalid string and buffer index --- src/Buffer.test.ts | 33 +++++++++++++++++++++++++++++++-- src/Buffer.ts | 2 +- src/Linkifier.ts | 8 ++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 6b6adc11..5c1e56c9 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -355,7 +355,7 @@ describe('Buffer', () => { let terminal: TestTerminal; beforeEach(() => { - terminal = new TestTerminal({rows: 5, cols: 10}); + terminal = new TestTerminal({rows: 5, cols: 10, scrollback: 5}); }); it('multiline ascii', () => { @@ -516,9 +516,38 @@ describe('Buffer', () => { assert.deepEqual([(j / terminal.cols) | 0, j % terminal.cols], bufferIndex); } }); + + it('test fully wrapped buffer up to last char', () => { + const input = Array(6).join('1234567890'); + terminal.writeSync(input); + const s = terminal.buffer.iterator(true).next().content; + assert.equal(input, s); + for (let i = 0; i < input.length; ++i) { + const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i); + assert.equal(input[i], terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]); + } + }); + + it('test fully wrapped buffer up to last char with full width odd', () => { + const input = 'a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301' + + 'a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301a¥\u0301'; + terminal.writeSync(input); + const s = terminal.buffer.iterator(true).next().content; + assert.equal(input, s); + for (let i = 0; i < input.length; ++i) { + const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i); + assert.equal( + (!(i % 3)) + ? input[i] + : (i % 3 === 1) + ? input.substr(i, 2) + : input.substr(i-1, 2), + terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]); + } + }); }); describe('BufferStringIterator', function(): void { - it('iterator does not ovrflow buffer limits', function(): void { + it('iterator does not overflow buffer limits', function(): void { const terminal = new TestTerminal({rows: 5, cols: 10, scrollback: 5}); const data = [ 'aaaaaaaaaa', diff --git a/src/Buffer.ts b/src/Buffer.ts index 10f1d1b2..df4134ee 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -252,7 +252,7 @@ export class Buffer implements IBuffer { while (stringIndex) { const line = this.lines.get(lineIndex); if (!line) { - [-1, -1]; + return [-1, -1]; } for (let i = 0; i < line.length; ++i) { stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length; diff --git a/src/Linkifier.ts b/src/Linkifier.ts index 2ec3ed40..53247c95 100644 --- a/src/Linkifier.ts +++ b/src/Linkifier.ts @@ -219,9 +219,17 @@ export class Linkifier extends EventEmitter implements ILinkifier { // also correct regex and string search offsets for the next loop run stringIndex = text.indexOf(uri, stringIndex + 1); rex.lastIndex = stringIndex + uri.length; + if (stringIndex < 0) { + // invalid stringIndex (should not have happened) + break; + } // get the buffer index as [absolute row, col] for the match const bufferIndex = this._terminal.buffer.stringIndexToBufferIndex(rowIndex, stringIndex); + if (bufferIndex[0] < 0) { + // invalid bufferIndex (should not have happened) + break; + } const line = this._terminal.buffer.lines.get(bufferIndex[0]); const char = line.get(bufferIndex[1]); From 0da630010279cbebd4363adb44991ba7c2652373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Tue, 18 Dec 2018 10:50:30 +0100 Subject: [PATCH 22/42] make linter happy --- src/Buffer.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 5c1e56c9..0546dfe8 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -541,7 +541,7 @@ describe('Buffer', () => { ? input[i] : (i % 3 === 1) ? input.substr(i, 2) - : input.substr(i-1, 2), + : input.substr(i - 1, 2), terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]); } }); From 04ac36fc1611a35ae22dea7ae1a9db4d39a0adeb Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Tue, 18 Dec 2018 16:22:41 -0800 Subject: [PATCH 23/42] Fix translateToString endCol default arg --- src/Buffer.ts | 2 +- src/BufferLine.test.ts | 5 +++++ src/BufferLine.ts | 14 ++++++-------- src/SelectionManager.ts | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index df4134ee..74750a8b 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -275,7 +275,7 @@ export class Buffer implements IBuffer { * @param startCol The column to start at. * @param endCol The column to end at. */ - public translateBufferLineToString(lineIndex: number, trimRight: boolean, startCol: number = 0, endCol: number = null): string { + public translateBufferLineToString(lineIndex: number, trimRight: boolean, startCol: number = 0, endCol?: number): string { const line = this.lines.get(lineIndex); if (!line) { return ''; diff --git a/src/BufferLine.test.ts b/src/BufferLine.test.ts index 93b2759f..fbf8b051 100644 --- a/src/BufferLine.test.ts +++ b/src/BufferLine.test.ts @@ -325,5 +325,10 @@ describe('BufferLine', function(): void { chai.expect(line.translateToString(false)).equal(' '); chai.expect(line.translateToString(true)).equal(''); }); + it('should work with endCol=0', () => { + const line = new TestBufferLine(10, [DEFAULT_ATTR, NULL_CELL_CHAR, 0, NULL_CELL_CODE], false); + line.set(0, [1, 'a', 1, 'a'.charCodeAt(0)]); + chai.expect(line.translateToString(true, 0, 0)).equal(''); + }); }); }); diff --git a/src/BufferLine.ts b/src/BufferLine.ts index bcc3d1bb..3f93af62 100644 --- a/src/BufferLine.ts +++ b/src/BufferLine.ts @@ -118,13 +118,12 @@ export class BufferLineJSArray implements IBufferLine { return 0; } - public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = null): string { - let length = endCol || this.length; + public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = this.length): string { if (trimRight) { - length = Math.min(length, this.getTrimmedLength()); + endCol = Math.min(endCol, this.getTrimmedLength()); } let result = ''; - while (startCol < length) { + while (startCol < endCol) { result += this.get(startCol)[CHAR_DATA_CHAR_INDEX] || WHITESPACE_CELL_CHAR; startCol += this.get(startCol)[CHAR_DATA_WIDTH_INDEX] || 1; } @@ -305,13 +304,12 @@ export class BufferLine implements IBufferLine { return 0; } - public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = null): string { - let length = endCol || this.length; + public translateToString(trimRight: boolean = false, startCol: number = 0, endCol: number = this.length): string { if (trimRight) { - length = Math.min(length, this.getTrimmedLength()); + endCol = Math.min(endCol, this.getTrimmedLength()); } let result = ''; - while (startCol < length) { + while (startCol < endCol) { const stringData = this._data[startCol * CELL_SIZE + Cell.STRING]; result += (stringData & IS_COMBINED_BIT_MASK) ? this._combined[startCol] : (stringData) ? String.fromCharCode(stringData) : WHITESPACE_CELL_CHAR; startCol += this._data[startCol * CELL_SIZE + Cell.WIDTH] || 1; diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 4bac0400..1aea1cb5 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -198,7 +198,7 @@ export class SelectionManager extends EventEmitter implements ISelectionManager } } else { // Get first row - const startRowEndCol = start[1] === end[1] ? end[0] : null; + const startRowEndCol = start[1] === end[1] ? end[0] : undefined; result.push(this._buffer.translateBufferLineToString(start[1], true, start[0], startRowEndCol)); // Get middle rows From 9cd7bf2af1178c29d6be4254bd13ed0c191ae299 Mon Sep 17 00:00:00 2001 From: Linmiao Xu Date: Wed, 19 Dec 2018 13:11:45 +0900 Subject: [PATCH 24/42] Weblinks should not allow quotes at end of urls enclosed in quotes --- src/addons/webLinks/webLinks.test.ts | 24 ++++++++++++++++++++++++ src/addons/webLinks/webLinks.ts | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/addons/webLinks/webLinks.test.ts b/src/addons/webLinks/webLinks.test.ts index 8ada2510..1e8a4ae7 100644 --- a/src/addons/webLinks/webLinks.test.ts +++ b/src/addons/webLinks/webLinks.test.ts @@ -63,4 +63,28 @@ describe('webLinks addon', () => { assert.equal(uri, 'http://foo.com/colon:test'); }); + + it('should not allow " character at the end of a URI enclosed with ""', () => { + const term = new MockTerminal(); + webLinks.webLinksInit(term); + + const row = '"http://foo.com/"'; + + const match = row.match(term.regex); + const uri = match[term.options.matchIndex]; + + assert.equal(uri, 'http://foo.com/'); + }); + + it('should not allow \' character at the end of a URI enclosed with \'\'', () => { + const term = new MockTerminal(); + webLinks.webLinksInit(term); + + const row = '\'http://foo.com/\''; + + const match = row.match(term.regex); + const uri = match[term.options.matchIndex]; + + assert.equal(uri, 'http://foo.com/'); + }); }); diff --git a/src/addons/webLinks/webLinks.ts b/src/addons/webLinks/webLinks.ts index 75d79104..f0d69cc5 100644 --- a/src/addons/webLinks/webLinks.ts +++ b/src/addons/webLinks/webLinks.ts @@ -14,7 +14,7 @@ const ipClause = '((\\d{1,3}\\.){3}\\d{1,3})'; const localHostClause = '(localhost)'; const portClause = '(:\\d{1,5})'; const hostClause = '((' + domainBodyClause + '\\.' + tldClause + ')|' + ipClause + '|' + localHostClause + ')' + portClause + '?'; -const pathClause = '(\\/[\\/\\w\\.\\-%~:]*)*([^:\\s])'; +const pathClause = '(\\/[\\/\\w\\.\\-%~:]*)*([^:"\'\\s])'; const queryStringHashFragmentCharacterSet = '[0-9\\w\\[\\]\\(\\)\\/\\?\\!#@$%&\'*+,:;~\\=\\.\\-]*'; const queryStringClause = '(\\?' + queryStringHashFragmentCharacterSet + ')?'; const hashFragmentClause = '(#' + queryStringHashFragmentCharacterSet + ')?'; From 86f194a6d34590ac3098480137c0e7ff7abf098b Mon Sep 17 00:00:00 2001 From: Linmiao Xu Date: Wed, 19 Dec 2018 13:50:41 +0900 Subject: [PATCH 25/42] Fix npm scripts when using node 10+ --- yarn.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/yarn.lock b/yarn.lock index 7ebdbf3c..01de5ab7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4479,9 +4479,9 @@ nanomatch@^1.2.9: to-regex "^3.0.1" natives@^1.1.0: - version "1.1.4" - resolved "https://registry.yarnpkg.com/natives/-/natives-1.1.4.tgz#2f0f224fc9a7dd53407c7667c84cf8dbe773de58" - integrity sha512-Q29yeg9aFKwhLVdkTAejM/HvYG0Y1Am1+HUkFQGn5k2j8GS+v60TVmZh6nujpEAj/qql+wGUrlryO8bF+b1jEg== + version "1.1.6" + resolved "https://registry.yarnpkg.com/natives/-/natives-1.1.6.tgz#a603b4a498ab77173612b9ea1acdec4d980f00bb" + integrity sha512-6+TDFewD4yxY14ptjKaS63GVdtKiES1pTPyxn9Jb0rBqPMZ7VcCiooEhPNsr+mqHtMGxa/5c/HhcC4uPEUw/nA== needle@^2.2.1: version "2.2.1" From 5ebd543b8b5388f396f6637732de1ea4a10f4f3f Mon Sep 17 00:00:00 2001 From: Linmiao Xu Date: Wed, 19 Dec 2018 14:32:15 +0900 Subject: [PATCH 26/42] Fix missing quotes in import statement in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c654f288..86ac1f09 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Addons are JavaScript modules that extend the `Terminal` prototype with new meth To use an addon, just import the JavaScript module and pass it to `Terminal`'s `applyAddon` method: ```javascript -import { Terminal } from xterm; +import { Terminal } from 'xterm'; import * as fit from 'xterm/lib/addons/fit/fit'; From 1104bcbccb523a856f2181060fc34ff73e4a3d48 Mon Sep 17 00:00:00 2001 From: Ashwin Ramaswami Date: Thu, 20 Dec 2018 09:41:44 -0500 Subject: [PATCH 27/42] Allow holding key on mac to send multiple keys to terminal --- src/core/input/Keyboard.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/input/Keyboard.ts b/src/core/input/Keyboard.ts index 8c6f3c59..eccc3769 100644 --- a/src/core/input/Keyboard.ts +++ b/src/core/input/Keyboard.ts @@ -349,6 +349,8 @@ export function evaluateKeyboardEvent( if (ev.keyCode === 65) { // cmd + a result.type = KeyboardResultType.SELECT_ALL; } + } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.key !== 'Shift') { + result.key = ev.key; } break; } From ec1060187d4789508a779744ab208efb0a92ca61 Mon Sep 17 00:00:00 2001 From: Ashwin Ramaswami Date: Thu, 20 Dec 2018 09:50:18 -0500 Subject: [PATCH 28/42] Add tests --- src/core/input/Keyboard.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/core/input/Keyboard.test.ts b/src/core/input/Keyboard.test.ts index e9846831..e0735a4a 100644 --- a/src/core/input/Keyboard.test.ts +++ b/src/core/input/Keyboard.test.ts @@ -281,5 +281,15 @@ describe('Keyboard', () => { assert.equal(testEvaluateKeyboardEvent({ keyCode: 0, key: 'UIKeyInputDownArrow' }).key, '\x1b[B'); assert.equal(testEvaluateKeyboardEvent({ keyCode: 0, key: 'UIKeyInputDownArrow' }, { applicationCursorMode: true }).key, '\x1bOB'); }); + + it('should handle lowercase letters', () => { + assert.equal(testEvaluateKeyboardEvent({ keyCode: 65, key: 'a' }).key, 'a'); + assert.equal(testEvaluateKeyboardEvent({ keyCode: 189, key: '-' }).key, '-'); + }); + + it('should handle uppercase letters', () => { + assert.equal(testEvaluateKeyboardEvent({ shiftKey: true, keyCode: 65, key: 'A' }).key, 'A'); + }); + }); }); From b820f23da6817e299ca18e11bbd1008c45b0a024 Mon Sep 17 00:00:00 2001 From: Ashwin Ramaswami Date: Thu, 20 Dec 2018 09:50:57 -0500 Subject: [PATCH 29/42] Add additional test --- src/core/input/Keyboard.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/input/Keyboard.test.ts b/src/core/input/Keyboard.test.ts index e0735a4a..a0dc3cbc 100644 --- a/src/core/input/Keyboard.test.ts +++ b/src/core/input/Keyboard.test.ts @@ -289,6 +289,7 @@ describe('Keyboard', () => { it('should handle uppercase letters', () => { assert.equal(testEvaluateKeyboardEvent({ shiftKey: true, keyCode: 65, key: 'A' }).key, 'A'); + assert.equal(testEvaluateKeyboardEvent({ shiftKey: true, keyCode: 49, key: '!' }).key, '!'); }); }); From 0782fbb8284a0813d1edd8e11931f3977397a86e Mon Sep 17 00:00:00 2001 From: Ashwin Ramaswami Date: Thu, 20 Dec 2018 09:57:24 -0500 Subject: [PATCH 30/42] Fix check --- src/core/input/Keyboard.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/input/Keyboard.ts b/src/core/input/Keyboard.ts index eccc3769..31e97e35 100644 --- a/src/core/input/Keyboard.ts +++ b/src/core/input/Keyboard.ts @@ -349,7 +349,7 @@ export function evaluateKeyboardEvent( if (ev.keyCode === 65) { // cmd + a result.type = KeyboardResultType.SELECT_ALL; } - } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.key !== 'Shift') { + } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.keyCode >= 65) { result.key = ev.key; } break; From 64dd9d46fbe18720651ad218ef39375d414fece7 Mon Sep 17 00:00:00 2001 From: Ashwin Ramaswami Date: Thu, 20 Dec 2018 14:21:35 -0500 Subject: [PATCH 31/42] Change keyCode cutoff --- src/core/input/Keyboard.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/input/Keyboard.ts b/src/core/input/Keyboard.ts index 31e97e35..21c81a81 100644 --- a/src/core/input/Keyboard.ts +++ b/src/core/input/Keyboard.ts @@ -349,7 +349,7 @@ export function evaluateKeyboardEvent( if (ev.keyCode === 65) { // cmd + a result.type = KeyboardResultType.SELECT_ALL; } - } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.keyCode >= 65) { + } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.keyCode >= 48) { result.key = ev.key; } break; From ff73f705aeaaff6d1cbeb3ccdbb557c45e169890 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 20 Dec 2018 11:47:51 -0800 Subject: [PATCH 32/42] Fix winptyCompat wrapped line heuristic Part of Microsoft/vscode#65485 --- src/addons/winptyCompat/winptyCompat.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/addons/winptyCompat/winptyCompat.ts b/src/addons/winptyCompat/winptyCompat.ts index 2513dce6..aec580ed 100644 --- a/src/addons/winptyCompat/winptyCompat.ts +++ b/src/addons/winptyCompat/winptyCompat.ts @@ -7,7 +7,8 @@ import { Terminal } from 'xterm'; import { IWinptyCompatAddonTerminal } from './Interfaces'; const CHAR_DATA_CODE_INDEX = 3; -const NULL_CELL_CODE = 32; +const NULL_CELL_CODE = 0; +const WHITESPACE_CELL_CODE = 32; export function winptyCompatInit(terminal: Terminal): void { const addonTerminal = terminal; @@ -32,7 +33,7 @@ export function winptyCompatInit(terminal: Terminal): void { const line = addonTerminal._core.buffer.lines.get(addonTerminal._core.buffer.ybase + addonTerminal._core.buffer.y - 1); const lastChar = line.get(addonTerminal.cols - 1); - if (lastChar[CHAR_DATA_CODE_INDEX] !== NULL_CELL_CODE) { + if (lastChar[CHAR_DATA_CODE_INDEX] !== NULL_CELL_CODE && lastChar[CHAR_DATA_CODE_INDEX] !== WHITESPACE_CELL_CODE) { const nextLine = addonTerminal._core.buffer.lines.get(addonTerminal._core.buffer.ybase + addonTerminal._core.buffer.y); nextLine.isWrapped = true; } From 3eac446a854c253a7bec6475c3736b48f3ea4c18 Mon Sep 17 00:00:00 2001 From: Robin Sturm Date: Thu, 20 Dec 2018 11:54:26 -0800 Subject: [PATCH 33/42] bind function before calling --- src/addons/fullscreen/fullscreen.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/addons/fullscreen/fullscreen.ts b/src/addons/fullscreen/fullscreen.ts index 4d05e904..083c00c2 100644 --- a/src/addons/fullscreen/fullscreen.ts +++ b/src/addons/fullscreen/fullscreen.ts @@ -22,6 +22,7 @@ export function toggleFullScreen(term: Terminal, fullscreen: boolean): void { fn = term.element.classList.add; } + fn = fn.bind(term.element.classList); fn('fullscreen'); } From a4b4d082fb1f0a84105554fc4c42edb07bbaf362 Mon Sep 17 00:00:00 2001 From: Ashwin Ramaswami Date: Fri, 21 Dec 2018 07:38:14 -0500 Subject: [PATCH 34/42] Don't include num lock and scroll lock --- src/core/input/Keyboard.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/input/Keyboard.ts b/src/core/input/Keyboard.ts index 21c81a81..37e663f5 100644 --- a/src/core/input/Keyboard.ts +++ b/src/core/input/Keyboard.ts @@ -349,7 +349,8 @@ export function evaluateKeyboardEvent( if (ev.keyCode === 65) { // cmd + a result.type = KeyboardResultType.SELECT_ALL; } - } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.keyCode >= 48) { + } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.keyCode >= 48 + && ev.keyCode !== 144 && ev.keyCode !== 145) { // Include only keys that that result in a character; don't include num lock and scroll lock result.key = ev.key; } break; From 78e7e39bb48c17e197d1b6080635289dbdf3cbf4 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 21 Dec 2018 23:04:38 -0800 Subject: [PATCH 35/42] Improve the readme --- README.md | 57 +++++++++++++++++++------------------------------------ 1 file changed, 19 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 86ac1f09..35ccc9b6 100644 --- a/README.md +++ b/README.md @@ -2,23 +2,20 @@ [![Build Status](https://dev.azure.com/xtermjs/xterm.js/_apis/build/status/xtermjs.xterm.js)](https://dev.azure.com/xtermjs/xterm.js/_build/latest?definitionId=3) [![Coverage Status](https://coveralls.io/repos/github/xtermjs/xterm.js/badge.svg?branch=master)](https://coveralls.io/github/xtermjs/xterm.js?branch=master) -[![Gitter](https://badges.gitter.im/sourcelair/xterm.js.svg)](https://gitter.im/sourcelair/xterm.js?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) -[![jsDelivr Hits](https://data.jsdelivr.com/v1/package/npm/xterm/badge?style=rounded)](https://www.jsdelivr.com/package/npm/xterm) -Xterm.js is a terminal front-end component written in JavaScript that works in the browser. - -It enables applications to provide fully featured terminals to their users and create great development experiences. +Xterm.js is a front-end component written in TypeScript that lets applications bring fully-featured terminals to their users in the browser. It's used by popular projects such as VS Code, Hyper and Theia. ## Features -- **Text-based application support**: Use xterm.js to work with applications like `bash`, `git` etc. -- **Curses-based application support**: Use xterm.js to work with applications like `vim`, `tmux` etc. -- **Mouse events support**: Xterm.js captures mouse events like click and scroll and passes them to the terminal's back-end controlling process -- **CJK (Chinese, Japanese, Korean) character support**: Xterm.js renders CJK characters seamlessly -- **IME support**: Insert international (including CJK) characters using IME input with your keyboard -- **Self-contained library**: Xterm.js works on its own. It does not require any external libraries like jQuery or React to work -- **Modular, event-based API**: Lets you build addons and themes with ease + +- **Terminal apps just work**: Xterm.js works with most terminal apps such as `bash`, `vim` and `tmux`, this includes support for curses-based apps and mouse event support +- **Perfomant**: Xterm.js is *really* fast, it even includes a GPU-accelerated renderer +- **Rich unicode support**: Supports CJK, emojis and IMEs +- **Self-contained**: Requires zero dependencies to work +- **Accessible**: Screen reader support can be turned on using the `screenReaderMode` option +- **And much more**: Links, theming, addons, well documented API, etc. ## What xterm.js is not + - Xterm.js is not a terminal application that you can download and use on your computer - Xterm.js is not `bash`. Xterm.js can be connected to processes like `bash` and let you interact with them (provide input, receive output) @@ -30,7 +27,7 @@ First you need to install the module, we ship exclusively through [npm](https:// npm install xterm ``` -To start using xterm.js on your browser, add the `xterm.js` and `xterm.css` to the head of your html page. Then create a `
` onto which xterm can attach itself. +To start using xterm.js on your browser, add the `xterm.js` and `xterm.css` to the head of your html page. Then create a `
` onto which xterm can attach itself. Finally instantiate the `Terminal` object and then call the `open` function with the DOM object of the `div`. ```html @@ -50,25 +47,17 @@ To start using xterm.js on your browser, add the `xterm.js` and `xterm.css` to t ``` -Finally instantiate the `Terminal` object and then call the `open` function with the DOM object of the `div`. - ### Importing -The proposed way to load xterm.js is via the ES6 module syntax. +The recommended way to load xterm.js is via the ES6 module syntax: ```javascript import { Terminal } from 'xterm'; ``` -### API - -The full API for xterm.js is contained within the [TypeScript declaration file](https://github.com/xtermjs/xterm.js/blob/master/typings/xterm.d.ts), use the branch/tag picker in GitHub (`w`) to navigate to the correct version of the API. - -Note that some APIs are marked *experimental*, these are added so we can experiment with new ideas without committing to support it like a normal semver API. Note that these APIs can change radically between versions so be sure to read release notes if you plan on using experimental APIs. - ### Addons -Addons are JavaScript modules that extend the `Terminal` prototype with new methods and attributes to provide additional functionality. There are a handful available in the main repository in the `src/addons` directory and you can even write your own, by using xterm.js' public API. +Addons are JavaScript modules that extend the `Terminal` prototype with new methods and attributes to provide additional functionality. There are a handful available in the main repository in the `src/addons` directory and you can even write your own by using the [public API](https://github.com/xtermjs/xterm.js/blob/master/typings/xterm.d.ts). To use an addon, just import the JavaScript module and pass it to `Terminal`'s `applyAddon` method: @@ -76,7 +65,6 @@ To use an addon, just import the JavaScript module and pass it to `Terminal`'s ` import { Terminal } from 'xterm'; import * as fit from 'xterm/lib/addons/fit/fit'; - Terminal.applyAddon(fit); var xterm = new Terminal(); // Instantiate the terminal @@ -87,25 +75,16 @@ You will also need to include the addon's CSS file if it has one in the folder. #### Importing Addons in TypeScript -There are currently no typings for addons if they are accessed via extending Terminal prototype, so you will need to upcast if using TypeScript, eg. `(xterm).fit()`. - -Alternatively, you can import addon function and enhance the terminal on demand. This would have better typing support and is friendly to treeshaking. E.g.: +There are currently no typings for addons if they are accessed via extending Terminal prototype, so you will need to upcast if using TypeScript, eg. `(xterm as any).fit()`. Alternatively, you can import the addon function and enhance the terminal on demand. This has better typing support and is friendly to treeshaking. ```typescript import { Terminal } from 'xterm'; import { fit } from 'xterm/lib/addons/fit/fit'; const xterm = new Terminal(); -// Fit the terminal when necessary: -fit(xterm); +fit(xterm); // Fit the terminal when necessary ``` -#### Third party addons - -There are also the following third party addons available: - -- [xterm-webfont](https://www.npmjs.com/package/xterm-webfont) - ## Browser Support Since xterm.js is typically implemented as a developer tool, only modern browsers are supported officially. Here is a list of the versions we aim to support: @@ -116,11 +95,13 @@ Since xterm.js is typically implemented as a developer tool, only modern browser - Safari latest - IE11 -Xterm.js works seamlessly in Electron apps and may even work on earlier versions of the browsers but these are the browsers we strive to keep working. +Xterm.js works seamlessly in [Electron](https://electronjs.org/) apps and may even work on earlier versions of the browsers, these are the versions we strive to keep working. ## API -The current full API documentation is available in the [TypeScript declaration file on the repository](https://github.com/xtermjs/xterm.js/blob/master/typings/xterm.d.ts), switch the tag (press `w` when viewing the file) to point at the specific version tag you're using. +The full API for xterm.js is contained within the [TypeScript declaration file](https://github.com/xtermjs/xterm.js/blob/master/typings/xterm.d.ts), use the branch/tag picker in GitHub (`w`) to navigate to the correct version of the API. + +Note that some APIs are marked *experimental*, these are added to enable experimentation with new ideas without committing to support it like a normal [semver](https://semver.org/) API. Note that these APIs can change radically between versions so be sure to read release notes if you plan on using experimental APIs. ## Real-world uses Xterm.js is used in several world-class applications to provide great terminal experiences. @@ -182,7 +163,7 @@ Do you use xterm.js in your application as well? Please [open a Pull Request](ht Xterm.js follows a monthly release cycle roughly. -The existing releases are available at this GitHub repo's [Releases](https://github.com/sourcelair/xterm.js/releases), while the roadmap is available as [Milestones](https://github.com/sourcelair/xterm.js/milestones). +All current and past releases are available on this repo's [Releases page](https://github.com/sourcelair/xterm.js/releases), while a rough roadmap is available by looking through [Milestones](https://github.com/sourcelair/xterm.js/milestones). ## Contributing From 81ada399892ccba0b2d03e6e75fb14594775a0e0 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 21 Dec 2018 23:07:19 -0800 Subject: [PATCH 36/42] Remove Gitter link from issue template Fixes #1838 --- .github/ISSUE_TEMPLATE/question.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md index 10eee3d9..1fe5c763 100644 --- a/.github/ISSUE_TEMPLATE/question.md +++ b/.github/ISSUE_TEMPLATE/question.md @@ -1,8 +1,8 @@ --- name: Question -about: The issue tracker is not for questions. Please ask questions on https://stackoverflow.com/questions/tagged/xtermjs or https://gitter.im/sourcelair/xterm.js. +about: The issue tracker is not for questions. Please ask questions on https://stackoverflow.com/questions/tagged/xtermjs --- 🛑 The issue tracker is not for questions 🛑 -If you have a question, please ask it on https://stackoverflow.com/questions/tagged/xtermjs or https://gitter.im/sourcelair/xterm.js. +If you have a question, please ask it on https://stackoverflow.com/questions/tagged/xtermjs. From 204e42a1513fdbdeb0a664e2471161d770f8c2c1 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 26 Dec 2018 15:15:12 -0800 Subject: [PATCH 37/42] Fix incremental search --- src/addons/search/SearchHelper.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index 408e1f29..d3040f73 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -49,8 +49,7 @@ export class SearchHelper implements ISearchHelper { // For incremental search, use existing row if (this._terminal.getSelection().length !== 0) { startRow = incremental ? selectionManager.selectionStart[1] : selectionManager.selectionEnd[1]; - // TODO: Fix for incremental - startCol = this._terminal._core.selectionManager.selectionEnd[0]; + startCol = incremental ? selectionManager.selectionStart[0] : selectionManager.selectionEnd[0]; } } From 76302e2146bb2724137a86aa73d6b3b3c4f709c6 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 26 Dec 2018 15:32:34 -0800 Subject: [PATCH 38/42] Get multiple matches working after incremental changes --- src/addons/search/SearchHelper.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index d3040f73..0a66fdff 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -52,11 +52,12 @@ export class SearchHelper implements ISearchHelper { startCol = incremental ? selectionManager.selectionStart[0] : selectionManager.selectionEnd[0]; } } + console.log(`Start from ${startCol},${startRow}`); this._initLinesCache(); // Search from startRow to end - for (let y = incremental ? startRow : startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { + for (let y = startRow; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { result = this._findInLine(term, { row: y, col: startCol }, searchOptions); if (result) { break; @@ -111,7 +112,7 @@ export class SearchHelper implements ISearchHelper { this._initLinesCache(); // Search from startRow to top - for (let y = incremental ? startRow : startRow - 1; y >= 0; y--) { + for (let y = startRow; y >= 0; y--) { result = this._findInLine(term, {row: y, col: startCol}, searchOptions, isReverseSearch); if (result) { break; @@ -216,6 +217,7 @@ export class SearchHelper implements ISearchHelper { resultIndex = searchStringLine.lastIndexOf(searchTerm, searchIndex.col - searchTerm.length); } else { resultIndex = searchStringLine.indexOf(searchTerm, searchIndex.col); + console.log('resultIndex', resultIndex); } } From e0d535e0da1050aaa19860747c0cdedf6017cd38 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 26 Dec 2018 15:49:08 -0800 Subject: [PATCH 39/42] Fix previous search, remove incremental previous search --- demo/client.ts | 6 ++-- src/addons/search/Interfaces.ts | 5 ++- src/addons/search/SearchHelper.ts | 57 ++++++++++++++++++------------- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/demo/client.ts b/demo/client.ts index d99ff66d..acc83cb8 100644 --- a/demo/client.ts +++ b/demo/client.ts @@ -113,9 +113,9 @@ function createTerminal(): void { }); addDomListener(actionElements.findPrevious, 'keyup', (e) => { - const searchOptions = getSearchOptions(); - searchOptions.incremental = e.key !== `Enter`; - term.findPrevious(actionElements.findPrevious.value, searchOptions); + if (e.key === `Enter`) { + term.findPrevious(actionElements.findPrevious.value, getSearchOptions()); + } }); // fit is called within a setTimeout, cols and rows need this. diff --git a/src/addons/search/Interfaces.ts b/src/addons/search/Interfaces.ts index 76fe4bc9..2489844e 100644 --- a/src/addons/search/Interfaces.ts +++ b/src/addons/search/Interfaces.ts @@ -25,7 +25,10 @@ export interface ISearchOptions { regex?: boolean; wholeWord?: boolean; caseSensitive?: boolean; - /** Assume caller implements 'search as you type' where findNext gets called when search input changes */ + /** + * Use this when you want the selection to expand if it still matches as the + * user types. Note that this only affects findNext. + */ incremental?: boolean; } diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index 0a66fdff..8972917b 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -52,24 +52,27 @@ export class SearchHelper implements ISearchHelper { startCol = incremental ? selectionManager.selectionStart[0] : selectionManager.selectionEnd[0]; } } - console.log(`Start from ${startCol},${startRow}`); this._initLinesCache(); - // Search from startRow to end - for (let y = startRow; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { - result = this._findInLine(term, { row: y, col: startCol }, searchOptions); - if (result) { - break; + // Search startRow + result = this._findInLine(term, { row: startRow, col: startCol }, searchOptions); + + // Search from startRow + 1 to end + if (!result) { + for (let y = startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { + result = this._findInLine(term, { row: y, col: 0 }, searchOptions); + if (result) { + break; + } } - startCol = 0; } - // Search from the top to the startRow + // Search from the top to the startRow (search the whole startRow again in + // case startCol > 0) if (!result) { - for (let y = 0; y < startRow; y++) { - startCol = 0; - result = this._findInLine(term, {row: y, col: startCol}, searchOptions); + for (let y = 0; y <= startRow; y++) { + result = this._findInLine(term, {row: y, col: 0}, searchOptions); if (result) { break; } @@ -89,7 +92,6 @@ export class SearchHelper implements ISearchHelper { */ public findPrevious(term: string, searchOptions?: ISearchOptions): boolean { const selectionManager = this._terminal._core.selectionManager; - const {incremental} = searchOptions; let result: ISearchResult; if (!term || term.length === 0) { @@ -111,21 +113,31 @@ export class SearchHelper implements ISearchHelper { this._initLinesCache(); - // Search from startRow to top - for (let y = startRow; y >= 0; y--) { - result = this._findInLine(term, {row: y, col: startCol}, searchOptions, isReverseSearch); - if (result) { - break; + // Search startRow + result = this._findInLine(term, { row: startRow, col: startCol }, searchOptions, isReverseSearch); + + // Search from startRow - 1 to top + if (!result) { + for (let y = startRow - 1; y >= 0; y--) { + result = this._findInLine(term, { + row: y, + col: this._terminal._core.buffer.lines.get(y).length + }, searchOptions, isReverseSearch); + if (result) { + break; + } } - startCol = y > 0 ? this._terminal._core.buffer.lines.get(y - 1).length : 0; } - // Search from the bottom to startRow + // Search from the bottom to startRow (search the whole startRow again in + // case startCol > 0) if (!result) { const searchFrom = this._terminal._core.buffer.ybase + this._terminal.rows - 1; - for (let y = searchFrom; y > startRow; y--) { - startCol = this._terminal._core.buffer.lines.get(y).length; - result = this._findInLine(term, {row: y, col: startCol}, searchOptions, isReverseSearch); + for (let y = searchFrom; y >= startRow; y--) { + result = this._findInLine(term, { + row: y, + col: this._terminal._core.buffer.lines.get(y).length + }, searchOptions, isReverseSearch); if (result) { break; } @@ -217,7 +229,6 @@ export class SearchHelper implements ISearchHelper { resultIndex = searchStringLine.lastIndexOf(searchTerm, searchIndex.col - searchTerm.length); } else { resultIndex = searchStringLine.indexOf(searchTerm, searchIndex.col); - console.log('resultIndex', resultIndex); } } From 1a1f7e984fcda6ca4f565a717d8f5ed8fe0a993f Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 26 Dec 2018 16:03:31 -0800 Subject: [PATCH 40/42] Remove ISearchIndex object --- src/addons/search/Interfaces.ts | 7 ++--- src/addons/search/SearchHelper.ts | 49 ++++++++++++++----------------- src/addons/search/search.test.ts | 34 ++++++++++----------- 3 files changed, 41 insertions(+), 49 deletions(-) diff --git a/src/addons/search/Interfaces.ts b/src/addons/search/Interfaces.ts index 2489844e..a1f05895 100644 --- a/src/addons/search/Interfaces.ts +++ b/src/addons/search/Interfaces.ts @@ -32,11 +32,8 @@ export interface ISearchOptions { incremental?: boolean; } -export interface ISearchIndex { +export interface ISearchResult { + term: string; col: number; row: number; } - -export interface ISearchResult extends ISearchIndex { - term: string; -} diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index 8972917b..756f2da7 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult, ISearchIndex } from './Interfaces'; +import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult } from './Interfaces'; const NON_WORD_CHARACTERS = ' ~!@#$%^&*()+`-=[]{}|\;:"\',./<>?'; const LINES_CACHE_TIME_TO_LIVE = 15 * 1000; // 15 secs @@ -56,12 +56,12 @@ export class SearchHelper implements ISearchHelper { this._initLinesCache(); // Search startRow - result = this._findInLine(term, { row: startRow, col: startCol }, searchOptions); + result = this._findInLine(term, startRow, startCol, searchOptions); // Search from startRow + 1 to end if (!result) { for (let y = startRow + 1; y < this._terminal._core.buffer.ybase + this._terminal.rows; y++) { - result = this._findInLine(term, { row: y, col: 0 }, searchOptions); + result = this._findInLine(term, y, 0, searchOptions); if (result) { break; } @@ -72,7 +72,7 @@ export class SearchHelper implements ISearchHelper { // case startCol > 0) if (!result) { for (let y = 0; y <= startRow; y++) { - result = this._findInLine(term, {row: y, col: 0}, searchOptions); + result = this._findInLine(term, y, 0, searchOptions); if (result) { break; } @@ -114,15 +114,12 @@ export class SearchHelper implements ISearchHelper { this._initLinesCache(); // Search startRow - result = this._findInLine(term, { row: startRow, col: startCol }, searchOptions, isReverseSearch); + result = this._findInLine(term, startRow, startCol, searchOptions, isReverseSearch); // Search from startRow - 1 to top if (!result) { for (let y = startRow - 1; y >= 0; y--) { - result = this._findInLine(term, { - row: y, - col: this._terminal._core.buffer.lines.get(y).length - }, searchOptions, isReverseSearch); + result = this._findInLine(term, y, this._terminal._core.buffer.lines.get(y).length, searchOptions, isReverseSearch); if (result) { break; } @@ -134,10 +131,7 @@ export class SearchHelper implements ISearchHelper { if (!result) { const searchFrom = this._terminal._core.buffer.ybase + this._terminal.rows - 1; for (let y = searchFrom; y >= startRow; y--) { - result = this._findInLine(term, { - row: y, - col: this._terminal._core.buffer.lines.get(y).length - }, searchOptions, isReverseSearch); + result = this._findInLine(term, y, this._terminal._core.buffer.lines.get(y).length, searchOptions, isReverseSearch); if (result) { break; } @@ -187,20 +181,21 @@ export class SearchHelper implements ISearchHelper { * started on an earlier line then it is skipped since it will be properly searched when the terminal line that the * text starts on is searched. * @param term The search term. - * @param y The line to search. + * @param row The line to start the search from. + * @param col The column to start the search from. * @param searchOptions Search options. * @return The search result if it was found. */ - protected _findInLine(term: string, searchIndex: ISearchIndex, searchOptions: ISearchOptions = {}, isReverseSearch: boolean = false): ISearchResult { - if (this._terminal._core.buffer.lines.get(searchIndex.row).isWrapped) { + protected _findInLine(term: string, row: number, col: number, searchOptions: ISearchOptions = {}, isReverseSearch: boolean = false): ISearchResult { + if (this._terminal._core.buffer.lines.get(row).isWrapped) { return; } - let stringLine = this._linesCache ? this._linesCache[searchIndex.row] : void 0; + let stringLine = this._linesCache ? this._linesCache[row] : void 0; if (stringLine === void 0) { - stringLine = this.translateBufferLineToStringWithWrap(searchIndex.row, true); + stringLine = this.translateBufferLineToStringWithWrap(row, true); if (this._linesCache) { - this._linesCache[searchIndex.row] = stringLine; + this._linesCache[row] = stringLine; } } @@ -212,37 +207,37 @@ export class SearchHelper implements ISearchHelper { const searchRegex = RegExp(searchTerm, 'g'); let foundTerm: RegExpExecArray; if (isReverseSearch) { - while (foundTerm = searchRegex.exec(searchStringLine.slice(0, searchIndex.col))) { + while (foundTerm = searchRegex.exec(searchStringLine.slice(0, col))) { resultIndex = searchRegex.lastIndex - foundTerm[0].length; term = foundTerm[0]; searchRegex.lastIndex -= (term.length - 1); } } else { - foundTerm = searchRegex.exec(searchStringLine.slice(searchIndex.col)); + foundTerm = searchRegex.exec(searchStringLine.slice(col)); if (foundTerm && foundTerm[0].length > 0) { - resultIndex = searchIndex.col + (searchRegex.lastIndex - foundTerm[0].length); + resultIndex = col + (searchRegex.lastIndex - foundTerm[0].length); term = foundTerm[0]; } } } else { if (isReverseSearch) { - resultIndex = searchStringLine.lastIndexOf(searchTerm, searchIndex.col - searchTerm.length); + resultIndex = searchStringLine.lastIndexOf(searchTerm, col - searchTerm.length); } else { - resultIndex = searchStringLine.indexOf(searchTerm, searchIndex.col); + resultIndex = searchStringLine.indexOf(searchTerm, col); } } if (resultIndex >= 0) { // Adjust the row number and search index if needed since a "line" of text can span multiple rows if (resultIndex >= this._terminal.cols) { - searchIndex.row += Math.floor(resultIndex / this._terminal.cols); + row += Math.floor(resultIndex / this._terminal.cols); resultIndex = resultIndex % this._terminal.cols; } if (searchOptions.wholeWord && !this._isWholeWord(resultIndex, searchStringLine, term)) { return; } - const line = this._terminal._core.buffer.lines.get(searchIndex.row); + const line = this._terminal._core.buffer.lines.get(row); for (let i = 0; i < resultIndex; i++) { const charData = line.get(i); @@ -261,7 +256,7 @@ export class SearchHelper implements ISearchHelper { return { term, col: resultIndex, - row: searchIndex.row + row }; } } diff --git a/src/addons/search/search.test.ts b/src/addons/search/search.test.ts index e9c11fb2..6551fa8b 100644 --- a/src/addons/search/search.test.ts +++ b/src/addons/search/search.test.ts @@ -7,7 +7,7 @@ declare var require: any; import { assert, expect } from 'chai'; import * as search from './search'; import { SearchHelper } from './SearchHelper'; -import { ISearchOptions, ISearchResult, ISearchIndex } from './Interfaces'; +import { ISearchOptions, ISearchResult } from './Interfaces'; class MockTerminalPlain {} @@ -30,10 +30,10 @@ class MockTerminal { class TestSearchHelper extends SearchHelper { public findInLine(term: string, rowNumber: number, searchOptions?: ISearchOptions): ISearchResult { - return this._findInLine(term, {row: rowNumber, col: 0}, searchOptions); + return this._findInLine(term, rowNumber, 0, searchOptions); } - public findFromIndex(term: string, searchIndex: ISearchIndex, searchOptions?: ISearchOptions, isReverseSearch?: boolean): ISearchResult { - return this._findInLine(term, searchIndex, searchOptions, isReverseSearch); + public findFromIndex(term: string, row: number, col: number, searchOptions?: ISearchOptions, isReverseSearch?: boolean): ISearchResult { + return this._findInLine(term, row, col, searchOptions, isReverseSearch); } } @@ -258,11 +258,11 @@ describe('search addon', () => { wholeWord: false, caseSensitive: false }; - const find0 = term.searchHelper.findFromIndex('hello', {row: 0, col: 0}, searchOptions); - const find1 = term.searchHelper.findFromIndex('hello', {row: 0, col: find0.col + find0.term.length}, searchOptions); - const find2 = term.searchHelper.findFromIndex('aaaa', {row: 1, col: 0}, searchOptions); - const find3 = term.searchHelper.findFromIndex('aaaa', {row: 1, col: find2.col + find2.term.length}, searchOptions); - const find4 = term.searchHelper.findFromIndex('aaaa', {row: 1, col: find3.col + find3.term.length}, searchOptions); + const find0 = term.searchHelper.findFromIndex('hello', 0, 0, searchOptions); + const find1 = term.searchHelper.findFromIndex('hello', 0, find0.col + find0.term.length, searchOptions); + const find2 = term.searchHelper.findFromIndex('aaaa', 1, 0, searchOptions); + const find3 = term.searchHelper.findFromIndex('aaaa', 1, find2.col + find2.term.length, searchOptions); + const find4 = term.searchHelper.findFromIndex('aaaa', 1, find3.col + find3.term.length, searchOptions); expect(find0).eql({col: 0, row: 0, term: 'hello'}); expect(find1).eql({col: 9, row: 0, term: 'hello'}); expect(find2).eql({col: 0, row: 1, term: 'aaaa'}); @@ -280,10 +280,10 @@ describe('search addon', () => { caseSensitive: false }; const isReverseSearch = true; - const find0 = term.searchHelper.findFromIndex('is', {row: 0, col: 16}, searchOptions, isReverseSearch); - const find1 = term.searchHelper.findFromIndex('is', {row: 0, col: find0.col}, searchOptions, isReverseSearch); - const find2 = term.searchHelper.findFromIndex('it', {row: 0, col: 16}, searchOptions, isReverseSearch); - const find3 = term.searchHelper.findFromIndex('it', {row: 0, col: find2.col}, searchOptions, isReverseSearch); + const find0 = term.searchHelper.findFromIndex('is', 0, 16, searchOptions, isReverseSearch); + const find1 = term.searchHelper.findFromIndex('is', 0, find0.col, searchOptions, isReverseSearch); + const find2 = term.searchHelper.findFromIndex('it', 0, 16, searchOptions, isReverseSearch); + const find3 = term.searchHelper.findFromIndex('it', 0, find2.col, searchOptions, isReverseSearch); expect(find0).eql({col: 14, row: 0, term: 'is'}); expect(find1).eql({col: 3, row: 0, term: 'is'}); expect(find2).eql({col: 11, row: 0, term: 'it'}); @@ -300,10 +300,10 @@ describe('search addon', () => { caseSensitive: true }; const isReverseSearch = true; - const find0 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: 16}, searchOptions, isReverseSearch); - const find1 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find0.col}, searchOptions, isReverseSearch); - const find2 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find1.col}, searchOptions, isReverseSearch); - const find3 = term.searchHelper.findFromIndex('[A-Z]{3}', {row: 0, col: find2.col}, searchOptions, isReverseSearch); + const find0 = term.searchHelper.findFromIndex('[A-Z]{3}', 0, 16, searchOptions, isReverseSearch); + const find1 = term.searchHelper.findFromIndex('[A-Z]{3}', 0, find0.col, searchOptions, isReverseSearch); + const find2 = term.searchHelper.findFromIndex('[A-Z]{3}', 0, find1.col, searchOptions, isReverseSearch); + const find3 = term.searchHelper.findFromIndex('[A-Z]{3}', 0, find2.col, searchOptions, isReverseSearch); expect(find0).eql({col: 13, row: 0, term: 'ABC'}); expect(find1).eql({col: 10, row: 0, term: 'ABC'}); expect(find2).eql({col: 3, row: 0, term: 'ABC'}); From d698faa18366df2592f5992ace5517d366382f79 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Wed, 26 Dec 2018 16:12:53 -0800 Subject: [PATCH 41/42] Comment how the regex reverse search while works --- src/addons/search/SearchHelper.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/addons/search/SearchHelper.ts b/src/addons/search/SearchHelper.ts index 756f2da7..42a3a122 100644 --- a/src/addons/search/SearchHelper.ts +++ b/src/addons/search/SearchHelper.ts @@ -207,6 +207,7 @@ export class SearchHelper implements ISearchHelper { const searchRegex = RegExp(searchTerm, 'g'); let foundTerm: RegExpExecArray; if (isReverseSearch) { + // This loop will get the resultIndex of the _last_ regex match in the range 0..col while (foundTerm = searchRegex.exec(searchStringLine.slice(0, col))) { resultIndex = searchRegex.lastIndex - foundTerm[0].length; term = foundTerm[0]; From 9bcfb4d7225e178a93389b96c9b1225e85f74182 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 27 Dec 2018 10:04:48 -0800 Subject: [PATCH 42/42] Tidy up wrapping style --- src/core/input/Keyboard.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/core/input/Keyboard.ts b/src/core/input/Keyboard.ts index 37e663f5..9d86b349 100644 --- a/src/core/input/Keyboard.ts +++ b/src/core/input/Keyboard.ts @@ -349,8 +349,9 @@ export function evaluateKeyboardEvent( if (ev.keyCode === 65) { // cmd + a result.type = KeyboardResultType.SELECT_ALL; } - } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.keyCode >= 48 - && ev.keyCode !== 144 && ev.keyCode !== 145) { // Include only keys that that result in a character; don't include num lock and scroll lock + } else if (ev.key && !ev.ctrlKey && !ev.altKey && !ev.metaKey && + ev.keyCode >= 48 && ev.keyCode !== 144 && ev.keyCode !== 145) { + // Include only keys that that result in a character; don't include num lock and scroll lock result.key = ev.key; } break;