Skip to content

Commit 00c1211

Browse files
authored
Merge pull request #1740 from jerch/bufferline_fixes
fix handling of length in BufferLine.resize
2 parents 5bc7fc4 + d25e55f commit 00c1211

File tree

2 files changed

+78
-36
lines changed

2 files changed

+78
-36
lines changed

src/BufferLine.test.ts

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,71 @@ describe('BufferLine', function(): void {
133133
const line3 = line.clone();
134134
chai.expect(TestBufferLine.prototype.toArray.apply(line3)).eql(line.toArray());
135135
});
136-
it('resize enlarge', function(): void {
137-
const line = new TestBufferLine(5, [1, 'a', 0, 'a'.charCodeAt(0)], false);
138-
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)]);
139-
chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
140-
});
141-
it('resize shrink(true)', function(): void {
142-
const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
143-
line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], true);
144-
chai.expect(line.toArray()).eql(Array(5).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
145-
});
146-
it('resize shrink(false)', function(): void {
147-
const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
148-
line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], false);
149-
chai.expect(line.toArray()).eql(Array(5).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
150-
});
151-
it('resize shrink(false) + shrink(false)', function(): void {
152-
const line = new TestBufferLine(20, [1, 'a', 0, 'a'.charCodeAt(0)], false);
153-
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
154-
line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], false);
155-
chai.expect(line.toArray()).eql(Array(5).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
156-
});
157-
it('resize shrink(false) + enlarge', function(): void {
158-
const line = new TestBufferLine(20, [1, 'a', 0, 'a'.charCodeAt(0)], false);
159-
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
160-
line.resize(15, [1, 'a', 0, 'a'.charCodeAt(0)]);
161-
chai.expect(line.toArray()).eql(Array(15).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
136+
describe('resize', function(): void {
137+
it('enlarge(false)', function(): void {
138+
const line = new TestBufferLine(5, [1, 'a', 0, 'a'.charCodeAt(0)], false);
139+
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)]);
140+
chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
141+
});
142+
it('enlarge(true)', function(): void {
143+
const line = new TestBufferLine(5, [1, 'a', 0, 'a'.charCodeAt(0)], false);
144+
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], true);
145+
chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
146+
});
147+
it('shrink(true) - should apply new size', function(): void {
148+
const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
149+
line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], true);
150+
chai.expect(line.toArray()).eql(Array(5).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
151+
});
152+
it('shrink(false) - should not apply new size', function(): void {
153+
const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
154+
line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], false);
155+
chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
156+
});
157+
it('shrink(false) + shrink(false) - should not apply new size', function(): void {
158+
const line = new TestBufferLine(20, [1, 'a', 0, 'a'.charCodeAt(0)], false);
159+
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
160+
line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], false);
161+
chai.expect(line.toArray()).eql(Array(20).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
162+
});
163+
it('shrink(false) + enlarge(false) to smaller than before', function(): void {
164+
const line = new TestBufferLine(20, [1, 'a', 0, 'a'.charCodeAt(0)], false);
165+
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
166+
line.resize(15, [1, 'a', 0, 'a'.charCodeAt(0)]);
167+
chai.expect(line.toArray()).eql(Array(20).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
168+
});
169+
it('shrink(false) + enlarge(false) to bigger than before', function(): void {
170+
const line = new TestBufferLine(20, [1, 'a', 0, 'a'.charCodeAt(0)], false);
171+
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
172+
line.resize(25, [1, 'a', 0, 'a'.charCodeAt(0)]);
173+
chai.expect(line.toArray()).eql(Array(25).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
174+
});
175+
it('shrink(false) + resize shrink=true should enforce shrinking', function(): void {
176+
const line = new TestBufferLine(20, [1, 'a', 0, 'a'.charCodeAt(0)], false);
177+
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
178+
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], true);
179+
chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
180+
});
181+
it('enlarge from 0 length', function(): void {
182+
const line = new TestBufferLine(0, [1, 'a', 0, 'a'.charCodeAt(0)], false);
183+
line.resize(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
184+
chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
185+
});
186+
it('shrink to 0 length', function(): void {
187+
const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
188+
line.resize(0, [1, 'a', 0, 'a'.charCodeAt(0)], true);
189+
chai.expect(line.toArray()).eql(Array(0).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
190+
});
191+
it('shrink(false) to 0 and enlarge to different sizes', function(): void {
192+
const line = new TestBufferLine(10, [1, 'a', 0, 'a'.charCodeAt(0)], false);
193+
line.resize(0, [1, 'a', 0, 'a'.charCodeAt(0)], false);
194+
chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
195+
line.resize(5, [1, 'a', 0, 'a'.charCodeAt(0)], false);
196+
chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
197+
line.resize(7, [1, 'a', 0, 'a'.charCodeAt(0)], false);
198+
chai.expect(line.toArray()).eql(Array(10).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
199+
line.resize(7, [1, 'a', 0, 'a'.charCodeAt(0)], true);
200+
chai.expect(line.toArray()).eql(Array(7).fill([1, 'a', 0, 'a'.charCodeAt(0)]));
201+
});
162202
});
163203
});

src/BufferLine.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ export class BufferLine implements IBufferLine {
7777

7878
/** resize line to cols filling new cells with fill */
7979
public resize(cols: number, fillCharData: CharData, shrink: boolean = false): void {
80+
while (this._data.length < cols) {
81+
this._data.push(fillCharData);
82+
}
8083
if (shrink) {
8184
while (this._data.length > cols) {
8285
this._data.pop();
8386
}
8487
}
85-
while (this._data.length < cols) {
86-
this._data.push(fillCharData);
87-
}
88-
this.length = cols;
88+
this.length = this._data.length;
8989
}
9090

9191
public fill(fillCharData: CharData): void {
@@ -141,11 +141,13 @@ export class BufferLineTypedArray implements IBufferLine {
141141
if (!fillCharData) {
142142
fillCharData = [0, NULL_CELL_CHAR, NULL_CELL_WIDTH, NULL_CELL_CODE];
143143
}
144-
this._data = new Uint32Array(cols * CELL_SIZE);
145-
for (let i = 0; i < cols; ++i) {
146-
this.set(i, fillCharData);
144+
if (cols) {
145+
this._data = new Uint32Array(cols * CELL_SIZE);
146+
for (let i = 0; i < cols; ++i) {
147+
this.set(i, fillCharData);
148+
}
147149
}
148-
this.length = cols || 0;
150+
this.length = cols;
149151
}
150152

151153
public get(index: number): CharData {
@@ -212,12 +214,12 @@ export class BufferLineTypedArray implements IBufferLine {
212214
}
213215

214216
public resize(cols: number, fillCharData: CharData, shrink: boolean = false): void {
215-
if (cols === this.length) {
217+
if (cols === this.length || (!shrink && cols < this.length)) {
216218
return;
217219
}
218220
if (cols > this.length) {
219221
const data = new Uint32Array(cols * CELL_SIZE);
220-
if (this._data) {
222+
if (this.length) {
221223
if (cols * CELL_SIZE < this._data.length) {
222224
data.set(this._data.subarray(0, cols * CELL_SIZE));
223225
} else {

0 commit comments

Comments
 (0)