From 249f8800af98bf9a715b77bedd086bd350f975c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Mon, 7 Jan 2019 11:30:26 +0100 Subject: [PATCH 1/3] account empty cells in stringIndexToBufferIndex --- src/Buffer.test.ts | 6 +++--- src/Buffer.ts | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 0546dfe8..663c7000 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -511,7 +511,7 @@ describe('Buffer', () => { 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); + const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i, true); const j = (i - 0) << 1; assert.deepEqual([(j / terminal.cols) | 0, j % terminal.cols], bufferIndex); } @@ -523,7 +523,7 @@ describe('Buffer', () => { 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); + const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i, true); assert.equal(input[i], terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]); } }); @@ -535,7 +535,7 @@ describe('Buffer', () => { 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); + const bufferIndex = terminal.buffer.stringIndexToBufferIndex(0, i, true); assert.equal( (!(i % 3)) ? input[i] diff --git a/src/Buffer.ts b/src/Buffer.ts index 625a2497..7b3bcee5 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -220,14 +220,16 @@ export class Buffer implements IBuffer { * @param stringIndex index within the string * @param startCol column offset the string was retrieved from */ - public stringIndexToBufferIndex(lineIndex: number, stringIndex: number): BufferIndex { + public stringIndexToBufferIndex(lineIndex: number, stringIndex: number, trimRight: boolean = false): BufferIndex { while (stringIndex) { const line = this.lines.get(lineIndex); if (!line) { return [-1, -1]; } - for (let i = 0; i < line.length; ++i) { - stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length; + const length = (trimRight) ? line.getTrimmedLength() : line.length; + for (let i = 0; i < length; ++i) { + if (line.get(i)[CHAR_DATA_WIDTH_INDEX]) + stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1; if (stringIndex < 0) { return [lineIndex, i]; } From 883ad01bd508e6df1f69127449b31710c8d9b6c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Mon, 7 Jan 2019 11:39:16 +0100 Subject: [PATCH 2/3] make linter happy --- src/Buffer.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Buffer.ts b/src/Buffer.ts index 7b3bcee5..0ad86bf1 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -228,8 +228,9 @@ export class Buffer implements IBuffer { } const length = (trimRight) ? line.getTrimmedLength() : line.length; for (let i = 0; i < length; ++i) { - if (line.get(i)[CHAR_DATA_WIDTH_INDEX]) - stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1; + if (line.get(i)[CHAR_DATA_WIDTH_INDEX]) { + stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1; // WHITESPACE_CELL_CHAR.length + } if (stringIndex < 0) { return [lineIndex, i]; } From 7b7f85e3cf0976b157a64c727276ad3b9d807e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Thu, 31 Jan 2019 22:56:55 +0100 Subject: [PATCH 3/3] add test for correct tab handling, docs --- src/Buffer.test.ts | 7 +++++++ src/Buffer.ts | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Buffer.test.ts b/src/Buffer.test.ts index 1ef2271f..53de19b7 100644 --- a/src/Buffer.test.ts +++ b/src/Buffer.test.ts @@ -1312,6 +1312,13 @@ describe('Buffer', () => { terminal.buffer.lines.get(bufferIndex[0]).get(bufferIndex[1])[CHAR_DATA_CHAR_INDEX]); } }); + + it('should handle \t in lines correctly', () => { + const input = '\thttps://google.de'; + terminal.writeSync(input); + const s = terminal.buffer.iterator(true).next().content; + assert.equal(s, Array(terminal.getOption('tabStopWidth') + 1).join(' ') + 'https://google.de'); + }); }); describe('BufferStringIterator', function(): void { it('iterator does not overflow buffer limits', function(): void { diff --git a/src/Buffer.ts b/src/Buffer.ts index 8eddf73f..7f4b6071 100644 --- a/src/Buffer.ts +++ b/src/Buffer.ts @@ -458,7 +458,9 @@ export class Buffer implements IBuffer { const length = (trimRight) ? line.getTrimmedLength() : line.length; for (let i = 0; i < length; ++i) { if (line.get(i)[CHAR_DATA_WIDTH_INDEX]) { - stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1; // WHITESPACE_CELL_CHAR.length + // empty cells report a string length of 0, but get replaced + // with a whitespace in translateToString, thus replace with 1 + stringIndex -= line.get(i)[CHAR_DATA_CHAR_INDEX].length || 1; } if (stringIndex < 0) { return [lineIndex, i];