Get reflow smaller working for wide chars

This commit is contained in:
Daniel Imms
2019-01-23 22:40:40 -08:00
parent ce69dceee7
commit df7cd9c319
4 changed files with 32 additions and 11 deletions
+2 -2
View File
@@ -568,8 +568,8 @@ describe('Buffer', () => {
buffer.resize(11, 10); buffer.resize(11, 10);
assert.equal(buffer.ybase, 0); assert.equal(buffer.ybase, 0);
assert.equal(buffer.lines.length, 10); assert.equal(buffer.lines.length, 10);
assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉', '1'); assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉');
assert.equal(buffer.lines.get(1).translateToString(true), '语汉语汉语', '2'); assert.equal(buffer.lines.get(1).translateToString(true), '语汉语汉语');
assert.equal(buffer.lines.get(2).translateToString(true), '汉语'); assert.equal(buffer.lines.get(2).translateToString(true), '汉语');
buffer.resize(10, 10); buffer.resize(10, 10);
assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉'); assert.equal(buffer.lines.get(0).translateToString(true), '汉语汉语汉');
+8 -1
View File
@@ -429,7 +429,7 @@ export class Buffer implements IBuffer {
let destCol = destLineLengths[destLineIndex]; // cellsNeeded % newCols; let destCol = destLineLengths[destLineIndex]; // cellsNeeded % newCols;
if (destCol === 0) { if (destCol === 0) {
destLineIndex--; destLineIndex--;
destCol = newCols; destCol = destLineLengths[destLineIndex];
} }
let srcLineIndex = wrappedLines.length - linesToAdd - 1; let srcLineIndex = wrappedLines.length - linesToAdd - 1;
let srcCol = lastLineLength; let srcCol = lastLineLength;
@@ -449,6 +449,13 @@ export class Buffer implements IBuffer {
} }
} }
// Null out the end of the line ends if a wide character wrapped to the following line
for (let i = 0; i < wrappedLines.length; i++) {
if (destLineLengths[i] < newCols) {
wrappedLines[i].set(destLineLengths[i], FILL_CHAR_DATA);
}
}
// Adjust viewport as needed // Adjust viewport as needed
let viewportAdjustments = linesToAdd - trimmedLines; let viewportAdjustments = linesToAdd - trimmedLines;
while (viewportAdjustments-- > 0) { while (viewportAdjustments-- > 0) {
+13
View File
@@ -5,6 +5,7 @@
import { assert } from 'chai'; import { assert } from 'chai';
import { BufferLine } from './BufferLine'; import { BufferLine } from './BufferLine';
import { reflowSmallerGetNewLineLengths } from './BufferReflow'; import { reflowSmallerGetNewLineLengths } from './BufferReflow';
import { NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE } from './Buffer';
describe('BufferReflow', () => { describe('BufferReflow', () => {
describe('reflowSmallerGetNewLineLengths', () => { describe('reflowSmallerGetNewLineLengths', () => {
@@ -76,5 +77,17 @@ describe('BufferReflow', () => {
assert.deepEqual(reflowSmallerGetNewLineLengths([line1, line2], 6, 3), [3, 3, 3, 3], 'lines: a汉, 语b, a汉, 语b'); assert.deepEqual(reflowSmallerGetNewLineLengths([line1, line2], 6, 3), [3, 3, 3, 3], 'lines: a汉, 语b, a汉, 语b');
assert.deepEqual(reflowSmallerGetNewLineLengths([line1, line2], 6, 2), [1, 2, 2, 2, 2, 2, 1], 'lines: a, 汉, 语, ba, 汉, 语, b'); assert.deepEqual(reflowSmallerGetNewLineLengths([line1, line2], 6, 2), [1, 2, 2, 2, 2, 2, 1], 'lines: a, 汉, 语, ba, 汉, 语, b');
}); });
it('should work on lines ending in null space', () => {
const line = new BufferLine(5);
line.set(0, [null, '汉', 2, '汉'.charCodeAt(0)]);
line.set(1, [null, '', 0, undefined]);
line.set(2, [null, '语', 2, '语'.charCodeAt(0)]);
line.set(3, [null, '', 0, undefined]);
line.set(4, [null, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE]);
assert.equal(line.translateToString(true), '汉语');
assert.equal(line.translateToString(false), '汉语 ');
assert.deepEqual(reflowSmallerGetNewLineLengths([line], 4, 3), [2, 2], 'line: 汉, 语');
assert.deepEqual(reflowSmallerGetNewLineLengths([line], 4, 2), [2, 2], 'line: 汉, 语');
});
}); });
}); });
+9 -8
View File
@@ -80,21 +80,22 @@ export function reflowSmallerGetNewLineLengths(wrappedLines: BufferLine[], oldCo
// Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and // Use srcCol and srcLine to find the new wrapping point, use that to get the cellsAvailable and
// linesNeeded // linesNeeded
let srcCol = -1; let srcCol = 0;
let srcLine = 0; let srcLine = 0;
let cellsAvailable = 0; let cellsAvailable = 0;
while (cellsAvailable < cellsNeeded) { while (cellsAvailable < cellsNeeded) {
srcCol += newCols; if (cellsNeeded - cellsAvailable < newCols) {
if (srcCol >= oldCols) {
srcCol -= oldCols;
srcLine++;
}
if (srcLine >= wrappedLines.length) {
// Add the final line and exit the loop // Add the final line and exit the loop
newLineLengths.push(cellsNeeded - cellsAvailable); newLineLengths.push(cellsNeeded - cellsAvailable);
break; break;
} }
const endsWithWide = wrappedLines[srcLine].getWidth(srcCol) === 2; srcCol += newCols;
const oldTrimmedLength = wrappedLines[srcLine].getTrimmedLength();
if (srcCol > oldTrimmedLength) {
srcCol -= oldTrimmedLength;
srcLine++;
}
const endsWithWide = wrappedLines[srcLine].getWidth(srcCol - 1) === 2;
if (endsWithWide) { if (endsWithWide) {
srcCol--; srcCol--;
} }