Skip to content

Commit

Permalink
Make buffer length = rows + scrollback
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed Aug 6, 2017
1 parent 687a5e2 commit 109d467
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/Buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ describe('Buffer', () => {
});

describe('constructor', () => {
it('should create a CircularList with max length equal to scrollback, for its lines', () => {
it('should create a CircularList with max length equal to rows + scrollback, for its lines', () => {
assert.instanceOf(buffer.lines, CircularList);
assert.equal(buffer.lines.maxLength, terminal.options.scrollback);
assert.equal(buffer.lines.maxLength, terminal.rows + terminal.options.scrollback);
});
it('should set the Buffer\'s scrollBottom value equal to the terminal\'s rows -1', () => {
assert.equal(buffer.scrollBottom, terminal.rows - 1);
Expand Down
2 changes: 1 addition & 1 deletion src/Buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Buffer implements IBuffer {
this.scrollBottom = 0;
this.scrollTop = 0;
this.tabs = {};
this._lines = new CircularList<LineData>(this._terminal.options.scrollback);
this._lines = new CircularList<LineData>(this._terminal.rows + this._terminal.options.scrollback);
this.scrollBottom = this._terminal.rows - 1;
}

Expand Down
12 changes: 12 additions & 0 deletions src/BufferSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ export class BufferSet extends EventEmitter implements IBufferSet {
this.emit('activate', this._alt);
}

/**
* Refreshes the max length of the buffer set, this should be called whenever
* terminal rows or scrollback changes.
* @param rows The number of terminal rows.
*/
public refreshMaxLength(rows: number): void {
this._normal.lines.maxLength = rows + this._terminal.options.scrollback;
// TODO: The alt buffer should never have scrollback, see https://github.com/sourcelair/xterm.js/issues/802
this._alt.lines.maxLength = rows + this._terminal.options.scrollback;
}

/**
* Resizes both normal and alt buffers, adjusting their data accordingly.
* @param newCols The new number of columns.
Expand All @@ -84,5 +95,6 @@ export class BufferSet extends EventEmitter implements IBufferSet {
public resize(newCols: number, newRows: number): void {
this._normal.resize(newCols, newRows);
this._alt.resize(newCols, newRows);
this.refreshMaxLength(newRows);
}
}
24 changes: 7 additions & 17 deletions src/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,18 +439,10 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
}
switch (key) {
case 'scrollback':
if (value < this.rows) {
let msg = 'Setting the scrollback value less than the number of rows ';

msg += `(${this.rows}) is not allowed.`;

console.warn(msg);
return;
}

if (this.options[key] !== value) {
if (this.buffer.lines.length > value) {
const amountToTrim = this.buffer.lines.length - value;
const newBufferLength = this.rows + value;
if (this.buffer.lines.length > newBufferLength) {
const amountToTrim = this.buffer.lines.length - newBufferLength;
const needsRefresh = (this.buffer.ydisp - amountToTrim < 0);
this.buffer.lines.trimStart(amountToTrim);
this.buffer.ybase = Math.max(this.buffer.ybase - amountToTrim, 0);
Expand All @@ -459,8 +451,6 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
this.refresh(0, this.rows - 1);
}
}
this.buffer.lines.maxLength = value;
this.viewport.syncScrollArea();
}
break;
}
Expand All @@ -473,6 +463,10 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
this.element.classList.toggle(`xterm-cursor-style-underline`, value === 'underline');
this.element.classList.toggle(`xterm-cursor-style-bar`, value === 'bar');
break;
case 'scrollback':
this.buffers.refreshMaxLength(this.rows);
this.viewport.syncScrollArea();
break;
case 'tabStopWidth': this.setupStops(); break;
}
}
Expand Down Expand Up @@ -1935,10 +1929,6 @@ export class Terminal extends EventEmitter implements ITerminal, IInputHandlingT
return;
}

if (y > this.getOption('scrollback')) {
this.setOption('scrollback', y);
}

let line;
let el;
let i;
Expand Down
17 changes: 1 addition & 16 deletions src/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ describe('xterm.js', function() {
});

describe('setOption', function() {
let originalWarn;
let warnCallCount;
beforeEach(() => {
originalWarn = console.warn;
warnCallCount = 0;
console.warn = () => warnCallCount++;
});
afterEach(() => {
console.warn = originalWarn;
});
it('should set the option correctly', function() {
xterm.setOption('cursorBlink', true);
assert.equal(xterm.cursorBlink, true);
Expand All @@ -63,12 +53,7 @@ describe('xterm.js', function() {
assert.equal(xterm.options.cursorBlink, false);
});
it('should throw when setting a non-existant option', function() {
assert.throws(xterm.setOption.bind(xterm, 'fake', true));
});
it('should warn and do nothing when scrollback is less than number of rows', function() {
xterm.setOption('scrollback', xterm.rows - 1);
assert.equal(xterm.getOption('scrollback'), 1000);
assert.equal(warnCallCount, 1);
assert.throws(() => xterm.setOption('fake', true));
});
});

Expand Down
3 changes: 3 additions & 0 deletions src/utils/CircularList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export class CircularList<T> extends EventEmitter implements ICircularList<T> {
}

public set maxLength(newMaxLength: number) {
if (this.maxLength === newMaxLength) {
return;
}
// Reconstruct array, starting at index 0. Only transfer values from the
// indexes 0 to length.
let newArray = new Array<T>(newMaxLength);
Expand Down

0 comments on commit 109d467

Please sign in to comment.