Improve Buffer.resize to work with uninitialized buffers

Fixes #924
This commit is contained in:
Daniel Imms
2017-08-25 13:14:30 -07:00
parent 54cf30ae68
commit bc86664726
+69 -69
View File
@@ -94,11 +94,6 @@ export class Buffer implements IBuffer {
* @param newRows The new number of rows. * @param newRows The new number of rows.
*/ */
public resize(newCols: number, newRows: number): void { public resize(newCols: number, newRows: number): void {
// Don't resize the buffer if it's empty and hasn't been used yet.
if (this._lines.length === 0) {
return;
}
// Increase max length if needed before adjustments to allow space to fill // Increase max length if needed before adjustments to allow space to fill
// as required. // as required.
const newMaxLength = this._getCorrectBufferLength(newRows); const newMaxLength = this._getCorrectBufferLength(newRows);
@@ -106,83 +101,88 @@ export class Buffer implements IBuffer {
this._lines.maxLength = newMaxLength; this._lines.maxLength = newMaxLength;
} }
// Deal with columns increasing (we don't do anything when columns reduce) // The following adjustments should only happen if the buffer has been
if (this._terminal.cols < newCols) { // initialized/filled.
const ch: CharData = [this._terminal.defAttr, ' ', 1]; // does xterm use the default attr? if (this._lines.length > 0) {
for (let i = 0; i < this._lines.length; i++) { // Deal with columns increasing (we don't do anything when columns reduce)
// TODO: This should be removed, with tests setup for the case that was if (this._terminal.cols < newCols) {
// causing the underlying bug, see https://github.com/sourcelair/xterm.js/issues/824 const ch: CharData = [this._terminal.defAttr, ' ', 1]; // does xterm use the default attr?
if (this._lines.get(i) === undefined) { for (let i = 0; i < this._lines.length; i++) {
this._lines.set(i, this._terminal.blankLine(undefined, undefined, newCols)); // TODO: This should be removed, with tests setup for the case that was
} // causing the underlying bug, see https://github.com/sourcelair/xterm.js/issues/824
while (this._lines.get(i).length < newCols) { if (this._lines.get(i) === undefined) {
this._lines.get(i).push(ch); this._lines.set(i, this._terminal.blankLine(undefined, undefined, newCols));
}
while (this._lines.get(i).length < newCols) {
this._lines.get(i).push(ch);
}
} }
} }
}
// Resize rows in both directions as needed // Resize rows in both directions as needed
let addToY = 0; let addToY = 0;
if (this._terminal.rows < newRows) { if (this._terminal.rows < newRows) {
for (let y = this._terminal.rows; y < newRows; y++) { for (let y = this._terminal.rows; y < newRows; y++) {
if (this._lines.length < newRows + this.ybase) { if (this._lines.length < newRows + this.ybase) {
if (this.ybase > 0 && this._lines.length <= this.ybase + this.y + addToY + 1) { if (this.ybase > 0 && this._lines.length <= this.ybase + this.y + addToY + 1) {
// There is room above the buffer and there are no empty elements below the line, // There is room above the buffer and there are no empty elements below the line,
// scroll up // scroll up
this.ybase--; this.ybase--;
addToY++; addToY++;
if (this.ydisp > 0) { if (this.ydisp > 0) {
// Viewport is at the top of the buffer, must increase downwards // Viewport is at the top of the buffer, must increase downwards
this.ydisp--; this.ydisp--;
}
} else {
// Add a blank line if there is no buffer left at the top to scroll to, or if there
// are blank lines after the cursor
this._lines.push(this._terminal.blankLine(undefined, undefined, newCols));
}
}
}
} else { // (this._terminal.rows >= newRows)
for (let y = this._terminal.rows; y > newRows; y--) {
if (this._lines.length > newRows + this.ybase) {
if (this._lines.length > this.ybase + this.y + 1) {
// The line is a blank line below the cursor, remove it
this._lines.pop();
} else {
// The line is the cursor, scroll down
this.ybase++;
this.ydisp++;
} }
} else {
// Add a blank line if there is no buffer left at the top to scroll to, or if there
// are blank lines after the cursor
this._lines.push(this._terminal.blankLine(undefined, undefined, newCols));
} }
} }
} }
} else { // (this._terminal.rows >= newRows)
for (let y = this._terminal.rows; y > newRows; y--) { // Reduce max length if needed after adjustments, this is done after as it
if (this._lines.length > newRows + this.ybase) { // would otherwise cut data from the bottom of the buffer.
if (this._lines.length > this.ybase + this.y + 1) { if (newMaxLength < this._lines.maxLength) {
// The line is a blank line below the cursor, remove it // Trim from the top of the buffer and adjust ybase and ydisp.
this._lines.pop(); const amountToTrim = this._lines.length - newMaxLength;
} else { if (amountToTrim > 0) {
// The line is the cursor, scroll down this._lines.trimStart(amountToTrim);
this.ybase++; this.ybase = Math.max(this.ybase - amountToTrim, 0);
this.ydisp++; this.ydisp = Math.max(this.ydisp - amountToTrim, 0);
}
} }
this._lines.maxLength = newMaxLength;
} }
}
// Reduce max length if needed after adjustments, this is done after as it // Make sure that the cursor stays on screen
// would otherwise cut data from the bottom of the buffer. if (this.y >= newRows) {
if (newMaxLength < this._lines.maxLength) { this.y = newRows - 1;
// Trim from the top of the buffer and adjust ybase and ydisp.
const amountToTrim = this._lines.length - newMaxLength;
if (amountToTrim > 0) {
this._lines.trimStart(amountToTrim);
this.ybase = Math.max(this.ybase - amountToTrim, 0);
this.ydisp = Math.max(this.ydisp - amountToTrim, 0);
} }
this._lines.maxLength = newMaxLength; if (addToY) {
this.y += addToY;
}
if (this.x >= newCols) {
this.x = newCols - 1;
}
this.scrollTop = 0;
} }
// Make sure that the cursor stays on screen
if (this.y >= newRows) {
this.y = newRows - 1;
}
if (addToY) {
this.y += addToY;
}
if (this.x >= newCols) {
this.x = newCols - 1;
}
this.scrollTop = 0;
this.scrollBottom = newRows - 1; this.scrollBottom = newRows - 1;
} }