Skip to content

Commit

Permalink
Merge pull request #276 from Tyriar/173_clear_api
Browse files Browse the repository at this point in the history
Expose API to clear the terminal
  • Loading branch information
Tyriar authored Sep 17, 2016
2 parents 59e72b8 + 26fc539 commit 3c9d8b0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/xterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3054,6 +3054,24 @@ Terminal.prototype.eraseLeft = function(x, y) {
this.updateRange(y);
};

/**
* Clears the entire buffer, making the prompt line the new first line.
*/
Terminal.prototype.clear = function() {
if (this.ybase === 0 && this.y === 0) {
// Don't clear if it's already clear
return;
}
this.lines = [this.lines[this.ybase + this.y]];
this.ydisp = 0;
this.ybase = 0;
this.y = 0;
for (var i = 1; i < this.rows; i++) {
this.lines.push(this.blankLine());
}
this.refresh(0, this.rows - 1);
this.emit('scroll', this.ydisp);
};

/**
* Erase all content in the given line
Expand Down
45 changes: 45 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,51 @@ describe('xterm.js', function() {
});
});

describe('clear', function() {
it('should clear a buffer equal to rows', function() {
var promptLine = xterm.lines[xterm.ybase + xterm.y];
xterm.clear();
assert.equal(xterm.y, 0);
assert.equal(xterm.ybase, 0);
assert.equal(xterm.ydisp, 0);
assert.equal(xterm.lines.length, xterm.rows);
assert.deepEqual(xterm.lines[0], promptLine);
for (var i = 1; i < xterm.rows; i++) {
assert.deepEqual(xterm.lines[0], xterm.blankLine());
}
});
it('should clear a buffer larger than rows', function() {
// Fill the buffer with dummy rows
for (var i = 0; i < xterm.rows * 2; i++) {
xterm.write('test\n');
}

var promptLine = xterm.lines[xterm.ybase + xterm.y];
xterm.clear();
assert.equal(xterm.y, 0);
assert.equal(xterm.ybase, 0);
assert.equal(xterm.ydisp, 0);
assert.equal(xterm.lines.length, xterm.rows);
assert.deepEqual(xterm.lines[0], promptLine);
for (var i = 1; i < xterm.rows; i++) {
assert.deepEqual(xterm.lines[i], xterm.blankLine());
}
});
it('should not break the prompt when cleared twice', function() {
var promptLine = xterm.lines[xterm.ybase + xterm.y];
xterm.clear();
xterm.clear();
assert.equal(xterm.y, 0);
assert.equal(xterm.ybase, 0);
assert.equal(xterm.ydisp, 0);
assert.equal(xterm.lines.length, xterm.rows);
assert.deepEqual(xterm.lines[0], promptLine);
for (var i = 1; i < xterm.rows; i++) {
assert.deepEqual(xterm.lines[i], xterm.blankLine());
}
});
});

describe('evaluateKeyEscapeSequence', function() {
it('should return the correct escape sequence for unmodified keys', function() {
// Backspace
Expand Down

0 comments on commit 3c9d8b0

Please sign in to comment.